|
htils 1
A small set of utilities for C programming.
|
#include <string.h>#include <htils/arena.h>#include <htils/assert.h>#include <htils/basictypes.h>#include <htils/string.h>#include <htils/stringmap.h>Macros | |
| #define | DEFAULT_CAPACITY 16 |
Functions | |
| static u64 | hash_key (const string *key) |
| Hash a key into an index. | |
| static void | sm_insert_direct (stringmap_t *map, const string *key, void *value, u64 vsize) |
| Inserts a key-value pair into the stringmap directly. | |
| static void | sm_grow (stringmap_t *map) |
| Grow a stringmap if it's too small. | |
| stringmap_t * | sm_new (arena_t *arena, const u64 capacity) |
| Initializes a new stringmap. | |
| stringmap_result_t | __sm_insert (stringmap_t *map, const string *key, const void *value, const u64 vsize) |
Insert key and value into the stringmap. | |
| stringmap_result_t | sm_kill (stringmap_t *map, const string *key) |
Remove / Kill entry at key from the stringmap. | |
| void * | sm_get (stringmap_t *map, const string *key) |
| Get a V from the stringmap. | |
| #define DEFAULT_CAPACITY 16 |
| stringmap_result_t __sm_insert | ( | stringmap_t * | map, |
| const string * | key, | ||
| const void * | value, | ||
| u64 | vsize | ||
| ) |
Insert key and value into the stringmap.
Hashes and duplicates the key and sets the value, if the key already exists, it will be updated, if the capacity is too small, it will be automatically grown.
| stringmap | The stringmap to insert into. |
| key | The key to insert. |
| value | The value to associate with the key. |
| vsize | The size of the value. |
stringmap and key must be valid and cannot be null.value can't be null, as it would leave to a null dereference.Hash a key into an index.
Hashes a key into an index, using the FNV-1a algorithm.
| key | The key to hash. |
key must be valid and cannot be null.| void * sm_get | ( | stringmap_t * | map, |
| const string * | key | ||
| ) |
Get a V from the stringmap.
Gets an entry from the stringmap, if the entry doesn't exist, it will be null.
| stringmap | The stringmap to get from. |
| key | The key to get. |
stringmap and key must be valid and cannot be null.
|
static |
Grow a stringmap if it's too small.
By doubling the capacity, allocating a new block using arena_alloc(), and copying over all occupied entries.
| map | The stringmap to grow. |
map must be valid and cannot be null.
|
static |
Inserts a key-value pair into the stringmap directly.
By first hashing the key, then iterating through the entries.
| map | The stringmap to insert into. |
| key | The key to insert. |
| value | The value to insert. |
| vsize | The size of the value. |
map, key, and value must be valid and cannot be null.vsize must be greater than 0 and the explicit size of the value, since @value is a void * it has to be provided. | stringmap_result_t sm_kill | ( | stringmap_t * | map, |
| const string * | key | ||
| ) |
Remove / Kill entry at key from the stringmap.
Marks an entry as DEAD, if the key doesn't exist, it will return NOT_FOUND, otherwise it will return KILLED.
| stringmap | The stringmap to remove from. |
| key | The key to remove. |
stringmap and key must be valid and cannot be null.| stringmap_t * sm_new | ( | arena_t * | arena, |
| const u64 | capacity | ||
| ) |
Initializes a new stringmap.
Optionally use a nullable type for stringmaps.
With the capacity, and arena, if the given capacity is 0, it will use the built in default capacity, which is 16.
arena must be valid and cannot be null.