forked from sysprog21/phonebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phonebook_hash.c
153 lines (131 loc) · 3.34 KB
/
phonebook_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include "phonebook_hash.h"
/* FILL YOUR OWN IMPLEMENTATION HERE! */
entry *findName(char lastName[], entry *pHead[])
{
unsigned int n = BKDRHash(lastName);
n %= SIZE;
entry *tmp;
if (pHead[n]->pNext)
tmp = pHead[n]->pNext;
else
return NULL;
while (tmp) {
if (strcasecmp(lastName, tmp->lastName) == 0)
return tmp;
tmp = tmp->pNext;
}
return NULL;
}
entry *append(char lastName[], entry *e)
{
/* allocate memory for the new entry and put lastName */
e->pNext = (entry *) malloc(sizeof(entry));
e = e->pNext;
strcpy(e->lastName, lastName);
e->pNext = NULL;
return e;
}
unsigned int BKDRHash(char lastName[])
{
unsigned int seed = 131;
unsigned int hash = 0;
int i = 0;
while (i < strlen(lastName)) {
hash = hash * seed + lastName[i];
i++;
}
return (hash & 0x7FFFFFFF) % SIZE;
}
unsigned int ELFHash(char lastName[])
{
unsigned int hash = 0;
unsigned int x = 0;
int i = 0;
while (i < strlen(lastName)) {
hash = (hash << 4) + lastName[i];
if ((x = hash & 0xF0000000L) != 0) {
hash ^= (x >> 24);
hash &= ~x;
}
i++;
}
return (hash & 0x7FFFFFFF) % SIZE;
}
unsigned int PJWHash(char lastName[])
{
unsigned int BitsInUnignedInt = (unsigned int)(sizeof(unsigned int) * 8);
unsigned int ThreeQuarters = (unsigned int)((BitsInUnignedInt * 3) / 4);
unsigned int OneEighth = (unsigned int)(BitsInUnignedInt / 8);
unsigned int HighBits = (unsigned int)(0xFFFFFFFF) << (BitsInUnignedInt - OneEighth);
unsigned int hash = 0;
unsigned int test = 0;
int i = 0;
while (i < strlen(lastName)) {
hash = (hash << OneEighth) + lastName[i];
if ((test = hash & HighBits) != 0) {
hash = ((hash ^ (test >> ThreeQuarters)) & (~HighBits));
}
i++;
}
return (hash & 0x7FFFFFFF) % SIZE;
}
unsigned int APHash(char lastName[])
{
unsigned int hash = 0;
int i = 0;
while (i < strlen(lastName)) {
if ((i & 1) == 0) {
hash ^= ((hash << 7) ^ lastName[i] ^ (hash >> 3));
} else {
hash ^= (~((hash << 11) ^ lastName[i] ^ (hash >> 5)));
}
i++;
}
return (hash & 0x7FFFFFFF) % SIZE;
}
unsigned int SDBMHash(char lastName[])
{
unsigned int hash = 0;
int i = 0;
while (i < strlen(lastName)) {
// equivalent to: hash = 65599*hash + (*str++);
hash = lastName[i] + (hash << 6) + (hash << 16) - hash;
i++;
}
return (hash & 0x7FFFFFFF) % SIZE;
}
unsigned int RSHash(char lastName[])
{
unsigned int b = 378551;
unsigned int a = 63689;
unsigned int hash = 0;
int i = 0;
while (i < strlen(lastName)) {
hash = hash * a + lastName[i];
a *= b;
i++;
}
return (hash & 0x7FFFFFFF) % SIZE;
}
unsigned int JSHash(char lastName[])
{
unsigned int hash = 1315423911;
int i = 0;
while (i < strlen(lastName)) {
hash ^= ((hash << 5) + lastName[i] + (hash >> 2));
i++;
}
return (hash & 0x7FFFFFFF) % SIZE;
}
void free_list(entry *e)
{
entry *tmp;
while ((tmp = e) != NULL) {
e = e->pNext;
free(tmp);
}
}