-
Notifications
You must be signed in to change notification settings - Fork 1
/
sparr.c
253 lines (197 loc) · 4.86 KB
/
sparr.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// sparr.c -- Slow but memory-efficient sparse array.
/*
This is exactly the same idea as what you'll find in Google's sparsehash
package, but without the STL and C++ diarrhea.
http://code.google.com/p/google-sparsehash/
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "sparr.h"
#include "util.h"
/* Bitmask stuff */
static uint8_t
charbit(uint8_t i)
{
return i >> 3;
}
static uint8_t
modbit(uint8_t i)
{
return 1 << (i & 7);
}
static int
bmtest(uint8_t *bm, uint16_t i)
{
return bm[charbit(i)] & modbit(i);
}
static int
bmset(uint8_t *bm, uint16_t i)
{
return bm[charbit(i)] |= modbit(i);
}
static int
bmclear(uint8_t *bm, uint16_t i)
{
return bm[charbit(i)] &= ~modbit(i);
}
/* count the bits up to, but not including, index i */
static uint16_t
bmcount(uint8_t *bm, uint16_t i)
{
uint16_t c, res = 0;
static const char cbits[256] = {
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
};
for (c = i / 8; c; c--) res += cbits[*bm++];
return res + cbits[*bm & ((1 << (i % 8)) - 1)];
}
/* Sparse Group */
spgroup
make_spgroup()
{
spgroup g;
g = malloc(sizeof(struct spgroup));
if (!g) return warn("malloc"), (spgroup) 0;
memset(g, 0, sizeof(*g));
return g;
}
int
spgroup_test(spgroup g, uint16_t i)
{
return bmtest(g->mask, i);
}
dirent
spgroup_get(spgroup g, uint16_t i)
{
if (!bmtest(g->mask, i)) return 0;
return g->slots[bmcount(g->mask, i)];
}
int
spgroup_rm(spgroup g, uint16_t i)
{
dirent *nslots;
uint16_t slot;
if (!bmtest(g->mask, i)) return 0;
nslots = malloc(sizeof(dirent) * (g->fill - 1));
if (!nslots) return warn("malloc"), -1;
slot = bmcount(g->mask, i);
memcpy(nslots, g->slots, sizeof(dirent) * slot);
memcpy(nslots + slot, g->slots + slot + 1,
sizeof(dirent) * (g->fill - slot - 1));
free(g->slots);
g->slots = nslots;
g->fill--;
bmclear(g->mask, i);
return 0;
}
int
spgroup_set(spgroup g, uint16_t i, dirent v)
{
uint16_t slot;
if (!v) return spgroup_rm(g, i);
slot = bmcount(g->mask, i);
if (!bmtest(g->mask, i)) {
dirent *nslots;
nslots = malloc(sizeof(dirent) * (g->fill + 1));
if (!nslots) return warn("malloc"), -1;
memcpy(nslots, g->slots, sizeof(dirent) * slot);
memcpy(nslots + slot + 1, g->slots + slot,
sizeof(dirent) * (g->fill - slot));
free(g->slots);
g->slots = nslots;
g->fill++;
bmset(g->mask, i);
}
g->slots[slot] = v;
return 0;
}
void
spgroup_free(spgroup g)
{
if (!g) return;
free(g->slots);
free(g);
}
/* Sparse Array */
sparr
make_sparr(int cap)
{
sparr a;
int ngroups = CEILDIV(cap, GROUP_SIZE);
a = malloc(sizeof(struct sparr) + ngroups * sizeof(spgroup));
if (!a) return warn("malloc"), (sparr) 0;
a->cap = cap;
a->fill = 0;
memset(a->groups, 0, ngroups * sizeof(spgroup));
return a;
}
static size_t
group_num(size_t i)
{
return i / GROUP_SIZE;
}
static uint16_t
pos_in_group(size_t i)
{
return i % GROUP_SIZE;
}
int
sparr_test(sparr a, size_t i)
{
size_t gnum = group_num(i);
if (!a->groups[gnum]) return 0;
return spgroup_test(a->groups[gnum], pos_in_group(i));
}
dirent
sparr_get(sparr a, size_t i)
{
size_t gnum = group_num(i);
if (!a->groups[gnum]) return 0;
return spgroup_get(a->groups[gnum], pos_in_group(i));
}
int
sparr_set(sparr a, size_t i, dirent v)
{
int r;
uint16_t old_g_fill;
spgroup g;
size_t gnum = group_num(i);
if (!a->groups[gnum]) a->groups[gnum] = make_spgroup();
if (!a->groups[gnum]) return warnx("make_spgroup"), -1;
g = a->groups[gnum];
old_g_fill = g->fill;
r = spgroup_set(g, pos_in_group(i), v);
a->fill += g->fill - old_g_fill;
return r;
}
int
sparr_rm(sparr a, size_t i)
{
return sparr_set(a, i, 0);
}
void
sparr_free(sparr a)
{
int i, ngroups = CEILDIV(a->cap, GROUP_SIZE);
for (i = 0; i < ngroups; i++) {
spgroup_free(a->groups[i]);
}
free(a);
}