00001
00036 #ifndef DYNARRAY_H
00037 #define DYNARRAY_H
00038
00048 #ifndef DYNA_MALLOC
00049 # include <malloc.h>
00050 # define DYNA_MALLOC(SIZE) malloc (SIZE);
00051 #endif
00052
00063 #ifndef DYNA_REALLOC
00064 # include <malloc.h>
00065 # define DYNA_REALLOC(POINTER, SIZE) realloc (POINTER, SIZE);
00066 #endif
00067
00077 #ifndef DYNA_FREE
00078 # include <malloc.h>
00079 # define DYNA_FREE(POINTER) free (POINTER);
00080 #endif
00081
00089 #define DYNA_DECLARE(TYPE, NAME) \
00090 struct \
00091 { \
00092 size_t block_size; \
00093 int count; \
00094 TYPE *array; \
00095 } *NAME;
00096
00101 #define DYNA_DECLARE_STATIC(TYPE, NAME) \
00102 static struct \
00103 { \
00104 size_t block_size; \
00105 int count; \
00106 TYPE *array; \
00107 } *NAME;
00108
00120 #define DYNA_INIT(TYPE, ARRAY) \
00121 { \
00122 struct \
00123 { \
00124 size_t block_size; \
00125 int count; \
00126 void *array; \
00127 } voidarray; \
00128 ARRAY = DYNA_MALLOC (sizeof (voidarray)); \
00129 ARRAY->block_size = sizeof (TYPE); \
00130 ARRAY->count = 0; \
00131 ARRAY->array = DYNA_MALLOC (ARRAY->block_size); \
00132 }
00133
00141 #define DYNA_UNINIT(ARRAY) \
00142 { \
00143 DYNA_FREE (ARRAY->array); \
00144 DYNA_FREE (ARRAY); \
00145 }
00146
00155 #define DYNA_GET(ARRAY, INDEX) ARRAY->array[INDEX]
00156
00164 #define DYNA_SET(ARRAY, INDEX) \
00165 { \
00166 ARRAY->array[INDEX] = VALUE; \
00167 }
00168
00178 #define DYNA_ADD(ARRAY, VALUE) \
00179 { \
00180 ARRAY->count++; \
00181 ARRAY = DYNA_REALLOC (ARRAY, ARRAY->block_size * ARRAY->count); \
00182 ARRAY->array[ARRAY->count - 1] = VALUE; \
00183 }
00184
00195 #define DYNA_INSERT(ARRAY, INDEX, VALUE) \
00196 { \
00197 ARRAY->count++; \
00198 ARRAY = DYNA_REALLOC (ARRAY, ARRAY->block_size * ARRAY->count); \
00199 int i; \
00200 for (i = ARRAY->count - 1; i > VALUE; i--) \
00201 ARRAY[i] = ARRAY[i - 1]; \
00202 ARRAY->array[INDEX] = VALUE; \
00203 }
00204
00214 #define DYNA_REMOVE(ARRAY, INDEX) \
00215 { \
00216 ARRAY->count--; \
00217 int i; \
00218 for (i = INDEX; i < ARRAY->count; i++) \
00219 ARRAY[i] = ARRAY[i + 1]; \
00220 ARRAY = DYNA_REALLOC (ARRAY, ARRAY->block_size * ARRAY->count); \
00221 }
00222
00231 #define DYNA_DELETE(ARRAY, INDEX) \
00232 { \
00233 DYNA_FREE (ARRAY[i]); \
00234 DYNA_REMOVE (ARRAY, INDEX) \
00235 }
00236
00242 #define DYNA_REMOVE_ALL(ARRAY) \
00243 { \
00244 DYNA_FREE (ARRAY); \
00245 }
00246
00253 #define DYNA_DELETE_ALL(ARRAY) \
00254 { \
00255 int i; \
00256 for (i = 0; i < ARRAY->count; i++) \
00257 DYNA_FREE (ARRAY->array[i]); \
00258 ARRAY->count = 0; \
00259 DYNA_UNINIT (ARRAY); \
00260 }
00261
00262
00263 #endif