static const hashkit_st global_default_hash= {
.base_hash= {
- .type= HASHKIT_HASH_DEFAULT,
.function= hashkit_one_at_a_time,
.context= NULL
},
return self->base_hash.function(key, key_length);
}
+hashkit_return_t hashkit_set_base_function(hashkit_st *self, hashkit_hash_algorithm_t hash_algorithm)
+{
+ switch (hash_algorithm)
+ {
+ case HASHKIT_HASH_DEFAULT:
+ self->base_hash.function= hashkit_one_at_a_time;
+ break;
+ case HASHKIT_HASH_MD5:
+ self->base_hash.function= hashkit_md5;
+ break;
+ case HASHKIT_HASH_CRC:
+ self->base_hash.function= hashkit_crc32;
+ break;
+ case HASHKIT_HASH_FNV1_64:
+ self->base_hash.function= hashkit_fnv1_64;
+ break;
+ case HASHKIT_HASH_FNV1A_64:
+ self->base_hash.function= hashkit_fnv1a_64;
+ break;
+ case HASHKIT_HASH_FNV1_32:
+ self->base_hash.function= hashkit_fnv1_32;
+ break;
+ case HASHKIT_HASH_FNV1A_32:
+ self->base_hash.function= hashkit_fnv1a_32;
+ break;
+ case HASHKIT_HASH_HSIEH:
+#ifdef HAVE_HSIEH_HASH
+ self->base_hash.function= hashkit_hsieh;
+ break;
+#else
+ return HASHKIT_FAILURE;
+#endif
+ case HASHKIT_HASH_MURMUR:
+ self->base_hash.function= hashkit_murmur;
+ break;
+ case HASHKIT_HASH_JENKINS:
+ self->base_hash.function= hashkit_jenkins;
+ break;
+ case HASHKIT_HASH_MAX:
+ default:
+ return HASHKIT_FAILURE;
+ break;
+ }
+
+ self->base_hash.context= NULL;
+
+ return HASHKIT_SUCCESS;
+}
+
+hashkit_return_t hashkit_set_base_function_custom(hashkit_st *self, hashkit_hash_fn function, void *context)
+{
+ if (function)
+ {
+ self->base_hash.function= function;
+ self->base_hash.context= context;
+
+ return HASHKIT_SUCCESS;
+ }
+
+ return HASHKIT_FAILURE;
+}
+
uint32_t libhashkit_generate_value(const char *key, size_t key_length, hashkit_hash_algorithm_t hash_algorithm)
{
switch (hash_algorithm)
struct hashkit_st
{
struct {
- hashkit_hash_algorithm_t type;
hashkit_hash_fn function;
void *context;
} base_hash;
HASHKIT_API
uint32_t hashkit_generate_value(const hashkit_st *self, const char *key, size_t key_length);
+HASHKIT_API
+hashkit_return_t hashkit_set_base_function(hashkit_st *hash, hashkit_hash_algorithm_t hash_algorithm);
+
+HASHKIT_API
+hashkit_return_t hashkit_set_base_function_custom(hashkit_st *hash, hashkit_hash_fn function, void *context);
+
HASHKIT_API
uint32_t libhashkit_generate_value(const char *key, size_t key_length, hashkit_hash_algorithm_t hash_algorithm);
extern "C" {
#endif
-const char *hashkit_strerror(hashkit_st *ptr __attribute__((unused)), hashkit_return_t rc);
+HASHKIT_API
+ const char *hashkit_strerror(hashkit_st *ptr __attribute__((unused)), hashkit_return_t rc);
#ifdef __cplusplus
}
return TEST_SUCCESS;
}
+static test_return_t hashkit_set_base_function_test(hashkit_st *hashk)
+{
+ for (hashkit_hash_algorithm_t algo = HASHKIT_HASH_DEFAULT; algo < HASHKIT_HASH_MAX; algo++)
+ {
+ hashkit_return_t rc;
+ uint32_t x;
+ const char **ptr;
+ uint32_t *list;
+
+ rc= hashkit_set_base_function(hashk, algo);
+
+ /* Hsieh is disabled most of the time for patent issues */
+ if (rc == HASHKIT_FAILURE && algo == HASHKIT_HASH_HSIEH)
+ continue;
+
+ test_true(rc == HASHKIT_SUCCESS);
+
+ switch (algo)
+ {
+ case HASHKIT_HASH_DEFAULT:
+ list= one_at_a_time_values;
+ break;
+ case HASHKIT_HASH_MD5:
+ list= md5_values;
+ break;
+ case HASHKIT_HASH_CRC:
+ list= crc_values;
+ break;
+ case HASHKIT_HASH_FNV1_64:
+ list= fnv1_64_values;
+ break;
+ case HASHKIT_HASH_FNV1A_64:
+ list= fnv1a_64_values;
+ break;
+ case HASHKIT_HASH_FNV1_32:
+ list= fnv1_32_values;
+ break;
+ case HASHKIT_HASH_FNV1A_32:
+ list= fnv1a_32_values;
+ break;
+ case HASHKIT_HASH_HSIEH:
+ list= hsieh_values;
+ break;
+ case HASHKIT_HASH_MURMUR:
+ list= murmur_values;
+ break;
+ case HASHKIT_HASH_JENKINS:
+ list= jenkins_values;
+ break;
+ case HASHKIT_HASH_MAX:
+ default:
+ list= NULL;
+ test_fail("We ended up on a non-existent hash");
+ }
+
+ // Now we make sure we did set the hash correctly.
+ for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
+ {
+ uint32_t hash_val;
+
+ hash_val= hashkit_generate_value(hashk, *ptr, strlen(*ptr));
+ test_true(list[x] == hash_val);
+ }
+ }
+
+ return TEST_SUCCESS;
+}
+
+static test_return_t hashkit_set_base_function_custom_test(hashkit_st *hashk)
+{
+ hashkit_return_t rc;
+ uint32_t x;
+ const char **ptr;
+
+
+ rc= hashkit_set_base_function_custom(hashk, hashkit_md5, NULL);
+ test_true(rc == HASHKIT_SUCCESS);
+
+ for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
+ {
+ uint32_t hash_val;
+
+ hash_val= hashkit_generate_value(hashk, *ptr, strlen(*ptr));
+ test_true(md5_values[x] == hash_val);
+ }
+
+ return TEST_SUCCESS;
+}
+
test_st hashkit_st_functions[] ={
{"hashkit_generate_value", 0, (test_callback_fn)hashkit_generate_value_test},
+ {"hashkit_set_base_function", 0, (test_callback_fn)hashkit_set_base_function_test},
+ {"hashkit_set_base_function_custom", 0, (test_callback_fn)hashkit_set_base_function_custom_test},
{0, 0, 0}
};