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