ZEDA  1.6.18
zeda_array.h
Go to the documentation of this file.
1 /* ZEDA - Elementary Data and Algorithms
2  * Copyright (C) 1998 Tomomichi Sugihara (Zhidao)
3  */
9 #ifndef __ZEDA_ARRAY_H__
10 #define __ZEDA_ARRAY_H__
11 
12 #include <zeda/zeda_misc.h>
13 #include <zeda/zeda_name.h>
14 
16 
17 /* ********************************************************** *//* ************************************************** */
20 
46 #ifdef __cplusplus
47 #define zArrayClass(array_t,cell_t) \
48 struct array_t{\
49  uint size;\
50  cell_t *buf;\
51 }
52 #else
53 #define zArrayClass(array_t,cell_t) \
54 typedef struct{\
55  uint size;\
56  cell_t *buf;\
57 } array_t
58 #endif /* __cplusplus */
59 
60 #define zArraySize(a) (a)->size
61 #define zArrayBuf(a) (a)->buf
62 
63 #define zArrayPosIsValid(a,p) ( (p) < zArraySize(a) && (p) >= 0 )
64 
65 /* NOTE: do not use the following macro before allocating buffer */
66 #define zArrayElemSize(a) sizeof(*zArrayBuf(a))
67 
68 #define zArrayElemNC(a,i) ( &zArrayBuf(a)[i] )
69 #define zArrayElem(a,i) ( zArrayPosIsValid(a,i) ? zArrayElemNC(a,i) : NULL )
70 #define zArraySetElemNC(a,i,d) memcpy( zArrayElemNC(a,i), (d), zArrayElemSize(a) )
71 #define zArraySetElem(a,i,d) ( zArrayPosIsValid(a,i) ? zArraySetElemNC(a,i,d) : NULL )
72 
73 #define zArrayHead(a) zArrayElemNC( a, zArraySize(a)-1 )
74 #define zArrayNeck(a) zArrayElemNC( a, zArraySize(a)-2 )
75 #define zArrayTail(a) zArrayElemNC( a, 0 )
76 
77 #define zArrayInit(arr) do{\
78  zArraySize(arr) = 0;\
79  zArrayBuf(arr) = NULL;\
80 } while(0)
81 
86 #define zArrayAlloc(arr,type,n) do{\
87  if( (n) <= 0 ) zArrayInit( arr );\
88  else{\
89  if( !( zArrayBuf(arr) = zAlloc( type, n ) ) ){\
90  ZALLOCERROR();\
91  zArraySize(arr) = 0;\
92  } else\
93  zArraySize(arr) = (n);\
94  }\
95 } while(0)
96 
100 #define zArrayFree(arr) do{\
101  free( zArrayBuf(arr) );\
102  zArrayInit( arr );\
103 } while(0)
104 
108 #define zArrayFindName(arr,name,ptr) \
109  zNameFind( zArrayBuf(arr), zArraySize(arr), name, ptr )
110 
111 #ifndef __KERNEL__
112 /* since realloc() is not implemented in kernel space,
113  * the following methods are only available in user space.
114  */
115 
122 #define zArrayAdd(arr,type,dat) do{\
123  type *__zarray_ap;\
124  __zarray_ap = zRealloc( zArrayBuf(arr), type, zArraySize(arr)+1 );\
125  if( __zarray_ap == NULL )\
126  ZALLOCERROR();\
127  else{\
128  zArraySize(arr)++;\
129  zArrayBuf(arr) = __zarray_ap;\
130  zArraySetElemNC( arr, zArraySize(arr)-1, dat );\
131  }\
132 } while(0)
133 
140 #define zArrayInsert(arr,type,pos,dat) do{\
141  type *__zarray_ap;\
142  if( (pos) == zArraySize(arr) ){\
143  zArrayAdd( arr, type, dat );\
144  } else if( zArrayPosIsValid(arr,pos) ){\
145  __zarray_ap = zRealloc( zArrayBuf(arr), type, zArraySize(arr)+1 );\
146  if( __zarray_ap == NULL )\
147  ZALLOCERROR();\
148  else{\
149  zArrayBuf(arr) = __zarray_ap;\
150  memmove( zArrayElemNC(arr,(pos)+1), zArrayElemNC(arr,pos), sizeof(type)*(zArraySize(arr)-(pos)) );\
151  zArraySetElemNC( arr, pos, dat );\
152  zArraySize(arr)++;\
153  }\
154  } else{\
155  ZRUNWARN( "invalid position %d/%d in array specified", pos, zArraySize(arr)-1 );\
156  }\
157 } while(0)
158 
165 #define zArrayDelete(arr,type,pos) do{\
166  type *__zarray_ap;\
167  if( zArrayPosIsValid(arr,pos) ){\
168  if( (pos) < zArraySize(arr)-1 )\
169  memmove( zArrayElemNC(arr,pos), zArrayElemNC(arr,(pos)+1), sizeof(type)*(zArraySize(arr)-(pos)-1) );\
170  if( zArraySize(arr) > 1 ){\
171  __zarray_ap = zRealloc( zArrayBuf(arr), type, zArraySize(arr)-1 );\
172  if( __zarray_ap == NULL )\
173  ZALLOCERROR();\
174  else{\
175  zArraySize(arr)--;\
176  zArrayBuf(arr) = __zarray_ap;\
177  }\
178  } else{\
179  zArrayFree( arr );\
180  }\
181  } else{\
182  ZRUNWARN( "invalid position %d/%d in array specified", pos, zArraySize(arr)-1 );\
183  }\
184 } while(0)
185 
192 #define zArrayAppend(arr,subarr,type) do{\
193  type *__zarray_ap;\
194  __zarray_ap = zRealloc( zArrayBuf(arr), type, zArraySize(arr)+zArraySize(subarr) );\
195  if( __zarray_ap == NULL )\
196  ZALLOCERROR();\
197  else{\
198  zArrayBuf(arr) = __zarray_ap;\
199  memcpy( zArrayElemNC(arr,zArraySize(arr)), zArrayBuf(subarr), zArraySize(subarr)*sizeof(type) );\
200  zArraySize(arr) += zArraySize(subarr);\
201  }\
202 } while(0)
203 
204 #endif /* __KERNEL__ */
205 
218 __EXPORT void zQuickSort(void *array, size_t nmemb, size_t size, int (*cmp)(void*,void*,void*), void *priv);
219 
228 #define zArrayQuickSort(arr,cmp,priv) zQuickSort( (void*)zArrayBuf(arr), zArraySize(arr), zArrayElemSize(arr), cmp, priv )
229 
232 /* ********************************************************** *//* ************************************************** */
235 
258 #ifdef __cplusplus
259 #define zArray2Class(array_t,cell_t) \
260 struct array_t{\
261  uint size[2];\
262  cell_t *buf;\
263 }
264 #else
265 #define zArray2Class(array_t,cell_t) \
266 typedef struct{\
267  uint size[2];\
268  cell_t *buf;\
269 } array_t
270 #endif /* __cplusplus */
271 
272 #define zArray2Size(a,i) (a)->size[i]
273 #define zArray2RowSize(a) (a)->size[0]
274 #define zArray2ColSize(a) (a)->size[1]
275 #define zArray2Buf(a) ( (a)->buf )
276 
277 #define zArray2PosIsValid(a,r,c) ( (r) < zArray2RowSize(a) && (r) >= 0 && (c) < zArray2ColSize(a) && (c) >= 0 )
278 
279 /* NOTE: do not use the following macro before allocating buffer */
280 #define zArray2ElemSize(a) sizeof(*zArray2Buf(a))
281 
282 #define zArray2ElemNC(a,i,j) ( &zArray2Buf(a)[(i)*zArray2ColSize(a) + (j)] )
283 #define zArray2Elem(a,i,j) ( zArray2PosIsValid(a,i,j) ? zArray2ElemNC(a,i,j) : NULL )
284 #define zArray2SetElemNC(a,i,j,d) memcpy( zArray2ElemNC(a,i,j), (d), zArray2ElemSize(a) )
285 #define zArray2SetElem(a,i,j,d) ( zArray2PosIsValid(a,i,j) ? zArray2SetElemNC(a,i,j,d) : NULL )
286 
287 #define zArray2Init(arr) do{\
288  zArray2RowSize(arr) = zArray2ColSize(arr) = 0;\
289  zArray2Buf(arr) = NULL;\
290 } while(0)
291 
296 #define zArray2Alloc(arr,type,r,c) do{\
297  zArray2Init( arr );\
298  if( (r) > 0 && (c) > 0 && !( zArray2Buf(arr) = zAlloc(type,(r)*(c)) ) ){\
299  ZALLOCERROR();\
300  zArray2RowSize(arr) = 0;\
301  zArray2ColSize(arr) = 0;\
302  } else{\
303  zArray2RowSize(arr) = (r);\
304  zArray2ColSize(arr) = (c);\
305  }\
306 } while(0)
307 
311 #define zArray2Free(arr) do{\
312  free( zArray2Buf(arr) );\
313  zArray2Init( arr );\
314 } while(0)
315 
319 
320 #endif /* __ZEDA_ARRAY_H__ */
#define __END_DECLS
Definition: zeda_defs.h:30
miscellanies.
#define __BEGIN_DECLS
Definition: zeda_defs.h:26
common named class.
#define __EXPORT
Definition: zeda_compat.h:32
void zQuickSort(void *array, size_t nmemb, size_t size, int(*cmp)(void *, void *, void *), void *priv)
quick sort for a pointer array.