cdsa
Arena allocators and friends
Loading...
Searching...
No Matches
dict.h File Reference
#include <stddef.h>
#include "arena.h"

Data Structures

struct  Dict
 Represents a dictionary of key-data pairs. More...
 
struct  DictItem
 Represents a single item of a dict. More...
 

Macros

#define dict_for_each(item, self)
 Iterate over all items of a dict.
 

Typedefs

typedef void * DictDataCopy(Arena *, void *, const void *, long)
 Data copy function.
 

Functions

static Dict dict_create (Arena *arena, long size)
 Create a new dict.
 
static void * dict_insert (Dict *self, const void *key, long size, void *data)
 Insert a new item into a dict.
 
static void * dict_remove (Dict *self, const void *key, long size)
 Remove an item from a dict.
 
static void * dict_find (const Dict *self, const void *key, long size)
 Find an item of a dict.
 
static Dict dict_clone (const Dict *self, Arena *arena)
 Create a clone of a dict.
 
static DictItemdict_items (const Dict *self, Arena *arena)
 Retrieve an array of dict items.
 

Variables

static constexpr long dict_hash_shift = 2
 Number of hash bits to shift out.
 
static constexpr long dict_branch_select = 64 - dict_hash_shift
 Bit mask to select branch.
 

Macro Definition Documentation

◆ dict_for_each

#define dict_for_each (   item,
  self 
)
Value:
for (auto(item) = (self)->begin; item; (item) = (item)->next) \
if ((item)->key.size)

Iterate over all items of a dict.

Parameters
itemCurrent dict item
selfPointer to a dict

Function Documentation

◆ dict_create()

static Dict dict_create ( Arena arena,
long  size 
)
static

Create a new dict.

Parameters
arenaPointer to an arena allocator
sizeSize of item data in bytes (optional)
Returns
New dict instance
Note
If size == 0, the data pointers will be directly assigned rather than copied

◆ dict_insert()

static void * dict_insert ( Dict self,
const void *  key,
long  size,
void *  data 
)
static

Insert a new item into a dict.

Parameters
selfPointer to a dict
keyPointer to the key data
sizeSize of the key data in bytes (optional)
dataPointer to the item data
Returns
Pointer to the item data if the key already exists, or nullptr if it is a new key
Note
If size == 0, key is assumed to be a null-terminated string

◆ dict_remove()

static void * dict_remove ( Dict self,
const void *  key,
long  size 
)
static

Remove an item from a dict.

Parameters
selfPointer to a dict
keyPointer to the key data
sizeSize of the key data in bytes (optional)
Returns
Pointer to the item data, or nullptr if the key is not found
Note
If size == 0, key is assumed to be a null-terminated string

◆ dict_find()

static void * dict_find ( const Dict self,
const void *  key,
long  size 
)
static

Find an item of a dict.

Parameters
selfPointer to a dict
keyPointer to the key data
sizeSize of the key data in bytes (optional)
Returns
Pointer to the item data, or nullptr if the key is not found
Note
If size == 0, key is assumed to be a null-terminated string

◆ dict_clone()

static Dict dict_clone ( const Dict self,
Arena arena 
)
static

Create a clone of a dict.

Parameters
selfPointer to a dict
arenaPointer to an arena allocator (optional)
Returns
Cloned dict instance
Note
If no arena allocator is passed, the arena allocator of the dict is used

◆ dict_items()

static DictItem * dict_items ( const Dict self,
Arena arena 
)
static

Retrieve an array of dict items.

Parameters
selfPointer to a dict
arenaPointer to an arena allocator (optional)
Returns
Pointer to an array of items
Note
If no arena allocator is passed, the arena allocator of the dict is used