34#define _Class _HashTable
36#define HASHTABLE_DEFAULT_CAPACITY 16
37#define HASHTABLE_GROW_FACTOR 2
38#define HASHTABLE_MAX_LOAD 0.75f
40#pragma mark - Built-in hash/equal functions
46 const char *s = (
const char *) key;
50 h = ((h << 5) + h) + (size_t) c;
59 return strcmp((
const char *) a, (
const char *) b) == 0;
66 const char *s = (
const char *) key;
70 h = ((h << 5) + h) + (size_t) tolower(c);
79 return strcasecmp((
const char *) a, (
const char *) b) == 0;
86 return (
size_t) (uintptr_t) key;
105 for (
size_t i = 0; i < this->capacity; i++) {
106 HashTableEntry *e = this->buckets[i];
108 HashTableEntry *
next = e->next;
109 if (this->destroyKey) {
110 this->destroyKey(e->key);
112 if (this->destroyValue) {
113 this->destroyValue(e->value);
125#pragma mark - HashTable
132 return $(self,
get, key) != NULL;
143 for (
size_t i = 0; i < self->
capacity; i++) {
144 for (
const HashTableEntry *e = self->
buckets[i]; e; e = e->next) {
145 enumerator(self, e->key, e->value,
data);
158 for (
const HashTableEntry *e = self->
buckets[bin]; e; e = e->next) {
159 if (self->
equal(e->key, key)) {
204 HashTableEntry **e = &self->
buckets[bin];
206 if (self->
equal((*e)->key, key)) {
207 HashTableEntry *found = *e;
229 for (
size_t i = 0; i < self->
capacity; i++) {
230 HashTableEntry *e = self->
buckets[i];
232 HashTableEntry *
next = e->next;
253 HashTableEntry **buckets = calloc(capacity,
sizeof(HashTableEntry *));
256 for (
size_t i = 0; i < self->
capacity; i++) {
257 for (HashTableEntry *e = self->
buckets[i]; e; ) {
258 HashTableEntry *
next = e->next;
259 const size_t bin = self->
hash(e->key) % capacity;
260 e->next = buckets[bin];
283 for (HashTableEntry *e = self->
buckets[bin]; e; e = e->next) {
284 if (self->
equal(e->key, key)) {
297 HashTableEntry *e = calloc(1,
sizeof(HashTableEntry));
337 .interfaceOffset = offsetof(
HashTable, interface),
338 .interfaceSize =
sizeof(HashTableInterface),
static int hash(const Object *self)
Class * _initialize(const ClassDef *def)
Initializes the given Class.
#define super(type, obj, method,...)
Utilities for calculating hash values.
static ident get(const HashTable *self, const ident key)
static HashTable * initWithCapacity(HashTable *self, HashTableHashFunc hash, HashTableEqualFunc equal, size_t capacity)
size_t HashTableHashDirect(const ident key)
bool HashTableEqualStr(const ident a, const ident b)
bool HashTableEqualDirect(const ident a, const ident b)
static void _remove(HashTable *self, const ident key)
size_t HashTableHashStri(const ident key)
static HashTable * init(HashTable *self, HashTableHashFunc hash, HashTableEqualFunc equal)
#define HASHTABLE_MAX_LOAD
static void dealloc(Object *self)
#define HASHTABLE_GROW_FACTOR
static void resize(HashTable *self, size_t capacity)
Rehashes all entries into a new bucket array of the given capacity.
static void initialize(Class *clazz)
size_t HashTableHashStr(const ident key)
Common hash functions for use with HashTable.
#define HASHTABLE_DEFAULT_CAPACITY
static void enumerate(const HashTable *self, HashTableEnumerator enumerator, ident data)
bool HashTableEqualStri(const ident a, const ident b)
static void removeAll(HashTable *self)
static bool containsKey(const HashTable *self, const ident key)
Hash tables with user-supplied hash and equality functions for raw C types.
bool(* HashTableEqualFunc)(const ident a, const ident b)
A function that tests equality of two keys.
size_t(* HashTableHashFunc)(const ident key)
A function that computes a hash for the given key.
void(* HashTableEnumerator)(const HashTable *table, ident key, ident value, ident data)
The HashTableEnumerator function type.
static Unicode next(StringReader *self, StringReaderMode mode)
void * ident
The identity type, similar to Objective-C id.
#define do_once(once, block)
Executes the given block at most one time.
ClassDefs are passed to _initialize via an archetype to initialize a Class.
The runtime representation of a Class.
ident interface
The interface of the Class.
Hash tables with user-supplied hash and equality functions.
size_t capacity
The number of buckets.
void remove(HashTable *self, const ident key)
Removes the entry for the given key.
HashTableEntry ** buckets
The buckets.
HashTable * initWithCapacity(HashTable *self, HashTableHashFunc hash, HashTableEqualFunc equal, size_t capacity)
Initializes this HashTable with the given capacity.
size_t count
The number of entries.
Consumer destroyKey
Optional destructor called when a key is removed or replaced.
HashTableHashFunc hash
The hash function.
Consumer destroyValue
Optional destructor called when a value is removed or replaced.
HashTableEqualFunc equal
The equality function.
Object is the root Class of The Objectively Class hierarchy.