1 /*******************************************************************
2 uLan Utilities Library - C library of basic reusable constructions
4 ul_gsa.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 *******************************************************************/
27 #include "ul_utdefs.h"
28 #include "ul_itbase.h"
38 /* function to compare fields of two array items */
39 typedef int gsa_cmp_fnc_t(const void *a, const void *b);
41 /* compare two integer fields */
42 int gsa_cmp_int(const void *a, const void *b);
44 /* compare two unsigned long fields */
45 int gsa_cmp_ulong(const void *a, const void *b);
47 /* define structure representing head of array */
48 #define GSA_ARRAY_FOR(_type) \
52 unsigned alloc_count; \
54 gsa_cmp_fnc_t *cmp_fnc; \
57 /* array type for functions independent on stored type */
58 typedef GSA_ARRAY_FOR(void) gsa_array_t;
60 /* initialize array head structure */
62 gsa_struct_init(gsa_array_t *array, int key_offs,
63 gsa_cmp_fnc_t *cmp_fnc);
65 /* delete all pointers from array */
67 gsa_delete_all(gsa_array_t *array);
69 /* Core binary search routine for GSA arrays
70 searches in "array" for index "indx" of item
71 with value of item field at offset "key_offs"
72 equal to "*key". Values are compared by function
74 Integer "mode" modifies search algorithm
75 0 .. finds index of any item with field value "*key"
76 1 .. finds index of first item with "*key"
77 2 .. index points after last item with "*key" value
78 reworded - index points at first item with higher
79 value of field or after last item
80 Return of nonzerro value indicates match found.
83 gsa_bsearch_indx(gsa_array_t *array, void *key, int key_offs,
84 gsa_cmp_fnc_t *cmp_fnc, int mode, unsigned *indx);
86 /* returns item with key field value equal to "*key" or NULL */
88 gsa_find(gsa_array_t *array, void *key);
90 /* same as above, but first matching item is found */
92 gsa_find_first(gsa_array_t *array, void *key);
94 /* find index "indx" of the first matching item */
96 gsa_find_indx(gsa_array_t *array, void *key, int *indx);
98 /* insert new item at index "where" */
100 gsa_insert_at(gsa_array_t *array, void *item, unsigned where);
102 /* insert new item at the right index,
103 "mode" has same meaning as in "gsa_bsearch_indx"
104 if mode==0 then strict sorting is required
105 and violation result in ignore of new item
109 gsa_insert(gsa_array_t *array, void *item, int mode);
111 /* delete item at index */
113 gsa_delete_at(gsa_array_t *array, unsigned indx);
115 /* delete item from array */
117 gsa_delete(gsa_array_t *array, void *item);
119 /* set new sorting field and function
120 returns 0 if no change needed */
122 gsa_setsort(gsa_array_t *array, int key_offs,
123 gsa_cmp_fnc_t *cmp_fnc);
125 /*===========================================================*/
126 /* Macrodefinitions to prepare custom GSA arrays with */
129 * struct gsa_array_field_t - Structure Representing Anchor of ustom GSA Array
130 * @items: pointer to array of pointers to individual items
131 * @count: number of items in the sorted array
132 * @alloc_count: allocated pointer array capacity
135 typedef struct gsa_array_field_t{
138 unsigned alloc_count;
141 typedef struct gsa_static_array_field_t{
144 } gsa_static_array_field_t;
146 void gsa_cust_init_array_field(gsa_array_field_t *array);
147 int gsa_cust_insert_at(gsa_array_field_t *array, void *item, unsigned where);
148 int gsa_cust_delete_at(gsa_array_field_t *array, unsigned indx);
151 /* User must provide his/her own compare routine with
152 int cust_cmp_fnc(cust_key_t *a, cust_key_t *b) */
154 /*** Base declaration of custom GSA array ***/
155 #define GSA_BASE_CUST_DEC(cust_prefix, cust_array_t, cust_item_t, cust_key_t,\
156 cust_array_field, cust_item_key, cust_cmp_fnc) \
158 static inline cust_item_t * \
159 cust_prefix##_indx2item(const cust_array_t *array, unsigned indx) \
161 if(indx>=array->cust_array_field.count) return NULL;\
162 return (cust_item_t*)array->cust_array_field.items[indx];\
165 static inline cust_key_t *\
166 cust_prefix##_indx2key(const cust_array_t *array, unsigned indx)\
167 { return &(cust_prefix##_indx2item(array, indx)->cust_item_key);}\
169 int cust_prefix##_bsearch_indx(const cust_array_t *array, const cust_key_t *key, int mode, unsigned *indxp);\
171 static inline cust_item_t *\
172 cust_prefix##_at(const cust_array_t *array, unsigned indx)\
174 return cust_prefix##_indx2item(array, indx);\
177 static inline cust_item_t *\
178 cust_prefix##_find(const cust_array_t *array, cust_key_t *key)\
181 if(!cust_prefix##_bsearch_indx(array, key, 0, &indx)) return NULL;\
182 return cust_prefix##_indx2item(array, indx);\
185 static inline cust_item_t *\
186 cust_prefix##_find_first(const cust_array_t *array, cust_key_t *key)\
189 if(!cust_prefix##_bsearch_indx(array, key, GSA_FFIRST, &indx)) return NULL;\
190 return cust_prefix##_indx2item(array, indx);\
193 static inline unsigned \
194 cust_prefix##_find_first_indx(const cust_array_t *array, cust_key_t *key)\
197 if(!cust_prefix##_bsearch_indx(array, key, GSA_FFIRST, &indx)) return -1;\
201 static inline cust_item_t *\
202 cust_prefix##_find_after(const cust_array_t *array, cust_key_t *key)\
205 if(!cust_prefix##_bsearch_indx(array, key, GSA_FAFTER, &indx)) return NULL;\
206 return cust_prefix##_indx2item(array, indx);\
209 static inline unsigned \
210 cust_prefix##_find_after_indx(const cust_array_t *array, cust_key_t *key)\
213 cust_prefix##_bsearch_indx(array, key, GSA_FAFTER, &indx);\
218 cust_prefix##_is_empty(const cust_array_t *array)\
220 return !array->cust_array_field.count;\
223 static inline cust_item_t *\
224 cust_prefix##_first(const cust_array_t *array)\
226 return cust_prefix##_indx2item(array, 0);\
229 static inline unsigned \
230 cust_prefix##_first_indx(const cust_array_t *array)\
235 static inline cust_item_t *\
236 cust_prefix##_last(const cust_array_t *array)\
238 return cust_prefix##_indx2item(array, array->cust_array_field.count-1);\
241 static inline unsigned \
242 cust_prefix##_last_indx(const cust_array_t *array)\
244 return array->cust_array_field.count-1;\
247 /*** Iterators for GSA arrays ***/
248 #define GSA_IT_CUST_DEC(cust_prefix, cust_array_t, cust_item_t, cust_key_t) \
251 cust_array_t *container;\
253 } cust_prefix##_it_t;\
255 static inline cust_item_t *\
256 cust_prefix##_it2item(const cust_prefix##_it_t *it)\
258 return cust_prefix##_indx2item(it->container,it->indx);\
262 cust_prefix##_first_it(cust_array_t *container, cust_prefix##_it_t *it)\
264 it->container=container;\
265 it->indx=cust_prefix##_first_indx(container);\
269 cust_prefix##_last_it(cust_array_t *container, cust_prefix##_it_t *it)\
271 it->container=container;\
272 it->indx=cust_prefix##_last_indx(container);\
276 cust_prefix##_next_it(cust_prefix##_it_t *it)\
278 if(it->indx<=cust_prefix##_last_indx(it->container)) it->indx++;\
279 else it->indx=cust_prefix##_first_indx(it->container);\
283 cust_prefix##_prev_it(cust_prefix##_it_t *it)\
285 if(it->indx<=cust_prefix##_last_indx(it->container)) it->indx--;\
286 else it->indx=cust_prefix##_first_indx(it->container);\
290 cust_prefix##_is_end_it(cust_prefix##_it_t *it)\
292 if(!it->container) return 1;\
293 return it->indx>cust_prefix##_last_indx(it->container);\
297 cust_prefix##_find_it(cust_array_t *container, cust_key_t *key, cust_prefix##_it_t *it)\
299 it->container=container;\
300 return (it->indx=cust_prefix##_find_first_indx(container, key))!=(unsigned)-1;\
304 cust_prefix##_find_first_it(cust_array_t *container, cust_key_t *key, cust_prefix##_it_t *it)\
306 it->container=container;\
307 return (it->indx=cust_prefix##_find_first_indx(container, key))!=(unsigned)-1;\
311 cust_prefix##_find_after_it(cust_array_t *container, cust_key_t *key, cust_prefix##_it_t *it)\
313 it->container=container;\
314 return (it->indx=cust_prefix##_find_after_indx(container, key))!=(unsigned)-1;\
317 /* Declaration of new static custom array without support of runtime modifications */
318 #define GSA_STATIC_CUST_DEC(cust_prefix, cust_array_t, cust_item_t, cust_key_t,\
319 cust_array_field, cust_item_key, cust_cmp_fnc) \
321 GSA_BASE_CUST_DEC(cust_prefix, cust_array_t, cust_item_t, cust_key_t,\
322 cust_array_field, cust_item_key, cust_cmp_fnc) \
324 GSA_IT_CUST_DEC(cust_prefix, const cust_array_t, cust_item_t, cust_key_t)
326 /*** Declaration of dynamic custom array with ***/
327 #define GSA_CUST_DEC(cust_prefix, cust_array_t, cust_item_t, cust_key_t,\
328 cust_array_field, cust_item_key, cust_cmp_fnc) \
330 GSA_BASE_CUST_DEC(cust_prefix, cust_array_t, cust_item_t, cust_key_t,\
331 cust_array_field, cust_item_key, cust_cmp_fnc) \
333 int cust_prefix##_insert(cust_array_t *array, cust_item_t *item);\
334 int cust_prefix##_delete(cust_array_t *array, const cust_item_t *item);\
337 cust_prefix##_init_array_field(cust_array_t *array)\
339 gsa_cust_init_array_field(&array->cust_array_field);\
343 cust_prefix##_insert_at(cust_array_t *array, cust_item_t *item, unsigned indx)\
345 return gsa_cust_insert_at(&array->cust_array_field, item, indx);\
349 cust_prefix##_delete_at(cust_array_t *array, unsigned indx)\
351 return gsa_cust_delete_at(&array->cust_array_field, indx);\
354 static inline cust_item_t *\
355 cust_prefix##_cut_last(cust_array_t *array)\
357 if(cust_prefix##_is_empty(array)) return NULL;\
358 return (cust_item_t *)array->cust_array_field.items\
359 [--array->cust_array_field.count];\
362 GSA_IT_CUST_DEC(cust_prefix, cust_array_t, cust_item_t, cust_key_t) \
365 cust_prefix##_delete_it(cust_prefix##_it_t *it)\
367 cust_prefix##_delete_at(it->container,it->indx);\
371 /* The next implementation of foreaach is elegant, but can not
372 be used in C99 non-conformant C compiler */
375 #define gsa_cust_for_each(cust_prefix, array, ptr) \
376 for(unsigned __fe_indx=cust_prefix##_first_indx(array);\
377 (ptr=cust_prefix##_indx2item(array,__fe_indx));\
380 #define gsa_cust_for_each_rev(cust_prefix, array, ptr) \
381 for(unsigned __fe_indx=cust_prefix##_last_indx(array);\
382 (ptr=cust_prefix##_indx2item(array,__fe_indx));\
385 #define gsa_cust_for_each_from(cust_prefix, array, key, ptr) \
386 for(unsigned __fe_indx=cust_prefix##_find_first_indx(array, key); \
387 (ptr=cust_prefix##_indx2item(array,__fe_indx)); \
390 #define gsa_cust_for_each_after(cust_prefix, array, key, ptr) \
391 for(unsigned __fe_indx=cust_prefix##_find_after_indx(array, key); \
392 (ptr=cust_prefix##_indx2item(array,__fe_indx)); \
397 #define gsa_cust_for_each_cut(cust_prefix, array, ptr) \
398 for(;(ptr=cust_prefix##_cut_last(array));)
405 #endif /* _UL_GSA_H */