annotate array.h @ 27:01c3a2088ab4

fix some more bugs
author David A. Holland
date Mon, 20 Dec 2010 05:01:18 -0500
parents 1fbcbd58742e
children b156910b59b2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
1 /*-
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
2 * Copyright (c) 2009 The NetBSD Foundation, Inc.
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
3 * All rights reserved.
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
4 *
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
5 * This code is derived from software contributed to The NetBSD Foundation
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
6 * by David A. Holland.
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
7 *
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
8 * Redistribution and use in source and binary forms, with or without
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
9 * modification, are permitted provided that the following conditions
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
10 * are met:
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
11 * 1. Redistributions of source code must retain the above copyright
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
12 * notice, this list of conditions and the following disclaimer.
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
13 * 2. Redistributions in binary form must reproduce the above copyright
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
14 * notice, this list of conditions and the following disclaimer in the
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
15 * documentation and/or other materials provided with the distribution.
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
16 *
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
27 * POSSIBILITY OF SUCH DAMAGE.
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
28 */
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
29
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
30 #ifndef ARRAY_H
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
31 #define ARRAY_H
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
32
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents: 2
diff changeset
33 #include "inlinedefs.h" // XXX
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
34 #include "utils.h"
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
35
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
36 #define ARRAYS_CHECKED
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
37
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
38 #ifdef ARRAYS_CHECKED
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
39 #include <assert.h>
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
40 #define arrayassert assert
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
41 #else
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
42 #define arrayassert(x) ((void)(x))
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
43 #endif
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
44
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
45 ////////////////////////////////////////////////////////////
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
46 // type and base operations
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
47
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
48 struct array {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
49 void **v;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
50 unsigned num, max;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
51 };
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
52
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
53 struct array *array_create(void);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
54 void array_destroy(struct array *);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
55 void array_init(struct array *);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
56 void array_cleanup(struct array *);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
57 unsigned array_num(const struct array *);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
58 void *array_get(const struct array *, unsigned index_);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
59 void array_set(const struct array *, unsigned index_, void *val);
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
60 void array_setsize(struct array *, unsigned num);
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
61 void array_add(struct array *, void *val, unsigned *index_ret);
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
62 void array_insert(struct array *a, unsigned index_);
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
63 void array_remove(struct array *a, unsigned index_);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
64
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
65 ////////////////////////////////////////////////////////////
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
66 // inlining for base operations
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
67
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
68 #ifndef ARRAYINLINE
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
69 #define ARRAYINLINE __c99inline
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
70 #endif
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
71
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
72 ARRAYINLINE unsigned
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
73 array_num(const struct array *a)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
74 {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
75 return a->num;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
76 }
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
77
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
78 ARRAYINLINE void *
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
79 array_get(const struct array *a, unsigned index_)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
80 {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
81 arrayassert(index_ < a->num);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
82 return a->v[index_];
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
83 }
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
84
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
85 ARRAYINLINE void
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
86 array_set(const struct array *a, unsigned index_, void *val)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
87 {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
88 arrayassert(index_ < a->num);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
89 a->v[index_] = val;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
90 }
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
91
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
92 ARRAYINLINE void
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
93 array_add(struct array *a, void *val, unsigned *index_ret)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
94 {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
95 unsigned index_ = a->num;
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
96 array_setsize(a, index_+1);
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
97 a->v[index_] = val;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
98 if (index_ret != NULL) {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
99 *index_ret = index_;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
100 }
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
101 }
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
102
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
103 ////////////////////////////////////////////////////////////
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
104 // bits for declaring and defining typed arrays
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
105
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
106 /*
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
107 * Usage:
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
108 *
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
109 * DECLARRAY_BYTYPE(foo, bar) declares "struct foo", which is
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
110 * an array of pointers to "bar", plus the operations on it.
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
111 *
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
112 * DECLARRAY(foo) is equivalent to DECLARRAY_BYTYPE(fooarray, struct foo).
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
113 *
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
114 * DEFARRAY_BYTYPE and DEFARRAY are the same as DECLARRAY except that
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
115 * they define the operations, and both take an extra argument INLINE.
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
116 * For C99 this should be INLINE in header files and empty in the
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
117 * master source file, the same as the usage of ARRAYINLINE above and
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
118 * in array.c.
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
119 *
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
120 * Example usage in e.g. item.h of some game:
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
121 *
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
122 * DECLARRAY_BYTYPE(stringarray, char);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
123 * DECLARRAY(potion);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
124 * DECLARRAY(sword);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
125 *
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
126 * #ifndef ITEMINLINE
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
127 * #define ITEMINLINE INLINE
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
128 * #endif
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
129 *
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
130 * DEFARRAY_BYTYPE(stringarray, char, ITEMINLINE);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
131 * DEFARRAY(potion, ITEMINLINE);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
132 * DEFARRAY(sword, ITEMINLINE);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
133 *
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
134 * Then item.c would do "#define ITEMINLINE" before including item.h.
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
135 */
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
136
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
137 #define DECLARRAY_BYTYPE(ARRAY, T) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
138 struct ARRAY { \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
139 struct array arr; \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
140 }; \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
141 \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
142 struct ARRAY *ARRAY##_create(void); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
143 void ARRAY##_destroy(struct ARRAY *a); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
144 void ARRAY##_init(struct ARRAY *a); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
145 void ARRAY##_cleanup(struct ARRAY *a); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
146 unsigned ARRAY##_num(const struct ARRAY *a); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
147 T *ARRAY##_get(const struct ARRAY *a, unsigned index_); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
148 void ARRAY##_set(struct ARRAY *a, unsigned index_, T *val); \
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
149 void ARRAY##_setsize(struct ARRAY *a, unsigned num); \
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
150 void ARRAY##_add(struct ARRAY *a, T *val, unsigned *index_ret); \
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
151 void ARRAY##_insert(struct ARRAY *a, unsigned index_); \
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
152 void ARRAY##_remove(struct ARRAY *a, unsigned index_)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
153
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
154
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
155 #define DEFARRAY_BYTYPE(ARRAY, T, INLINE) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
156 INLINE void \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
157 ARRAY##_init(struct ARRAY *a) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
158 { \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
159 array_init(&a->arr); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
160 } \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
161 \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
162 INLINE void \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
163 ARRAY##_cleanup(struct ARRAY *a) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
164 { \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
165 array_cleanup(&a->arr); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
166 } \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
167 \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
168 INLINE struct \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
169 ARRAY *ARRAY##_create(void) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
170 { \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
171 struct ARRAY *a; \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
172 \
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
173 a = domalloc(sizeof(*a)); \
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
174 ARRAY##_init(a); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
175 return a; \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
176 } \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
177 \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
178 INLINE void \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
179 ARRAY##_destroy(struct ARRAY *a) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
180 { \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
181 ARRAY##_cleanup(a); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
182 free(a); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
183 } \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
184 \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
185 INLINE unsigned \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
186 ARRAY##_num(const struct ARRAY *a) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
187 { \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
188 return array_num(&a->arr); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
189 } \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
190 \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
191 INLINE T * \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
192 ARRAY##_get(const struct ARRAY *a, unsigned index_) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
193 { \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
194 return (T *)array_get(&a->arr, index_); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
195 } \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
196 \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
197 INLINE void \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
198 ARRAY##_set(struct ARRAY *a, unsigned index_, T *val) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
199 { \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
200 array_set(&a->arr, index_, (void *)val); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
201 } \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
202 \
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
203 INLINE void \
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
204 ARRAY##_setsize(struct ARRAY *a, unsigned num) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
205 { \
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
206 array_setsize(&a->arr, num); \
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
207 } \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
208 \
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
209 INLINE void \
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
210 ARRAY##_add(struct ARRAY *a, T *val, unsigned *ret) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
211 { \
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
212 array_add(&a->arr, (void *)val, ret); \
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
213 } \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
214 \
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
215 INLINE void \
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
216 ARRAY##_insert(struct ARRAY *a, unsigned index_) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
217 { \
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
218 array_insert(&a->arr, index_); \
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
219 } \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
220 \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
221 INLINE void \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
222 ARRAY##_remove(struct ARRAY *a, unsigned index_) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
223 { \
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
224 array_remove(&a->arr, index_); \
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
225 }
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
226
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
227 #define DECLARRAY(T) DECLARRAY_BYTYPE(T##array, struct T)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
228 #define DEFARRAY(T, INLINE) DEFARRAY_BYTYPE(T##array, struct T, INLINE)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
229
9
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
230 #define DESTROYALL_ARRAY(T, INLINE) \
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
231 void T##array_destroyall(struct T##array *arr); \
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
232 \
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
233 INLINE void \
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
234 T##array_destroyall(struct T##array *arr) \
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
235 { \
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
236 unsigned i, num; \
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
237 struct T *t; \
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
238 \
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
239 num = T##array_num(arr); \
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
240 for (i=0; i<num; i++) { \
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
241 t = T##array_get(arr, i); \
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
242 T##_destroy(t); \
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
243 } \
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
244 T##array_setsize(arr, 0); \
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
245 }
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
246
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
247
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
248 ////////////////////////////////////////////////////////////
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
249 // basic array types
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
250
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
251 DECLARRAY_BYTYPE(stringarray, char);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
252 DEFARRAY_BYTYPE(stringarray, char, ARRAYINLINE);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
253
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
254 #endif /* ARRAY_H */