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
)
13 switch (hash_algorithm
)
15 case HASHKIT_HASH_DEFAULT
:
16 self
->function
= hashkit_one_at_a_time
;
18 case HASHKIT_HASH_MD5
:
19 self
->function
= hashkit_md5
;
21 case HASHKIT_HASH_CRC
:
22 self
->function
= hashkit_crc32
;
24 case HASHKIT_HASH_FNV1_64
:
25 self
->function
= hashkit_fnv1_64
;
27 case HASHKIT_HASH_FNV1A_64
:
28 self
->function
= hashkit_fnv1a_64
;
30 case HASHKIT_HASH_FNV1_32
:
31 self
->function
= hashkit_fnv1_32
;
33 case HASHKIT_HASH_FNV1A_32
:
34 self
->function
= hashkit_fnv1a_32
;
36 case HASHKIT_HASH_HSIEH
:
37 #ifdef HAVE_HSIEH_HASH
38 self
->function
= hashkit_hsieh
;
41 return HASHKIT_FAILURE
;
43 case HASHKIT_HASH_MURMUR
:
44 #ifdef HAVE_MURMUR_HASH
45 self
->function
= hashkit_murmur
;
48 return HASHKIT_FAILURE
;
50 case HASHKIT_HASH_JENKINS
:
51 self
->function
= hashkit_jenkins
;
53 case HASHKIT_HASH_CUSTOM
:
54 return HASHKIT_INVALID_ARGUMENT
;
55 case HASHKIT_HASH_MAX
:
57 return HASHKIT_INVALID_HASH
;
62 return HASHKIT_SUCCESS
;
65 hashkit_return_t
hashkit_set_function(hashkit_st
*self
, hashkit_hash_algorithm_t hash_algorithm
)
67 return _set_function(&self
->base_hash
, hash_algorithm
);
70 hashkit_return_t
hashkit_set_distribution_function(hashkit_st
*self
, hashkit_hash_algorithm_t hash_algorithm
)
72 return _set_function(&self
->distribution_hash
, hash_algorithm
);
75 static hashkit_return_t
_set_custom_function(struct hashkit_st::hashkit_function_st
*self
, hashkit_hash_fn function
, void *context
)
79 self
->function
= function
;
80 self
->context
= context
;
82 return HASHKIT_SUCCESS
;
85 return HASHKIT_FAILURE
;
88 hashkit_return_t
hashkit_set_custom_function(hashkit_st
*self
, hashkit_hash_fn function
, void *context
)
90 return _set_custom_function(&self
->base_hash
, function
, context
);
93 hashkit_return_t
hashkit_set_custom_distribution_function(hashkit_st
*self
, hashkit_hash_fn function
, void *context
)
95 return _set_custom_function(&self
->distribution_hash
, function
, context
);
98 static hashkit_hash_algorithm_t
get_function_type(const hashkit_hash_fn function
)
100 if (function
== hashkit_one_at_a_time
)
102 return HASHKIT_HASH_DEFAULT
;
104 else if (function
== hashkit_md5
)
106 return HASHKIT_HASH_MD5
;
108 else if (function
== hashkit_crc32
)
110 return HASHKIT_HASH_CRC
;
112 else if (function
== hashkit_fnv1_64
)
114 return HASHKIT_HASH_FNV1_64
;
116 else if (function
== hashkit_fnv1a_64
)
118 return HASHKIT_HASH_FNV1A_64
;
120 else if (function
== hashkit_fnv1_32
)
122 return HASHKIT_HASH_FNV1_32
;
124 else if (function
== hashkit_fnv1a_32
)
126 return HASHKIT_HASH_FNV1A_32
;
128 #ifdef HAVE_HSIEH_HASH
129 else if (function
== hashkit_hsieh
)
131 return HASHKIT_HASH_HSIEH
;
134 #ifdef HAVE_MURMUR_HASH
135 else if (function
== hashkit_murmur
)
137 return HASHKIT_HASH_MURMUR
;
140 else if (function
== hashkit_jenkins
)
142 return HASHKIT_HASH_JENKINS
;
145 return HASHKIT_HASH_CUSTOM
;
148 hashkit_hash_algorithm_t
hashkit_get_function(const hashkit_st
*self
)
150 return get_function_type(self
->base_hash
.function
);
153 hashkit_hash_algorithm_t
hashkit_get_distribution_function(const hashkit_st
*self
)
155 return get_function_type(self
->distribution_hash
.function
);