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 .type
= HASHKIT_HASH_DEFAULT
,
14 .function
= hashkit_one_at_a_time
,
20 @note We make no assumptions that "hashk" has been, or not been allocated from heap/stack. We just know we didn't do it.
22 hashkit_st
*hashkit_create(hashkit_st
*self
)
26 self
= (hashkit_st
*)malloc(sizeof(hashkit_st
));
32 self
->options
.is_allocated
= true;
36 self
->options
.is_allocated
= false;
39 self
->base_hash
= global_default_hash
.base_hash
;
45 void hashkit_free(hashkit_st
*self
)
47 if (hashkit_is_allocated(self
))
54 @note We do assume source is valid. If source does not exist, we allocate.
56 hashkit_st
*hashkit_clone(hashkit_st
*destination
, const hashkit_st
*source
)
60 return hashkit_create(destination
);
63 /* new_clone will be a pointer to destination */
64 destination
= hashkit_create(destination
);
66 // Should only happen on allocation failure.
67 if (destination
== NULL
)
72 destination
->base_hash
= source
->base_hash
;
78 uint32_t hashkit_generate_value(const hashkit_st
*self
, const char *key
, size_t key_length
)
80 return self
->base_hash
.function(key
, key_length
);
83 uint32_t libhashkit_generate_value(const char *key
, size_t key_length
, hashkit_hash_algorithm_t hash_algorithm
)
85 switch (hash_algorithm
)
87 case HASHKIT_HASH_DEFAULT
:
88 return hashkit_one_at_a_time(key
, key_length
);
89 case HASHKIT_HASH_MD5
:
90 return hashkit_md5(key
, key_length
);
91 case HASHKIT_HASH_CRC
:
92 return hashkit_crc32(key
, key_length
);
93 case HASHKIT_HASH_FNV1_64
:
94 return hashkit_fnv1_64(key
, key_length
);
95 case HASHKIT_HASH_FNV1A_64
:
96 return hashkit_fnv1a_64(key
, key_length
);
97 case HASHKIT_HASH_FNV1_32
:
98 return hashkit_fnv1_32(key
, key_length
);
99 case HASHKIT_HASH_FNV1A_32
:
100 return hashkit_fnv1a_32(key
, key_length
);
101 case HASHKIT_HASH_HSIEH
:
102 #ifdef HAVE_HSIEH_HASH
103 return hashkit_hsieh(key
, key_length
);
107 case HASHKIT_HASH_MURMUR
:
108 return hashkit_murmur(key
, key_length
);
109 case HASHKIT_HASH_JENKINS
:
110 return hashkit_jenkins(key
, key_length
);
111 case HASHKIT_HASH_MAX
:
114 fprintf(stderr
, "hashkit_hash_t was extended but libhashkit_generate_value was not updated\n");