2 Copyright 2011-2015 David Robillard <http://drobilla.net>
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.
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.
23 #include "zix/common.h"
36 typedef struct ZixHashImpl ZixHash
;
39 Function for computing the hash of an element.
41 typedef uint32_t (*ZixHashFunc
)(const void* value
);
44 Function to visit a hash element.
46 typedef void (*ZixHashVisitFunc
)(void* value
,
50 Create a new hash table.
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.
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.
63 zix_hash_new(ZixHashFunc hash_func
,
64 ZixEqualFunc equal_func
,
71 zix_hash_free(ZixHash
* hash
);
74 Return the number of elements in `hash`.
77 zix_hash_size(const ZixHash
* hash
);
80 Insert an item into `hash`.
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.
85 If a matching value already exists, ZIX_STATUS_EXISTS will be returned, and
86 `inserted` will be pointed to the existing value.
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.
94 zix_hash_insert(ZixHash
* hash
,
96 const void** inserted
);
99 Remove an item from `hash`.
101 @param hash The hash table.
102 @param value The value to remove.
103 @return ZIX_STATUS_SUCCES or ZIX_STATUS_NOT_FOUND.
106 zix_hash_remove(ZixHash
* hash
,
110 Search for an item in `hash`.
112 @param hash The hash table.
113 @param value The value to search for.
116 zix_hash_find(const ZixHash
* hash
,
120 Call `f` on each value in `hash`.
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`.
127 zix_hash_foreach(ZixHash
* hash
,
140 #endif /* ZIX_HASH_H */