Veritable Lasagna
An Allocator & Data Structure Library for C.
Loading...
Searching...
No Matches
vl_hashtable.h
Go to the documentation of this file.
1
14#ifndef VL_HASHTABLE_H
15#define VL_HASHTABLE_H
16
17#include "vl_arena.h"
18#include "vl_hash.h"
19
20#define VL_HASHTABLE_ITER_INVALID 0
21
22#ifndef VL_HASHTABLE_RESIZE_FACTOR
23#define VL_HASHTABLE_RESIZE_FACTOR 0.8
24#endif
25
26#ifndef VL_HASHTABLE_DEFAULT_SIZE
27#define VL_HASHTABLE_DEFAULT_SIZE 128
28#endif
29
36#define VL_HASHTABLE_FOREACH(table, trackVar) \
37 for (vl_hash_iter trackVar = vlHashTableFront(table); trackVar != VL_HASHTABLE_ITER_INVALID; \
38 trackVar = vlHashTableNext(table, trackVar))
39
40typedef vl_arena_ptr vl_hash_iter;
41
78typedef struct
79{
80 vl_memory* table; // holds mapping from hash values to collision list heads
81 // in the arena
82 vl_arena data; // holds the node key/value data
83 vl_hash_function hashFunc; // hash function; hashes keys
84
85 vl_dsidx_t totalElements; // total number of mapped elements
87
92typedef struct
93{
94 vl_uint16_t keySize;
95 vl_uint16_t valSize;
96 vl_hash keyHash;
97 vl_arena_ptr next;
98} vl_hashtable_header;
99
120VL_API void vlHashTableInit(vl_hashtable* table, vl_hash_function hashFunc);
121
140VL_API void vlHashTableFree(vl_hashtable* table);
141
162
180VL_API void vlHashTableDelete(vl_hashtable* table);
181
209VL_API vl_hash_iter vlHashTableInsert(vl_hashtable* table, const void* key, vl_memsize_t keySize,
210 vl_memsize_t dataSize);
211
230VL_API void vlHashTableRemoveKey(vl_hashtable* table, const void* key, vl_memsize_t keyLen);
231
249VL_API void vlHashTableRemoveIter(vl_hashtable* table, vl_hash_iter iter);
250
271VL_API void vlHashTableClear(vl_hashtable* table);
272
300VL_API vl_hashtable* vlHashTableClone(const vl_hashtable* table, vl_hashtable* dest);
301
330
352VL_API int vlHashTableCopy(vl_hashtable* src, vl_hashtable* dest);
353
378VL_API void vlHashTableReserve(vl_hashtable* table, vl_memsize_t buckets, vl_memsize_t heapSize);
379
400VL_API vl_hash_iter vlHashTableFind(vl_hashtable* table, const void* key, vl_memsize_t keySize);
401
423 /*out*/ vl_memsize_t* size);
424
448 /*out*/ vl_memsize_t* size);
449
469
490
491// notice "back" and "prev" functions are omitted here.
492// considering there is no well-defined order that should be expected from this
493// structure, it shouldn't make much difference to iterate forward or backward,
494// so long as all elements are encompassed in the iteration.
495
496// that said, it can often be observed that iteration occurs in the reverse
497// order of insertion *up until* the first resize of the table. this is due to
498// how the underlying arena behaves, slicing memory off the end of the first
499// free block of memory.
500
501#endif // VL_HASHTABLE_H
An arena allocator for efficient memory management.
Definition vl_arena.h:84
vl_hash(* vl_hash_function)(const void *data, vl_memsize_t dataSize)
Hash function typedef.
Definition vl_hash.h:33
VL_API vl_hash_iter vlHashTableFront(vl_hashtable *table)
Returns the "first" iterator for the specified table.
Definition vl_hashtable.c:338
VL_API int vlHashTableCopy(vl_hashtable *src, vl_hashtable *dest)
Copies the entirety of one hashtable to another.
Definition vl_hashtable.c:268
vl_memory * table
Definition vl_hashtable.h:80
VL_API vl_hash_iter vlHashTableInsert(vl_hashtable *table, const void *key, vl_memsize_t keySize, vl_memsize_t dataSize)
Claims a chunk of memory associated with the specified key. If the key already exists,...
Definition vl_hashtable.c:75
VL_API void vlHashTableRemoveIter(vl_hashtable *table, vl_hash_iter iter)
Removes the hash element represented by the specified iterator.
Definition vl_hashtable.c:181
vl_dsidx_t totalElements
Definition vl_hashtable.h:85
VL_API vl_transient * vlHashTableSampleValue(vl_hashtable *table, vl_hash_iter iter, vl_memsize_t *size)
Samples the value of the key-value pair indicated by the specified iterator.
Definition vl_hashtable.c:330
VL_API vl_hash_iter vlHashTableFind(vl_hashtable *table, const void *key, vl_memsize_t keySize)
Searches the hashtable for an element with the specified key.
Definition vl_hashtable.c:152
VL_API void vlHashTableInit(vl_hashtable *table, vl_hash_function hashFunc)
Initializes the specified table with a hash function.
Definition vl_hashtable.c:47
vl_arena_ptr vl_hash_iter
Definition vl_hashtable.h:40
VL_API void vlHashTableRemoveKey(vl_hashtable *table, const void *key, vl_memsize_t keyLen)
Removes the element represented by the specified key.
Definition vl_hashtable.c:176
VL_API vl_hash_iter vlHashTableNext(vl_hashtable *table, vl_hash_iter iter)
Returns the "next" iterator relative to the specified iterator.
Definition vl_hashtable.c:363
vl_arena data
Definition vl_hashtable.h:82
vl_hash_function hashFunc
Definition vl_hashtable.h:83
VL_API vl_hashtable * vlHashTableClone(const vl_hashtable *table, vl_hashtable *dest)
Clones the specified hashtable to another.
Definition vl_hashtable.c:235
VL_API void vlHashTableFree(vl_hashtable *table)
De-initializes and frees the internal resources of the specified table.
Definition vl_hashtable.c:56
VL_API void vlHashTableDelete(vl_hashtable *table)
De-initializes and deletes the specified table and its resources.
Definition vl_hashtable.c:69
VL_API const vl_transient * vlHashTableSampleKey(vl_hashtable *table, vl_hash_iter iter, vl_memsize_t *size)
Samples the key of the key-value pair indicated by the specified iterator.
Definition vl_hashtable.c:322
VL_API vl_hashtable * vlHashTableNew(vl_hash_function func)
Allocates on the heap, initializes, and returns a new hash table instance.
Definition vl_hashtable.c:62
VL_API void vlHashTableClear(vl_hashtable *table)
Clears the specified hash table so it can be used as if it was just created.
Definition vl_hashtable.c:228
VL_API vl_hash_iter vlHashTableCopyElement(vl_hashtable *src, vl_hash_iter iter, vl_hashtable *dest)
Copies a single element of a hashtable from one table to another.
Definition vl_hashtable.c:253
VL_API void vlHashTableReserve(vl_hashtable *table, vl_memsize_t buckets, vl_memsize_t heapSize)
Reserves memory in the hashtable before requiring it.
Definition vl_hashtable.c:289
A dynamically-sized hash table with variable-sized keys and values.
Definition vl_hashtable.h:79
VL_MEMORY_T vl_memory
Definition vl_memory.h:108
VL_MEMORY_T vl_transient
Definition vl_memory.h:118
VL_STRUCTURE_INDEX_T vl_dsidx_t
Index type for data structures.
Definition vl_numtypes.h:75