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_MURMUR3
:
61 case HASHKIT_HASH_MURMUR
:
62 if (libhashkit_has_algorithm(HASHKIT_HASH_MURMUR
))
64 self
->function
= hashkit_murmur
;
67 return HASHKIT_INVALID_ARGUMENT
;
69 case HASHKIT_HASH_JENKINS
:
70 self
->function
= hashkit_jenkins
;
73 case HASHKIT_HASH_CUSTOM
:
74 return HASHKIT_INVALID_ARGUMENT
;
76 case HASHKIT_HASH_DEFAULT
:
77 self
->function
= hashkit_one_at_a_time
;
80 case HASHKIT_HASH_MAX
:
81 self
->function
= hashkit_one_at_a_time
;
82 return HASHKIT_INVALID_HASH
;
87 return HASHKIT_SUCCESS
;
90 hashkit_return_t
hashkit_set_function(hashkit_st
*self
, hashkit_hash_algorithm_t hash_algorithm
)
92 return _set_function(&self
->base_hash
, hash_algorithm
);
95 hashkit_return_t
hashkit_set_distribution_function(hashkit_st
*self
, hashkit_hash_algorithm_t hash_algorithm
)
97 return _set_function(&self
->distribution_hash
, hash_algorithm
);
100 static hashkit_return_t
_set_custom_function(struct hashkit_st::hashkit_function_st
*self
, hashkit_hash_fn function
, void *context
)
104 return HASHKIT_INVALID_ARGUMENT
;
109 self
->function
= function
;
110 self
->context
= context
;
112 return HASHKIT_SUCCESS
;
115 return HASHKIT_FAILURE
;
118 hashkit_return_t
hashkit_set_custom_function(hashkit_st
*self
, hashkit_hash_fn function
, void *context
)
122 return HASHKIT_INVALID_ARGUMENT
;
126 return _set_custom_function(&self
->base_hash
, function
, context
);
129 hashkit_return_t
hashkit_set_custom_distribution_function(hashkit_st
*self
, hashkit_hash_fn function
, void *context
)
133 return HASHKIT_INVALID_ARGUMENT
;
136 return _set_custom_function(&self
->distribution_hash
, function
, context
);
139 static hashkit_hash_algorithm_t
get_function_type(const hashkit_hash_fn function
)
141 if (function
== hashkit_one_at_a_time
)
143 return HASHKIT_HASH_DEFAULT
;
145 else if (function
== hashkit_md5
)
147 return HASHKIT_HASH_MD5
;
149 else if (function
== hashkit_crc32
)
151 return HASHKIT_HASH_CRC
;
153 else if (function
== hashkit_fnv1_64
)
155 return HASHKIT_HASH_FNV1_64
;
157 else if (function
== hashkit_fnv1a_64
)
159 return HASHKIT_HASH_FNV1A_64
;
161 else if (function
== hashkit_fnv1_32
)
163 return HASHKIT_HASH_FNV1_32
;
165 else if (function
== hashkit_fnv1a_32
)
167 return HASHKIT_HASH_FNV1A_32
;
169 else if (function
== hashkit_hsieh
)
171 return HASHKIT_HASH_HSIEH
;
173 else if (function
== hashkit_murmur
)
175 return HASHKIT_HASH_MURMUR
;
177 else if (function
== hashkit_jenkins
)
179 return HASHKIT_HASH_JENKINS
;
182 return HASHKIT_HASH_CUSTOM
;
185 hashkit_hash_algorithm_t
hashkit_get_function(const hashkit_st
*self
)
189 return HASHKIT_HASH_DEFAULT
;
192 return get_function_type(self
->base_hash
.function
);
195 hashkit_hash_algorithm_t
hashkit_get_distribution_function(const hashkit_st
*self
)
199 return HASHKIT_HASH_DEFAULT
;
202 return get_function_type(self
->distribution_hash
.function
);