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 
4 This software was developed in the APRIL Robotics Lab under the
5 direction of Edwin Olson, ebolson@umich.edu. This software may be
6 available under alternative licensing terms; contact the address above.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 
11 1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 The views and conclusions contained in the software and documentation are those
29 of the authors and should not be interpreted as representing official policies,
30 either expressed or implied, of the Regents of The University of Michigan.
31 */
32 
33 #ifndef _ZHASH_H
34 #define _ZHASH_H
35 
36 #include <stdint.h>
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 #include "zarray.h"
43 
44 
60 typedef struct zhash zhash_t;
61 
62 // The contents of the iterator should be considered private. However,
63 // since our usage model prefers stack-based allocation of iterators,
64 // we must publicly declare them.
66 {
68  const zhash_t *czh;
69  int last_entry; // points to the last entry returned by _next
70 };
71 
73 
83 zhash_t *zhash_create(size_t keysz, size_t valuesz,
84  uint32_t(*hash)(const void *a),
85  int(*equals)(const void *a, const void *b));
86 
94 void zhash_destroy(zhash_t *zh);
95 
103 zhash_t * zhash_copy(const zhash_t* other);
104 
111 int zhash_contains(const zhash_t *zh, const void *key);
112 
119 int zhash_get(const zhash_t *zh, const void *key, void *out_value);
120 
132 int zhash_get_volatile(const zhash_t *zh, const void *key, void *out_p);
133 
170 int zhash_put(zhash_t *zh, const void *key, const void *value, void *oldkey, void *oldvalue);
171 
182 int zhash_remove(zhash_t *zh, const void *key, void *oldkey, void *oldvalue);
183 
189 void zhash_clear(zhash_t *zh);
190 
195 int zhash_size(const zhash_t *zh);
196 
207 
220 
230 int zhash_iterator_next(zhash_iterator_t *zit, void *outkey, void *outvalue);
231 
250 int zhash_iterator_next_volatile(zhash_iterator_t *zit, void *outkey, void *outvalue);
251 
261 
268 void zhash_map_keys(zhash_t *zh, void (*f)());
269 
276 void zhash_map_values(zhash_t *zh, void (*f)());
277 
289 void zhash_vmap_keys(zhash_t *vh, void (*f)());
290 
302 void zhash_vmap_values(zhash_t *vh, void (*f)());
303 
309 zarray_t *zhash_keys(const zhash_t *zh);
310 
316 zarray_t *zhash_values(const zhash_t *zh);
317 
322 uint32_t zhash_uint32_hash(const void *a);
323 
328 int zhash_uint32_equals(const void *a, const void *b);
329 
334 uint32_t zhash_uint64_hash(const void *a);
335 
340 int zhash_uint64_equals(const void *a, const void *b);
341 
343 // functions for keys that can be compared via their pointers.
349 uint32_t zhash_ptr_hash(const void *a);
350 
355 int zhash_ptr_equals(const void *a, const void *b);
356 
358 // Functions for string-typed keys
364 uint32_t zhash_str_hash(const void *a);
365 
370 int zhash_str_equals(const void *a, const void *b);
371 
372 void zhash_debug(zhash_t *zh);
373 
374  static inline zhash_t *zhash_str_str_create(void)
375  {
376  return zhash_create(sizeof(char*), sizeof(char*),
378  }
379 
380 
381 
382 // for zhashes that map strings to strings, this is a convenience
383 // function that allows easier retrieval of values. NULL is returned
384 // if the key is not found.
385 static inline char *zhash_str_str_get(zhash_t *zh, const char *key)
386 {
387  char *value;
388  if (zhash_get(zh, &key, &value))
389  return value;
390  return NULL;
391 }
392 
393  static inline void zhash_str_str_put(zhash_t *zh, char *key, char *value)
394  {
395  char *oldkey, *oldval;
396  if (zhash_put(zh, &key, &value, &oldkey, &oldval)) {
397  free(oldkey);
398  free(oldval);
399  }
400  }
401 
402  static inline void zhash_str_str_destroy(zhash_t *zh)
403  {
404  zhash_iterator_t zit;
405  zhash_iterator_init(zh, &zit);
406 
407  char *key, *value;
408  while (zhash_iterator_next(&zit, &key, &value)) {
409  free(key);
410  free(value);
411  }
412 
413  zhash_destroy(zh);
414  }
415 
416 
417 static inline uint32_t zhash_int_hash(const void *_a)
418 {
419  assert(_a != NULL);
420 
421  uint32_t a = *((int*) _a);
422  return a;
423 }
424 
425 static inline int zhash_int_equals(const void *_a, const void *_b)
426 {
427  assert(_a != NULL);
428  assert(_b != NULL);
429 
430  int a = *((int*) _a);
431  int b = *((int*) _b);
432 
433  return a==b;
434 }
435 
436 #ifdef __cplusplus
437 }
438 #endif
439 
440 #endif
zarray_t * zhash_values(const zhash_t *zh)
Definition: zhash.c:440
int zhash_get_volatile(const zhash_t *zh, const void *key, void *out_p)
Definition: zhash.c:123
int zhash_str_equals(const void *a, const void *b)
Definition: zhash.c:529
zarray_t * zhash_keys(const zhash_t *zh)
Definition: zhash.c:423
int last_entry
Definition: zhash.h:69
void zhash_iterator_init_const(const zhash_t *zh, zhash_iterator_t *zit)
Definition: zhash.c:284
void zhash_vmap_values(zhash_t *vh, void(*f)())
Definition: zhash.c:407
zhash_t * zh
Definition: zhash.h:67
uint32_t zhash_ptr_hash(const void *a)
Definition: zhash.c:503
Definition: zhash.c:46
int zhash_contains(const zhash_t *zh, const void *key)
Definition: zhash.c:271
int zhash_uint32_equals(const void *a, const void *b)
Definition: zhash.c:466
void zhash_iterator_remove(zhash_iterator_t *zit)
Definition: zhash.c:332
void zhash_clear(zhash_t *zh)
Definition: zhash.c:117
int zhash_size(const zhash_t *zh)
Definition: zhash.c:112
const zhash_t * czh
Definition: zhash.h:68
void zhash_destroy(zhash_t *zh)
Definition: zhash.c:103
void zhash_map_keys(zhash_t *zh, void(*f)())
Definition: zhash.c:359
static zhash_t * zhash_str_str_create(void)
Definition: zhash.h:374
uint32_t zhash_str_hash(const void *a)
Definition: zhash.c:540
void zhash_debug(zhash_t *zh)
Definition: zhash.c:557
int zhash_iterator_next(zhash_iterator_t *zit, void *outkey, void *outvalue)
Definition: zhash.c:315
int zhash_put(zhash_t *zh, const void *key, const void *value, void *oldkey, void *oldvalue)
Definition: zhash.c:152
int zhash_iterator_next_volatile(zhash_iterator_t *zit, void *outkey, void *outvalue)
Definition: zhash.c:291
int zhash_remove(zhash_t *zh, const void *key, void *oldkey, void *oldvalue)
Definition: zhash.c:208
Definition: zarray.h:49
static int zhash_int_equals(const void *_a, const void *_b)
Definition: zhash.h:425
void zhash_map_values(zhash_t *zh, void(*f)())
Definition: zhash.c:392
int zhash_get(const zhash_t *zh, const void *key, void *out_value)
Definition: zhash.c:141
static void zhash_str_str_put(zhash_t *zh, char *key, char *value)
Definition: zhash.h:393
static char * zhash_str_str_get(zhash_t *zh, const char *key)
Definition: zhash.h:385
int zhash_uint64_equals(const void *a, const void *b)
Definition: zhash.c:485
void zhash_vmap_keys(zhash_t *vh, void(*f)())
Definition: zhash.c:375
static void zhash_str_str_destroy(zhash_t *zh)
Definition: zhash.h:402
static uint32_t zhash_int_hash(const void *_a)
Definition: zhash.h:417
uint32_t zhash_uint64_hash(const void *a)
Definition: zhash.c:477
zhash_t * zhash_copy(const zhash_t *other)
Definition: zhash.c:253
uint32_t zhash_uint32_hash(const void *a)
Definition: zhash.c:458
void zhash_iterator_init(zhash_t *zh, zhash_iterator_t *zit)
Definition: zhash.c:277
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:97
int zhash_ptr_equals(const void *a, const void *b)
Definition: zhash.c:518


apriltags2
Author(s): Danylo Malyuta
autogenerated on Fri Oct 19 2018 04:02:32