mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-22 05:21:49 +03:00
Add files via upload
This commit is contained in:
parent
d43e80fa44
commit
c524b9a0ca
7
Hash/README.md
Normal file
7
Hash/README.md
Normal file
@ -0,0 +1,7 @@
|
||||
# Hash algorithms
|
||||
|
||||
Overview files **hash.h** and **hash.c**
|
||||
* sdbm
|
||||
* djb2
|
||||
* xor8 (8 bit)
|
||||
* adler_32 (32 bit)
|
63
Hash/hash.c
Normal file
63
Hash/hash.c
Normal file
@ -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;
|
||||
}
|
43
Hash/hash.h
Normal file
43
Hash/hash.h
Normal file
@ -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
|
20
Hash/test_program.c
Normal file
20
Hash/test_program.c
Normal file
@ -0,0 +1,20 @@
|
||||
/*
|
||||
author: Christian Bender
|
||||
This file contains a simple test program for each hash-function.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user