]> rtime.felk.cvut.cz Git - ulut.git/blobdiff - ulut/ul_gsacust.h
Added support for static GSA arrays with insert/delete functionality.
[ulut.git] / ulut / ul_gsacust.h
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