-
Notifications
You must be signed in to change notification settings - Fork 154
/
arrayu64.c
327 lines (284 loc) · 8.39 KB
/
arrayu64.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* arrayu64.c - expanding array of 64 bit unsigned numbers
*
* Copyright (c) 1994-2011 Carnegie Mellon University. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The name "Carnegie Mellon University" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For permission or any legal
* details, please contact
* Carnegie Mellon University
* Center for Technology Transfer and Enterprise Creation
* 4615 Forbes Avenue
* Suite 302
* Pittsburgh, PA 15213
* (412) 268-7393, fax: (412) 268-7395
* innovation@andrew.cmu.edu
*
* 4. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by Computing Services
* at Carnegie Mellon University (http://www.cmu.edu/computing/)."
*
* CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Author: Bron Gondwana
* Start Date: 2013/02/12
*/
#include <config.h>
#include <string.h>
#include "arrayu64.h"
#include "util.h"
#include "xmalloc.h"
EXPORTED arrayu64_t *arrayu64_new(void)
{
return xzmalloc(sizeof(arrayu64_t));
}
EXPORTED void arrayu64_fini(arrayu64_t *au)
{
if (!au)
return;
free(au->data);
au->data = NULL;
au->count = 0;
au->alloc = 0;
}
EXPORTED void arrayu64_free(arrayu64_t *au)
{
if (!au)
return;
arrayu64_fini(au);
free(au);
}
#define QUANTUM 16
static inline size_t grow(size_t have, size_t want)
{
size_t x = MAX(QUANTUM, have);
while (x < want)
x *= 2;
return x;
}
/* XXX n.b. unlike some other ensure_allocs, this one doesn't always
* XXX leave an extra NULL at the end.
*/
static void ensure_alloc(arrayu64_t *au, size_t newalloc)
{
if (newalloc <= au->alloc)
return;
newalloc = grow(au->alloc, newalloc);
au->data = xzrealloc(au->data,
sizeof(uint64_t) * au->alloc,
sizeof(uint64_t) * newalloc);
au->alloc = newalloc;
}
/*
* Normalise the index passed by a caller, to a value in the range
* 0..count-1, or < 0 for invalid, assuming the function we're
* performing does not have the side effect of expanding the array.
* Note that doesn't necessarily mean the array is read-only, e.g.
* arrayu64_remove() modifies the array but does not expand the array if
* given an index outside the array's current bounds. In Perl style,
* negative indexes whose absolute value is less than the length of the
* array are treated as counting back from the end, e.g. idx=-1 means
* the final element.
*/
static inline int adjust_index_ro(const arrayu64_t *au, int idx)
{
if (idx >= 0 && (unsigned) idx >= au->count)
return -1;
else if (idx < 0)
idx += au->count;
return idx;
}
/*
* Like adjust_index_ro(), with extra complication that the function
* we're performing will expand the array if either the adjusted index
* points outside the current bounds of the array, or @grow tells us
* that we're about to need more space in the array.
*/
static inline int adjust_index_rw(arrayu64_t *au, int idx, int grow)
{
if (idx >= 0 && (unsigned) idx >= au->count) {
/* expanding the array as a side effect @idx pointing
* outside the current bounds, plus perhaps @grow */
ensure_alloc(au, idx+grow);
} else if (idx < 0) {
/* adjust Perl-style negative indices */
idx += au->count;
if (idx >= 0 && grow)
ensure_alloc(au, au->count+grow);
} else if (grow) {
/* expanding the array due to an insert or append */
ensure_alloc(au, au->count+grow);
}
return idx;
}
EXPORTED arrayu64_t *arrayu64_dup(const arrayu64_t *au)
{
arrayu64_t *new = arrayu64_new();
size_t i;
arrayu64_truncate(new, au->count);
for (i = 0 ; i < au->count ; i++)
new->data[i] = au->data[i];
return new;
}
EXPORTED int arrayu64_append(arrayu64_t *au, uint64_t val)
{
int pos = au->count++;
ensure_alloc(au, au->count);
au->data[pos] = val;
return pos;
}
EXPORTED int arrayu64_add(arrayu64_t *au, uint64_t val)
{
int pos = arrayu64_find(au, val, 0);
if (pos < 0) pos = arrayu64_append(au, val);
return pos;
}
EXPORTED void arrayu64_set(arrayu64_t *au, int idx, uint64_t val)
{
if ((idx = adjust_index_rw(au, idx, 0)) < 0)
return;
au->data[idx] = val;
/* adjust the count if we just sparsely expanded the array */
if ((unsigned) idx >= au->count)
au->count = idx+1;
}
EXPORTED void arrayu64_insert(arrayu64_t *au, int idx, uint64_t val)
{
if ((idx = adjust_index_rw(au, idx, 1)) < 0)
return;
if ((unsigned) idx < au->count)
memmove(au->data+idx+1, au->data+idx,
sizeof(uint64_t) * (au->count-idx));
au->data[idx] = val;
au->count++;
}
EXPORTED uint64_t arrayu64_remove(arrayu64_t *au, int idx)
{
uint64_t val;
if ((idx = adjust_index_ro(au, idx)) < 0)
return 0;
val = au->data[idx];
au->count--;
if ((unsigned) idx < au->count)
memmove(au->data+idx, au->data+idx+1,
sizeof(uint64_t) * (au->count-idx));
au->data[au->count] = 0;
return val;
}
EXPORTED int arrayu64_remove_all(arrayu64_t *au, uint64_t val)
{
int i = 0;
int count = 0;
for (;;) {
i = arrayu64_find(au, val, i);
if (i < 0)
break;
count++;
arrayu64_remove(au, i);
}
return count;
}
EXPORTED void arrayu64_truncate(arrayu64_t *au, size_t newlen)
{
if (newlen == au->count)
return;
if (newlen > au->count) {
ensure_alloc(au, newlen);
}
else {
memset(au->data+newlen, 0, sizeof(uint64_t) * (au->count - newlen));
}
au->count = newlen;
}
/* note: values outside the range are all zero */
EXPORTED uint64_t arrayu64_nth(const arrayu64_t *au, int idx)
{
if ((idx = adjust_index_ro(au, idx)) < 0)
return 0;
return au->data[idx];
}
EXPORTED uint64_t arrayu64_max(const arrayu64_t *au)
{
uint64_t max = 0;
size_t i;
for (i = 0; i < au->count; i++) {
if (au->data[i] > max)
max = au->data[i];
}
return max;
}
static int _numeric_sort(const void *a, const void *b)
{
uint64_t av = *((uint64_t *)a);
uint64_t bv = *((uint64_t *)b);
if (av == bv)
return 0;
if (av < bv)
return -1;
return 1;
}
EXPORTED void arrayu64_sort(arrayu64_t *au, arrayu64_cmp_fn_t *cmp)
{
if (!cmp) cmp = _numeric_sort;
qsort(au->data, au->count, sizeof(uint64_t), cmp);
}
EXPORTED void arrayu64_uniq(arrayu64_t *au)
{
size_t i;
for (i = 1; i < au->count; i++) {
if (au->data[i-1] == au->data[i])
arrayu64_remove(au, i--);
}
}
EXPORTED off_t arrayu64_find(const arrayu64_t *au, uint64_t val, off_t idx)
{
size_t i;
if ((idx = adjust_index_ro(au, idx)) < 0)
return -1;
for (i = idx; i < au->count; i++) {
if (au->data[i] == val)
return i;
}
return -1;
}
// needs a sorted array
EXPORTED off_t arrayu64_bsearch(const arrayu64_t *au, uint64_t val)
{
if (!au->count) return -1;
size_t low = 0;
size_t high = au->count - 1;
while (low <= high) {
off_t mid = (high - low)/2 + low;
uint64_t this = arrayu64_nth(au, mid);
if (this == val)
return mid;
if (this > val)
high = mid - 1;
else
low = mid + 1;
}
return -1;
}
EXPORTED size_t arrayu64_size(const arrayu64_t *au)
{
return au->count;
}