2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
11 static const hashkit_st global_default_hash
= {
13 .function
= hashkit_one_at_a_time
,
19 @note We make no assumptions that "hashk" has been, or not been allocated from heap/stack. We just know we didn't do it.
21 hashkit_st
*hashkit_create(hashkit_st
*self
)
25 self
= (hashkit_st
*)malloc(sizeof(hashkit_st
));
31 self
->options
.is_allocated
= true;
35 self
->options
.is_allocated
= false;
38 self
->base_hash
= global_default_hash
.base_hash
;
44 void hashkit_free(hashkit_st
*self
)
46 if (hashkit_is_allocated(self
))
53 @note We do assume source is valid. If source does not exist, we allocate.
55 hashkit_st
*hashkit_clone(hashkit_st
*destination
, const hashkit_st
*source
)
59 return hashkit_create(destination
);
62 /* new_clone will be a pointer to destination */
63 destination
= hashkit_create(destination
);
65 // Should only happen on allocation failure.
66 if (destination
== NULL
)
71 destination
->base_hash
= source
->base_hash
;
77 uint32_t hashkit_generate_value(const hashkit_st
*self
, const char *key
, size_t key_length
)
79 return self
->base_hash
.function(key
, key_length
);
82 hashkit_return_t
hashkit_set_base_function(hashkit_st
*self
, hashkit_hash_algorithm_t hash_algorithm
)
84 switch (hash_algorithm
)
86 case HASHKIT_HASH_DEFAULT
:
87 self
->base_hash
.function
= hashkit_one_at_a_time
;
89 case HASHKIT_HASH_MD5
:
90 self
->base_hash
.function
= hashkit_md5
;
92 case HASHKIT_HASH_CRC
:
93 self
->base_hash
.function
= hashkit_crc32
;
95 case HASHKIT_HASH_FNV1_64
:
96 self
->base_hash
.function
= hashkit_fnv1_64
;
98 case HASHKIT_HASH_FNV1A_64
:
99 self
->base_hash
.function
= hashkit_fnv1a_64
;
101 case HASHKIT_HASH_FNV1_32
:
102 self
->base_hash
.function
= hashkit_fnv1_32
;
104 case HASHKIT_HASH_FNV1A_32
:
105 self
->base_hash
.function
= hashkit_fnv1a_32
;
107 case HASHKIT_HASH_HSIEH
:
108 #ifdef HAVE_HSIEH_HASH
109 self
->base_hash
.function
= hashkit_hsieh
;
112 return HASHKIT_FAILURE
;
114 case HASHKIT_HASH_MURMUR
:
115 self
->base_hash
.function
= hashkit_murmur
;
117 case HASHKIT_HASH_JENKINS
:
118 self
->base_hash
.function
= hashkit_jenkins
;
120 case HASHKIT_HASH_MAX
:
122 return HASHKIT_FAILURE
;
126 self
->base_hash
.context
= NULL
;
128 return HASHKIT_SUCCESS
;
131 hashkit_return_t
hashkit_set_base_function_custom(hashkit_st
*self
, hashkit_hash_fn function
, void *context
)
135 self
->base_hash
.function
= function
;
136 self
->base_hash
.context
= context
;
138 return HASHKIT_SUCCESS
;
141 return HASHKIT_FAILURE
;
144 uint32_t libhashkit_generate_value(const char *key
, size_t key_length
, hashkit_hash_algorithm_t hash_algorithm
)
146 switch (hash_algorithm
)
148 case HASHKIT_HASH_DEFAULT
:
149 return hashkit_one_at_a_time(key
, key_length
);
150 case HASHKIT_HASH_MD5
:
151 return hashkit_md5(key
, key_length
);
152 case HASHKIT_HASH_CRC
:
153 return hashkit_crc32(key
, key_length
);
154 case HASHKIT_HASH_FNV1_64
:
155 return hashkit_fnv1_64(key
, key_length
);
156 case HASHKIT_HASH_FNV1A_64
:
157 return hashkit_fnv1a_64(key
, key_length
);
158 case HASHKIT_HASH_FNV1_32
:
159 return hashkit_fnv1_32(key
, key_length
);
160 case HASHKIT_HASH_FNV1A_32
:
161 return hashkit_fnv1a_32(key
, key_length
);
162 case HASHKIT_HASH_HSIEH
:
163 #ifdef HAVE_HSIEH_HASH
164 return hashkit_hsieh(key
, key_length
);
168 case HASHKIT_HASH_MURMUR
:
169 return hashkit_murmur(key
, key_length
);
170 case HASHKIT_HASH_JENKINS
:
171 return hashkit_jenkins(key
, key_length
);
172 case HASHKIT_HASH_MAX
:
175 fprintf(stderr
, "hashkit_hash_t was extended but libhashkit_generate_value was not updated\n");