Cleanup around the timeout code.
[awesomized/libmemcached] / libhashkit / function.cc
1 /* HashKit
2 * Copyright (C) 2010 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 */
8
9 #include <libhashkit/common.h>
10
11 static hashkit_return_t _set_function(struct hashkit_st::hashkit_function_st *self, hashkit_hash_algorithm_t hash_algorithm)
12 {
13 if (self == NULL)
14 {
15 return HASHKIT_INVALID_ARGUMENT;
16 }
17
18 switch (hash_algorithm)
19 {
20 case HASHKIT_HASH_MD5:
21 self->function= hashkit_md5;
22 break;
23
24 case HASHKIT_HASH_CRC:
25 self->function= hashkit_crc32;
26 break;
27
28 case HASHKIT_HASH_FNV1_64:
29 if (libhashkit_has_algorithm(HASHKIT_HASH_FNV1_64))
30 {
31 self->function= hashkit_fnv1_64;
32 break;
33 }
34 return HASHKIT_INVALID_ARGUMENT;
35
36 case HASHKIT_HASH_FNV1A_64:
37 if (libhashkit_has_algorithm(HASHKIT_HASH_FNV1_64))
38 {
39 self->function= hashkit_fnv1a_64;
40 break;
41 }
42 return HASHKIT_INVALID_ARGUMENT;
43
44 case HASHKIT_HASH_FNV1_32:
45 self->function= hashkit_fnv1_32;
46 break;
47
48 case HASHKIT_HASH_FNV1A_32:
49 self->function= hashkit_fnv1a_32;
50 break;
51
52 case HASHKIT_HASH_HSIEH:
53 if (libhashkit_has_algorithm(HASHKIT_HASH_HSIEH))
54 {
55 self->function= hashkit_hsieh;
56 break;
57 }
58 return HASHKIT_INVALID_ARGUMENT;
59
60 case HASHKIT_HASH_MURMUR3:
61 case HASHKIT_HASH_MURMUR:
62 if (libhashkit_has_algorithm(HASHKIT_HASH_MURMUR))
63 {
64 self->function= hashkit_murmur;
65 break;
66 }
67 return HASHKIT_INVALID_ARGUMENT;
68
69 case HASHKIT_HASH_JENKINS:
70 self->function= hashkit_jenkins;
71 break;
72
73 case HASHKIT_HASH_CUSTOM:
74 return HASHKIT_INVALID_ARGUMENT;
75
76 case HASHKIT_HASH_DEFAULT:
77 self->function= hashkit_one_at_a_time;
78 break;
79
80 case HASHKIT_HASH_MAX:
81 self->function= hashkit_one_at_a_time;
82 return HASHKIT_INVALID_HASH;
83 }
84
85 self->context= NULL;
86
87 return HASHKIT_SUCCESS;
88 }
89
90 hashkit_return_t hashkit_set_function(hashkit_st *self, hashkit_hash_algorithm_t hash_algorithm)
91 {
92 return _set_function(&self->base_hash, hash_algorithm);
93 }
94
95 hashkit_return_t hashkit_set_distribution_function(hashkit_st *self, hashkit_hash_algorithm_t hash_algorithm)
96 {
97 return _set_function(&self->distribution_hash, hash_algorithm);
98 }
99
100 static hashkit_return_t _set_custom_function(struct hashkit_st::hashkit_function_st *self, hashkit_hash_fn function, void *context)
101 {
102 if (self == NULL)
103 {
104 return HASHKIT_INVALID_ARGUMENT;
105 }
106
107 if (function)
108 {
109 self->function= function;
110 self->context= context;
111
112 return HASHKIT_SUCCESS;
113 }
114
115 return HASHKIT_FAILURE;
116 }
117
118 hashkit_return_t hashkit_set_custom_function(hashkit_st *self, hashkit_hash_fn function, void *context)
119 {
120 if (self == NULL)
121 {
122 return HASHKIT_INVALID_ARGUMENT;
123 }
124
125
126 return _set_custom_function(&self->base_hash, function, context);
127 }
128
129 hashkit_return_t hashkit_set_custom_distribution_function(hashkit_st *self, hashkit_hash_fn function, void *context)
130 {
131 if (self == NULL)
132 {
133 return HASHKIT_INVALID_ARGUMENT;
134 }
135
136 return _set_custom_function(&self->distribution_hash, function, context);
137 }
138
139 static hashkit_hash_algorithm_t get_function_type(const hashkit_hash_fn function)
140 {
141 if (function == hashkit_one_at_a_time)
142 {
143 return HASHKIT_HASH_DEFAULT;
144 }
145 else if (function == hashkit_md5)
146 {
147 return HASHKIT_HASH_MD5;
148 }
149 else if (function == hashkit_crc32)
150 {
151 return HASHKIT_HASH_CRC;
152 }
153 else if (function == hashkit_fnv1_64)
154 {
155 return HASHKIT_HASH_FNV1_64;
156 }
157 else if (function == hashkit_fnv1a_64)
158 {
159 return HASHKIT_HASH_FNV1A_64;
160 }
161 else if (function == hashkit_fnv1_32)
162 {
163 return HASHKIT_HASH_FNV1_32;
164 }
165 else if (function == hashkit_fnv1a_32)
166 {
167 return HASHKIT_HASH_FNV1A_32;
168 }
169 else if (function == hashkit_hsieh)
170 {
171 return HASHKIT_HASH_HSIEH;
172 }
173 else if (function == hashkit_murmur)
174 {
175 return HASHKIT_HASH_MURMUR;
176 }
177 else if (function == hashkit_jenkins)
178 {
179 return HASHKIT_HASH_JENKINS;
180 }
181
182 return HASHKIT_HASH_CUSTOM;
183 }
184
185 hashkit_hash_algorithm_t hashkit_get_function(const hashkit_st *self)
186 {
187 if (self == NULL)
188 {
189 return HASHKIT_HASH_DEFAULT;
190 }
191
192 return get_function_type(self->base_hash.function);
193 }
194
195 hashkit_hash_algorithm_t hashkit_get_distribution_function(const hashkit_st *self)
196 {
197 if (self == NULL)
198 {
199 return HASHKIT_HASH_DEFAULT;
200 }
201
202 return get_function_type(self->distribution_hash.function);
203 }