WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
zhash.h
Go to the documentation of this file.
1/* Copyright (C) 2013-2016, The Regents of The University of Michigan.
2All rights reserved.
3This software was developed in the APRIL Robotics Lab under the
4direction of Edwin Olson, ebolson@umich.edu. This software may be
5available under alternative licensing terms; contact the address above.
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met:
81. Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
102. Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
13THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23The views and conclusions contained in the software and documentation are those
24of the authors and should not be interpreted as representing official policies,
25either expressed or implied, of the Regents of The University of Michigan.
26*/
27
28#pragma once
29
30#include <stdint.h>
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
36#include "zarray.h"
37
38
39/**
40 * A hash table for structs and primitive types that stores entries by value.
41 * - The size of the key/values must be known at instantiation time, and remain fixed.
42 * e.g. for pointers: zhash_create(sizeof(void*), sizeof(void*)....)
43 * for structs: zhash_create(sizeof(struct key_struct), sizeof(struct value_struct)...)
44 * for bytes: zhash_create(sizeof(uint8_t), sizeof(uint8_t)...)
45 * - Entries are copied by value. This means you must always pass a reference to the start
46 * of 'key_size' and 'value_size' bytes, which you have already malloc'd or stack allocated
47 * - This data structure can be used to store types of any size, from bytes & doubles to
48 * user defined structs
49 * Note: if zhash stores pointers, user must be careful to manually manage the lifetime
50 * of the memory they point to.
51 *
52 */
53
54typedef struct zhash zhash_t;
55
56// The contents of the iterator should be considered private. However,
57// since our usage model prefers stack-based allocation of iterators,
58// we must publicly declare them.
60{
62 const zhash_t *czh;
63 int last_entry; // points to the last entry returned by _next
64};
65
67
68/**
69 * Create, initializes, and returns an empty hash table structure. It is the
70 * caller's responsibility to call zhash_destroy() on the returned array when it
71 * is no longer needed.
72 *
73 * The size of values used in the hash and equals function must match 'keysz'.
74 * I.e. if keysz = sizeof(uint64_t), then hash() and equals() should accept
75 * parameters as *uint64_t.
76 */
77zhash_t *zhash_create(size_t keysz, size_t valuesz,
78 uint32_t(*hash)(const void *a),
79 int(*equals)(const void *a, const void *b));
80
81/**
82 * Frees all resources associated with the hash table structure which was
83 * created by zhash_create(). After calling, 'zh' will no longer be valid for storage.
84 *
85 * If 'zh' contains pointer data, it is the caller's responsibility to manage
86 * the resources pointed to by those pointers.
87 */
89
90/**
91 * Creates and returns a new identical copy of the zhash (i.e. a "shallow" copy).
92 * If you're storing pointers, be sure not to double free their pointees!
93 * It is the caller's responsibility to call zhash_destroy() on the returned array
94 * when it is no longer needed (in addition to the zhash_destroy() call for the
95 * original zhash).
96 */
97zhash_t * zhash_copy(const zhash_t* other);
98
99/**
100 * Determines whether the supplied key value exists as an entry in the zhash
101 * table. If zhash stores pointer types as keys, this function can differentiate
102 * between a non-existent key and a key mapped to NULL.
103 * Returns 1 if the supplied key exists in the zhash table, else 0.
104 */
105int zhash_contains(const zhash_t *zh, const void *key);
106
107/**
108 * Retrieves the value for the given key, if it exists, by copying its contents
109 * into the space pointed to by 'out_value', which must already be allocated.
110 * Returns 1 if the supplied key exists in the table, else 0, in which case
111 * the contents of 'out_value' will be unchanged.
112 */
113int zhash_get(const zhash_t *zh, const void *key, void *out_value);
114
115/**
116 * Similar to zhash_get(), but more dangerous. Provides a pointer to the zhash's
117 * internal storage. This can be used to make simple modifications to
118 * the underlying data while avoiding the memcpys associated with
119 * zhash_get and zhash_put. However, some zhash operations (that
120 * resize the underlying storage, in particular) render this pointer
121 * invalid. For maximum safety, call no other zhash functions for the
122 * period during which you intend to use the pointer.
123 * 'out_p' should be a pointer to the pointer which will be set to the internal
124 * data address.
125 */
126int zhash_get_volatile(const zhash_t *zh, const void *key, void *out_p);
127
128/**
129 * Adds a key/value pair to the hash table, if the supplied key does not already
130 * exist in the table, or overwrites the value for the supplied key if it does
131 * already exist. In the latter case, the previous contents of the key and value
132 * will be copied into the spaces pointed to by 'oldkey' and 'oldvalue', respectively,
133 * if they are not NULL.
134 *
135 * The key/value is added to / updated in the hash table by copying 'keysz' bytes
136 * from the data pointed to by 'key' and 'valuesz' bytes from the data pointed
137 * to by 'value'. It is up to the caller to manage the memory allocation of the
138 * passed-in values, zhash will store and manage a copy.
139 *
140 * NOTE: If the key is a pointer type (such as a string), the contents of the
141 * data that it points to must not be modified after the call to zhash_put(),
142 * or future zhash calls will not successfully locate the key (using either its
143 * previous or new value).
144 *
145 * NOTE: When using array data as a key (such as a string), the array should not
146 * be passed directly or it will cause a segmentation fault when it is dereferenced.
147 * Instead, pass a pointer which points to the array location, i.e.:
148 * char key[strlen];
149 * char *keyptr = key;
150 * zhash_put(zh, &keyptr, ...)
151 *
152 * Example:
153 * char * key = ...;
154 * zarray_t * val = ...;
155 * char * old_key = NULL;
156 * zarray_t * old_val = NULL;
157 * if (zhash_put(zh, &key, &val, &old_key, &old_value))
158 * // manage resources for old_key and old_value
159 *
160 * Returns 1 if the supplied key previously existed in the table, else 0, in
161 * which case the data pointed to by 'oldkey' and 'oldvalue' will be set to zero
162 * if they are not NULL.
163 */
164int zhash_put(zhash_t *zh, const void *key, const void *value, void *oldkey, void *oldvalue);
165
166/**
167 * Removes from the zhash table the key/value pair for the supplied key, if
168 * it exists. If it does, the contents of the key and value will be copied into
169 * the spaces pointed to by 'oldkey' and 'oldvalue', respectively, if they are
170 * not NULL. If the key does not exist, the data pointed to by 'oldkey' and
171 * 'oldvalue' will be set to zero if they are not NULL.
172 *
173 * Returns 1 if the key existed and was removed, else 0, indicating that the
174 * table contents were not changed.
175 */
176int zhash_remove(zhash_t *zh, const void *key, void *oldkey, void *oldvalue);
177
178/**
179 * Removes all entries in the has table to create the equivalent of starting from
180 * a zhash_create(), using the same size parameters. If any elements need to be
181 * freed manually, this will need to occur before calling clear.
182 */
184
185/**
186 * Retrieves the current number of key/value pairs currently contained in the
187 * zhash table, or 0 if the table is empty.
188 */
190
191/**
192 * Initializes an iterator which can be used to traverse the key/value pairs of
193 * the supplied zhash table via successive calls to zhash_iterator_next() or
194 * zhash_iterator_next_volatile(). The iterator can also be used to remove elements
195 * from the zhash with zhash_iterator_remove().
196 *
197 * Any modifications to the zhash table structure will invalidate the
198 * iterator, with the exception of zhash_iterator_remove().
199 */
201
202/**
203 * Initializes an iterator which can be used to traverse the key/value pairs of
204 * the supplied zhash table via successive calls to zhash_iterator_next() or
205 * zhash_iterator_next_volatile().
206 *
207 * An iterator initialized with this function cannot be used with
208 * zhash_iterator_remove(). For that you must use zhash_iterator_init().
209 *
210 * Any modifications to the zhash table structure will invalidate the
211 * iterator.
212 */
214
215/**
216 * Retrieves the next key/value pair from a zhash table via the (previously-
217 * initialized) iterator. Copies the key and value data into the space
218 * pointed to by outkey and outvalue, respectively, if they are not NULL.
219 *
220 * Returns 1 if the call retrieved the next available key/value pair, else 0
221 * indicating that no entries remain, in which case the contents of outkey and
222 * outvalue will remain unchanged.
223 */
224int zhash_iterator_next(zhash_iterator_t *zit, void *outkey, void *outvalue);
225
226/**
227 * Similar to zhash_iterator_next() except that it retrieves a pointer to zhash's
228 * internal storage. This can be used to avoid the memcpys associated with
229 * zhash_iterator_next(). Call no other zhash functions for the
230 * period during which you intend to use the pointer.
231 * 'outkey' and 'outvalue' should be pointers to the pointers which will be set
232 * to the internal data addresses.
233 *
234 * Example:
235 * key_t *outkey;
236 * value_t *outvalue;
237 * if (zhash_iterator_next_volatile(&zit, &outkey, &outvalue))
238 * // access internal key and value storage via outkey and outvalue
239 *
240 * Returns 1 if the call retrieved the next available key/value pair, else 0
241 * indicating that no entries remain, in which case the pointers outkey and
242 * outvalue will remain unchanged.
243 */
244int zhash_iterator_next_volatile(zhash_iterator_t *zit, void *outkey, void *outvalue);
245
246/**
247 * Removes from the zhash table the key/value pair most recently returned via
248 * a call to zhash_iterator_next() or zhash_iterator_next_volatile() for the
249 * supplied iterator.
250 *
251 * Requires that the iterator was initialized with zhash_iterator_init(),
252 * not zhash_iterator_init_const().
253 */
255
256/**
257 * Calls the supplied function with a pointer to every key in the hash table in
258 * turn. The function will be passed a pointer to the table's internal storage
259 * for the key, which the caller should not modify, as the hash table will not be
260 * re-indexed. The function may be NULL, in which case no action is taken.
261 */
262void zhash_map_keys(zhash_t *zh, void (*f)(void *));
263
264/**
265 * Calls the supplied function with a pointer to every value in the hash table in
266 * turn. The function will be passed a pointer to the table's internal storage
267 * for the value, which the caller may safely modify. The function may be NULL,
268 * in which case no action is taken.
269 */
270void zhash_map_values(zhash_t *zh, void (*f)(void *));
271
272/**
273 * Calls the supplied function with a copy of every key in the hash table in
274 * turn. While zhash_map_keys() passes a pointer to internal storage, this function
275 * passes a copy of the actual storage. If the zhash stores pointers to data,
276 * functions like free() can be used directly with zhash_vmap_keys().
277 * The function may be NULL, in which case no action is taken.
278 *
279 * NOTE: zhash_vmap_keys() can only be used with pointer-data keys.
280 * Use with non-pointer keys (i.e. integer, double, etc.) will likely cause a
281 * segmentation fault.
282 */
283void zhash_vmap_keys(zhash_t *vh, void (*f)(void *));
284
285/**
286 * Calls the supplied function with a copy of every value in the hash table in
287 * turn. While zhash_map_values() passes a pointer to internal storage, this function
288 * passes a copy of the actual storage. If the zhash stores pointers to data,
289 * functions like free() can be used directly with zhash_vmap_values().
290 * The function may be NULL, in which case no action is taken.
291 *
292 * NOTE: zhash_vmap_values() can only be used with pointer-data values.
293 * Use with non-pointer values (i.e. integer, double, etc.) will likely cause a
294 * segmentation fault.
295 */
296void zhash_vmap_values(zhash_t *vh, void (*f)(void *));
297
298/**
299 * Returns an array which contains copies of all of the hash table's keys, in no
300 * particular order. It is the caller's responsibility to call zarray_destroy()
301 * on the returned structure when it is no longer needed.
302 */
304
305/**
306 * Returns an array which contains copies of all of the hash table's values, in no
307 * particular order. It is the caller's responsibility to call zarray_destroy()
308 * on the returned structure when it is no longer needed.
309 */
311
312/**
313 * Defines a hash function which will calculate a zhash value for uint32_t input
314 * data. Can be used with zhash_create() for a key size of sizeof(uint32_t).
315 */
316uint32_t zhash_uint32_hash(const void *a);
317
318/**
319 * Defines a function to compare zhash values for uint32_t input data.
320 * Can be used with zhash_create() for a key size of sizeof(uint32_t).
321 */
322int zhash_uint32_equals(const void *a, const void *b);
323
324/**
325 * Defines a hash function which will calculate a zhash value for uint64_t input
326 * data. Can be used with zhash_create() for a key size of sizeof(uint64_t).
327 */
328uint32_t zhash_uint64_hash(const void *a);
329
330/**
331 * Defines a function to compare zhash values for uint64_t input data.
332 * Can be used with zhash_create() for a key size of sizeof(uint64_t).
333 */
334int zhash_uint64_equals(const void *a, const void *b);
335
336/////////////////////////////////////////////////////
337// functions for keys that can be compared via their pointers.
338/**
339 * Defines a hash function which will calculate a zhash value for pointer input
340 * data. Can be used with zhash_create() for a key size of sizeof(void*). Will
341 * use only the pointer value itself for computing the hash value.
342 */
343uint32_t zhash_ptr_hash(const void *a);
344
345/**
346 * Defines a function to compare zhash values for pointer input data.
347 * Can be used with zhash_create() for a key size of sizeof(void*).
348 */
349int zhash_ptr_equals(const void *a, const void *b);
350
351/////////////////////////////////////////////////////
352// Functions for string-typed keys
353/**
354 * Defines a hash function which will calculate a zhash value for string input
355 * data. Can be used with zhash_create() for a key size of sizeof(char*). Will
356 * use the contents of the string in computing the hash value.
357 */
358uint32_t zhash_str_hash(const void *a);
359
360/**
361 * Defines a function to compare zhash values for string input data.
362 * Can be used with zhash_create() for a key size of sizeof(char*).
363 */
364int zhash_str_equals(const void *a, const void *b);
365
367
368 static inline zhash_t *zhash_str_str_create(void)
369 {
370 return zhash_create(sizeof(char*), sizeof(char*),
372 }
373
374
375
376// for zhashes that map strings to strings, this is a convenience
377// function that allows easier retrieval of values. NULL is returned
378// if the key is not found.
379static inline char *zhash_str_str_get(zhash_t *zh, const char *key)
380{
381 char *value;
382 if (zhash_get(zh, &key, &value))
383 return value;
384 return NULL;
385}
386
387 static inline void zhash_str_str_put(zhash_t *zh, char *key, char *value)
388 {
389 char *oldkey, *oldval;
390 if (zhash_put(zh, &key, &value, &oldkey, &oldval)) {
391 free(oldkey);
392 free(oldval);
393 }
394 }
395
396 static inline void zhash_str_str_destroy(zhash_t *zh)
397 {
399 zhash_iterator_init(zh, &zit);
400
401 char *key, *value;
402 while (zhash_iterator_next(&zit, &key, &value)) {
403 free(key);
404 free(value);
405 }
406
408 }
409
410
411static inline uint32_t zhash_int_hash(const void *_a)
412{
413 assert(_a != NULL);
414
415 uint32_t a = *((int*) _a);
416 return a;
417}
418
419static inline int zhash_int_equals(const void *_a, const void *_b)
420{
421 assert(_a != NULL);
422 assert(_b != NULL);
423
424 int a = *((int*) _a);
425 int b = *((int*) _b);
426
427 return a==b;
428}
429
430#ifdef __cplusplus
431}
432#endif
and restrictions which apply to each piece of software is included later in this file and or inside of the individual applicable source files The disclaimer of warranty in the WPILib license above applies to all code in and nothing in any of the other licenses gives permission to use the names of FIRST nor the names of the WPILib contributors to endorse or promote products derived from this software The following pieces of software have additional or alternate and or and nanopb were all modified for use in Google Inc All rights reserved Redistribution and use in source and binary with or without are permitted provided that the following conditions are this list of conditions and the following disclaimer *Redistributions in binary form must reproduce the above copyright this list of conditions and the following disclaimer in the documentation and or other materials provided with the distribution *Neither the name of Google Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED BUT NOT LIMITED THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY OR CONSEQUENTIAL WHETHER IN STRICT OR EVEN IF ADVISED OF THE POSSIBILITY OF SUCH January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation and configuration files Object form shall mean any form resulting from mechanical transformation or translation of a Source including but not limited to compiled object generated and conversions to other media types Work shall mean the work of whether in Source or Object made available under the as indicated by a copyright notice that is included in or attached to the whether in Source or Object that is based or other modifications as a an original work of authorship For the purposes of this Derivative Works shall not include works that remain separable or merely the Work and Derivative Works thereof Contribution shall mean any work of including the original version of the Work and any modifications or additions to that Work or Derivative Works that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner For the purposes of this submitted means any form of or written communication sent to the Licensor or its including but not limited to communication on electronic mailing source code control and issue tracking systems that are managed or on behalf the Licensor for the purpose of discussing and improving the but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as Not a Contribution Contributor shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work Grant of Copyright License Subject to the terms and conditions of this each Contributor hereby grants to You a non no royalty free
Definition ThirdPartyNotices.txt:164
Definition zarray.h:44
Definition zhash.h:60
int last_entry
Definition zhash.h:63
zhash_t * zh
Definition zhash.h:61
const zhash_t * czh
Definition zhash.h:62
static zhash_t * zhash_str_str_create(void)
Definition zhash.h:368
int zhash_remove(zhash_t *zh, const void *key, void *oldkey, void *oldvalue)
Removes from the zhash table the key/value pair for the supplied key, if it exists.
int zhash_get_volatile(const zhash_t *zh, const void *key, void *out_p)
Similar to zhash_get(), but more dangerous.
void zhash_destroy(zhash_t *zh)
Frees all resources associated with the hash table structure which was created by zhash_create().
void zhash_iterator_init(zhash_t *zh, zhash_iterator_t *zit)
Initializes an iterator which can be used to traverse the key/value pairs of the supplied zhash table...
int zhash_put(zhash_t *zh, const void *key, const void *value, void *oldkey, void *oldvalue)
Adds a key/value pair to the hash table, if the supplied key does not already exist in the table,...
uint32_t zhash_ptr_hash(const void *a)
Defines a hash function which will calculate a zhash value for pointer input data.
void zhash_iterator_remove(zhash_iterator_t *zit)
Removes from the zhash table the key/value pair most recently returned via a call to zhash_iterator_n...
int zhash_get(const zhash_t *zh, const void *key, void *out_value)
Retrieves the value for the given key, if it exists, by copying its contents into the space pointed t...
static uint32_t zhash_int_hash(const void *_a)
Definition zhash.h:411
int zhash_uint64_equals(const void *a, const void *b)
Defines a function to compare zhash values for uint64_t input data.
int zhash_uint32_equals(const void *a, const void *b)
Defines a function to compare zhash values for uint32_t input data.
static int zhash_int_equals(const void *_a, const void *_b)
Definition zhash.h:419
void zhash_iterator_init_const(const zhash_t *zh, zhash_iterator_t *zit)
Initializes an iterator which can be used to traverse the key/value pairs of the supplied zhash table...
int zhash_iterator_next_volatile(zhash_iterator_t *zit, void *outkey, void *outvalue)
Similar to zhash_iterator_next() except that it retrieves a pointer to zhash's internal storage.
int zhash_str_equals(const void *a, const void *b)
Defines a function to compare zhash values for string input data.
zhash_t * zhash_copy(const zhash_t *other)
Creates and returns a new identical copy of the zhash (i.e.
int zhash_size(const zhash_t *zh)
Retrieves the current number of key/value pairs currently contained in the zhash table,...
void zhash_debug(zhash_t *zh)
uint32_t zhash_uint64_hash(const void *a)
Defines a hash function which will calculate a zhash value for uint64_t input data.
static void zhash_str_str_destroy(zhash_t *zh)
Definition zhash.h:396
zarray_t * zhash_keys(const zhash_t *zh)
Returns an array which contains copies of all of the hash table's keys, in no particular order.
int zhash_iterator_next(zhash_iterator_t *zit, void *outkey, void *outvalue)
Retrieves the next key/value pair from a zhash table via the (previously- initialized) iterator.
zarray_t * zhash_values(const zhash_t *zh)
Returns an array which contains copies of all of the hash table's values, in no particular order.
void zhash_map_values(zhash_t *zh, void(*f)(void *))
Calls the supplied function with a pointer to every value in the hash table in turn.
int zhash_ptr_equals(const void *a, const void *b)
Defines a function to compare zhash values for pointer input data.
void zhash_map_keys(zhash_t *zh, void(*f)(void *))
Calls the supplied function with a pointer to every key in the hash table in turn.
int zhash_contains(const zhash_t *zh, const void *key)
Determines whether the supplied key value exists as an entry in the zhash table.
struct zhash zhash_t
A hash table for structs and primitive types that stores entries by value.
Definition zhash.h:54
void zhash_vmap_values(zhash_t *vh, void(*f)(void *))
Calls the supplied function with a copy of every value in the hash table in turn.
void zhash_clear(zhash_t *zh)
Removes all entries in the has table to create the equivalent of starting from a zhash_create(),...
void zhash_vmap_keys(zhash_t *vh, void(*f)(void *))
Calls the supplied function with a copy of every key in the hash table in turn.
static void zhash_str_str_put(zhash_t *zh, char *key, char *value)
Definition zhash.h:387
uint32_t zhash_str_hash(const void *a)
Defines a hash function which will calculate a zhash value for string input data.
uint32_t zhash_uint32_hash(const void *a)
Defines a hash function which will calculate a zhash value for uint32_t input data.
static char * zhash_str_str_get(zhash_t *zh, const char *key)
Definition zhash.h:379
zhash_t * zhash_create(size_t keysz, size_t valuesz, uint32_t(*hash)(const void *a), int(*equals)(const void *a, const void *b))
Create, initializes, and returns an empty hash table structure.