tommyhash.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 
32 #ifndef __TOMMYHASH_H
33 #define __TOMMYHASH_H
34 
35 #include "tommytypes.h"
36 
37 /******************************************************************************/
38 /* hash */
39 
44 
63 tommy_uint32_t tommy_hash_u32(tommy_uint32_t init_val, const void* void_key, tommy_size_t key_len);
64 
83 tommy_uint64_t tommy_hash_u64(tommy_uint64_t init_val, const void* void_key, tommy_size_t key_len);
84 
101 tommy_uint32_t tommy_strhash_u32(tommy_uint64_t init_val, const void* void_key);
102 
109 {
110  key -= key << 6;
111  key ^= key >> 17;
112  key -= key << 9;
113  key ^= key << 4;
114  key -= key << 3;
115  key ^= key << 10;
116  key ^= key >> 15;
117 
118  return key;
119 }
120 
127 {
128  key = ~key + (key << 21);
129  key = key ^ (key >> 24);
130  key = key + (key << 3) + (key << 8);
131  key = key ^ (key >> 14);
132  key = key + (key << 2) + (key << 4);
133  key = key ^ (key >> 28);
134  key = key + (key << 31);
135 
136  return key;
137 }
138 
139 #endif
140 
tommy_uint32_t tommy_hash_u32(tommy_uint32_t init_val, const void *void_key, tommy_size_t key_len)
Hash function with a 32 bits result.
tommy_uint32_t tommy_key_t
Key type used in indexed data structures to store the key or the hash value.
Definition: tommytypes.h:160
uint32_t tommy_uint32_t
Generic uint32_t type.
Definition: tommytypes.h:46
tommy_key_t tommy_hash_t
Hash type used in hashtables.
Definition: tommyhash.h:43
tommy_uint32_t tommy_inthash_u32(tommy_uint32_t key)
Integer reversible hash function for 32 bits.
Definition: tommyhash.h:108
tommy_uint64_t tommy_hash_u64(tommy_uint64_t init_val, const void *void_key, tommy_size_t key_len)
Hash function with a 64 bits result.
tommy_uint64_t tommy_inthash_u64(tommy_uint64_t key)
Integer reversible hash function for 64 bits.
Definition: tommyhash.h:126
size_t tommy_size_t
Generic size_t type.
Definition: tommytypes.h:50
Generic types.
uint64_t tommy_uint64_t
Generic uint64_t type.
Definition: tommytypes.h:47
tommy_uint32_t tommy_strhash_u32(tommy_uint64_t init_val, const void *void_key)
String hash function with a 32 bits result.