2 * Copyright (c) 2007 Ivan Leben
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library in the file COPYING;
16 * if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 /*------------------------------------------------------------
22 * The base for any type of array. According to appropriate
23 * macro definitions, specific array types will be generated
24 * and their manipulation functions respectively.
26 * This code assumes the following are defined:
27 * _ITEM_T: the type of the items in the array
28 * _ARRAY_T: the name of the structure
29 * _FUNC_T: the prefix to prepend to each function
31 * And at least one of these:
32 * _ARRAY_DECLARE: generate structure declaration
33 * _ARRAY_DEFINE: generate function definitions
34 *-----------------------------------------------------------*/
36 #ifndef __SHARRAYBASE_H
37 #define __SHARRAYBASE_H
41 #define VAL(x,y) x ## y
42 #define JN(x,y) VAL(x,y)
46 /*--------------------------------------------
47 * The rest is not #ifndef protected to allow
48 * for various array type definitions.
49 *--------------------------------------------*/
64 void JN(_ARRAY_T,_ctor) (_ARRAY_T *a)
67 a->items = (_ITEM_T*)malloc(sizeof(_ITEM_T));
85 void JN(_ARRAY_T,_dtor) (_ARRAY_T *a)
97 void JN(_FUNC_T,Clear) (_ARRAY_T *a)
107 /*--------------------------------------------------------
108 * Set the capacity of the array. In case of reallocation
109 * the items are not preserved.
110 *--------------------------------------------------------*/
112 int JN(_FUNC_T,Realloc) (_ARRAY_T *a, SHint newsize)
115 _ITEM_T *newitems = 0;
117 SH_ASSERT(newsize > 0);
118 if (newsize == a->capacity)
121 newitems = (_ITEM_T*)malloc(newsize * sizeof(_ITEM_T));
133 a->capacity = newsize;
142 /*------------------------------------------------------
143 * Asserts the capacity is at least [newsize]. In case
144 * of reallocation items are not preserved.
145 *------------------------------------------------------*/
147 int JN(_FUNC_T,Reserve) (_ARRAY_T *a, SHint newsize)
150 _ITEM_T *newitems = 0;
152 SH_ASSERT(newsize >= 0);
153 if (newsize <= a->capacity)
156 newitems = (_ITEM_T*)malloc(newsize * sizeof(_ITEM_T));
168 a->capacity = newsize;
176 /*------------------------------------------------------
177 * Asserts the capacity is at least [newsize]. In case
178 * of reallocation items are copied.
179 *------------------------------------------------------*/
181 int JN(_FUNC_T,ReserveAndCopy) (_ARRAY_T *a, SHint newsize)
184 _ITEM_T *newitems = 0;
186 SH_ASSERT(newsize >= 0);
187 if (newsize <= a->capacity)
190 newitems = (_ITEM_T*)realloc(a->items, newsize * sizeof(_ITEM_T));
199 a->capacity = newsize;
207 int JN(_FUNC_T,PushBack) (_ARRAY_T *a, _ITEM_T item)
210 if (a->capacity == 0) {
211 JN(_FUNC_T,Realloc)(a, 1);
216 if (a->size + 1 > a->capacity)
217 JN(_FUNC_T,ReserveAndCopy)(a, a->capacity*2);
222 a->items[a->size++] = item;
230 int JN(_FUNC_T,PushBackP) (_ARRAY_T *a, _ITEM_T *item)
233 if (a->capacity == 0) {
234 JN(_FUNC_T,Realloc)(a, 1);
239 if (a->size + 1 > a->capacity)
240 JN(_FUNC_T,ReserveAndCopy)(a, a->capacity*2);
245 a->items[a->size++] = *item;
253 void JN(_FUNC_T,PopBack) (_ARRAY_T *a)
264 _ITEM_T JN(_FUNC_T,Front) (_ARRAY_T *a)
275 _ITEM_T* JN(_FUNC_T,FrontP) (_ARRAY_T *a)
286 _ITEM_T JN(_FUNC_T,Back) (_ARRAY_T *a)
290 return a->items[a->size - 1];
297 _ITEM_T* JN(_FUNC_T,BackP) (_ARRAY_T *a)
301 return &a->items[a->size - 1];
308 _ITEM_T JN(_FUNC_T,At) (_ARRAY_T *a, SHint index)
311 SH_ASSERT(index >= 0);
312 SH_ASSERT(index < a->size);
313 return a->items[index];
320 _ITEM_T* JN(_FUNC_T,AtP) (_ARRAY_T *a, SHint index)
323 SH_ASSERT(index >= 0);
324 SH_ASSERT(index < a->size);
325 return &a->items[index];
331 SHint JN(_FUNC_T,Find) (_ARRAY_T *a, _ITEM_T item)
335 for (i=0; i<a->size; ++i) {
337 if (_COMPARE_T(a->items[i], item))
340 if (a->items[i] == item)
351 void JN(_FUNC_T,RemoveAt) (_ARRAY_T *a, SHint index)
355 SH_ASSERT(index >= 0);
356 SH_ASSERT(index < a->size);
357 for (i=index; i<a->size-1; ++i)
358 a->items[i] = a->items[i+1];
370 #undef _ARRAY_DECLARE