This patch creates a couple of tests around how autoeject works.
[m6w6/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 switch (hash_algorithm)
14 {
15 case HASHKIT_HASH_DEFAULT:
16 self->function= hashkit_one_at_a_time;
17 break;
18 case HASHKIT_HASH_MD5:
19 self->function= hashkit_md5;
20 break;
21 case HASHKIT_HASH_CRC:
22 self->function= hashkit_crc32;
23 break;
24 case HASHKIT_HASH_FNV1_64:
25 self->function= hashkit_fnv1_64;
26 break;
27 case HASHKIT_HASH_FNV1A_64:
28 self->function= hashkit_fnv1a_64;
29 break;
30 case HASHKIT_HASH_FNV1_32:
31 self->function= hashkit_fnv1_32;
32 break;
33 case HASHKIT_HASH_FNV1A_32:
34 self->function= hashkit_fnv1a_32;
35 break;
36 case HASHKIT_HASH_HSIEH:
37 #ifdef HAVE_HSIEH_HASH
38 self->function= hashkit_hsieh;
39 break;
40 #else
41 return HASHKIT_FAILURE;
42 #endif
43 case HASHKIT_HASH_MURMUR:
44 #ifdef HAVE_MURMUR_HASH
45 self->function= hashkit_murmur;
46 break;
47 #else
48 return HASHKIT_FAILURE;
49 #endif
50 case HASHKIT_HASH_JENKINS:
51 self->function= hashkit_jenkins;
52 break;
53 case HASHKIT_HASH_CUSTOM:
54 return HASHKIT_INVALID_ARGUMENT;
55 case HASHKIT_HASH_MAX:
56 default:
57 return HASHKIT_INVALID_HASH;
58 }
59
60 self->context= NULL;
61
62 return HASHKIT_SUCCESS;
63 }
64
65 hashkit_return_t hashkit_set_function(hashkit_st *self, hashkit_hash_algorithm_t hash_algorithm)
66 {
67 return _set_function(&self->base_hash, hash_algorithm);
68 }
69
70 hashkit_return_t hashkit_set_distribution_function(hashkit_st *self, hashkit_hash_algorithm_t hash_algorithm)
71 {
72 return _set_function(&self->distribution_hash, hash_algorithm);
73 }
74
75 static hashkit_return_t _set_custom_function(struct hashkit_st::hashkit_function_st *self, hashkit_hash_fn function, void *context)
76 {
77 if (function)
78 {
79 self->function= function;
80 self->context= context;
81
82 return HASHKIT_SUCCESS;
83 }
84
85 return HASHKIT_FAILURE;
86 }
87
88 hashkit_return_t hashkit_set_custom_function(hashkit_st *self, hashkit_hash_fn function, void *context)
89 {
90 return _set_custom_function(&self->base_hash, function, context);
91 }
92
93 hashkit_return_t hashkit_set_custom_distribution_function(hashkit_st *self, hashkit_hash_fn function, void *context)
94 {
95 return _set_custom_function(&self->distribution_hash, function, context);
96 }
97
98 static hashkit_hash_algorithm_t get_function_type(const hashkit_hash_fn function)
99 {
100 if (function == hashkit_one_at_a_time)
101 {
102 return HASHKIT_HASH_DEFAULT;
103 }
104 else if (function == hashkit_md5)
105 {
106 return HASHKIT_HASH_MD5;
107 }
108 else if (function == hashkit_crc32)
109 {
110 return HASHKIT_HASH_CRC;
111 }
112 else if (function == hashkit_fnv1_64)
113 {
114 return HASHKIT_HASH_FNV1_64;
115 }
116 else if (function == hashkit_fnv1a_64)
117 {
118 return HASHKIT_HASH_FNV1A_64;
119 }
120 else if (function == hashkit_fnv1_32)
121 {
122 return HASHKIT_HASH_FNV1_32;
123 }
124 else if (function == hashkit_fnv1a_32)
125 {
126 return HASHKIT_HASH_FNV1A_32;
127 }
128 #ifdef HAVE_HSIEH_HASH
129 else if (function == hashkit_hsieh)
130 {
131 return HASHKIT_HASH_HSIEH;
132 }
133 #endif
134 #ifdef HAVE_MURMUR_HASH
135 else if (function == hashkit_murmur)
136 {
137 return HASHKIT_HASH_MURMUR;
138 }
139 #endif
140 else if (function == hashkit_jenkins)
141 {
142 return HASHKIT_HASH_JENKINS;
143 }
144
145 return HASHKIT_HASH_CUSTOM;
146 }
147
148 hashkit_hash_algorithm_t hashkit_get_function(const hashkit_st *self)
149 {
150 return get_function_type(self->base_hash.function);
151 }
152
153 hashkit_hash_algorithm_t hashkit_get_distribution_function(const hashkit_st *self)
154 {
155 return get_function_type(self->distribution_hash.function);
156 }