Veritable Lasagna
An Allocator & Data Structure Library for C.
Loading...
Searching...
No Matches
vl_hashtable.h
Go to the documentation of this file.
1#ifndef VL_HASHTABLE_H
2#define VL_HASHTABLE_H
3
4#include "vl_arena.h"
5#include "vl_hash.h"
6
7#define VL_HASHTABLE_ITER_INVALID 0
8
9#ifndef VL_HASHTABLE_RESIZE_FACTOR
10#define VL_HASHTABLE_RESIZE_FACTOR 0.8
11#endif
12
13#ifndef VL_HASHTABLE_DEFAULT_SIZE
14#define VL_HASHTABLE_DEFAULT_SIZE 128
15#endif
16
23#define VL_HASHTABLE_FOREACH(table, trackVar) for(vl_hash_iter trackVar = vlHashTableFront(table); trackVar != VL_HASHTABLE_ITER_INVALID; trackVar = vlHashTableNext(table, trackVar))
24
26
45typedef struct{
46 vl_memory* table; //holds mapping from hash values to collision list heads in the arena
47 vl_arena data; //holds the node key/value data
48 vl_hash_function hashFunc; //hash function; hashes keys
49
50 vl_dsidx_t totalElements; //total number of mapped elements
52
57typedef struct{
58 vl_memsize_t keySize;
59 vl_memsize_t valSize;
60 vl_hash keyHash;
61 vl_arena_ptr next;
62} vl_hashtable_header;
63
75void vlHashTableInit(vl_hashtable* table, vl_hash_function hashFunc);
76
86void vlHashTableFree(vl_hashtable* table);
87
100
109
121vl_hash_iter vlHashTableInsert(vl_hashtable* table, const void* key, vl_memsize_t keySize, vl_memsize_t dataSize);
122
130void vlHashTableRemoveKey(vl_hashtable* table, const void* key, vl_memsize_t keyLen);
131
139
149void vlHashTableClear(vl_hashtable* table);
150
151
168
183
196
210void vlHashTableReserve(vl_hashtable* table, vl_memsize_t buckets, vl_memsize_t heapSize);
211
220vl_hash_iter vlHashTableFind(vl_hashtable* table, const void* key, vl_memsize_t keySize);
221
231
241
249
258
259//notice "back" and "prev" functions are omitted here.
260//considering there is no well-defined order that should be expected from this structure,
261//it shouldn't make much difference to iterate forward or backward, so long as all
262//elements are encompassed in the iteration.
263
264//that said, it can often be observed that iteration occurs in the reverse order of
265//insertion *up until* the first resize of the table.
266//this is due to how the underlying arena behaves, slicing memory off the end of the first free block of memory.
267
268#endif //VL_HASHTABLE_H
vl_uintptr_t vl_arena_ptr
Definition vl_arena.h:6
An arena allocator.
Definition vl_arena.h:57
vl_hash(* vl_hash_function)(const void *data, vl_memsize_t dataSize)
Hash function typedef.
Definition vl_hash.h:19
vl_ularge_t vl_hash
Hash function return type.
Definition vl_hash.h:9
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:66
vl_hashtable * vlHashTableClone(const vl_hashtable *table, vl_hashtable *dest)
Clones the specified hashtable to another.
Definition vl_hashtable.c:206
vl_memory * table
Definition vl_hashtable.h:46
vl_dsidx_t totalElements
Definition vl_hashtable.h:50
vl_hash_iter vlHashTableFind(vl_hashtable *table, const void *key, vl_memsize_t keySize)
Definition vl_hashtable.c:135
void vlHashTableInit(vl_hashtable *table, vl_hash_function hashFunc)
Initializes the specified table. Specifies the hash function to use on keys.
Definition vl_hashtable.c:42
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:200
vl_hash_iter vlHashTableNext(vl_hashtable *table, vl_hash_iter iter)
Definition vl_hashtable.c:323
vl_arena_ptr vl_hash_iter
Definition vl_hashtable.h:25
vl_arena data
Definition vl_hashtable.h:47
vl_hash_function hashFunc
Definition vl_hashtable.h:48
void vlHashTableDelete(vl_hashtable *table)
Definition vl_hashtable.c:61
void vlHashTableFree(vl_hashtable *table)
Frees the specified table.
Definition vl_hashtable.c:50
const vl_transient * vlHashTableSampleKey(vl_hashtable *table, vl_hash_iter iter, vl_memsize_t *size)
Definition vl_hashtable.c:287
int vlHashTableCopy(vl_hashtable *src, vl_hashtable *dest)
Copies the entirety of one hashtable to another.
Definition vl_hashtable.c:237
vl_transient * vlHashTableSampleValue(vl_hashtable *table, vl_hash_iter iter, vl_memsize_t *size)
Definition vl_hashtable.c:294
vl_hash_iter vlHashTableFront(vl_hashtable *table)
Definition vl_hashtable.c:301
void vlHashTableRemoveIter(vl_hashtable *table, vl_hash_iter iter)
Definition vl_hashtable.c:161
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:256
void vlHashTableRemoveKey(vl_hashtable *table, const void *key, vl_memsize_t keyLen)
Definition vl_hashtable.c:157
vl_hashtable * vlHashTableNew(vl_hash_function func)
Allocates on the heap, initializes, and returns a new hash table. Specifies the hash function to use ...
Definition vl_hashtable.c:55
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:223
A dynamically-sized hash table.
Definition vl_hashtable.h:45
VL_MEMORY_SIZE_T vl_memsize_t
Definition vl_memory.h:18
VL_MEMORY_T vl_memory
Definition vl_memory.h:76
VL_MEMORY_T vl_transient
Definition vl_memory.h:85
VL_STRUCTURE_INDEX_T vl_dsidx_t
Index type for data structures.
Definition vl_numtypes.h:13