htils 1
A small set of utilities for C programming.
Loading...
Searching...
No Matches
stringmap.h File Reference
#include <htils/basictypes.h>
#include <htils/arena.h>
#include <htils/string.h>

Go to the source code of this file.

Data Structures

struct  stringmap_entry
 A stringmap entry. More...
struct  stringmap
 A stringmap. More...

Macros

#define SM_VAL(val)
#define sm_insert(map, key, value)

Typedefs

typedef enum stringmap_entry_state stringmap_entry_state_t
typedef enum stringmap_result stringmap_result_t
typedef struct stringmap_entry stringmap_entry_t
 A stringmap entry.
typedef struct stringmap stringmap_t
 A stringmap.

Enumerations

enum  stringmap_entry_state { EMPTY , OCCUPIED , DEAD }
enum  stringmap_result { CREATED , UPDATED , NOT_FOUND , KILLED }

Functions

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, u64 vsize)
 Insert key and value into the stringmap.
stringmap_result_t sm_kill (stringmap_t *map, const string *key)
 Remove the entry at key from the stringmap.
void * sm_get (stringmap_t *map, const string *key)
 Get a value from the stringmap.

Macro Definition Documentation

◆ sm_insert

#define sm_insert ( map,
key,
value )
Value:
__sm_insert(map, key, SM_VAL(value))
stringmap_result_t __sm_insert(stringmap_t *map, const string *key, const void *value, u64 vsize)
Insert key and value into the stringmap.
Definition stringmap.c:141
#define SM_VAL(val)
Definition stringmap.h:133

◆ SM_VAL

#define SM_VAL ( val)
Value:
(val), sizeof(*val)

Typedef Documentation

◆ stringmap_entry_state_t

◆ stringmap_entry_t

A stringmap entry.

Parameters
keyThe key of the entry.
valueThe value of the entry.
vsizeThe size of the value.
stateThe state of the entry.

◆ stringmap_result_t

◆ stringmap_t

typedef struct stringmap stringmap_t

A stringmap.

Parameters
arenaThe arena to allocate from.
entriesThe entries of the stringmap.
capacityThe capacity of the stringmap.
countThe amount of entries in the stringmap.
dead_entriesThe amount of dead entries of the stringmap.

Enumeration Type Documentation

◆ stringmap_entry_state

Enumerator
EMPTY 

The state of a stringmap entry.

The entry is empty.

OCCUPIED 

The entry is occupied.

DEAD 

The entry is dead.

◆ stringmap_result

Enumerator
CREATED 

The result of a stringmap operation.

The entry was created.

UPDATED 

The entry was updated.

NOT_FOUND 

The entry was not found.

KILLED 

The entry was killed.

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
mapThe stringmap to insert into.
keyThe key to insert.
valueThe value to associate with the key.
vsizeThe size of the value.
Precondition
  • map and key must be valid and cannot be null.
  • value can't be null, as it would lead to a null dereference.
Returns
The result of the insert (which will be either CREATED or UPDATED in this case).

◆ sm_get()

void * sm_get ( stringmap_t * map,
const string * key )

Get a value from the stringmap.

Returns the value stored at key, or null if the key is absent or the entry is dead.

Parameters
mapThe stringmap to get from.
keyThe key to get.
Precondition
map 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_kill()

stringmap_result_t sm_kill ( stringmap_t * map,
const string * key )

Remove the 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
mapThe stringmap to remove from.
keyThe key to remove.
Precondition
map 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.