-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathohashtbl.c
212 lines (174 loc) · 4.83 KB
/
ohashtbl.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
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
208
209
210
211
212
/*
* _____
* ANSI / ___/
* / /__
* \___/
*
* Filename: ohashtbl.c
* Author : Kyle Loudon/Dan Levin
* Date : Mon Apr 08 12:15:39 2013
* Version : 0.51
* ---
* Description: An open-addressed hashtable implemented as a pure, generic ADT - written in ANSI C
*
* Date Revision message
* 150331 This code ready for version 0.51
*
*/
/**
* @file ohashtbl.c
**/
#include <stdio.h>
#include <stdlib.h>
#include "ohashtbl.h"
struct OHtbl_
{
int positions;
void *vacated;
int (*h1)(const void *key);
int (*h2)(const void *key);
int (*match)(const void *key1, const void *key2);
void (*destroy)(void *data);
int size;
void **table;
};
/* Reserve a sentinel memory address for vacated elements */
static char vacated;
/* FUNCTION DEFINITIONS */
OHtbl OHTBLinit(int positions, int (*h1)(const void *key), int (*h2)(const void *key),
int (*match)(const void *key1, const void *key2), void (*destroy)(void *data))
{
OHtbl htbl;
int i;
/* Allocate space for the open-addressed hash table header */
if ((htbl = malloc(sizeof(struct OHtbl_))) == NULL)
return NULL;
/* Allocate space for the hash table */
if ((htbl->table = (void **)malloc(positions * sizeof(void *))) == NULL)
return NULL;
/* Initialize each position */
htbl->positions = positions;
for (i = 0; i < htbl->positions; i++)
htbl->table[i] = NULL;
/* Set the vacated member to the sentinel memory address reserved for this */
htbl->vacated = &vacated;
/* Encapsulate the functions */
htbl->h1 = h1;
htbl->h2 = h2;
htbl->match = match;
htbl->destroy = destroy;
/* Initialize the number of elements in the table */
htbl->size = 0;
return htbl;
}
void OHTBLdestroy(OHtbl htbl)
{
int i;
if (htbl->destroy != NULL)
{
/* Call a user-defined function to free dynamically allocated data */
for (i = 0; i < htbl->positions; i++)
{
if (htbl->table[i] != NULL && htbl->table[i] != htbl->vacated)
htbl->destroy(htbl->table[i]);
}
}
/* Free the storage allocated for the hash table */
free(htbl->table);
/* Free the storage allocated for the table header */
free(htbl);
}
int OHTBLinsert(OHtbl htbl, const void *data)
{
void *temp;
int position, i;
/* Do not exceed the number of positions in the table */
if (htbl->size == htbl->positions)
return -1;
/* Do nothing if the data is already in the table */
temp = (void *)data;
if (OHTBLlookup(htbl, &temp) == 0) /* Duplicate found! */
return 1;
/* Use double hashing to hash the key */
for (i = 0; i < htbl->positions; i++)
{
position = (htbl->h1(data) + (i * htbl->h2(data))) % htbl->positions;
if (htbl->table[position] == NULL || htbl->table[position] == htbl->vacated)
{
/* Insert the data into the table */
htbl->table[position] = (void *)data;
htbl->size++;
return 0;
}
}
/* Return that the hash functions were selected incorrectly */
return -1;
}
int OHTBLremove(OHtbl htbl, void **data)
{
int position, i;
/* Use double hashing to hash the key */
for (i = 0; i < htbl->positions; i++)
{
position = (htbl->h1(*data) + (i * htbl->h2(*data))) % htbl->positions;
if (htbl->table[position] == NULL)
{
/* Return that the data was not found */
return -1;
}
else if (htbl->table[position] == htbl->vacated)
{
/* Search beyond vacated positions */
continue;
}
else if (htbl->match(htbl->table[position], *data))
{
/* Pass back the data from the table */
*data = htbl->table[position];
htbl->table[position] = htbl->vacated;
htbl->size--;
return 0;
}
}
/* Return that the data was not found */
return -1;
}
int OHTBLlookup(const OHtbl htbl, void **data)
{
int position, i;
/* Use double hashing to hash the key */
for (i = 0; i < htbl->positions; i++)
{
position = (htbl->h1(*data) + (i * htbl->h2(*data))) % htbl->positions;
if (htbl->table[position] == NULL)
{
/* Return that the data was not found */
return -1;
}
else if (htbl->match(htbl->table[position], *data))
{
/* Pass back the data from the table */
*data = htbl->table[position];
return 0;
}
}
/* Return that the data was not found... */
return -1;
}
int OHTBLsize(OHtbl htbl)
{
return htbl->size;
}
void OHTBLprint(OHtbl htbl, void (*callback)(const void *data))
{
int i;
for (i=0; i<htbl->positions; i++)
{
if (htbl->table[i] == NULL)
printf("\nNULL");
else if (htbl->table[i] == htbl->vacated)
printf("\nVACATED");
else
callback(htbl->table[i]);
}
}