-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashtable.c
More file actions
146 lines (129 loc) · 3.39 KB
/
hashtable.c
File metadata and controls
146 lines (129 loc) · 3.39 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
/* This is free and unencumbered software released into the public domain.
* Refer to LICENSE.txt in this directory. */
/* Randomized stress test of an open addressing hash table. */
#include <assert.h>
#include <fcntl.h>
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
/* Fixed-size closed hash table representing a set of integers. */
typedef struct table table_t;
/* Return empty table with n slots, or NULL if allocation fails. */
table_t *make_table(size_t n, size_t skip);
/* Return true iff element is in the table. */
bool table_contains(const table_t *table, int element);
/* Try to add element to table; return false if table is full. */
bool table_add(table_t *table, int element);
/* Remove element from table if it is present. */
void table_remove(table_t *table, int element);
/* Unused slot in table. */
#define UNUSED INT_MIN
struct table
{
size_t n; /* Number of slots in table. */
size_t skip; /* Slots to skip when traversing hash chain. */
int slot[0]; /* Array of slots containing elements or UNUSED. */
};
table_t *
make_table(size_t n, size_t skip)
{
table_t *table = malloc(sizeof *table + n * sizeof table->slot[0]);
if (table)
{
table->n = n;
table->skip = skip;
for (size_t i = 0; i < n; ++i)
{
table->slot[i] = UNUSED;
}
}
return table;
}
/* Find index of slot containing element, or the index of the empty
* slot where it should be added. Return true if successful, false if
* table is full. */
static bool
_table_find(size_t *index, const table_t *table, int element)
{
size_t i = (size_t)element % table->n;
size_t j = i;
assert(element != UNUSED);
while (table->slot[j] != element && table->slot[j] != UNUSED)
{
j = (j + table->skip) % table->n; /* Next slot in hash chain. */
if (j == i)
{
return false;
}
}
*index = j;
return true;
}
bool
table_contains(const table_t *table, int element)
{
size_t i;
return _table_find(&i, table, element) && table->slot[i] == element;
}
bool
table_add(table_t *table, int element)
{
size_t i;
if (_table_find(&i, table, element))
{
table->slot[i] = element;
return true;
}
return false;
}
void
table_remove(table_t *table, int element)
{
size_t i;
if (_table_find(&i, table, element))
{
table->slot[i] = UNUSED;
}
}
/* Randomized stress test. */
int
main(int argc, char **argv)
{
unsigned seed;
if (argc == 2)
{
seed = strtoul(argv[1], NULL, 10);
}
else
{
int fd = open("/dev/urandom", O_RDONLY);
assert(fd >= 0);
read(fd, &seed, sizeof seed);
close(fd);
}
srand(seed);
table_t *table = make_table(100, 17);
assert(table);
bool indicator[10000] = { false }; /* Expected contents of the table. */
for (unsigned i = 0; i < sizeof indicator; ++i)
{
int element = rand() % sizeof indicator;
if (indicator[element])
{
assert(table_contains(table, element));
table_remove(table, element);
indicator[element] = false;
}
else
{
assert(!table_contains(table, element));
if (table_add(table, element))
{
indicator[element] = true;
}
}
}
free(table);
return EXIT_SUCCESS;
}