]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/ShivaVG/src/shArrayBase.h
Update for OpenSceneGraph 3.3.2 API changes.
[simgear.git] / simgear / canvas / ShivaVG / src / shArrayBase.h
1 /*
2  * Copyright (c) 2007 Ivan Leben
3  *
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.
8  * 
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.
13  * 
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
18  *
19  */
20
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.
25  *
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
30  *
31  * And at least one of these:
32  * _ARRAY_DECLARE: generate structure declaration
33  * _ARRAY_DEFINE: generate function definitions
34  *-----------------------------------------------------------*/
35
36 #ifndef __SHARRAYBASE_H
37 #define __SHARRAYBASE_H
38
39 #include "shDefs.h"
40
41 #define VAL(x,y) x ## y
42 #define JN(x,y) VAL(x,y)
43
44 #endif
45
46 /*--------------------------------------------
47  * The rest is not #ifndef protected to allow
48  * for various array type definitions.
49  *--------------------------------------------*/
50
51
52 #ifdef _ARRAY_DECLARE
53 typedef struct
54 {
55   _ITEM_T *items;
56   SHint32 capacity;
57   SHint32 size;
58   SHint outofmemory;
59   
60 } _ARRAY_T;
61 #endif
62
63
64 void JN(_ARRAY_T,_ctor) (_ARRAY_T *a)
65 #ifdef _ARRAY_DEFINE
66
67   a->items = (_ITEM_T*)malloc(sizeof(_ITEM_T));
68   
69   if (!a->items) {
70     a->outofmemory = 1;
71     a->capacity = 0;
72     a->size = 0;
73     return;
74   }
75   
76   a->outofmemory = 0;
77   a->capacity = 1;
78   a->size = 0;
79 }
80 #else
81 ;
82 #endif
83
84
85 void JN(_ARRAY_T,_dtor) (_ARRAY_T *a)
86 #ifdef _ARRAY_DEFINE
87 {
88   if (a->items) {
89     free(a->items);
90     a->items = NULL;
91   }
92 }
93 #else
94 ;
95 #endif
96
97 void JN(_FUNC_T,Clear) (_ARRAY_T *a)
98 #ifdef _ARRAY_DEFINE
99 {
100   a->size = 0;
101 }
102 #else
103 ;
104 #endif
105
106
107 /*--------------------------------------------------------
108  * Set the capacity of the array. In case of reallocation
109  * the items are not preserved.
110  *--------------------------------------------------------*/
111
112 int JN(_FUNC_T,Realloc) (_ARRAY_T *a, SHint newsize)
113 #ifdef _ARRAY_DEFINE
114 {
115   _ITEM_T *newitems = 0;
116   
117   SH_ASSERT(newsize > 0);
118   if (newsize == a->capacity)
119     return 1;
120   
121   newitems = (_ITEM_T*)malloc(newsize * sizeof(_ITEM_T));
122   
123   if (!newitems) {
124     a->outofmemory = 1;
125     return 0;
126   }
127   
128   if (a->items)
129     free(a->items);
130   
131   a->outofmemory = 0;
132   a->items = newitems;
133   a->capacity = newsize;
134   a->size = 0;
135   return 1;
136 }
137 #else
138 ;
139 #endif
140
141
142 /*------------------------------------------------------
143  * Asserts the capacity is at least [newsize]. In case
144  * of reallocation items are not preserved.
145  *------------------------------------------------------*/
146
147 int JN(_FUNC_T,Reserve) (_ARRAY_T *a, SHint newsize)
148 #ifdef _ARRAY_DEFINE
149 {
150   _ITEM_T *newitems = 0;
151   
152   SH_ASSERT(newsize >= 0);
153   if (newsize <= a->capacity)
154     return 1;
155   
156   newitems = (_ITEM_T*)malloc(newsize * sizeof(_ITEM_T));
157   
158   if (!newitems) {
159     a->outofmemory = 1;
160     return 0;
161   }
162   
163   if (a->items)
164     free(a->items);
165   
166   a->outofmemory = 0;
167   a->items = newitems;
168   a->capacity = newsize;
169   a->size = 0;
170   return 1;
171 }
172 #else
173 ;
174 #endif
175
176 /*------------------------------------------------------
177  * Asserts the capacity is at least [newsize]. In case
178  * of reallocation items are copied.
179  *------------------------------------------------------*/
180
181 int JN(_FUNC_T,ReserveAndCopy) (_ARRAY_T *a, SHint newsize)
182 #ifdef _ARRAY_DEFINE
183 {
184   _ITEM_T *newitems = 0;
185   
186   SH_ASSERT(newsize >= 0);
187   if (newsize <= a->capacity)
188     return 1;
189   
190   newitems = (_ITEM_T*)realloc(a->items, newsize * sizeof(_ITEM_T));
191   
192   if (!newitems) {
193     a->outofmemory = 1;
194     return 0;
195   }
196   
197   a->outofmemory = 0;
198   a->items = newitems;
199   a->capacity = newsize;
200   return 1;
201 }
202 #else
203 ;
204 #endif
205
206
207 int JN(_FUNC_T,PushBack) (_ARRAY_T *a, _ITEM_T item)
208 #ifdef _ARRAY_DEFINE
209 {
210   if (a->capacity == 0) {
211     JN(_FUNC_T,Realloc)(a, 1);
212     if (a->outofmemory)
213       return 0;
214   }
215   
216   if (a->size + 1 > a->capacity)
217     JN(_FUNC_T,ReserveAndCopy)(a, a->capacity*2);
218   
219   if (a->outofmemory)
220     return 0;
221   
222   a->items[a->size++] = item;
223   return 1;
224 }
225 #else
226 ;
227 #endif
228
229
230 int JN(_FUNC_T,PushBackP) (_ARRAY_T *a, _ITEM_T *item)
231 #ifdef _ARRAY_DEFINE
232 {
233   if (a->capacity == 0) {
234     JN(_FUNC_T,Realloc)(a, 1);
235     if (a->outofmemory)
236       return 0;
237   }
238   
239   if (a->size + 1 > a->capacity)
240     JN(_FUNC_T,ReserveAndCopy)(a, a->capacity*2);
241   
242   if (a->outofmemory)
243     return 0;
244   
245   a->items[a->size++] = *item;
246   return 1;
247 }
248 #else
249 ;
250 #endif
251
252
253 void JN(_FUNC_T,PopBack) (_ARRAY_T *a)
254 #ifdef _ARRAY_DEFINE
255 {
256   SH_ASSERT(a->size);
257   --a->size;
258 }
259 #else
260 ;
261 #endif
262
263
264 _ITEM_T JN(_FUNC_T,Front) (_ARRAY_T *a)
265 #ifdef _ARRAY_DEFINE
266 {
267   SH_ASSERT(a->size);
268   return a->items[0];
269 }
270 #else
271 ;
272 #endif
273
274
275 _ITEM_T* JN(_FUNC_T,FrontP) (_ARRAY_T *a)
276 #ifdef _ARRAY_DEFINE
277 {
278   SH_ASSERT(a->size);
279   return &a->items[0];
280 }
281 #else
282 ;
283 #endif
284
285
286 _ITEM_T JN(_FUNC_T,Back) (_ARRAY_T *a)
287 #ifdef _ARRAY_DEFINE
288 {
289   SH_ASSERT(a->size);
290   return a->items[a->size - 1];
291 }
292 #else
293 ;
294 #endif
295
296
297 _ITEM_T* JN(_FUNC_T,BackP) (_ARRAY_T *a)
298 #ifdef _ARRAY_DEFINE
299 {
300   SH_ASSERT(a->size);
301   return &a->items[a->size - 1];
302 }
303 #else
304 ;
305 #endif
306
307
308 _ITEM_T JN(_FUNC_T,At) (_ARRAY_T *a, SHint index)
309 #ifdef _ARRAY_DEFINE
310 {
311   SH_ASSERT(index >= 0);
312   SH_ASSERT(index < a->size);
313   return a->items[index];
314 }
315 #else
316 ;
317 #endif
318
319
320 _ITEM_T* JN(_FUNC_T,AtP) (_ARRAY_T *a, SHint index)
321 #ifdef _ARRAY_DEFINE
322 {
323   SH_ASSERT(index >= 0);
324   SH_ASSERT(index < a->size);
325   return &a->items[index];
326 }
327 #else
328 ;
329 #endif
330
331 SHint JN(_FUNC_T,Find) (_ARRAY_T *a, _ITEM_T item)
332 #ifdef _ARRAY_DEFINE
333 {
334   int i;
335   for (i=0; i<a->size; ++i) {
336     #ifdef _COMPARE_T
337     if (_COMPARE_T(a->items[i], item))
338       return i;
339     #else
340     if (a->items[i] == item)
341       return i;
342     #endif
343   }
344   
345   return -1;
346 }
347 #else
348 ;
349 #endif
350
351 void JN(_FUNC_T,RemoveAt) (_ARRAY_T *a, SHint index)
352 #ifdef _ARRAY_DEFINE
353 {
354   int i;
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];
359   a->size--;
360 }
361 #else
362 ;
363 #endif
364
365 #undef _ITEM_T
366 #undef _ARRAY_T
367 #undef _FUNC_T
368 #undef _COMPARE_T
369 #undef _ARRAY_DEFINE
370 #undef _ARRAY_DECLARE