annotate array.h @ 3:bfa97d43197e

support code
author David A. Holland
date Sun, 19 Dec 2010 16:51:57 -0500
parents 9c1cecba517c
children 0601b6e8e53d
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
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
33 #include "utils.h"
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
34
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
35 #define ARRAYS_CHECKED
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
36
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
37 #ifdef ARRAYS_CHECKED
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
38 #include <assert.h>
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
39 #define arrayassert assert
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
40 #else
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
41 #define arrayassert(x) ((void)(x))
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
42 #endif
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
43
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
44 ////////////////////////////////////////////////////////////
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
45 // type and base operations
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
46
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
47 struct array {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
48 void **v;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
49 unsigned num, max;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
50 };
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
51
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
52 struct array *array_create(void);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
53 void array_destroy(struct array *);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
54 void array_init(struct array *);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
55 void array_cleanup(struct array *);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
56 unsigned array_num(const struct array *);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
57 void *array_get(const struct array *, unsigned index_);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
58 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
59 void array_setsize(struct array *, unsigned num);
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
60 void array_add(struct array *, void *val, unsigned *index_ret);
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
61 void array_insert(struct array *a, unsigned index_);
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
62 void array_remove(struct array *a, unsigned index_);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
63
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
64 ////////////////////////////////////////////////////////////
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
65 // inlining for base operations
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
66
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
67 #ifndef ARRAYINLINE
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
68 #define ARRAYINLINE __c99inline
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
69 #endif
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
70
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
71 ARRAYINLINE unsigned
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
72 array_num(const struct array *a)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
73 {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
74 return a->num;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
75 }
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
76
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
77 ARRAYINLINE void *
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
78 array_get(const struct array *a, unsigned index_)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
79 {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
80 arrayassert(index_ < a->num);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
81 return a->v[index_];
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
82 }
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
83
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
84 ARRAYINLINE void
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
85 array_set(const struct array *a, unsigned index_, void *val)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
86 {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
87 arrayassert(index_ < a->num);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
88 a->v[index_] = val;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
89 }
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
90
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
91 ARRAYINLINE void
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
92 array_add(struct array *a, void *val, unsigned *index_ret)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
93 {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
94 unsigned index_ = a->num;
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
95 array_setsize(a, index_+1);
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
96 a->v[index_] = val;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
97 if (index_ret != NULL) {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
98 *index_ret = index_;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
99 }
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 // bits for declaring and defining typed arrays
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
104
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
105 /*
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
106 * Usage:
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
107 *
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
108 * DECLARRAY_BYTYPE(foo, bar) declares "struct foo", which is
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
109 * an array of pointers to "bar", plus the operations on it.
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
110 *
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
111 * DECLARRAY(foo) is equivalent to DECLARRAY_BYTYPE(fooarray, struct foo).
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
112 *
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
113 * DEFARRAY_BYTYPE and DEFARRAY are the same as DECLARRAY except that
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
114 * they define the operations, and both take an extra argument INLINE.
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
115 * For C99 this should be INLINE in header files and empty in the
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
116 * master source file, the same as the usage of ARRAYINLINE above and
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
117 * in array.c.
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
118 *
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
119 * Example usage in e.g. item.h of some game:
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
120 *
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
121 * DECLARRAY_BYTYPE(stringarray, char);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
122 * DECLARRAY(potion);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
123 * DECLARRAY(sword);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
124 *
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
125 * #ifndef ITEMINLINE
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
126 * #define ITEMINLINE INLINE
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
127 * #endif
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
128 *
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
129 * DEFARRAY_BYTYPE(stringarray, char, ITEMINLINE);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
130 * DEFARRAY(potion, ITEMINLINE);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
131 * DEFARRAY(sword, ITEMINLINE);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
132 *
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
133 * Then item.c would do "#define ITEMINLINE" before including item.h.
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
134 */
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
135
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
136 #define DECLARRAY_BYTYPE(ARRAY, T) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
137 struct ARRAY { \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
138 struct array arr; \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
139 }; \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
140 \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
141 struct ARRAY *ARRAY##_create(void); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
142 void ARRAY##_destroy(struct ARRAY *a); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
143 void ARRAY##_init(struct ARRAY *a); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
144 void ARRAY##_cleanup(struct ARRAY *a); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
145 unsigned ARRAY##_num(const struct ARRAY *a); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
146 T *ARRAY##_get(const struct ARRAY *a, unsigned index_); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
147 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
148 void ARRAY##_setsize(struct ARRAY *a, unsigned num); \
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
149 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
150 void ARRAY##_insert(struct ARRAY *a, unsigned index_); \
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
151 void ARRAY##_remove(struct ARRAY *a, unsigned index_)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
152
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
153
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
154 #define DEFARRAY_BYTYPE(ARRAY, T, INLINE) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
155 INLINE void \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
156 ARRAY##_init(struct ARRAY *a) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
157 { \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
158 array_init(&a->arr); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
159 } \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
160 \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
161 INLINE void \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
162 ARRAY##_cleanup(struct ARRAY *a) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
163 { \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
164 array_cleanup(&a->arr); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
165 } \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
166 \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
167 INLINE struct \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
168 ARRAY *ARRAY##_create(void) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
169 { \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
170 struct ARRAY *a; \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
171 \
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
172 a = domalloc(sizeof(*a)); \
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
173 ARRAY##_init(a); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
174 return a; \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
175 } \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
176 \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
177 INLINE void \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
178 ARRAY##_destroy(struct ARRAY *a) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
179 { \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
180 ARRAY##_cleanup(a); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
181 free(a); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
182 } \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
183 \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
184 INLINE unsigned \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
185 ARRAY##_num(const struct ARRAY *a) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
186 { \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
187 return array_num(&a->arr); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
188 } \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
189 \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
190 INLINE T * \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
191 ARRAY##_get(const struct ARRAY *a, unsigned index_) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
192 { \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
193 return (T *)array_get(&a->arr, index_); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
194 } \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
195 \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
196 INLINE void \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
197 ARRAY##_set(struct ARRAY *a, unsigned index_, T *val) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
198 { \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
199 array_set(&a->arr, index_, (void *)val); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
200 } \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
201 \
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
202 INLINE void \
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
203 ARRAY##_setsize(struct ARRAY *a, unsigned num) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
204 { \
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
205 array_setsize(&a->arr, num); \
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
206 } \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
207 \
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
208 INLINE void \
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
209 ARRAY##_add(struct ARRAY *a, T *val, unsigned *ret) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
210 { \
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
211 array_add(&a->arr, (void *)val, ret); \
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
212 } \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
213 \
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
214 INLINE void \
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
215 ARRAY##_insert(struct ARRAY *a, unsigned index_) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
216 { \
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
217 array_insert(&a->arr, index_); \
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
218 } \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
219 \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
220 INLINE void \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
221 ARRAY##_remove(struct ARRAY *a, unsigned index_) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
222 { \
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
223 array_remove(&a->arr, index_); \
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
224 }
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
225
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
226 #define DECLARRAY(T) DECLARRAY_BYTYPE(T##array, struct T)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
227 #define DEFARRAY(T, INLINE) DEFARRAY_BYTYPE(T##array, struct T, INLINE)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
228
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
229 ////////////////////////////////////////////////////////////
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
230 // basic array types
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
231
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
232 DECLARRAY_BYTYPE(stringarray, char);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
233 DEFARRAY_BYTYPE(stringarray, char, ARRAYINLINE);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
234
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
235 #endif /* ARRAY_H */