htils 1
A small set of utilities for C programming.
Loading...
Searching...
No Matches
stringmap.h File Reference
#include <htils/arena.h>
#include <htils/basictypes.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)   (val), sizeof(*val)
 
#define sm_insert(map, key, value)   __sm_insert(map, key, SM_VAL(value))
 

Typedefs

typedef enum stringmap_entry_state stringmap_entry_state_t
 The state of a stringmap entry.
 
typedef enum stringmap_result stringmap_result_t
 The result of a stringmap operation.
 
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 }
 The state of a stringmap entry. More...
 
enum  stringmap_result { CREATED , UPDATED , NOT_FOUND , KILLED }
 The result of a stringmap operation. More...
 

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 / 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

◆ sm_insert

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

◆ SM_VAL

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

Typedef Documentation

◆ stringmap_entry_state_t

The state of a stringmap entry.

Parameters
EMPTYThe entry is empty.
OCCUPIEDThe entry is occupied.
DEADThe entry is dead.

◆ stringmap_entry_t

A stringmap entry.

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

◆ stringmap_result_t

The result of a stringmap operation.

Parameters
CREATEDThe entry was created.
UPDATEDThe entry was updated.
NOT_FOUNDThe entry was not found.
KILLEDThe entry was killed.

◆ 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

The state of a stringmap entry.

Parameters
EMPTYThe entry is empty.
OCCUPIEDThe entry is occupied.
DEADThe entry is dead.
Enumerator
EMPTY 
OCCUPIED 
DEAD 

◆ stringmap_result

The result of a stringmap operation.

Parameters
CREATEDThe entry was created.
UPDATEDThe entry was updated.
NOT_FOUNDThe entry was not found.
KILLEDThe entry was killed.
Enumerator
CREATED 
UPDATED 
NOT_FOUND 
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
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).

◆ 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_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.