zhash.h
Go to the documentation of this file.
1 /* Copyright (C) 2013-2016, The Regents of The University of Michigan.
2 All rights reserved.
3 This software was developed in the APRIL Robotics Lab under the
4 direction of Edwin Olson, ebolson@umich.edu. This software may be
5 available under alternative licensing terms; contact the address above.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10 2. 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.
13 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20 ON 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
22 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 The views and conclusions contained in the software and documentation are those
24 of the authors and should not be interpreted as representing official policies,
25 either 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
33 extern "C" {
34 #endif
35 
36 #include "zarray.h"
37 
38 
54 typedef 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 
77 zhash_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 
88 void zhash_destroy(zhash_t *zh);
89 
97 zhash_t * zhash_copy(const zhash_t* other);
98 
105 int zhash_contains(const zhash_t *zh, const void *key);
106 
113 int zhash_get(const zhash_t *zh, const void *key, void *out_value);
114 
126 int zhash_get_volatile(const zhash_t *zh, const void *key, void *out_p);
127 
164 int zhash_put(zhash_t *zh, const void *key, const void *value, void *oldkey, void *oldvalue);
165 
176 int zhash_remove(zhash_t *zh, const void *key, void *oldkey, void *oldvalue);
177 
183 void zhash_clear(zhash_t *zh);
184 
189 int zhash_size(const zhash_t *zh);
190 
201 
214 
224 int zhash_iterator_next(zhash_iterator_t *zit, void *outkey, void *outvalue);
225 
244 int zhash_iterator_next_volatile(zhash_iterator_t *zit, void *outkey, void *outvalue);
245 
255 
262 void zhash_map_keys(zhash_t *zh, void (*f)());
263 
270 void zhash_map_values(zhash_t *zh, void (*f)());
271 
283 void zhash_vmap_keys(zhash_t *vh, void (*f)());
284 
296 void zhash_vmap_values(zhash_t *vh, void (*f)());
297 
303 zarray_t *zhash_keys(const zhash_t *zh);
304 
310 zarray_t *zhash_values(const zhash_t *zh);
311 
316 uint32_t zhash_uint32_hash(const void *a);
317 
322 int zhash_uint32_equals(const void *a, const void *b);
323 
328 uint32_t zhash_uint64_hash(const void *a);
329 
334 int zhash_uint64_equals(const void *a, const void *b);
335 
337 // functions for keys that can be compared via their pointers.
343 uint32_t zhash_ptr_hash(const void *a);
344 
349 int zhash_ptr_equals(const void *a, const void *b);
350 
352 // Functions for string-typed keys
358 uint32_t zhash_str_hash(const void *a);
359 
364 int zhash_str_equals(const void *a, const void *b);
365 
366 void zhash_debug(zhash_t *zh);
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.
379 static 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  {
398  zhash_iterator_t zit;
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 
407  zhash_destroy(zh);
408  }
409 
410 
411 static 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 
419 static 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
zarray_t * zhash_values(const zhash_t *zh)
Definition: zhash.c:436
int zhash_get_volatile(const zhash_t *zh, const void *key, void *out_p)
Definition: zhash.c:117
int zhash_str_equals(const void *a, const void *b)
Definition: zhash.c:525
zarray_t * zhash_keys(const zhash_t *zh)
Definition: zhash.c:419
int last_entry
Definition: zhash.h:63
void zhash_iterator_init_const(const zhash_t *zh, zhash_iterator_t *zit)
Definition: zhash.c:279
void zhash_vmap_values(zhash_t *vh, void(*f)())
Definition: zhash.c:403
zhash_t * zh
Definition: zhash.h:61
uint32_t zhash_ptr_hash(const void *a)
Definition: zhash.c:499
Definition: zhash.c:41
int zhash_contains(const zhash_t *zh, const void *key)
Definition: zhash.c:266
int zhash_uint32_equals(const void *a, const void *b)
Definition: zhash.c:462
void zhash_iterator_remove(zhash_iterator_t *zit)
Definition: zhash.c:327
void zhash_clear(zhash_t *zh)
Definition: zhash.c:111
int zhash_size(const zhash_t *zh)
Definition: zhash.c:106
const zhash_t * czh
Definition: zhash.h:62
void zhash_destroy(zhash_t *zh)
Definition: zhash.c:97
void zhash_map_keys(zhash_t *zh, void(*f)())
Definition: zhash.c:355
static zhash_t * zhash_str_str_create(void)
Definition: zhash.h:368
uint32_t zhash_str_hash(const void *a)
Definition: zhash.c:536
void zhash_debug(zhash_t *zh)
Definition: zhash.c:554
int zhash_iterator_next(zhash_iterator_t *zit, void *outkey, void *outvalue)
Definition: zhash.c:310
int zhash_put(zhash_t *zh, const void *key, const void *value, void *oldkey, void *oldvalue)
Definition: zhash.c:146
int zhash_iterator_next_volatile(zhash_iterator_t *zit, void *outkey, void *outvalue)
Definition: zhash.c:286
int zhash_remove(zhash_t *zh, const void *key, void *oldkey, void *oldvalue)
Definition: zhash.c:202
Definition: zarray.h:43
static int zhash_int_equals(const void *_a, const void *_b)
Definition: zhash.h:419
void zhash_map_values(zhash_t *zh, void(*f)())
Definition: zhash.c:388
int zhash_get(const zhash_t *zh, const void *key, void *out_value)
Definition: zhash.c:135
static void zhash_str_str_put(zhash_t *zh, char *key, char *value)
Definition: zhash.h:387
static char * zhash_str_str_get(zhash_t *zh, const char *key)
Definition: zhash.h:379
int zhash_uint64_equals(const void *a, const void *b)
Definition: zhash.c:481
void zhash_vmap_keys(zhash_t *vh, void(*f)())
Definition: zhash.c:371
static void zhash_str_str_destroy(zhash_t *zh)
Definition: zhash.h:396
static uint32_t zhash_int_hash(const void *_a)
Definition: zhash.h:411
uint32_t zhash_uint64_hash(const void *a)
Definition: zhash.c:473
zhash_t * zhash_copy(const zhash_t *other)
Definition: zhash.c:248
uint32_t zhash_uint32_hash(const void *a)
Definition: zhash.c:454
void zhash_iterator_init(zhash_t *zh, zhash_iterator_t *zit)
Definition: zhash.c:272
zhash_t * zhash_create(size_t keysz, size_t valuesz, uint32_t(*hash)(const void *a), int(*equals)(const void *a, const void *b))
Definition: zhash.c:91
int zhash_ptr_equals(const void *a, const void *b)
Definition: zhash.c:514


apriltag
Author(s): Edwin Olson , Max Krogius
autogenerated on Mon Jun 26 2023 02:26:35