tommyhashlin.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 
140 #ifndef __TOMMYHASHLIN_H
141 #define __TOMMYHASHLIN_H
142 
143 #include "tommyhash.h"
144 
145 /******************************************************************************/
146 /* hashlin */
147 
152 #define TOMMY_HASHLIN_BIT 6
153 
159 
163 #define TOMMY_HASHLIN_BIT_MAX 32
164 
169 typedef struct tommy_hashlin_struct {
170  tommy_hashlin_node** bucket[TOMMY_HASHLIN_BIT_MAX];
179 } tommy_hashlin;
180 
184 void tommy_hashlin_init(tommy_hashlin* hashlin);
185 
192 void tommy_hashlin_done(tommy_hashlin* hashlin);
193 
197 void tommy_hashlin_insert(tommy_hashlin* hashlin, tommy_hashlin_node* node, void* data, tommy_hash_t hash);
198 
210 void* tommy_hashlin_remove(tommy_hashlin* hashlin, tommy_search_func* cmp, const void* cmp_arg, tommy_hash_t hash);
211 
215 tommy_inline tommy_hashlin_node** tommy_hashlin_pos(tommy_hashlin* hashlin, tommy_hash_t pos)
216 {
217  tommy_uint_t bsr;
218 
219  /* get the highest bit set, in case of all 0, return 0 */
220  bsr = tommy_ilog2_u32(pos | 1);
221 
222  return &hashlin->bucket[bsr][pos];
223 }
224 
228 tommy_inline tommy_hashlin_node** tommy_hashlin_bucket_ref(tommy_hashlin* hashlin, tommy_hash_t hash)
229 {
230  tommy_count_t pos;
231  tommy_count_t high_pos;
232 
233  pos = hash & hashlin->low_mask;
234  high_pos = hash & hashlin->bucket_mask;
235 
236  /* if this position is already allocated in the high half */
237  if (pos < hashlin->split) {
238  /* The following assigment is expected to be implemented */
239  /* with a conditional move instruction */
240  /* that results in a little better and constant performance */
241  /* regardless of the split position. */
242  /* This affects mostly the worst case, when the split value */
243  /* is near at its half, resulting in a totally unpredictable */
244  /* condition by the CPU. */
245  /* In such case the use of the conditional move is generally faster. */
246 
247  /* use also the high bit */
248  pos = high_pos;
249  }
250 
251  return tommy_hashlin_pos(hashlin, pos);
252 }
253 
263 {
264  return *tommy_hashlin_bucket_ref(hashlin, hash);
265 }
266 
277 tommy_inline void* tommy_hashlin_search(tommy_hashlin* hashlin, tommy_search_func* cmp, const void* cmp_arg, tommy_hash_t hash)
278 {
279  tommy_hashlin_node* i = tommy_hashlin_bucket(hashlin, hash);
280 
281  while (i) {
282  /* we first check if the hash matches, as in the same bucket we may have multiples hash values */
283  if (i->key == hash && cmp(cmp_arg, i->data) == 0)
284  return i->data;
285  i = i->next;
286  }
287  return 0;
288 }
289 
296 
329 
333 void tommy_hashlin_foreach_arg(tommy_hashlin* hashlin, tommy_foreach_arg_func* func, void* arg);
334 
339 {
340  return hashlin->count;
341 }
342 
348 
349 #endif
350 
tommy_count_t bucket_mask
Bit mask to access the buckets.
Definition: tommyhashlin.h:173
tommy_count_t low_mask
Low order mask value.
Definition: tommyhashlin.h:175
void tommy_hashlin_insert(tommy_hashlin *hashlin, tommy_hashlin_node *node, void *data, tommy_hash_t hash)
Inserts an element in the hashtable.
tommy_uint_t bucket_bit
Bits used in the bit mask.
Definition: tommyhashlin.h:171
tommy_hashlin_node ** bucket[TOMMY_HASHLIN_BIT_MAX]
Dynamic array of hash buckets.
Definition: tommyhashlin.h:170
tommy_uint_t tommy_ilog2_u32(tommy_uint32_t value)
Bit scan reverse or integer log2.
Definition: tommytypes.h:331
tommy_uint32_t tommy_count_t
Generic unsigned integer for counting objects.
Definition: tommytypes.h:67
tommy_count_t count
Number of elements.
Definition: tommyhashlin.h:177
tommy_uint_t state
Reallocation state.
Definition: tommyhashlin.h:178
Hashtable container type.
Definition: tommyhashlin.h:169
struct tommy_node_struct * next
Next node.
Definition: tommytypes.h:188
tommy_count_t bucket_max
Number of buckets.
Definition: tommyhashlin.h:172
tommy_count_t tommy_hashlin_count(tommy_hashlin *hashlin)
Gets the number of elements.
Definition: tommyhashlin.h:338
void * tommy_hashlin_remove(tommy_hashlin *hashlin, tommy_search_func *cmp, const void *cmp_arg, tommy_hash_t hash)
Searches and removes an element from the hashtable.
void tommy_hashlin_foreach_arg(tommy_hashlin *hashlin, tommy_foreach_arg_func *func, void *arg)
Calls the specified function with an argument for each element in the hashtable.
tommy_key_t tommy_hash_t
Hash type used in hashtables.
Definition: tommyhash.h:43
int tommy_search_func(const void *arg, const void *obj)
Search function for elements.
Definition: tommytypes.h:278
void * tommy_hashlin_remove_existing(tommy_hashlin *hashlin, tommy_hashlin_node *node)
Removes an element from the hashtable.
struct tommy_hashlin_struct tommy_hashlin
Hashtable container type.
tommy_count_t low_max
Low order max value.
Definition: tommyhashlin.h:174
void tommy_hashlin_init(tommy_hashlin *hashlin)
Initializes the hashtable.
tommy_uint32_t tommy_uint_t
Generic unsigned integer type.
Definition: tommytypes.h:60
void tommy_hashlin_foreach(tommy_hashlin *hashlin, tommy_foreach_func *func)
Calls the specified function for each element in the hashtable.
tommy_node tommy_hashlin_node
Hashtable node.
Definition: tommyhashlin.h:158
void tommy_hashlin_done(tommy_hashlin *hashlin)
Deinitializes the hashtable.
tommy_hashlin_node * tommy_hashlin_bucket(tommy_hashlin *hashlin, tommy_hash_t hash)
Gets the bucket of the specified hash.
Definition: tommyhashlin.h:262
void * tommy_hashlin_search(tommy_hashlin *hashlin, tommy_search_func *cmp, const void *cmp_arg, tommy_hash_t hash)
Searches an element in the hashtable.
Definition: tommyhashlin.h:277
Data structure node.
Definition: tommytypes.h:183
void tommy_foreach_func(void *obj)
Foreach function.
Definition: tommytypes.h:289
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. ...
size_t tommy_size_t
Generic size_t type.
Definition: tommytypes.h:50
tommy_count_t split
Split position.
Definition: tommyhashlin.h:176
tommy_size_t tommy_hashlin_memory_usage(tommy_hashlin *hashlin)
Gets the size of allocated memory.
tommy_key_t key
Key used to store the node.
Definition: tommytypes.h:207
void tommy_foreach_arg_func(void *arg, void *obj)
Foreach function with an argument.
Definition: tommytypes.h:296