Sturk 1.0.2
Publish-subscribe C implementation.
Loading...
Searching...
No Matches
dict.h File Reference

Dictionary. More...

#include "st/arith.h"
#include "st/logger/except.h"
#include "st/rbtree.h"
Include dependency graph for dict.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  StDictNode
 Dictionary node. More...
 

Macros

#define st_dict_cast(dict)
 Get the pointer to the StDictNode member of the dictionary entry.
 
#define st_dict_setk(dict, key)
 Set a key string for a dictionary entry.
 
#define st_dict_getk(dict)
 Get the key string from a dictionary entry.
 
#define st_dict_datap(dict)
 Get a pointer to the data member of a dictionary entry.
 
#define st_dict_ins(dict, entry)
 Insert an entry into the dictionary.
 
#define st_dict_find(dict, key)
 In a dictionary, find the entry with the given key.
 
#define st_dict_first(dict)
 Find the first entry of the dictionary traversal.
 
#define st_dict_next(dict)
 Find the next entry of the dictionary traversal.
 
#define ST_DICT(name, type)
 Define the dictionary.
 

Functions

struct StDictNodest_dictnode_ins (struct StDictNode *root, struct StDictNode *entry)
 Insert an entry into the dictionary.
 
struct StDictNodest_dictnode_find (struct StDictNode *root, const char *str)
 In a dictionary, find the entry with the given key.
 
static struct StDictNodest_dictnode_from (struct StRbNode *ptr)
 Cast a StRbNode member out to the containing StDictNode structure.
 

Detailed Description

Dictionary.

This header file provides data types, functions and macros that define and operate on dictionaries. These dictionaries are key-value data structures where the key is always a sequence of characters (ie. string, char array).

Features

  • Intrusive.

Usage

typedef SomeData MyData;
ST_DICT(struct MyDict, MyData);
void push(struct MyDict** dictp, const char* key, MyData val)
{
struct MyDict* entry = malloc(sizeof(*entry));
st_dict_setk(entry, key);
*st_dict_data(entry) = val;
*dictp = st_dict_ins(*dictp, entry);
}
#define st_dict_ins(dict, entry)
Insert an entry into the dictionary.
Definition dict.h:184
#define ST_DICT(name, type)
Define the dictionary.
Definition dict.h:276
#define st_dict_setk(dict, key)
Set a key string for a dictionary entry.
Definition dict.h:126

Macro Definition Documentation

◆ ST_DICT

#define ST_DICT (   name,
  type 
)
Value:
name \
{ \
struct StDictNode dictnode; \
type data; \
}
Dictionary node.
Definition dict.h:288

Define the dictionary.

Parameters
[in]nameThe name of the type used for the dictionary.
[in]typeThe type of the data held by name.

This macro will define a compound type (must be struct or union) name, a type for a dictionary entry that holds the data of the type type.

◆ st_dict_cast

#define st_dict_cast (   dict)
Value:
({ \
__typeof__(dict) _dict2 = (dict); \
\
_dict2 ? &_dict2->dictnode : NULL; \
})

Get the pointer to the StDictNode member of the dictionary entry.

Parameters
[in]dictThe dictionary entry.
Returns
A pointer to the StDictNode.
Note
Compile with the GNU extension to enable a type check for the dict.

◆ st_dict_datap

#define st_dict_datap (   dict)
Value:
({ \
__typeof__(dict) _dict = (dict); \
\
ST_ENSURE(_dict, ST_ERROR, null_param); \
&_dict->data; \
})
@ ST_ERROR
error trace level.
Definition trace.h:80

Get a pointer to the data member of a dictionary entry.

Parameters
[in]dictThe entry.
Returns
The pointer to the data.
Note
Compile with the GNU extension to enable a type check for the dict.

◆ st_dict_find

#define st_dict_find (   dict,
  key 
)
Value:
({ \
__typeof__(dict) _dict = (dict); \
\
st_container_of( \
st_dictnode_find(st_dict_cast(_dict), (key)), \
__typeof__(*dict), dictnode); \
})
#define st_dict_cast(dict)
Get the pointer to the StDictNode member of the dictionary entry.
Definition dict.h:109
struct StDictNode * st_dictnode_find(struct StDictNode *root, const char *str)
In a dictionary, find the entry with the given key.

In a dictionary, find the entry with the given key.

Parameters
[in]dictThe root.
[in]keyThe key string.
Returns
The found entry or NULL if none found.
Note
Compile with the GNU extension to enable a type check for the dict.

◆ st_dict_first

#define st_dict_first (   dict)
Value:
({ \
__typeof__(dict) _dict = (dict); \
\
st_container_of( \
st_rb_first(&dict_cast(_dict)->node, 0)), \
__typeof__(*dict), dictnode); \
})
static struct StDictNode * st_dictnode_from(struct StRbNode *ptr)
Cast a StRbNode member out to the containing StDictNode structure.
Definition dict.h:338
struct StRbNode * st_rb_first(struct StRbNode *node, enum StBstOrder order)
Get the first node of the red-black tree for the given BST order.
#define dict_cast
Definition dict.h:44

Find the first entry of the dictionary traversal.

Parameters
[in]dictAny entry from the dictionary.

For the lexicographical order of the dictionary keys, find the entry with the first key.

Returns
The first entry.
Note
Compile with the GNU extension to enable a type check for the dict.

◆ st_dict_getk

#define st_dict_getk (   dict)
Value:
({ \
__typeof__(dict) _dict = (dict); \
\
ST_ENSURE(_dict, ST_ERROR, null_param); \
_dict->dictnode.str; \
})

Get the key string from a dictionary entry.

Parameters
[in]dictThe entry.
Returns
A key string.
Note
Compile with the GNU extension to enable a type check for the dict.

◆ st_dict_ins

#define st_dict_ins (   dict,
  entry 
)
Value:
({ \
__typeof__(dict) _dict = (dict); \
\
st_container_of( \
st_dict_cast(_dict), st_dict_cast(entry)), \
__typeof__(*dict), dictnode); \
})
struct StDictNode * st_dictnode_ins(struct StDictNode *root, struct StDictNode *entry)
Insert an entry into the dictionary.

Insert an entry into the dictionary.

Parameters
[in,out]dictThe root.
[in,out]entryThe new entry.
Returns
The new root.
Note
Compile with the GNU extension to enable a type check for the dict.

◆ st_dict_next

#define st_dict_next (   dict)
Value:
({ \
__typeof__(dict) _dict = (dict); \
\
st_container_of( \
&dict_cast(_dict)->node, ST_BST_INORDER)), \
__typeof__(*dict), dictnode); \
})
struct StRbNode * st_rb_next(struct StRbNode *node, enum StBstOrder order)
Get the next node for the current red-black tree node and the BST order.
@ ST_BST_INORDER
in-order.
Definition rbtree.h:51

Find the next entry of the dictionary traversal.

For the lexicographical order of the dictionary keys, find the entry with the next key.

Parameters
[in]dictThe entry.
Returns
The next entry.
Note
Compile with the GNU extension to enable a type check for the dict.

◆ st_dict_setk

#define st_dict_setk (   dict,
  key 
)
Value:
({ \
__typeof__(dict) _dict = (dict); \
\
ST_ENSURE(_dict, ST_ERROR, null_param); \
_dict->dictnode.str = (key); \
})

Set a key string for a dictionary entry.

Parameters
[in]dictThe entry.
[in]keyThe key string.
Note
Compile with the GNU extension to enable a type check for the dict.

Function Documentation

◆ st_dictnode_find()

struct StDictNode * st_dictnode_find ( struct StDictNode root,
const char *  str 
)

In a dictionary, find the entry with the given key.

Parameters
[in]rootThe root.
[in]strThe key string.
Returns
The found entry or NULL if none found.

◆ st_dictnode_from()

static inline struct StDictNode * st_dictnode_from ( struct StRbNode ptr)
inlinestatic

Cast a StRbNode member out to the containing StDictNode structure.

Parameters
[in]ptrThe pointer to the StRbNode member.
Returns
A pointer to the StDictNode structure.

◆ st_dictnode_ins()

struct StDictNode * st_dictnode_ins ( struct StDictNode root,
struct StDictNode entry 
)

Insert an entry into the dictionary.

Parameters
[in,out]rootThe root.
[in,out]entryThe new entry.
Returns
The new entry.