From 58c8a2f75228d2143b107adc0caeab977adaa073 Mon Sep 17 00:00:00 2001 From: ppisa Date: Fri, 19 Jun 2009 11:00:32 +0000 Subject: [PATCH 1/1] Added support for static GSA arrays with insert/delete functionality. Such array can utilize preallocated items pointers array. This means, that GSA_STATIC_CUST_DEC can be used with limited insert/delete functionality. The GSA_CONST_CUST_DEC should be used for strictly constant arrays with compile time filled data instead. --- ulut/ul_gsa.h | 41 ++++++++++++++++++++++++++++++++++--- ulut/ul_gsacust.h | 51 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 86 insertions(+), 6 deletions(-) diff --git a/ulut/ul_gsa.h b/ulut/ul_gsa.h index a3a6dfd..26ebe79 100644 --- a/ulut/ul_gsa.h +++ b/ulut/ul_gsa.h @@ -315,8 +315,8 @@ cust_prefix##_find_after_it(cust_array_t *container, cust_key_t const *key, cust return (it->indx=cust_prefix##_find_after_indx(container, key))!=(unsigned)-1;\ } -/* Declaration of new static custom array without support of runtime modifications */ -#define GSA_STATIC_CUST_DEC(cust_prefix, cust_array_t, cust_item_t, cust_key_t,\ +/* Declaration of new const custom array without support of runtime modifications */ +#define GSA_CONST_CUST_DEC(cust_prefix, cust_array_t, cust_item_t, cust_key_t,\ cust_array_field, cust_item_key, cust_cmp_fnc) \ \ GSA_BASE_CUST_DEC(cust_prefix, cust_array_t, cust_item_t, cust_key_t,\ @@ -324,7 +324,7 @@ GSA_BASE_CUST_DEC(cust_prefix, cust_array_t, cust_item_t, cust_key_t,\ \ GSA_IT_CUST_DEC(cust_prefix, const cust_array_t, cust_item_t, cust_key_t) -/*** Declaration of dynamic custom array with ***/ +/*** Declaration of dynamic custom array with full functions ***/ #define GSA_CUST_DEC(cust_prefix, cust_array_t, cust_item_t, cust_key_t,\ cust_array_field, cust_item_key, cust_cmp_fnc) \ \ @@ -374,6 +374,41 @@ cust_prefix##_delete_it(cust_prefix##_it_t *it)\ cust_prefix##_delete_at(it->container,it->indx);\ }\ +/*** Declaration of static custom array with limited functions ***/ +#define GSA_STATIC_CUST_DEC(cust_prefix, cust_array_t, cust_item_t, cust_key_t,\ + cust_array_field, cust_item_key, cust_cmp_fnc) \ +\ +GSA_BASE_CUST_DEC(cust_prefix, cust_array_t, cust_item_t, cust_key_t,\ + cust_array_field, cust_item_key, cust_cmp_fnc) \ +\ +int cust_prefix##_insert(cust_array_t *array, cust_item_t *item);\ +int cust_prefix##_delete(cust_array_t *array, const cust_item_t *item);\ +void cust_prefix##_init_array_field(cust_array_t *array);\ +int cust_prefix##_insert_at(cust_array_t *array, cust_item_t *item, unsigned indx);\ +int cust_prefix##_delete_at(cust_array_t *array, unsigned indx); \ +\ +static inline void \ +cust_prefix##_delete_all(cust_array_t *array)\ +{\ + cust_prefix##_init_array_field(array);\ +}\ +\ +static inline cust_item_t *\ +cust_prefix##_cut_last(cust_array_t *array)\ +{\ + if(cust_prefix##_is_empty(array)) return NULL;\ + return (cust_item_t *)array->cust_array_field.items\ + [--array->cust_array_field.count];\ +}\ +/*** Iterators ***/\ +GSA_IT_CUST_DEC(cust_prefix, cust_array_t, cust_item_t, cust_key_t) \ +\ +static inline void \ +cust_prefix##_delete_it(cust_prefix##_it_t *it)\ +{\ + cust_prefix##_delete_at(it->container,it->indx);\ +}\ + /* The next implementation of foreaach is elegant, but can not be used in C99 non-conformant C compiler */ diff --git a/ulut/ul_gsacust.h b/ulut/ul_gsacust.h index 68826a8..728bd03 100644 --- a/ulut/ul_gsacust.h +++ b/ulut/ul_gsacust.h @@ -30,8 +30,8 @@ extern "C" { #endif -/* Static version of custom GSA arrays. It does not support runtime modifications. */ -#define GSA_STATIC_CUST_IMP(cust_prefix, cust_array_t, cust_item_t, cust_key_t,\ +/* Constant version of custom GSA arrays. It does not support runtime modifications. */ +#define GSA_CONST_CUST_IMP(cust_prefix, cust_array_t, cust_item_t, cust_key_t,\ cust_array_field, cust_item_key, cust_cmp_fnc, cust_ins_fl) \ \ int \ @@ -82,7 +82,7 @@ cust_prefix##_bsearch_indx(const cust_array_t *array, cust_key_t const *key, \ #define GSA_CUST_IMP(cust_prefix, cust_array_t, cust_item_t, cust_key_t,\ cust_array_field, cust_item_key, cust_cmp_fnc, cust_ins_fl) \ \ -GSA_STATIC_CUST_IMP(cust_prefix, cust_array_t, cust_item_t, cust_key_t,\ +GSA_CONST_CUST_IMP(cust_prefix, cust_array_t, cust_item_t, cust_key_t,\ cust_array_field, cust_item_key, cust_cmp_fnc, cust_ins_fl) \ \ int \ @@ -109,6 +109,51 @@ cust_prefix##_delete(cust_array_t *array, const cust_item_t *item)\ } +/* Static version with limited support of insert and delete functions. */ +#define GSA_STATIC_CUST_IMP(cust_prefix, cust_array_t, cust_item_t, cust_key_t,\ + cust_array_field, cust_item_key, cust_cmp_fnc, cust_ins_fl) \ +\ +GSA_CUST_IMP(cust_prefix, cust_array_t, cust_item_t, cust_key_t,\ + cust_array_field, cust_item_key, cust_cmp_fnc, cust_ins_fl) \ +\ +void \ +cust_prefix##_init_array_field(cust_array_t *array)\ +{\ + array->cust_array_field.count=0;\ + array->cust_array_field.alloc_count=0;\ + array->cust_array_field.items=NULL;\ +}\ +\ +int \ +cust_prefix##_insert_at(cust_array_t *array, cust_item_t *item, unsigned indx)\ +{\ + unsigned cnt=array->cust_array_field.count; \ + uloi_ciddes_t **p; \ +\ + if(indx>cnt) indx=cnt;\ + if(cnt+1>=array->cust_array_field.alloc_count)\ + return -1;\ +\ + p=array->cust_array_field.items+indx;\ + memmove(p+1,p,(char*)(array->cust_array_field.items+cnt)-(char*)p);\ + array->cust_array_field.count=cnt+1;\ + *p=item;\ + return 0;\ +}\ +\ +int \ +cust_prefix##_delete_at(cust_array_t *array, unsigned indx)\ +{\ + unsigned cnt=array->cust_array_field.count;\ + uloi_ciddes_t **p;\ + if(indx>=cnt) return -1;\ + p=array->cust_array_field.items+indx;\ + array->cust_array_field.count=--cnt;\ + memmove(p,p+1,(array->cust_array_field.items+cnt-p)*sizeof(void *));\ + return 0;\ +}\ + + #ifdef __cplusplus } /* extern "C"*/ #endif -- 2.39.2