2 * Copyright (C) 2010 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
9 #include <libhashkit/common.h>
11 static hashkit_return_t
_set_function(struct hashkit_st::hashkit_function_st
*self
, hashkit_hash_algorithm_t hash_algorithm
)
15 return HASHKIT_INVALID_ARGUMENT
;
18 switch (hash_algorithm
)
20 case HASHKIT_HASH_MD5
:
21 self
->function
= hashkit_md5
;
24 case HASHKIT_HASH_CRC
:
25 self
->function
= hashkit_crc32
;
28 case HASHKIT_HASH_FNV1_64
:
29 if (libhashkit_has_algorithm(HASHKIT_HASH_FNV1_64
))
31 self
->function
= hashkit_fnv1_64
;
34 return HASHKIT_INVALID_ARGUMENT
;
36 case HASHKIT_HASH_FNV1A_64
:
37 if (libhashkit_has_algorithm(HASHKIT_HASH_FNV1_64
))
39 self
->function
= hashkit_fnv1a_64
;
42 return HASHKIT_INVALID_ARGUMENT
;
44 case HASHKIT_HASH_FNV1_32
:
45 self
->function
= hashkit_fnv1_32
;
48 case HASHKIT_HASH_FNV1A_32
:
49 self
->function
= hashkit_fnv1a_32
;
52 case HASHKIT_HASH_HSIEH
:
53 if (libhashkit_has_algorithm(HASHKIT_HASH_HSIEH
))
55 self
->function
= hashkit_hsieh
;
58 return HASHKIT_INVALID_ARGUMENT
;
60 case HASHKIT_HASH_MURMUR
:
61 if (libhashkit_has_algorithm(HASHKIT_HASH_MURMUR
))
63 self
->function
= hashkit_murmur
;
66 return HASHKIT_INVALID_ARGUMENT
;
68 case HASHKIT_HASH_JENKINS
:
69 self
->function
= hashkit_jenkins
;
72 case HASHKIT_HASH_CUSTOM
:
73 return HASHKIT_INVALID_ARGUMENT
;
75 case HASHKIT_HASH_DEFAULT
:
76 self
->function
= hashkit_one_at_a_time
;
79 case HASHKIT_HASH_MAX
:
80 self
->function
= hashkit_one_at_a_time
;
81 return HASHKIT_INVALID_HASH
;
86 return HASHKIT_SUCCESS
;
89 hashkit_return_t
hashkit_set_function(hashkit_st
*self
, hashkit_hash_algorithm_t hash_algorithm
)
91 return _set_function(&self
->base_hash
, hash_algorithm
);
94 hashkit_return_t
hashkit_set_distribution_function(hashkit_st
*self
, hashkit_hash_algorithm_t hash_algorithm
)
96 return _set_function(&self
->distribution_hash
, hash_algorithm
);
99 static hashkit_return_t
_set_custom_function(struct hashkit_st::hashkit_function_st
*self
, hashkit_hash_fn function
, void *context
)
103 return HASHKIT_INVALID_ARGUMENT
;
108 self
->function
= function
;
109 self
->context
= context
;
111 return HASHKIT_SUCCESS
;
114 return HASHKIT_FAILURE
;
117 hashkit_return_t
hashkit_set_custom_function(hashkit_st
*self
, hashkit_hash_fn function
, void *context
)
121 return HASHKIT_INVALID_ARGUMENT
;
125 return _set_custom_function(&self
->base_hash
, function
, context
);
128 hashkit_return_t
hashkit_set_custom_distribution_function(hashkit_st
*self
, hashkit_hash_fn function
, void *context
)
132 return HASHKIT_INVALID_ARGUMENT
;
135 return _set_custom_function(&self
->distribution_hash
, function
, context
);
138 static hashkit_hash_algorithm_t
get_function_type(const hashkit_hash_fn function
)
140 if (function
== hashkit_one_at_a_time
)
142 return HASHKIT_HASH_DEFAULT
;
144 else if (function
== hashkit_md5
)
146 return HASHKIT_HASH_MD5
;
148 else if (function
== hashkit_crc32
)
150 return HASHKIT_HASH_CRC
;
152 else if (function
== hashkit_fnv1_64
)
154 return HASHKIT_HASH_FNV1_64
;
156 else if (function
== hashkit_fnv1a_64
)
158 return HASHKIT_HASH_FNV1A_64
;
160 else if (function
== hashkit_fnv1_32
)
162 return HASHKIT_HASH_FNV1_32
;
164 else if (function
== hashkit_fnv1a_32
)
166 return HASHKIT_HASH_FNV1A_32
;
168 else if (function
== hashkit_hsieh
)
170 return HASHKIT_HASH_HSIEH
;
172 else if (function
== hashkit_murmur
)
174 return HASHKIT_HASH_MURMUR
;
176 else if (function
== hashkit_jenkins
)
178 return HASHKIT_HASH_JENKINS
;
181 return HASHKIT_HASH_CUSTOM
;
184 hashkit_hash_algorithm_t
hashkit_get_function(const hashkit_st
*self
)
188 return HASHKIT_HASH_DEFAULT
;
191 return get_function_type(self
->base_hash
.function
);
194 hashkit_hash_algorithm_t
hashkit_get_distribution_function(const hashkit_st
*self
)
198 return HASHKIT_HASH_DEFAULT
;
201 return get_function_type(self
->distribution_hash
.function
);