2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
11 static const hashkit_st global_default_hash
= {
13 .function
= hashkit_one_at_a_time
,
19 @note We make no assumptions that "hashk" has been, or not been allocated from heap/stack. We just know we didn't do it.
21 hashkit_st
*hashkit_create(hashkit_st
*self
)
25 self
= (hashkit_st
*)malloc(sizeof(hashkit_st
));
31 self
->options
.is_allocated
= true;
35 self
->options
.is_allocated
= false;
38 self
->base_hash
= global_default_hash
.base_hash
;
44 void hashkit_free(hashkit_st
*self
)
46 if (hashkit_is_allocated(self
))
52 hashkit_st
*hashkit_clone(hashkit_st
*destination
, const hashkit_st
*source
)
56 return hashkit_create(destination
);
59 /* new_clone will be a pointer to destination */
60 destination
= hashkit_create(destination
);
62 // Should only happen on allocation failure.
63 if (destination
== NULL
)
68 destination
->base_hash
= source
->base_hash
;
73 bool hashkit_compare(const hashkit_st
*first
, const hashkit_st
*second
)
75 if (first
->base_hash
.function
== second
->base_hash
.function
&&
76 first
->base_hash
.context
== second
->base_hash
.context
)
82 uint32_t hashkit_generate_value(const hashkit_st
*self
, const char *key
, size_t key_length
)
84 return self
->base_hash
.function(key
, key_length
, self
->base_hash
.context
);
87 hashkit_return_t
hashkit_set_base_function(hashkit_st
*self
, hashkit_hash_algorithm_t hash_algorithm
)
89 switch (hash_algorithm
)
91 case HASHKIT_HASH_DEFAULT
:
92 self
->base_hash
.function
= hashkit_one_at_a_time
;
94 case HASHKIT_HASH_MD5
:
95 self
->base_hash
.function
= hashkit_md5
;
97 case HASHKIT_HASH_CRC
:
98 self
->base_hash
.function
= hashkit_crc32
;
100 case HASHKIT_HASH_FNV1_64
:
101 self
->base_hash
.function
= hashkit_fnv1_64
;
103 case HASHKIT_HASH_FNV1A_64
:
104 self
->base_hash
.function
= hashkit_fnv1a_64
;
106 case HASHKIT_HASH_FNV1_32
:
107 self
->base_hash
.function
= hashkit_fnv1_32
;
109 case HASHKIT_HASH_FNV1A_32
:
110 self
->base_hash
.function
= hashkit_fnv1a_32
;
112 case HASHKIT_HASH_HSIEH
:
113 #ifdef HAVE_HSIEH_HASH
114 self
->base_hash
.function
= hashkit_hsieh
;
117 return HASHKIT_FAILURE
;
119 case HASHKIT_HASH_MURMUR
:
120 self
->base_hash
.function
= hashkit_murmur
;
122 case HASHKIT_HASH_JENKINS
:
123 self
->base_hash
.function
= hashkit_jenkins
;
125 case HASHKIT_HASH_CUSTOM
:
126 case HASHKIT_HASH_MAX
:
128 return HASHKIT_FAILURE
;
132 self
->base_hash
.context
= NULL
;
134 return HASHKIT_SUCCESS
;
137 hashkit_return_t
hashkit_set_base_function_custom(hashkit_st
*self
, hashkit_hash_fn function
, void *context
)
141 self
->base_hash
.function
= function
;
142 self
->base_hash
.context
= context
;
144 return HASHKIT_SUCCESS
;
147 return HASHKIT_FAILURE
;
150 hashkit_hash_algorithm_t
hashkit_get_base_function(const hashkit_st
*self
)
152 if (self
->base_hash
.function
== hashkit_one_at_a_time
)
154 return HASHKIT_HASH_DEFAULT
;
156 else if (self
->base_hash
.function
== hashkit_md5
)
158 return HASHKIT_HASH_MD5
;
160 else if (self
->base_hash
.function
== hashkit_crc32
)
162 return HASHKIT_HASH_CRC
;
164 else if (self
->base_hash
.function
== hashkit_fnv1_64
)
166 return HASHKIT_HASH_FNV1_64
;
168 else if (self
->base_hash
.function
== hashkit_fnv1a_64
)
170 return HASHKIT_HASH_FNV1A_64
;
172 else if (self
->base_hash
.function
== hashkit_fnv1_32
)
174 return HASHKIT_HASH_FNV1_32
;
176 else if (self
->base_hash
.function
== hashkit_fnv1a_32
)
178 return HASHKIT_HASH_FNV1A_32
;
180 #ifdef HAVE_HSIEH_HASH
181 else if (self
->base_hash
.function
== hashkit_hsieh
)
183 return HASHKIT_HASH_HSIEH
;
186 else if (self
->base_hash
.function
== hashkit_murmur
)
188 return HASHKIT_HASH_MURMUR
;
190 else if (self
->base_hash
.function
== hashkit_jenkins
)
192 return HASHKIT_HASH_JENKINS
;
195 return HASHKIT_HASH_CUSTOM
;
198 uint32_t libhashkit_generate_value(const char *key
, size_t key_length
, hashkit_hash_algorithm_t hash_algorithm
)
200 switch (hash_algorithm
)
202 case HASHKIT_HASH_DEFAULT
:
203 return libhashkit_one_at_a_time(key
, key_length
);
204 case HASHKIT_HASH_MD5
:
205 return libhashkit_md5(key
, key_length
);
206 case HASHKIT_HASH_CRC
:
207 return libhashkit_crc32(key
, key_length
);
208 case HASHKIT_HASH_FNV1_64
:
209 return libhashkit_fnv1_64(key
, key_length
);
210 case HASHKIT_HASH_FNV1A_64
:
211 return libhashkit_fnv1a_64(key
, key_length
);
212 case HASHKIT_HASH_FNV1_32
:
213 return libhashkit_fnv1_32(key
, key_length
);
214 case HASHKIT_HASH_FNV1A_32
:
215 return libhashkit_fnv1a_32(key
, key_length
);
216 case HASHKIT_HASH_HSIEH
:
217 #ifdef HAVE_HSIEH_HASH
218 return libhashkit_hsieh(key
, key_length
);
222 case HASHKIT_HASH_MURMUR
:
223 return libhashkit_murmur(key
, key_length
);
224 case HASHKIT_HASH_JENKINS
:
225 return libhashkit_jenkins(key
, key_length
);
226 case HASHKIT_HASH_CUSTOM
:
227 case HASHKIT_HASH_MAX
:
230 fprintf(stderr
, "hashkit_hash_t was extended but libhashkit_generate_value was not updated\n");