tommyhashtbl.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, Andrea Mazzoleni. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
127 #ifndef __TOMMYHASHTBL_H
128 #define __TOMMYHASHTBL_H
129 
130 #include "tommyhash.h"
131 
132 /******************************************************************************/
133 /* hashtable */
134 
140 
145 typedef struct tommy_hashtable_struct {
151 
156 void tommy_hashtable_init(tommy_hashtable* hashtable, tommy_count_t bucket_max);
157 
164 void tommy_hashtable_done(tommy_hashtable* hashtable);
165 
169 void tommy_hashtable_insert(tommy_hashtable* hashtable, tommy_hashtable_node* node, void* data, tommy_hash_t hash);
170 
182 void* tommy_hashtable_remove(tommy_hashtable* hashtable, tommy_search_func* cmp, const void* cmp_arg, tommy_hash_t hash);
183 
193 {
194  return hashtable->bucket[hash & hashtable->bucket_mask];
195 }
196 
207 tommy_inline void* tommy_hashtable_search(tommy_hashtable* hashtable, tommy_search_func* cmp, const void* cmp_arg, tommy_hash_t hash)
208 {
209  tommy_hashtable_node* i = tommy_hashtable_bucket(hashtable, hash);
210 
211  while (i) {
212  /* we first check if the hash matches, as in the same bucket we may have multiples hash values */
213  if (i->key == hash && cmp(cmp_arg, i->data) == 0)
214  return i->data;
215  i = i->next;
216  }
217  return 0;
218 }
219 
226 
259 
263 void tommy_hashtable_foreach_arg(tommy_hashtable* hashtable, tommy_foreach_arg_func* func, void* arg);
264 
269 {
270  return hashtable->count;
271 }
272 
278 
279 #endif
280 
tommy_size_t tommy_hashtable_memory_usage(tommy_hashtable *hashtable)
Gets the size of allocated memory.
tommy_count_t count
Number of elements.
Definition: tommyhashtbl.h:149
tommy_uint32_t tommy_count_t
Generic unsigned integer for counting objects.
Definition: tommytypes.h:67
struct tommy_node_struct * next
Next node.
Definition: tommytypes.h:188
void tommy_hashtable_init(tommy_hashtable *hashtable, tommy_count_t bucket_max)
Initializes the hashtable.
tommy_key_t tommy_hash_t
Hash type used in hashtables.
Definition: tommyhash.h:43
void tommy_hashtable_insert(tommy_hashtable *hashtable, tommy_hashtable_node *node, void *data, tommy_hash_t hash)
Inserts an element in the hashtable.
Hashtable container type.
Definition: tommyhashtbl.h:145
int tommy_search_func(const void *arg, const void *obj)
Search function for elements.
Definition: tommytypes.h:278
tommy_count_t bucket_mask
Bit mask to access the buckets.
Definition: tommyhashtbl.h:148
void * tommy_hashtable_search(tommy_hashtable *hashtable, tommy_search_func *cmp, const void *cmp_arg, tommy_hash_t hash)
Searches an element in the hashtable.
Definition: tommyhashtbl.h:207
tommy_hashtable_node * tommy_hashtable_bucket(tommy_hashtable *hashtable, tommy_hash_t hash)
Gets the bucket of the specified hash.
Definition: tommyhashtbl.h:192
void * tommy_hashtable_remove_existing(tommy_hashtable *hashtable, tommy_hashtable_node *node)
Removes an element from the hashtable.
void tommy_hashtable_foreach_arg(tommy_hashtable *hashtable, tommy_foreach_arg_func *func, void *arg)
Calls the specified function with an argument for each element in the hashtable.
tommy_hashtable_node ** bucket
Hash buckets.
Definition: tommyhashtbl.h:146
tommy_count_t bucket_max
Number of buckets.
Definition: tommyhashtbl.h:147
void tommy_hashtable_done(tommy_hashtable *hashtable)
Deinitializes the hashtable.
Data structure node.
Definition: tommytypes.h:183
void tommy_foreach_func(void *obj)
Foreach function.
Definition: tommytypes.h:289
tommy_count_t tommy_hashtable_count(tommy_hashtable *hashtable)
Gets the number of elements.
Definition: tommyhashtbl.h:268
void * data
Pointer to the object containing the node.
Definition: tommytypes.h:200
Hash functions for the use with tommy_hashtable, tommy_hashdyn and tommy_hashlin. ...
tommy_node tommy_hashtable_node
Hashtable node.
Definition: tommyhashtbl.h:139
size_t tommy_size_t
Generic size_t type.
Definition: tommytypes.h:50
void * tommy_hashtable_remove(tommy_hashtable *hashtable, tommy_search_func *cmp, const void *cmp_arg, tommy_hash_t hash)
Searches and removes an element from the hashtable.
struct tommy_hashtable_struct tommy_hashtable
Hashtable container type.
tommy_key_t key
Key used to store the node.
Definition: tommytypes.h:207
void tommy_hashtable_foreach(tommy_hashtable *hashtable, tommy_foreach_func *func)
Calls the specified function for each element in the hashtable.
void tommy_foreach_arg_func(void *arg, void *obj)
Foreach function with an argument.
Definition: tommytypes.h:296