From 138400e5587b4a716957157779ecd0b3092f9634 Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Wed, 22 Jul 2020 11:36:03 -0400 Subject: [PATCH] split sdbm code from hash.c to independent program --- hash/hash_sdbm.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 hash/hash_sdbm.c diff --git a/hash/hash_sdbm.c b/hash/hash_sdbm.c new file mode 100644 index 00000000..0f7a15ea --- /dev/null +++ b/hash/hash_sdbm.c @@ -0,0 +1,49 @@ +/** + * @addtogroup hash Hash algorithms + * @{ + * @file hash_sdbm.c + * @author [Christian Bender](https://github.com/christianbender) + * @brief [SDBM hash algorithm](http://www.cse.yorku.ca/~oz/hash.html) + */ +#include +#include +#include + +/** + * @brief SDBM algorithm implementation + * + * @param s NULL terminated string to hash + * @return 64-bit + */ +uint64_t sdbm(const char* s) +{ + uint64_t hash = 0; + int i = 0; + while (s[i] != '\0') + { + hash = s[i] + (hash << 6) + (hash << 16) - hash; + i++; + } + return hash; +} + +/** + * @brief Test function for ::sdbm + */ +void test_sdbm() +{ + assert(sdbm("Hello World") == 12881824461405877380); + assert(sdbm("Hello World!") == 7903571203300273309); + assert(sdbm("Hello world") == 15154913742888948900); + assert(sdbm("Hello world!") == 15254999417003201661); + printf("Tests passed\n"); +} + +/** @} */ + +/** Main function */ +int main() +{ + test_sdbm(); + return 0; +}