Fixed autoconf macros for cross compile
[awesomized/libmemcached] / libhashkit / function.c
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 "common.h"
10
11 static hashkit_return_t _set_function(struct 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 self->function= hashkit_murmur;
45 break;
46 case HASHKIT_HASH_JENKINS:
47 self->function= hashkit_jenkins;
48 break;
49 case HASHKIT_HASH_CUSTOM:
50 case HASHKIT_HASH_MAX:
51 default:
52 return HASHKIT_FAILURE;
53 }
54
55 self->context= NULL;
56
57 return HASHKIT_SUCCESS;
58 }
59
60 hashkit_return_t hashkit_set_function(hashkit_st *self, hashkit_hash_algorithm_t hash_algorithm)
61 {
62 return _set_function(&self->base_hash, hash_algorithm);
63 }
64
65 hashkit_return_t hashkit_set_distribution_function(hashkit_st *self, hashkit_hash_algorithm_t hash_algorithm)
66 {
67 return _set_function(&self->distribution_hash, hash_algorithm);
68 }
69
70 static hashkit_return_t _set_custom_function(struct hashkit_function_st *self, hashkit_hash_fn function, void *context)
71 {
72 if (function)
73 {
74 self->function= function;
75 self->context= context;
76
77 return HASHKIT_SUCCESS;
78 }
79
80 return HASHKIT_FAILURE;
81 }
82
83 hashkit_return_t hashkit_set_custom_function(hashkit_st *self, hashkit_hash_fn function, void *context)
84 {
85 return _set_custom_function(&self->base_hash, function, context);
86 }
87
88 hashkit_return_t hashkit_set_custom_distribution_function(hashkit_st *self, hashkit_hash_fn function, void *context)
89 {
90 return _set_custom_function(&self->distribution_hash, function, context);
91 }
92
93 static hashkit_hash_algorithm_t get_function_type(const hashkit_hash_fn function)
94 {
95 if (function == hashkit_one_at_a_time)
96 {
97 return HASHKIT_HASH_DEFAULT;
98 }
99 else if (function == hashkit_md5)
100 {
101 return HASHKIT_HASH_MD5;
102 }
103 else if (function == hashkit_crc32)
104 {
105 return HASHKIT_HASH_CRC;
106 }
107 else if (function == hashkit_fnv1_64)
108 {
109 return HASHKIT_HASH_FNV1_64;
110 }
111 else if (function == hashkit_fnv1a_64)
112 {
113 return HASHKIT_HASH_FNV1A_64;
114 }
115 else if (function == hashkit_fnv1_32)
116 {
117 return HASHKIT_HASH_FNV1_32;
118 }
119 else if (function == hashkit_fnv1a_32)
120 {
121 return HASHKIT_HASH_FNV1A_32;
122 }
123 #ifdef HAVE_HSIEH_HASH
124 else if (function == hashkit_hsieh)
125 {
126 return HASHKIT_HASH_HSIEH;
127 }
128 #endif
129 else if (function == hashkit_murmur)
130 {
131 return HASHKIT_HASH_MURMUR;
132 }
133 else if (function == hashkit_jenkins)
134 {
135 return HASHKIT_HASH_JENKINS;
136 }
137
138 return HASHKIT_HASH_CUSTOM;
139 }
140
141 hashkit_hash_algorithm_t hashkit_get_function(const hashkit_st *self)
142 {
143 return get_function_type(self->base_hash.function);
144 }
145
146 hashkit_hash_algorithm_t hashkit_get_distribution_function(const hashkit_st *self)
147 {
148 return get_function_type(self->distribution_hash.function);
149 }