-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.c
More file actions
executable file
·207 lines (175 loc) · 6.47 KB
/
hash.c
File metadata and controls
executable file
·207 lines (175 loc) · 6.47 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// ECE466 - Hash Table for Symbol Tables for a parser for a C compiler
// Miraj Patel, Hash Table implementation taken from DSA2 (ECE 165)
#include "hash.h"
#define FALSE 0
#define TRUE 1
// lookup table for large primes to use as table size
static int primeTable[10] = {1009, 2017, 4049, 8101, 16217,
32441, 64891, 196613, 393241, 786433};
struct hashTable *new_hashTable(int size) {
struct hashTable *table;
if ( (table = malloc(sizeof(struct hashTable))) == NULL ) {
fprintf(stderr, "Error allocating memory for new hash table: '%s'\n", strerror(errno));
return NULL;
}
table->capacity = getPrime(size);
table->filled = 0;
if ( (table->data = malloc((table->capacity)*sizeof(struct hashItem))) == NULL ) {
fprintf(stderr, "Error allocating memory for %d hash items inside new hash table: '%s'\n", table->capacity, strerror(errno));
return NULL;
}
int i;
for (i = 0; i < table->capacity; i++) {
table->data[i].isOccupied = FALSE;
table->data[i].isDeleted = FALSE;
table->data[i].key = NULL;
table->data[i].pv = NULL;
}
return table;
}
// Insert the specified key into the hash table. If an optional pointer is provided, associate that pointer with the key.
// Returns 0 on success, 1 if key already exists in hash table, 2 if hash table is full.
int insert(struct hashTable *theTable, char *key, void *pv) {
// Make sure hash table is not full
if (theTable->filled == theTable->capacity)
return 2;
// Compute index for the key, and see if it's available, else find next available index
int index = hash(theTable, key);
while (theTable->data[index].isOccupied) {
if ( !strcmp(theTable->data[index].key, key) && !theTable->data[index].isDeleted )
return 1;
index = ((index+1) % (theTable->capacity));
}
theTable->data[index].key = key;
theTable->data[index].isOccupied = TRUE;
theTable->data[index].isDeleted = FALSE;
theTable->data[index].pv = pv;
theTable->filled++;
if (theTable->filled > (theTable->capacity*3/4) ) {
// Needs at half full, rehashing with larger table would be ideal
fprintf(stderr, "Hash table is more than 75%% full\n");
}
if (theTable->filled == theTable->capacity) {
fprintf(stderr, "Warning: Hash table is now full!\n");
//return 2;
}
return 0;
}
// Check if the specified key is in the hash table. If so, return TRUE; otherwise, return FALSE.
int contains(struct hashTable *theTable, char *key) {
int index = findPos(theTable, key);
if (index == -1)
return FALSE;
else
return TRUE;
}
// Get the pointer associated with the specified key.
// If the key does not exist in the hash table, return NULL.
// If an optional pointer to a int is provided,
// set the int to TRUE if the key is in the hash table,
// and set the int to FALSE otherwise.
void* getPointer(struct hashTable *theTable, char *key, int *b) {
int index = findPos(theTable, key);
if (index == -1) {
if (b)
*b = FALSE;
return NULL;
}
else {
if (b)
*b = TRUE;
return theTable->data[index].pv;
}
}
// Set the pointer associated with the specified key.
// Returns 0 on success,
// 1 if the key does not exist in the hash table.
int setPointer(struct hashTable *theTable, char *key, void *pv) {
int index = findPos(theTable, key);
if (index == -1)
return 1;
else {
theTable->data[index].pv = pv;
return 0;
}
}
// Delete the item with the specified key. Returns TRUE on success, FALSE if the specified key is not in the hash table.
int removeItem(struct hashTable *theTable, char *key) {
int index = findPos(theTable, key);
if (index == -1)
return FALSE;
else {
theTable->data[index].isDeleted = TRUE;
theTable->data[index].pv = NULL;
return TRUE;
}
}
void printTable(struct hashTable *theTable) {
int i;
for (i = 0; i < theTable->capacity; i++) {
fprintf(stderr, "%dth key = %s\n", i, theTable->data[i].key);
}
}
// Private Member Functions
// The actual hash function
int hash(struct hashTable *theTable, char *key) {
// 5381 is good starting constant according to multiple sources
unsigned long hash = 5381;
unsigned int i,c;
for (i = 0; i < strlen(key); i++) {
c = (int) key[i];
hash = ((hash << 5) + hash) + c; // Basically: hash*33 + c
}
return (hash % (theTable->capacity));
}
// Search for an item with the specified key. Return the position if found, -1 otherwise.
int findPos(struct hashTable *theTable, char *key) {
int index = hash(theTable, key);
while(theTable->data[index].isOccupied) {
// If key found:
if ( !strcmp(theTable->data[index].key, key) && !theTable->data[index].isDeleted )
return index;
else {
index++;
index = (index % (theTable->capacity));
}
}
// Key not found
return -1;
}
/*
// Makes the hash table bigger. Returns TRUE on success, FALSE if memory allocation fails.
(C++ code from DSA Assignment)
int hashTable::rehash() {
//cout << "Rehashing..." << endl;
int prevSize = capacity;
capacity = getPrime(prevSize);
std::vector<hashItem> oldTable(0);
oldTable.swap(data);
data.resize(capacity);
filled = 0;
if (data.size() == capacity) {
capacity = capacity;
for (int i = 0; i < prevSize; i++) {
if (oldTable[i].isOccupied)
insert(oldTable[i].key, oldTable[i].pv);
}
oldTable.clear();
// shrink to fit is supposed reduce its capacity to its current size (0), but compile error of function not found
// oldTable.shrink_to_fit();
// swap with its temporary self - once out of score, oldTable's memory allocation reduces to that of the temp
oldTable.swap(oldTable);
return TRUE;
}
else {
return FALSE;
}
}
*/
// Return a prime number at least as large as size. Uses a precomputed sequence of selected prime numbers.
unsigned int getPrime(int size) {
int i = 0;
while (size >= primeTable[i])
i++;
return primeTable[i];
}