1 /*
2   Copyright 2011-2012 David Robillard <http://drobilla.net>
3 
4   Permission to use, copy, modify, and/or distribute this software for any
5   purpose with or without fee is hereby granted, provided that the above
6   copyright notice and this permission notice appear in all copies.
7 
8   THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9   WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10   MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11   ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16 
17 #ifndef ZIX_HASH_H
18 #define ZIX_HASH_H
19 
20 #include <stddef.h>
21 #include <stdint.h>
22 
23 #include "zix/common.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 /**
30    @addtogroup zix
31    @{
32    @name Hash
33    @{
34 */
35 
36 typedef struct ZixHashImpl ZixHash;
37 
38 /**
39    Function for computing the hash of an element.
40 */
41 typedef uint32_t (*ZixHashFunc)(const void* value);
42 
43 /**
44    Function to visit a hash element.
45 */
46 typedef void (*ZixHashVisitFunc)(void* value,
47                                  void* user_data);
48 
49 /**
50    Create a new hash table.
51 
52    To minimize space overhead, unlike many hash tables this stores a single
53    value, not a key and a value.  Any size of value can be stored, but all the
54    values in the hash table must be the same size, and the values must be safe
55    to copy with memcpy.  To get key:value behaviour, simply insert a struct
56    with a key and value into the hash.
57 
58    @param hash_func The hashing function.
59    @param equal_func A function to test value equality.
60    @param value_size The size of the values to be stored.
61 */
62 ZIX_API ZixHash*
63 zix_hash_new(ZixHashFunc  hash_func,
64              ZixEqualFunc equal_func,
65              size_t       value_size);
66 
67 /**
68    Free `hash`.
69 */
70 ZIX_API void
71 zix_hash_free(ZixHash* hash);
72 
73 /**
74    Return the number of elements in `hash`.
75 */
76 ZIX_API size_t
77 zix_hash_size(const ZixHash* hash);
78 
79 /**
80    Insert an item into `hash`.
81 
82    If no matching value is found, ZIX_STATUS_SUCCESS will be returned, and @p
83    inserted will be pointed to the copy of `value` made in the new hash node.
84 
85    If a matching value already exists, ZIX_STATUS_EXISTS will be returned, and
86    `inserted` will be pointed to the existing value.
87 
88    @param hash The hash table.
89    @param value The value to be inserted.
90    @param inserted The copy of `value` in the hash table.
91    @return ZIX_STATUS_SUCCESS, ZIX_STATUS_EXISTS, or ZIX_STATUS_NO_MEM.
92 */
93 ZIX_API ZixStatus
94 zix_hash_insert(ZixHash*     hash,
95                 const void*  value,
96                 const void** inserted);
97 
98 /**
99    Remove an item from `hash`.
100 
101    @param hash The hash table.
102    @param value The value to remove.
103    @return ZIX_STATUS_SUCCES or ZIX_STATUS_NOT_FOUND.
104 */
105 ZIX_API ZixStatus
106 zix_hash_remove(ZixHash*    hash,
107                 const void* value);
108 
109 /**
110    Search for an item in `hash`.
111 
112    @param hash The hash table.
113    @param value The value to search for.
114 */
115 ZIX_API const void*
116 zix_hash_find(const ZixHash* hash,
117               const void*    value);
118 
119 /**
120    Call `f` on each value in `hash`.
121 
122    @param hash The hash table.
123    @param f The function to call on each value.
124    @param user_data The user_data parameter passed to `f`.
125 */
126 ZIX_API void
127 zix_hash_foreach(ZixHash*         hash,
128                  ZixHashVisitFunc f,
129                  void*            user_data);
130 
131 /**
132    @}
133    @}
134 */
135 
136 #ifdef __cplusplus
137 }  /* extern "C" */
138 #endif
139 
140 #endif  /* ZIX_HASH_H */
141