htils 1
A small set of utilities for C programming.
Loading...
Searching...
No Matches
stringmap.c File Reference
#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_tsm_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.
 

Macro Definition Documentation

◆ DEFAULT_CAPACITY

#define DEFAULT_CAPACITY   16

Function Documentation

◆ __sm_insert()

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.

Note
This function is not meant to be run directly, and is called by the sm_ insert() macro.
Parameters
stringmapThe stringmap to insert into.
keyThe key to insert.
valueThe value to associate with the key.
vsizeThe size of the value.
Precondition
  • stringmap and key must be valid and cannot be null.
  • value can't be null, as it would leave to a null dereference.
Returns
The result of the insert (which will be either CREATED or UPDATED in this case).

◆ hash_key()

static u64 hash_key ( const string key)
static

Hash a key into an index.

Hashes a key into an index, using the FNV-1a algorithm.

Parameters
keyThe key to hash.
Precondition
key must be valid and cannot be null.
Returns
The hash generated from the key.

◆ sm_get()

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.

Parameters
stringmapThe stringmap to get from.
keyThe key to get.
Precondition
stringmap and key must be valid and cannot be null.
Returns
The value associated with the key, or null if it doesn't exist or is dead.

◆ sm_grow()

static void sm_grow ( stringmap_t map)
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.

Parameters
mapThe stringmap to grow.
Precondition
map must be valid and cannot be null.

◆ sm_insert_direct()

static void sm_insert_direct ( stringmap_t map,
const string key,
void *  value,
u64  vsize 
)
static

Inserts a key-value pair into the stringmap directly.

By first hashing the key, then iterating through the entries.

Parameters
mapThe stringmap to insert into.
keyThe key to insert.
valueThe value to insert.
vsizeThe size of the value.
Precondition
  • 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.

◆ sm_kill()

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.

Parameters
stringmapThe stringmap to remove from.
keyThe key to remove.
Precondition
stringmap and key must be valid and cannot be null.
Returns
The result of the remove (which will be either KILLED or NOT_FOUND in this case).

◆ sm_new()

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.

Parameters
arenaThe arena to allocate from.
capacityThe capacity of the stringmap.
Precondition
arena must be valid and cannot be null.
Returns
A pointer to the new stringmap.