Mercurial > ~dholland > hg > ag > index.cgi
comparison help2html/uintarray.c @ 0:13d2b8934445
Import AnaGram (near-)release tree into Mercurial.
author | David A. Holland |
---|---|
date | Sat, 22 Dec 2007 17:52:45 -0500 |
parents | |
children | 60b08b68c750 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:13d2b8934445 |
---|---|
1 #include <stdlib.h> | |
2 #include "must.h" | |
3 #include "uintarray.h" | |
4 | |
5 //////////////////////////////////////////////////////////// | |
6 | |
7 void uintarray_init(struct uintarray *a) { | |
8 a->v = NULL; | |
9 a->num = a->max = 0; | |
10 } | |
11 | |
12 struct uintarray *uintarray_create(void) { | |
13 struct uintarray *a; | |
14 a = must_malloc(sizeof(*a)); | |
15 uintarray_init(a); | |
16 return a; | |
17 } | |
18 | |
19 void uintarray_cleanup(struct uintarray *a) { | |
20 free(a->v); | |
21 a->v = NULL; | |
22 a->num = a->max = 0; | |
23 } | |
24 | |
25 void uintarray_destroy(struct uintarray *a) { | |
26 uintarray_cleanup(a); | |
27 free(a); | |
28 } | |
29 | |
30 //////////////////////////////////////////////////////////// | |
31 | |
32 /* | |
33 * I wish there were a way to check that these were the same as the | |
34 * inline versions, but apparently allowing such things to diverge is | |
35 * a feature of C99. Bleh. | |
36 */ | |
37 | |
38 unsigned uintarray_num(const struct uintarray *a) { | |
39 return a->num; | |
40 } | |
41 | |
42 unsigned uintarray_get(const struct uintarray *a, unsigned ix) { | |
43 assert(ix < a->num); | |
44 return a->v[ix]; | |
45 } | |
46 | |
47 void uintarray_set(struct uintarray *a, unsigned ix, unsigned val) { | |
48 assert(ix < a->num); | |
49 a->v[ix] = val; | |
50 } | |
51 | |
52 //////////////////////////////////////////////////////////// | |
53 | |
54 void uintarray_add(struct uintarray *a, unsigned val) { | |
55 unsigned ix; | |
56 | |
57 ix = a->num; | |
58 uintarray_setsize(a, a->num+1); | |
59 a->v[ix] = val; | |
60 } | |
61 | |
62 void uintarray_setsize(struct uintarray *a, unsigned newnum) { | |
63 unsigned newmax; | |
64 unsigned *newptr; | |
65 | |
66 if (newnum > a->max) { | |
67 newmax = a->max; | |
68 if (newmax == 0) { | |
69 newmax = 4; | |
70 } | |
71 while (newmax < newnum) { | |
72 newmax *= 2; | |
73 } | |
74 newptr = must_realloc(a->v, newmax*sizeof(unsigned)); | |
75 a->v = newptr; | |
76 a->max = newmax; | |
77 } | |
78 | |
79 a->num = newnum; | |
80 | |
81 /* Note: it's the user's responsibility to initialize the new space */ | |
82 } | |
83 | |
84 static int (*uintarray_usersort)(unsigned a, unsigned b); | |
85 | |
86 static int uintarray_sortthunk(const void *av, const void *bv) { | |
87 /* av and bv point to elements of the uintarray */ | |
88 unsigned a = *(const unsigned *)av; | |
89 unsigned b = *(const unsigned *)bv; | |
90 return uintarray_usersort(a, b); | |
91 } | |
92 | |
93 void uintarray_sort(struct uintarray *a, int (*f)(unsigned a, unsigned b)) { | |
94 uintarray_usersort = f; | |
95 qsort(a->v, a->num, sizeof(unsigned), uintarray_sortthunk); | |
96 } |