]>
diplodocus.org Git - nmh/blob - sbr/vector.c
e37606519826edff0a40f556d2c36935e74748bd
1 /* vector.c -- dynamically sized vectors
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.
9 * This file defines several kinds of vectors:
11 * svector: vector of char arrays
12 * ivector: vector of ints
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.
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)
26 /* The default number of char pointers in a struct svector. */
27 #define SVEC_INIT_SIZE 256
29 /* The default number of ints in a struct ivector. */
30 #define IVEC_INIT_SIZE 256
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.
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)
53 unsigned long tiny
[2]; /* Default fixed-size storage for bits. */
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
);
63 bvector_create (void) {
66 /* See "wider than unsigned long" comment above. */
67 assert (sizeof *vec
->bits
<= sizeof 1ul);
70 vec
->bits
= vec
->tiny
;
71 vec
->maxsize
= BVEC_INIT_SIZE
;
72 memset(vec
->tiny
, 0, sizeof vec
->tiny
);
78 bvector_copy (bvector_t dest
, bvector_t src
) {
79 size_t bytes
= BVEC_BYTES(src
->maxsize
);
81 if (dest
->bits
!= dest
->tiny
)
83 dest
->bits
= mh_xmalloc (bytes
);
84 memcpy (dest
->bits
, src
->bits
, bytes
);
85 dest
->maxsize
= src
->maxsize
;
89 bvector_free (bvector_t vec
) {
90 if (vec
->bits
!= vec
->tiny
)
96 bvector_clear (bvector_t vec
, size_t n
) {
98 vec
->bits
[BVEC_WORD(n
)] &= ~(1ul << BVEC_OFFSET(n
));
103 bvector_clear_all (bvector_t vec
) {
104 memset (vec
->bits
, 0, BVEC_BYTES(vec
->maxsize
));
109 bvector_set (bvector_t vec
, size_t n
) {
110 size_t word
= BVEC_WORD(n
);
111 size_t offset
= BVEC_OFFSET(n
);
113 if (n
>= vec
->maxsize
)
114 bvector_resize (vec
, n
);
115 vec
->bits
[word
] |= 1ul << offset
;
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
)));
127 bvector_resize (bvector_t vec
, size_t newsize
) {
128 size_t oldsize
= vec
->maxsize
;
131 while ((vec
->maxsize
*= 2) < newsize
)
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
);
138 vec
->bits
= mh_xrealloc(vec
->bits
, bytes
);
140 memset(vec
->bits
+ (oldsize
/ BVEC_BITS_BITS
), 0,
141 (vec
->maxsize
- oldsize
) / CHAR_BIT
);
145 bvector_first_bits (bvector_t vec
) {
156 static void svector_resize (svector_t
, size_t);
159 svector_create (size_t init_size
) {
164 vec
->maxsize
= init_size
? init_size
: SVEC_INIT_SIZE
;
165 bytes
= vec
->maxsize
* sizeof (char *);
166 vec
->strs
= mh_xcalloc (1, bytes
);
173 svector_free (svector_t vec
) {
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
;
186 svector_at (svector_t vec
, size_t i
) {
187 if (i
>= vec
->maxsize
)
188 svector_resize (vec
, i
);
193 * Return address of first element that stringwise matches s.
194 * The caller can replace the contents of the return address.
197 svector_find (svector_t vec
, const char *s
) {
199 char **str
= vec
->strs
;
201 for (i
= 0; i
< vec
->size
; ++i
, ++str
) {
202 if (*str
&& ! strcmp(*str
, s
)) {
211 svector_strs (svector_t vec
) {
216 svector_size (svector_t vec
) {
221 svector_resize (svector_t vec
, size_t maxsize
) {
222 size_t old_maxsize
= vec
->maxsize
;
224 while ((vec
->maxsize
*= 2) < maxsize
)
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
);
238 static void ivector_resize (ivector_t
, size_t);
241 ivector_create (size_t init_size
) {
246 vec
->maxsize
= init_size
? init_size
: IVEC_INIT_SIZE
;
247 bytes
= vec
->maxsize
* sizeof (int);
248 vec
->ints
= mh_xcalloc (1, bytes
);
255 ivector_free (ivector_t vec
) {
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
;
268 ivector_at (ivector_t vec
, size_t i
) {
269 if (i
>= vec
->maxsize
)
270 ivector_resize (vec
, i
);
275 ivector_atp (ivector_t vec
, size_t i
) {
276 if (i
>= vec
->maxsize
)
277 ivector_resize (vec
, i
);
278 return &vec
->ints
[i
];
282 ivector_resize (ivector_t vec
, size_t maxsize
) {
283 size_t old_maxsize
= vec
->maxsize
;
285 while ((vec
->maxsize
*= 2) < maxsize
)
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
);