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
151
156TOMMY_API void tommy_hashtable_init(tommy_hashtable* hashtable, tommy_size_t bucket_max);
157
164TOMMY_API void tommy_hashtable_done(tommy_hashtable* hashtable);
165
169TOMMY_API void tommy_hashtable_insert(tommy_hashtable* hashtable, tommy_hashtable_node* node, void* data, tommy_hash_t hash);
170
182TOMMY_API 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
207tommy_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 multiple hash values */
213 if (i->index == hash && cmp(cmp_arg, i->data) == 0)
214 return i->data;
215 i = i->next;
216 }
217 return 0;
218}
219
226
259
263TOMMY_API 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
Hashtable container type.
Definition: tommyhashtbl.h:145
tommy_hashtable_node ** bucket
Hash buckets.
Definition: tommyhashtbl.h:146
tommy_size_t bucket_mask
Bit mask to access the buckets.
Definition: tommyhashtbl.h:148
tommy_size_t bucket_max
Number of buckets.
Definition: tommyhashtbl.h:147
tommy_size_t count
Number of elements.
Definition: tommyhashtbl.h:149
Data structure node.
Definition: tommytypes.h:211
tommy_size_t index
Index of the node.
Definition: tommytypes.h:236
struct tommy_node_struct * next
Next node.
Definition: tommytypes.h:216
void * data
Pointer to the object containing the node.
Definition: tommytypes.h:228
Hash functions for the use with tommy_hashtable, tommy_hashdyn and tommy_hashlin.
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_API void tommy_hashtable_done(tommy_hashtable *hashtable)
Deinitializes the hashtable.
TOMMY_API void tommy_hashtable_init(tommy_hashtable *hashtable, tommy_size_t bucket_max)
Initializes the hashtable.
TOMMY_API void tommy_hashtable_insert(tommy_hashtable *hashtable, tommy_hashtable_node *node, void *data, tommy_hash_t hash)
Inserts an element in the hashtable.
tommy_node tommy_hashtable_node
Hashtable node.
Definition: tommyhashtbl.h:139
TOMMY_API tommy_size_t tommy_hashtable_memory_usage(tommy_hashtable *hashtable)
Gets the size of allocated memory.
TOMMY_API void * tommy_hashtable_remove_existing(tommy_hashtable *hashtable, tommy_hashtable_node *node)
Removes an element from the hashtable.
TOMMY_API 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_API 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.
TOMMY_API void tommy_hashtable_foreach(tommy_hashtable *hashtable, tommy_foreach_func *func)
Calls the specified function for each element in the hashtable.
struct tommy_hashtable_struct tommy_hashtable
Hashtable container type.
tommy_size_t tommy_hashtable_count(tommy_hashtable *hashtable)
Gets the number of elements.
Definition: tommyhashtbl.h:268
tommy_hashtable_node * tommy_hashtable_bucket(tommy_hashtable *hashtable, tommy_hash_t hash)
Gets the bucket of the specified hash.
Definition: tommyhashtbl.h:192
uint64_t tommy_size_t
Generic size_t type.
Definition: tommytypes.h:60
void tommy_foreach_func(void *obj)
Foreach function.
Definition: tommytypes.h:318
int tommy_search_func(const void *arg, const void *obj)
Search function for elements.
Definition: tommytypes.h:307
void tommy_foreach_arg_func(void *arg, void *obj)
Foreach function with an argument.
Definition: tommytypes.h:325
tommy_size_t tommy_hash_t
Type used in hashtables to store the hash of an object.
Definition: tommytypes.h:193