Sturk 1.0.2
Publish-subscribe C implementation.
Loading...
Searching...
No Matches
dict.h
Go to the documentation of this file.
1/*
2BSD 3-Clause License
3
4Copyright (c) 2025, Szymon Turno
5
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met:
8
91. Redistributions of source code must retain the above copyright notice, this
10 list of conditions and the following disclaimer.
11
122. Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
163. Neither the name of the copyright holder nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30*/
31
67#ifndef ST_DICT_H
68#define ST_DICT_H
69
70#include "st/arith.h"
71#include "st/logger/except.h"
72#include "st/rbtree.h"
73
74#ifdef __STRICT_ANSI__
75
76#define st_dict_cast(dict) ((struct StDictNode*)(dict))
77
78#define st_dict_setk(dict, key) ((dict)->dictnode.str = (key))
79
80#define st_dict_getk(dict) ((dict)->dictnode.str)
81
82#define st_dict_datap(dict) (&(dict)->data)
83
84#define st_dict_ins(dict, node) \
85 ((void*)st_dictnode_ins(st_dict_cast(dict), st_dict_cast(node)))
86
87#define st_dict_find(dict, key) \
88 ((void*)st_dictnode_find(st_dict_cast(dict), (key)))
89
90#define st_dict_first(dict) \
91 ((void*)(st_dictnode_from(st_rb_first(&dict_cast(dict)->node, 0))))
92
93#define st_dict_next(dict) \
94 ((void*)(st_dictnode_from(st_rb_next(&dict_cast(dict)->node, 0))))
95
96#else /* not defined: __STRICT_ANSI__ */
97
109#define st_dict_cast(dict) \
110 ({ \
111 __typeof__(dict) _dict2 = (dict); \
112 \
113 _dict2 ? &_dict2->dictnode : NULL; \
114 })
115
126#define st_dict_setk(dict, key) \
127 ({ \
128 __typeof__(dict) _dict = (dict); \
129 \
130 ST_ENSURE(_dict, ST_ERROR, null_param); \
131 _dict->dictnode.str = (key); \
132 })
133
145#define st_dict_getk(dict) \
146 ({ \
147 __typeof__(dict) _dict = (dict); \
148 \
149 ST_ENSURE(_dict, ST_ERROR, null_param); \
150 _dict->dictnode.str; \
151 })
152
164#define st_dict_datap(dict) \
165 ({ \
166 __typeof__(dict) _dict = (dict); \
167 \
168 ST_ENSURE(_dict, ST_ERROR, null_param); \
169 &_dict->data; \
170 })
171
184#define st_dict_ins(dict, entry) \
185 ({ \
186 __typeof__(dict) _dict = (dict); \
187 \
188 st_container_of( \
189 st_dictnode_ins( \
190 st_dict_cast(_dict), st_dict_cast(entry)), \
191 __typeof__(*dict), dictnode); \
192 })
193
206#define st_dict_find(dict, key) \
207 ({ \
208 __typeof__(dict) _dict = (dict); \
209 \
210 st_container_of( \
211 st_dictnode_find(st_dict_cast(_dict), (key)), \
212 __typeof__(*dict), dictnode); \
213 })
214
229#define st_dict_first(dict) \
230 ({ \
231 __typeof__(dict) _dict = (dict); \
232 \
233 st_container_of( \
234 st_dictnode_from( \
235 st_rb_first(&dict_cast(_dict)->node, 0)), \
236 __typeof__(*dict), dictnode); \
237 })
238
253#define st_dict_next(dict) \
254 ({ \
255 __typeof__(dict) _dict = (dict); \
256 \
257 st_container_of( \
258 st_dictnode_from(st_rb_next( \
259 &dict_cast(_dict)->node, ST_BST_INORDER)), \
260 __typeof__(*dict), dictnode); \
261 })
262
263#endif /* __STRICT_ANSI__ */
264
276#define ST_DICT(name, type) \
277 name \
278 { \
279 struct StDictNode dictnode; \
280 type data; \
281 }
282
295
301 char* str;
302};
303
314struct StDictNode*
315st_dictnode_ins(struct StDictNode* root, struct StDictNode* entry);
316
327struct StDictNode* st_dictnode_find(struct StDictNode* root, const char* str);
328
338static inline struct StDictNode* st_dictnode_from(struct StRbNode* ptr)
339{
340 return st_container_of(ptr, struct StDictNode, node);
341}
342
343#endif /* ST_DICT_H */
Exceptions.
Basic arithmetic operations.
#define st_container_of(ptr, type, member)
Cast a member of a structure out to the containing structure.
Definition arith.h:91
struct StDictNode * st_dictnode_ins(struct StDictNode *root, struct StDictNode *entry)
Insert an entry into the dictionary.
struct StDictNode * st_dictnode_find(struct StDictNode *root, const char *str)
In a dictionary, find the entry with the given key.
static struct StDictNode * st_dictnode_from(struct StRbNode *ptr)
Cast a StRbNode member out to the containing StDictNode structure.
Definition dict.h:338
Red-black tree.
Dictionary node.
Definition dict.h:288
char * str
The key string.
Definition dict.h:301
struct StRbNode node
The red-black tree node.
Definition dict.h:294
Node of a red-black tree.