From c524b9a0ca51805ddda716363c14c9d8acadbdd0 Mon Sep 17 00:00:00 2001 From: Christian Bender Date: Sun, 31 Dec 2017 22:58:30 +0100 Subject: [PATCH] Add files via upload --- Hash/README.md | 7 +++++ Hash/hash.c | 63 +++++++++++++++++++++++++++++++++++++++++++++ Hash/hash.h | 43 +++++++++++++++++++++++++++++++ Hash/test_program.c | 20 ++++++++++++++ 4 files changed, 133 insertions(+) create mode 100644 Hash/README.md create mode 100644 Hash/hash.c create mode 100644 Hash/hash.h create mode 100644 Hash/test_program.c diff --git a/Hash/README.md b/Hash/README.md new file mode 100644 index 00000000..1bf23b8f --- /dev/null +++ b/Hash/README.md @@ -0,0 +1,7 @@ +# Hash algorithms + +Overview files **hash.h** and **hash.c** +* sdbm +* djb2 +* xor8 (8 bit) +* adler_32 (32 bit) \ No newline at end of file diff --git a/Hash/hash.c b/Hash/hash.c new file mode 100644 index 00000000..220bb5ca --- /dev/null +++ b/Hash/hash.c @@ -0,0 +1,63 @@ +/* + author: Christian Bender + This is the implementation unit of the hash-functions. + + Overview about hash-functions: + + - sdbm + - djb2 + - xor8 (8 bits) + - adler_32 (32 bits) +*/ + +long long sdbm(char s[]) +{ + long long hash = 0; + int i = 0; + while (s[i] != '\0') + { + hash = s[i] + (hash << 6) + (hash << 16) - hash; + i++; + } + return hash; +} + +long long djb2(char s[]) +{ + long long hash = 5381; /* init value */ + int i = 0; + while (s[i] != '\0') + { + hash = ((hash << 5) + hash) + s[i]; + i++; + } + return hash; +} + +char xor8(char s[]) +{ + int hash = 0; + int i = 0; + while (s[i] != '\0') + { + hash = (hash + s[i]) & 0xff; + i++; + } + return (((hash ^ 0xff) + 1) & 0xff); +} + +int adler_32(char s[]) +{ + int a = 1; + int b = 0; + const int MODADLER = 65521; + + int i = 0; + while (s[i] != '\0') + { + a = (a + s[i]) % MODADLER; + b = (b + a) % MODADLER; + i++; + } + return (b << 16) | a; +} \ No newline at end of file diff --git a/Hash/hash.h b/Hash/hash.h new file mode 100644 index 00000000..7497b1b2 --- /dev/null +++ b/Hash/hash.h @@ -0,0 +1,43 @@ +/* + author: Christian Bender + This file contains the public interface + + Overview about hash-functions: + + - sdbm + - djb2 + - xor8 (8 bit) + - adler_32 (32 bits) +*/ + +#ifndef __HASH__H +#define __HASH__H + +/* + sdbm: implements the sdbm hash-algorithm + returns a whole number of type long long. +*/ +long long sdbm(char[]); + +/* + djb2: implements the djb2 hash-algorithm + returns a whole number of type long long. +*/ +long long djb2(char[]); + +/* + xor8: implements the xor8 hash-algorithm + returns a whole number of type char. + length: 8 bit +*/ +char xor8(char[]); + +/* + adler_32: implements the adler-32 hash-algorithm + returns a whole number of type int. + length: 32 bit + assumes: int has a length of 32 bits. +*/ +int adler_32(char[]); + +#endif \ No newline at end of file diff --git a/Hash/test_program.c b/Hash/test_program.c new file mode 100644 index 00000000..f9dcd299 --- /dev/null +++ b/Hash/test_program.c @@ -0,0 +1,20 @@ +/* + author: Christian Bender + This file contains a simple test program for each hash-function. +*/ + +#include +#include "hash.h" + +int main(void) +{ + char s[] = "name"; + + /* actual tests */ + printf("sdbm: %s --> %lld\n", s, sdbm(s)); + printf("djb2: %s --> %lld\n", s, djb2(s)); + printf("xor8: %s --> %i\n", s, xor8(s)); /* 8 bit */ + printf("adler_32: %s --> %i\n", s, adler_32(s)); /* 32 bit */ + + return 0; +} \ No newline at end of file