-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.c
64 lines (56 loc) · 1.46 KB
/
hash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Author: Dafu Ai 766586
*/
#include "hash.h"
#include "hashtable.h"
#include <stdlib.h>
#include <string.h>
#define MAXSTRLEN 256
/* Used as the second hashing function on double hash */
unsigned int linear_probe(void *e, unsigned int size) {
/* In order to hash e into closet following empty cell */
return 1;
}
/* Very simple hash */
unsigned int worst_hash(void *e, unsigned int size) {
(void) e;
(void) size;
return 0;
}
/* Basic numeric hash function */
unsigned int num_hash(long n, unsigned int size) {
return n % size;
}
/* Bad hash function */
unsigned int bad_hash(char *key, unsigned int size) {
static unsigned int a;
static int flag = 1;
if (flag == 1){
/* To be consistent only assgn to a once */
/* Assume strand() has been called */
a = rand()%size;
flag ++;
}
return (a*key[0])%size;
}
/* Universal hash function as described in Dasgupta et al 1.5.2 */
unsigned int universal_hash(unsigned char *key, unsigned int size) {
int n, i, sum = 0;
static int r[MAXSTRLEN];
static int flag = 1;
if (flag == 1){
/* To be consistent only assgn to r once */
for (i=0; i<MAXSTRLEN; i++) {
/* Assume strand() has been called */
r[i] = rand()%size;
}
flag ++;
}
/* Determine n of the summation*/
/* Assume maximum string length MAXSTRLEN*/
n = strlen((char*)key);
for (i=0; i<n; i++){
sum = sum + r[i]*key[i];
}
return sum%size;
}