Adding in patch for DEAD server completion.
[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_MURMUR:
61 if (libhashkit_has_algorithm(HASHKIT_HASH_MURMUR))
62 {
63 self->function= hashkit_murmur;
64 break;
65 }
66 return HASHKIT_INVALID_ARGUMENT;
67
68 case HASHKIT_HASH_JENKINS:
69 self->function= hashkit_jenkins;
70 break;
71
72 case HASHKIT_HASH_CUSTOM:
73 return HASHKIT_INVALID_ARGUMENT;
74
75 case HASHKIT_HASH_DEFAULT:
76 self->function= hashkit_one_at_a_time;
77 break;
78
79 case HASHKIT_HASH_MAX:
80 self->function= hashkit_one_at_a_time;
81 return HASHKIT_INVALID_HASH;
82 }
83
84 self->context= NULL;
85
86 return HASHKIT_SUCCESS;
87 }
88
89 hashkit_return_t hashkit_set_function(hashkit_st *self, hashkit_hash_algorithm_t hash_algorithm)
90 {
91 return _set_function(&self->base_hash, hash_algorithm);
92 }
93
94 hashkit_return_t hashkit_set_distribution_function(hashkit_st *self, hashkit_hash_algorithm_t hash_algorithm)
95 {
96 return _set_function(&self->distribution_hash, hash_algorithm);
97 }
98
99 static hashkit_return_t _set_custom_function(struct hashkit_st::hashkit_function_st *self, hashkit_hash_fn function, void *context)
100 {
101 if (self == NULL)
102 {
103 return HASHKIT_INVALID_ARGUMENT;
104 }
105
106 if (function)
107 {
108 self->function= function;
109 self->context= context;
110
111 return HASHKIT_SUCCESS;
112 }
113
114 return HASHKIT_FAILURE;
115 }
116
117 hashkit_return_t hashkit_set_custom_function(hashkit_st *self, hashkit_hash_fn function, void *context)
118 {
119 if (self == NULL)
120 {
121 return HASHKIT_INVALID_ARGUMENT;
122 }
123
124
125 return _set_custom_function(&self->base_hash, function, context);
126 }
127
128 hashkit_return_t hashkit_set_custom_distribution_function(hashkit_st *self, hashkit_hash_fn function, void *context)
129 {
130 if (self == NULL)
131 {
132 return HASHKIT_INVALID_ARGUMENT;
133 }
134
135 return _set_custom_function(&self->distribution_hash, function, context);
136 }
137
138 static hashkit_hash_algorithm_t get_function_type(const hashkit_hash_fn function)
139 {
140 if (function == hashkit_one_at_a_time)
141 {
142 return HASHKIT_HASH_DEFAULT;
143 }
144 else if (function == hashkit_md5)
145 {
146 return HASHKIT_HASH_MD5;
147 }
148 else if (function == hashkit_crc32)
149 {
150 return HASHKIT_HASH_CRC;
151 }
152 else if (function == hashkit_fnv1_64)
153 {
154 return HASHKIT_HASH_FNV1_64;
155 }
156 else if (function == hashkit_fnv1a_64)
157 {
158 return HASHKIT_HASH_FNV1A_64;
159 }
160 else if (function == hashkit_fnv1_32)
161 {
162 return HASHKIT_HASH_FNV1_32;
163 }
164 else if (function == hashkit_fnv1a_32)
165 {
166 return HASHKIT_HASH_FNV1A_32;
167 }
168 else if (function == hashkit_hsieh)
169 {
170 return HASHKIT_HASH_HSIEH;
171 }
172 else if (function == hashkit_murmur)
173 {
174 return HASHKIT_HASH_MURMUR;
175 }
176 else if (function == hashkit_jenkins)
177 {
178 return HASHKIT_HASH_JENKINS;
179 }
180
181 return HASHKIT_HASH_CUSTOM;
182 }
183
184 hashkit_hash_algorithm_t hashkit_get_function(const hashkit_st *self)
185 {
186 if (self == NULL)
187 {
188 return HASHKIT_HASH_DEFAULT;
189 }
190
191 return get_function_type(self->base_hash.function);
192 }
193
194 hashkit_hash_algorithm_t hashkit_get_distribution_function(const hashkit_st *self)
195 {
196 if (self == NULL)
197 {
198 return HASHKIT_HASH_DEFAULT;
199 }
200
201 return get_function_type(self->distribution_hash.function);
202 }