1 /*******************************************************************
2 uLan Utilities Library - C library of basic reusable constructions
4 ul_gsacust.h - generic sorted arrays
6 (C) Copyright 2001 by Pavel Pisa - Originator
8 The uLan utilities library can be used, copied and modified under
10 - GPL - GNU General Public License
11 - LGPL - GNU Lesser General Public License
12 - MPL - Mozilla Public License
13 - and other licenses added by project originators
14 Code can be modified and re-distributed under any combination
15 of the above listed licenses. If contributor does not agree with
16 some of the licenses, he/she can delete appropriate line.
17 Warning, if you delete all lines, you are not allowed to
18 distribute source code and/or binaries utilizing code.
20 See files COPYING and README for details.
22 *******************************************************************/
33 /* Constant version of custom GSA arrays. It does not support runtime modifications. */
34 #define GSA_CONST_CUST_IMP(cust_prefix, cust_array_t, cust_item_t, cust_key_t,\
35 cust_array_field, cust_item_key, cust_cmp_fnc, cust_ins_fl) \
38 cust_prefix##_bsearch_indx(const cust_array_t *array, cust_key_t const *key, \
39 int mode, unsigned *indx) \
43 if(!array->cust_array_field.items || !array->cust_array_field.count){\
48 b=array->cust_array_field.count;\
51 r=cust_cmp_fnc(cust_prefix##_indx2key(array, c), key);\
52 if(!r) if(!(mode&GSA_FAFTER)) break;\
59 return mode&GSA_FAFTER;\
63 /* equal items can be in range a to b-1 */\
64 /* routine looks for first one */\
68 r=cust_cmp_fnc(cust_prefix##_indx2key(array, c), key);\
81 /* Dynamic version with full support of insert and delete functions. */
82 #define GSA_CUST_IMP(cust_prefix, cust_array_t, cust_item_t, cust_key_t,\
83 cust_array_field, cust_item_key, cust_cmp_fnc, cust_ins_fl) \
85 GSA_CONST_CUST_IMP(cust_prefix, cust_array_t, cust_item_t, cust_key_t,\
86 cust_array_field, cust_item_key, cust_cmp_fnc, cust_ins_fl) \
89 cust_prefix##_insert(cust_array_t *array, cust_item_t *item)\
92 if(cust_prefix##_bsearch_indx(array, &item->cust_item_key, cust_ins_fl, &indx))\
93 if(!cust_ins_fl) return -1;\
94 return cust_prefix##_insert_at(array,item,indx);\
98 cust_prefix##_delete(cust_array_t *array, const cust_item_t *item)\
101 if(!cust_prefix##_bsearch_indx(array, &item->cust_item_key, GSA_FFIRST,&indx))\
103 while(cust_prefix##_indx2item(array, indx)!=item){\
104 if(++indx>=array->cust_array_field.count) return -1;\
105 if(cust_cmp_fnc(cust_prefix##_indx2key(array, indx),\
106 &item->cust_item_key)) return -1;\
108 return cust_prefix##_delete_at(array,indx);\
112 /* Static version with limited support of insert and delete functions. */
113 #define GSA_STATIC_CUST_IMP(cust_prefix, cust_array_t, cust_item_t, cust_key_t,\
114 cust_array_field, cust_item_key, cust_cmp_fnc, cust_ins_fl) \
116 GSA_CUST_IMP(cust_prefix, cust_array_t, cust_item_t, cust_key_t,\
117 cust_array_field, cust_item_key, cust_cmp_fnc, cust_ins_fl) \
120 cust_prefix##_init_array_field(cust_array_t *array)\
122 array->cust_array_field.count=0;\
123 array->cust_array_field.alloc_count=0;\
124 array->cust_array_field.items=NULL;\
128 cust_prefix##_insert_at(cust_array_t *array, cust_item_t *item, unsigned indx)\
130 unsigned cnt=array->cust_array_field.count; \
133 if(indx>cnt) indx=cnt;\
134 if(cnt+1>=array->cust_array_field.alloc_count)\
137 p=array->cust_array_field.items+indx;\
138 memmove(p+1,p,(char*)(array->cust_array_field.items+cnt)-(char*)p);\
139 array->cust_array_field.count=cnt+1;\
145 cust_prefix##_delete_at(cust_array_t *array, unsigned indx)\
147 unsigned cnt=array->cust_array_field.count;\
149 if(indx>=cnt) return -1;\
150 p=array->cust_array_field.items+indx;\
151 array->cust_array_field.count=--cnt;\
152 memmove(p,p+1,(array->cust_array_field.items+cnt-p)*sizeof(void *));\
161 #endif /* _UL_GSACUST_H */