-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.c
165 lines (121 loc) · 3.27 KB
/
set.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
#include "set.h"
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#define GROW_RATIO 4 // for every item in hash, there are 4 buckets
#define START_SIZE 13
#define unlikely(x) (__builtin_expect(!!(x), 0))
static inline int hashfn(void *key){
return (int)key / 16;
}
static short grow(set *s){
// grows a set by twice size
// returns 1 on failure
s->memsize *= 2;
free(s->buckets);
s->buckets = malloc(s->memsize * sizeof(_set_bucket));
if unlikely(!s->buckets){
return 1;
}
for (int i=0; i < s->memsize; i++){
s->buckets[i].key = NULL;
}
s->ptrs = realloc(s->ptrs, sizeof(void*) * (s->nitems + 1) * 2); // add 1 to be safe
if unlikely(!s->ptrs){
return 1;
}
for (int i=0; i < s->nitems; i++){
set_add(s, s->ptrs[i]);
}
return 0;
}
short set_add(set *s, void *ptr){
if ((s->nitems + 1) > (s->memsize / GROW_RATIO)){
// then grow the set
short failed = grow(s);
if unlikely(failed){
return 1;
}
};
s->ptrs[s->nitems] = ptr;
s->nitems++;
int i = hashfn(ptr) % s->memsize;
int ctr = 0;
while (s->buckets[i].key){ // quadratic bucket probe
i = (i + (ctr * ctr)) % s->memsize;
ctr += 1;
}
s->buckets[i].index = s->nitems;
s->buckets[i].key = ptr;
return 0;
}
set *set_new(void){
// initial size of 13 (nice and prime-y)
set *ret = malloc(sizeof ret);
if (unlikely(!ret)){
return NULL; // failed
}
ret->memsize = START_SIZE * GROW_RATIO;
ret->buckets = malloc(ret->memsize * sizeof(void*));
ret->ptrs = malloc(START_SIZE * sizeof(_set_bucket));
ret->nitems = 0;
for (int i=0; i < ret->memsize; i++){
ret->buckets[i].key = NULL; // initialize all ptrs to NULL
}
if unlikely((!ret->buckets) || (!ret->ptrs)){
free(ret->buckets);
free(ret->ptrs);
free(ret);
return NULL; // also failed
}
return ret;
}
short set_has(set *s, void *ptr){
int i = hashfn(ptr) % s->memsize;
int ctr = 0;
while (s->buckets[i].key != ptr){ // quadratic bucket probe
if (!s->buckets[i].key){
// if key ptr is null, return 0; unfound.
return 0;
}
i = (i + (ctr * ctr)) % s->memsize;
ctr += 1;
}
if(!s->buckets[i].key){
return 0;
}else{
return 1;
}
}
void set_remove(set *s, void *ptr){
int i = hashfn(ptr) % s->memsize;
int ctr = 0;
while (s->buckets[i].key != ptr){
if (!s->buckets[i].key){
// ptr is not in the set
// because data[i].key is NULL
return;
}
i = ((i + (ctr * ctr)) % s->memsize);
ctr += 1;
}
int index = s->buckets[i].index;
_set_bucket *end = s->ptrs[s->nitems-1];
s->ptrs[index] = end; // put last item in slot of other
end->index = index;
s->nitems--;
s->buckets[i].key = NULL;
}
void set_free(set *s){
free(s->buckets);
free(s->ptrs);
free(s);
}
void set_clear(set *s){
free(s->buckets);
free(s->ptrs);
s->memsize = START_SIZE * GROW_RATIO;
s->buckets = malloc(s->memsize * sizeof(void *));
s->ptrs = malloc(START_SIZE * sizeof(_set_bucket));
s->nitems = 0;
}