]> diplodocus.org Git - nmh/blob - sbr/vector.c
e37606519826edff0a40f556d2c36935e74748bd
[nmh] / sbr / vector.c
1 /* vector.c -- dynamically sized vectors
2 *
3 * This code is Copyright (c) 2013, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 /*
9 * This file defines several kinds of vectors:
10 * bvector: bit vector
11 * svector: vector of char arrays
12 * ivector: vector of ints
13 *
14 * The interfaces provide only the capabilities needed by nmh. The
15 * implementations rely on dynamic allocation, so that a vector
16 * can be as large as needed, as long as it fits in (virtual) memory.
17 */
18
19 #include <h/mh.h>
20 #include <h/utils.h>
21
22 /* The default size of a struct bvector's bits, measured in bits.
23 * The struct's tiny member is used for storage. */
24 #define BVEC_INIT_SIZE (sizeof *(((bvector_t)NULL)->tiny) * CHAR_BIT)
25
26 /* The default number of char pointers in a struct svector. */
27 #define SVEC_INIT_SIZE 256
28
29 /* The default number of ints in a struct ivector. */
30 #define IVEC_INIT_SIZE 256
31
32 /*
33 * These try to hide the type of the "bits" member of bvector. But if
34 * that is changed to a type that's wider than unsigned long, the 1ul
35 * constants in the code below must also be changed to a 1 that's at
36 * least as wide as the new type.
37 */
38
39 /* The *sizeof* struct bvector's bits member. Not its size in bits. */
40 #define BVEC_SIZEOF_BITS (sizeof *(((bvector_t)NULL)->bits))
41 /* The number of bits held in one element of the bits member. */
42 #define BVEC_BITS_BITS (BVEC_SIZEOF_BITS * CHAR_BIT)
43 /* The index of bit n in struct bvector's bits member. */
44 #define BVEC_WORD(n) ((n) / BVEC_BITS_BITS)
45 /* The index of bit n within a single struct bvector's bits member. */
46 #define BVEC_OFFSET(n) ((n) % BVEC_BITS_BITS)
47 /* The number of elements bits needs to cover bit n, measured in bytes. */
48 #define BVEC_BYTES(n) (((n) / BVEC_BITS_BITS + 1) * BVEC_SIZEOF_BITS)
49
50 struct bvector {
51 unsigned long *bits;
52 size_t maxsize;
53 unsigned long tiny[2]; /* Default fixed-size storage for bits. */
54 };
55
56 /* bvector_resize ensures the storage used for bits can cover bit
57 * newsize. It always increases the size of the storage used for bits,
58 * even if newsize would have been covered by the existing storage.
59 * Thus it's normally only called when it's known the storage must grow. */
60 static void bvector_resize (bvector_t vec, size_t newsize);
61
62 bvector_t
63 bvector_create (void) {
64 bvector_t vec;
65
66 /* See "wider than unsigned long" comment above. */
67 assert (sizeof *vec->bits <= sizeof 1ul);
68
69 NEW(vec);
70 vec->bits = vec->tiny;
71 vec->maxsize = BVEC_INIT_SIZE;
72 memset(vec->tiny, 0, sizeof vec->tiny);
73
74 return vec;
75 }
76
77 void
78 bvector_copy (bvector_t dest, bvector_t src) {
79 size_t bytes = BVEC_BYTES(src->maxsize);
80
81 if (dest->bits != dest->tiny)
82 free(dest->bits);
83 dest->bits = mh_xmalloc (bytes);
84 memcpy (dest->bits, src->bits, bytes);
85 dest->maxsize = src->maxsize;
86 }
87
88 void
89 bvector_free (bvector_t vec) {
90 if (vec->bits != vec->tiny)
91 free(vec->bits);
92 free (vec);
93 }
94
95 void
96 bvector_clear (bvector_t vec, size_t n) {
97 if (n < vec->maxsize)
98 vec->bits[BVEC_WORD(n)] &= ~(1ul << BVEC_OFFSET(n));
99 }
100
101
102 void
103 bvector_clear_all (bvector_t vec) {
104 memset (vec->bits, 0, BVEC_BYTES(vec->maxsize));
105 }
106
107
108 void
109 bvector_set (bvector_t vec, size_t n) {
110 size_t word = BVEC_WORD(n);
111 size_t offset = BVEC_OFFSET(n);
112
113 if (n >= vec->maxsize)
114 bvector_resize (vec, n);
115 vec->bits[word] |= 1ul << offset;
116 }
117
118 unsigned int
119 bvector_at (bvector_t vec, size_t i) {
120 if (i < vec->maxsize)
121 return !!(vec->bits[BVEC_WORD(i)] & (1ul << BVEC_OFFSET(i)));
122
123 return 0;
124 }
125
126 static void
127 bvector_resize (bvector_t vec, size_t newsize) {
128 size_t oldsize = vec->maxsize;
129 size_t bytes;
130
131 while ((vec->maxsize *= 2) < newsize)
132 ;
133 bytes = BVEC_BYTES(vec->maxsize);
134 if (vec->bits == vec->tiny) {
135 vec->bits = mh_xmalloc(bytes);
136 memcpy(vec->bits, vec->tiny, sizeof vec->tiny);
137 } else
138 vec->bits = mh_xrealloc(vec->bits, bytes);
139
140 memset(vec->bits + (oldsize / BVEC_BITS_BITS), 0,
141 (vec->maxsize - oldsize) / CHAR_BIT);
142 }
143
144 unsigned long
145 bvector_first_bits (bvector_t vec) {
146 return *vec->bits;
147 }
148
149
150 struct svector {
151 char **strs;
152 size_t maxsize;
153 size_t size;
154 };
155
156 static void svector_resize (svector_t, size_t);
157
158 svector_t
159 svector_create (size_t init_size) {
160 svector_t vec;
161 size_t bytes;
162
163 NEW(vec);
164 vec->maxsize = init_size ? init_size : SVEC_INIT_SIZE;
165 bytes = vec->maxsize * sizeof (char *);
166 vec->strs = mh_xcalloc (1, bytes);
167 vec->size = 0;
168
169 return vec;
170 }
171
172 void
173 svector_free (svector_t vec) {
174 free (vec->strs);
175 free (vec);
176 }
177
178 char *
179 svector_push_back (svector_t vec, char *s) {
180 if (++vec->size >= vec->maxsize)
181 svector_resize (vec, vec->size);
182 return vec->strs[vec->size-1] = s;
183 }
184
185 char *
186 svector_at (svector_t vec, size_t i) {
187 if (i >= vec->maxsize)
188 svector_resize (vec, i);
189 return vec->strs[i];
190 }
191
192 /*
193 * Return address of first element that stringwise matches s.
194 * The caller can replace the contents of the return address.
195 */
196 char **
197 svector_find (svector_t vec, const char *s) {
198 size_t i;
199 char **str = vec->strs;
200
201 for (i = 0; i < vec->size; ++i, ++str) {
202 if (*str && ! strcmp(*str, s)) {
203 return str;
204 }
205 }
206
207 return NULL;
208 }
209
210 char **
211 svector_strs (svector_t vec) {
212 return vec->strs;
213 }
214
215 size_t
216 svector_size (svector_t vec) {
217 return vec->size;
218 }
219
220 static void
221 svector_resize (svector_t vec, size_t maxsize) {
222 size_t old_maxsize = vec->maxsize;
223
224 while ((vec->maxsize *= 2) < maxsize)
225 ;
226 vec->strs = mh_xrealloc (vec->strs, vec->maxsize * sizeof (char *));
227 memset(vec->strs + old_maxsize, 0,
228 (vec->maxsize - old_maxsize) * sizeof *vec->strs);
229 }
230
231
232 struct ivector {
233 int *ints;
234 size_t maxsize;
235 size_t size;
236 };
237
238 static void ivector_resize (ivector_t, size_t);
239
240 ivector_t
241 ivector_create (size_t init_size) {
242 ivector_t vec;
243 size_t bytes;
244
245 NEW(vec);
246 vec->maxsize = init_size ? init_size : IVEC_INIT_SIZE;
247 bytes = vec->maxsize * sizeof (int);
248 vec->ints = mh_xcalloc (1, bytes);
249 vec->size = 0;
250
251 return vec;
252 }
253
254 void
255 ivector_free (ivector_t vec) {
256 free (vec->ints);
257 free (vec);
258 }
259
260 int
261 ivector_push_back (ivector_t vec, int n) {
262 if (++vec->size >= vec->maxsize)
263 ivector_resize (vec, vec->size);
264 return vec->ints[vec->size-1] = n;
265 }
266
267 int
268 ivector_at (ivector_t vec, size_t i) {
269 if (i >= vec->maxsize)
270 ivector_resize (vec, i);
271 return vec->ints[i];
272 }
273
274 int *
275 ivector_atp (ivector_t vec, size_t i) {
276 if (i >= vec->maxsize)
277 ivector_resize (vec, i);
278 return &vec->ints[i];
279 }
280
281 static void
282 ivector_resize (ivector_t vec, size_t maxsize) {
283 size_t old_maxsize = vec->maxsize;
284
285 while ((vec->maxsize *= 2) < maxsize)
286 ;
287 vec->ints = mh_xrealloc (vec->ints, vec->maxsize * sizeof (int));
288 memset(vec->ints + old_maxsize, 0,
289 (vec->maxsize - old_maxsize) * sizeof *vec->ints);
290 }