]> rtime.felk.cvut.cz Git - ulut.git/commitdiff
Added support for static GSA arrays with insert/delete functionality.
authorppisa <ppisa>
Fri, 19 Jun 2009 11:00:32 +0000 (11:00 +0000)
committerppisa <ppisa>
Fri, 19 Jun 2009 11:00:32 +0000 (11:00 +0000)
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
ulut/ul_gsacust.h

index a3a6dfdaa9f4e2b1728b6c501c48c35371222985..26ebe79fd15de9a427e3f67579fcc7cc224d6cb5 100644 (file)
@@ -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 */
index 68826a8fae0f4ddc2e9a1a355b98fefe1e32bd23..728bd03b037e5bbeb14d962d748974321250b52d 100644 (file)
@@ -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