Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
HashTable Struct Reference

#include <HashTable.h>

Overview

Hash tables with user-supplied hash and equality functions.

Definition at line 80 of file HashTable.h.

Inheritance diagram for HashTable:
Object

Properties

size_t count
 The number of entries.
 
Consumer destroyKey
 Optional destructor called when a key is removed or replaced.
 
Consumer destroyValue
 Optional destructor called when a value is removed or replaced.
 
HashTableEqualFunc equal
 The equality function.
 
HashTableHashFunc hash
 The hash function.
 
Object object
 The superclass.
 
- Properties inherited from Object
Classclazz
 Every instance of Object begins with a pointer to its Class.
 
unsigned int magic
 A header to allow introspection of Object types.
 

Methods

Class_HashTable (void)
 The HashTable archetype.
 
bool containsKey (const HashTable *self, const ident key)
 
void enumerate (const HashTable *self, HashTableEnumerator enumerator, ident data)
 Enumerates the entries of this HashTable with the given function.
 
ident get (const HashTable *self, const ident key)
 
HashTableinit (HashTable *self, HashTableHashFunc hash, HashTableEqualFunc equal)
 Initializes this HashTable with the given hash and equality functions.
 
HashTableinitWithCapacity (HashTable *self, HashTableHashFunc hash, HashTableEqualFunc equal, size_t capacity)
 Initializes this HashTable with the given capacity.
 
void remove (HashTable *self, const ident key)
 Removes the entry for the given key.
 
void removeAll (HashTable *self)
 Removes all entries from this HashTable.
 
void set (HashTable *self, const ident key, const ident value)
 Sets the value for the given key, replacing any existing entry.
 
- Methods inherited from Object
Class_Object (void)
 The Object archetype.
 
Objectcopy (const Object *self)
 Creates a shallow copy of this Object.
 
void dealloc (Object *self)
 Frees all resources held by this Object.
 
Stringdescription (const Object *self)
 
int hash (const Object *self)
 
Objectinit (Object *self)
 Initializes this Object.
 
bool isEqual (const Object *self, const Object *other)
 Tests equality of the other Object.
 
bool isKindOfClass (const Object *self, const Class *clazz)
 Tests for Class hierarchy membership.
 

Protected Attributes

HashTableEntry ** buckets
 The buckets.
 
size_t capacity
 The number of buckets.
 
HashTableInterface * interface
 The interface.
 
- Protected Attributes inherited from Object
ObjectInterface * interface
 The interface.
 

Property Details

◆ buckets

HashTableEntry** HashTable::buckets
protected

The buckets.

Definition at line 108 of file HashTable.h.

◆ capacity

size_t HashTable::capacity
protected

The number of buckets.

Definition at line 102 of file HashTable.h.

◆ count

size_t HashTable::count

The number of entries.

Definition at line 96 of file HashTable.h.

◆ destroyKey

Consumer HashTable::destroyKey

Optional destructor called when a key is removed or replaced.

Definition at line 123 of file HashTable.h.

◆ destroyValue

Consumer HashTable::destroyValue

Optional destructor called when a value is removed or replaced.

Definition at line 128 of file HashTable.h.

◆ equal

HashTableEqualFunc HashTable::equal

The equality function.

Definition at line 118 of file HashTable.h.

◆ hash

HashTableHashFunc HashTable::hash

The hash function.

Definition at line 113 of file HashTable.h.

◆ interface

HashTableInterface* HashTable::interface
protected

The interface.

Definition at line 91 of file HashTable.h.

◆ object

Object HashTable::object

The superclass.

Definition at line 85 of file HashTable.h.

Method Details

◆ _HashTable()

Class * _HashTable ( void  )

The HashTable archetype.

Returns
The HashTable Class.

Definition at line 328 of file HashTable.c.

328 {
329 static Class *clazz;
330 static Once once;
331
332 do_once(&once, {
333 clazz = _initialize(&(const ClassDef) {
334 .name = "HashTable",
335 .superclass = _Object(),
336 .instanceSize = sizeof(HashTable),
337 .interfaceOffset = offsetof(HashTable, interface),
338 .interfaceSize = sizeof(HashTableInterface),
340 });
341 });
342
343 return clazz;
344}
static void initialize(Class *clazz)
Definition Array.c:710
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
long Once
The Once type.
Definition Once.h:37
#define do_once(once, block)
Executes the given block at most one time.
Definition Once.h:43
ClassDefs are passed to _initialize via an archetype to initialize a Class.
Definition Class.h:41
The runtime representation of a Class.
Definition Class.h:95
Hash tables with user-supplied hash and equality functions.
Definition HashTable.h:80
HashTableInterface * interface
The interface.
Definition HashTable.h:91
Class * clazz
Every instance of Object begins with a pointer to its Class.
Definition Object.h:55
Class * _Object(void)
The Object archetype.
Definition Object.c:136

◆ containsKey()

bool containsKey ( const HashTable self,
const ident  key 
)
Parameters
selfThe HashTable.
keyThe key.
Returns
True if this HashTable contains the given key.

Definition at line 131 of file HashTable.c.

131 {
132 return $(self, get, key) != NULL;
133}
ident get(const HashTable *self, const ident key)
Definition HashTable.c:154

◆ enumerate()

void enumerate ( const HashTable self,
HashTableEnumerator  enumerator,
ident  data 
)

Enumerates the entries of this HashTable with the given function.

Parameters
selfThe HashTable.
enumeratorThe enumerator function.
dataUser data.

Definition at line 139 of file HashTable.c.

139 {
140
141 assert(enumerator);
142
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);
146 }
147 }
148}
static Data * data(void)
Definition Data.c:286
size_t capacity
The number of buckets.
Definition HashTable.h:102
HashTableEntry ** buckets
The buckets.
Definition HashTable.h:108

◆ get()

ident get ( const HashTable self,
const ident  key 
)
Parameters
selfThe HashTable.
keyThe key.
Returns
The value for the given key, or NULL if not found.

Definition at line 154 of file HashTable.c.

154 {
155
156 const size_t bin = self->hash(key) % self->capacity;
157
158 for (const HashTableEntry *e = self->buckets[bin]; e; e = e->next) {
159 if (self->equal(e->key, key)) {
160 return e->value;
161 }
162 }
163
164 return NULL;
165}
HashTableHashFunc hash
The hash function.
Definition HashTable.h:113
HashTableEqualFunc equal
The equality function.
Definition HashTable.h:118

◆ init()

HashTable * init ( HashTable self,
HashTableHashFunc  hash,
HashTableEqualFunc  equal 
)

Initializes this HashTable with the given hash and equality functions.

Parameters
selfThe HashTable.
hashThe hash function.
equalThe equality function.
Returns
The initialized HashTable, or NULL on error.

Definition at line 171 of file HashTable.c.

171 {
173}
#define HASHTABLE_DEFAULT_CAPACITY
Definition HashTable.c:36
HashTable * initWithCapacity(HashTable *self, HashTableHashFunc hash, HashTableEqualFunc equal, size_t capacity)
Initializes this HashTable with the given capacity.
Definition HashTable.c:179

◆ initWithCapacity()

HashTable * initWithCapacity ( HashTable self,
HashTableHashFunc  hash,
HashTableEqualFunc  equal,
size_t  capacity 
)

Initializes this HashTable with the given capacity.

Parameters
selfThe HashTable.
hashThe hash function.
equalThe equality function.
capacityThe initial bucket count.
Returns
The initialized HashTable, or NULL on error.

Definition at line 179 of file HashTable.c.

179 {
180
181 self = (HashTable *) super(Object, self, init);
182 if (self) {
183 assert(hash);
184 assert(equal);
185 assert(capacity);
186
187 self->hash = hash;
188 self->equal = equal;
189 self->capacity = capacity;
190 self->buckets = calloc(self->capacity, sizeof(HashTableEntry *));
191 assert(self->buckets);
192 }
193 return self;
194}
#define super(type, obj, method,...)
HashTable * init(HashTable *self, HashTableHashFunc hash, HashTableEqualFunc equal)
Initializes this HashTable with the given hash and equality functions.
Definition HashTable.c:171
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46

◆ remove()

void remove ( HashTable self,
const ident  key 
)

Removes the entry for the given key.

Parameters
selfThe HashTable.
keyThe key to remove.

◆ removeAll()

void removeAll ( HashTable self)

Removes all entries from this HashTable.

Parameters
selfThe HashTable.

Definition at line 227 of file HashTable.c.

227 {
228
229 for (size_t i = 0; i < self->capacity; i++) {
230 HashTableEntry *e = self->buckets[i];
231 while (e) {
232 HashTableEntry *next = e->next;
233 if (self->destroyKey) {
234 self->destroyKey(e->key);
235 }
236 if (self->destroyValue) {
237 self->destroyValue(e->value);
238 }
239 free(e);
240 e = next;
241 }
242 self->buckets[i] = NULL;
243 }
244
245 self->count = 0;
246}
static Unicode next(StringReader *self, StringReaderMode mode)
size_t count
The number of entries.
Definition HashTable.h:96
Consumer destroyKey
Optional destructor called when a key is removed or replaced.
Definition HashTable.h:123
Consumer destroyValue
Optional destructor called when a value is removed or replaced.
Definition HashTable.h:128

◆ set()

void set ( HashTable self,
const ident  key,
const ident  value 
)

Sets the value for the given key, replacing any existing entry.

Parameters
selfThe HashTable.
keyThe key.
valueThe value.

Definition at line 275 of file HashTable.c.

275 {
276
277 if ((float) self->count / (float) self->capacity >= HASHTABLE_MAX_LOAD) {
279 }
280
281 const size_t bin = self->hash(key) % self->capacity;
282
283 for (HashTableEntry *e = self->buckets[bin]; e; e = e->next) {
284 if (self->equal(e->key, key)) {
285 if (self->destroyKey) {
286 self->destroyKey(e->key);
287 }
288 if (self->destroyValue) {
289 self->destroyValue(e->value);
290 }
291 e->key = key;
292 e->value = value;
293 return;
294 }
295 }
296
297 HashTableEntry *e = calloc(1, sizeof(HashTableEntry));
298 assert(e);
299 e->key = key;
300 e->value = value;
301 e->next = self->buckets[bin];
302 self->buckets[bin] = e;
303 self->count++;
304}
#define HASHTABLE_MAX_LOAD
Definition HashTable.c:38
#define HASHTABLE_GROW_FACTOR
Definition HashTable.c:37
static void resize(HashTable *self, size_t capacity)
Rehashes all entries into a new bucket array of the given capacity.
Definition HashTable.c:251

The documentation for this struct was generated from the following files: