X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=include%2Flibfirm%2Fadt%2Farray.h;h=64d2275d3959deb4848a554875b5884615369f96;hb=fc906bb2542111be7aa6cfd082f86a84d43f03b4;hp=7a72b9e57b2252c0cf67e0441ac4b17bf6ad5999;hpb=435253f3a8766fec2ac2adf559bb10cfcb5d36f4;p=libfirm diff --git a/include/libfirm/adt/array.h b/include/libfirm/adt/array.h index 7a72b9e57..64d2275d3 100644 --- a/include/libfirm/adt/array.h +++ b/include/libfirm/adt/array.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved. + * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved. * * This file is part of libFirm. * @@ -20,7 +20,7 @@ /** * @file * @brief Dynamic and flexible arrays for C. - * @author Markus Armbruster + * @author Markus Armbruster, Michael Beck, Matthias Braun, Sebastian Hack * @version $Id$ */ #ifndef FIRM_ADT_ARRAY_H @@ -29,6 +29,7 @@ #include #include +#include "firm_config.h" #include "obst.h" #include "fourcc.h" #include "align.h" @@ -51,7 +52,7 @@ * first element of this array). */ #define NEW_ARR_F(type, nelts) \ - ((type *)_new_arr_f ((nelts), sizeof(type) * (nelts))) + ((type *)_new_arr_f((nelts), sizeof(type) * (nelts))) /** * Creates a new flexible array with the same number of elements as a @@ -67,7 +68,7 @@ * first element of this array). */ #define CLONE_ARR_F(type, arr) \ - NEW_ARR_F (type, ARR_LEN ((arr))) + NEW_ARR_F(type, ARR_LEN((arr))) /** * Duplicates an array and returns the new flexible one. @@ -82,14 +83,14 @@ * first element of this array). */ #define DUP_ARR_F(type, arr) \ - memcpy (CLONE_ARR_F (type, (arr)), (arr), sizeof(type) * ARR_LEN((arr))) + memcpy(CLONE_ARR_F(type, (arr)), (arr), sizeof(type) * ARR_LEN((arr))) /** * Delete a flexible array. * * @param arr The flexible array. */ -#define DEL_ARR_F(arr) (_del_arr_f ((arr))) +#define DEL_ARR_F(arr) (_del_arr_f((void *)(arr))) /** * Creates a dynamic array on an obstack. @@ -106,7 +107,7 @@ */ #define NEW_ARR_D(type, obstack, nelts) \ ( nelts \ - ? (type *)_new_arr_d ((obstack), (nelts), sizeof(type) * (nelts)) \ + ? (type *)_new_arr_d((obstack), (nelts), sizeof(type) * (nelts)) \ : (type *)arr_mt_descr.v.elts) /** @@ -124,7 +125,7 @@ * first element of this array). */ #define CLONE_ARR_D(type, obstack, arr) \ - NEW_ARR_D (type, (obstack), ARR_LEN ((arr))) + NEW_ARR_D(type, (obstack), ARR_LEN((arr))) /** * Duplicates an array and returns the new dynamic one. @@ -140,7 +141,7 @@ * first element of this array). */ #define DUP_ARR_D(type, obstack, arr) \ - memcpy (CLONE_ARR_D (type, (obstack), (arr)), (arr), sizeof(type) * ARR_LEN ((arr))) + memcpy(CLONE_ARR_D(type, (obstack), (arr)), (arr), sizeof(type) * ARR_LEN ((arr))) /** * Create an automatic array which will be deleted at return from function. @@ -156,10 +157,10 @@ #define NEW_ARR_A(type, var, n) \ do { \ int _nelts = (n); \ - assert (_nelts >= 0); \ - (var) = (void *)((_arr_descr *)alloca (_ARR_ELTS_OFFS + sizeof(type) * _nelts))->v.elts; \ - _ARR_SET_DBGINF (_ARR_DESCR ((var)), ARR_A_MAGIC, sizeof (type)); \ - (void)(_ARR_DESCR ((var))->nelts = _nelts); \ + assert(_nelts >= 0); \ + (var) = (void *)((_arr_descr *)alloca(_ARR_ELTS_OFFS + sizeof(type) * _nelts))->v.elts; \ + _ARR_SET_DBGINF(_ARR_DESCR ((var)), ARR_A_MAGIC, sizeof (type)); \ + (void)(_ARR_DESCR((var))->nelts = _nelts); \ } while (0) /** @@ -177,7 +178,7 @@ * first element of this array). */ #define CLONE_ARR_A(type, var, arr) \ - NEW_ARR_A (type, (var), ARR_LEN ((arr))) + NEW_ARR_A(type, (var), ARR_LEN((arr))) /** * Duplicates an array and returns a new automatic one. @@ -194,7 +195,7 @@ */ #define DUP_ARR_A(type, var, arr) \ do { CLONE_ARR_A(type, (var), (arr)); \ - memcpy ((var), (arr), sizeof (type) * ARR_LEN ((arr))); } \ + memcpy((var), (arr), sizeof (type) * ARR_LEN((arr))); } \ while (0) /** @@ -207,7 +208,7 @@ */ #define DECL_ARR_S(type, var, _nelts) \ ARR_STRUCT(type, (_nelts) ? (_nelts) : 1) _##var; \ - type *var = (_ARR_SET_DBGINF (&_##var, ARR_A_MAGIC, sizeof (type)), \ + type *var = (_ARR_SET_DBGINF(&_##var, ARR_A_MAGIC, sizeof (type)), \ _##var.nelts = _nelts, \ _##var.v.elts) @@ -216,7 +217,7 @@ * * @param arr a flexible, dynamic, automatic or static array. */ -#define ARR_LEN(arr) (ARR_VRFY ((arr)), _ARR_DESCR((arr))->nelts) +#define ARR_LEN(arr) (ARR_VRFY((arr)), _ARR_DESCR((arr))->nelts) /** * Resize a flexible array, allocate more data if needed but do NOT @@ -229,7 +230,7 @@ * @remark This macro may change arr, so update all references! */ #define ARR_RESIZE(type, arr, n) \ - ((arr) = _arr_resize ((arr), (n), sizeof(type))) + ((arr) = _arr_resize((void *)(arr), (n), sizeof(type))) /** * Resize a flexible array, always reallocate data. @@ -241,12 +242,12 @@ * @remark This macro may change arr, so update all references! */ #define ARR_SETLEN(type, arr, n) \ - ((arr) = _arr_setlen ((arr), (n), sizeof(type) * (n))) + ((arr) = _arr_setlen((void *)(arr), (n), sizeof(type) * (n))) /** Set a length smaller than the current length of the array. Do not * resize. len must be <= ARR_LEN(arr). */ #define ARR_SHRINKLEN(arr,len) \ - (ARR_VRFY ((arr)), assert(_ARR_DESCR((arr))->nelts >= len), \ + (ARR_VRFY((arr)), assert(_ARR_DESCR((arr))->nelts >= len), \ _ARR_DESCR((arr))->nelts = len) /** @@ -259,7 +260,7 @@ * @remark This macro may change arr, so update all references! */ #define ARR_EXTEND(type, arr, delta) \ - ARR_RESIZE (type, (arr), ARR_LEN ((arr)) + (delta)) + ARR_RESIZE(type, (arr), ARR_LEN((arr)) + (delta)) /** * Resize a flexible array to hold n elements only if it is currently shorter @@ -272,7 +273,7 @@ * @remark This macro may change arr, so update all references! */ #define ARR_EXTO(type, arr, n) \ - ((n) >= ARR_LEN ((arr)) ? ARR_RESIZE (type, (arr), (n)+1) : (arr)) + ((n) >= ARR_LEN((arr)) ? ARR_RESIZE(type, (arr), (n)+1) : (arr)) /** * Append one element to a flexible array. @@ -282,8 +283,7 @@ * @param elt The new element, must be of type (type). */ #define ARR_APP1(type, arr, elt) \ - (ARR_EXTEND (type, (arr), 1), (arr)[ARR_LEN ((arr))-1] = (elt)) - + (ARR_EXTEND(type, (arr), 1), (arr)[ARR_LEN((arr))-1] = (elt)) #ifdef NDEBUG # define ARR_VRFY(arr) ((void)0) @@ -297,7 +297,7 @@ || (_ARR_DESCR((arr))->u.allocated >= _ARR_DESCR((arr))->nelts)) \ && (_ARR_DESCR((arr))->nelts >= 0)) # define ARR_IDX_VRFY(arr, idx) \ - assert ((0 <= (idx)) && ((idx) < ARR_LEN ((arr)))) + assert((0 <= (idx)) && ((idx) < ARR_LEN((arr)))) #endif @@ -333,17 +333,123 @@ /** * The array descriptor header type. */ -typedef ARR_STRUCT (aligned_type, 1) _arr_descr; +typedef ARR_STRUCT(aligned_type, 1) _arr_descr; extern _arr_descr arr_mt_descr; -void *_new_arr_f (int, size_t); -void _del_arr_f (void *); -void *_new_arr_d (struct obstack *obstack, int nelts, size_t elts_size); -void *_arr_resize (void *, int, size_t); -void *_arr_setlen (void *, int, size_t); +void *_new_arr_f(int nelts, size_t elts_size); +void _del_arr_f(void *elts); +void *_new_arr_d(struct obstack *obstack, int nelts, size_t elts_size); +void *_arr_resize(void *elts, int nelts, size_t elts_size); +void *_arr_setlen(void *elts, int nelts, size_t elts_size); -#define _ARR_ELTS_OFFS offsetof (_arr_descr, v.elts) +#define _ARR_ELTS_OFFS offsetof(_arr_descr, v.elts) #define _ARR_DESCR(elts) ((_arr_descr *)(void *)((char *)(elts) - _ARR_ELTS_OFFS)) +/* + ____ _ _ _ +/ ___| ___ _ __| |_ ___ __| | / \ _ __ _ __ __ _ _ _ ___ +\___ \ / _ \| '__| __/ _ \/ _` | / _ \ | '__| '__/ _` | | | / __| + ___) | (_) | | | || __/ (_| | / ___ \| | | | | (_| | |_| \__ \ +|____/ \___/|_| \__\___|\__,_| /_/ \_\_| |_| \__,_|\__, |___/ + |___/ +*/ + +typedef int (_arr_cmp_func_t)(const void *a, const void *b); + +/** + * Do a binary search in an array. + * @param arr The array. + * @param elm_size The size of an array element. + * @param cmp A comparison function for two array elements (see qsort(3) for example). + * @param elm A pointer to the element we are looking for. + * @return This is somewhat tricky. Let res be the return value. + * If the return value is negative, then elm was not in the array + * but -res - 1 gives the proper location where it should be inserted. + * If res >= 0 then the element is in the array and res + * represents its index. + * That allows for testing membership and finding proper insertion indices. + * @note The differences to bsearch(3) which does not give proper insert locations + * in the case that the element is not conatined in the array. + */ +static INLINE __attribute__((const, unused)) int +_arr_bsearch(const void *arr, size_t elm_size, _arr_cmp_func_t *cmp, const void *elm) +{ + int hi = ARR_LEN(arr); + int lo = 0; + + while(lo < hi) { + int md = lo + ((hi - lo) >> 1); + int res = cmp((char *) arr + md * elm_size, elm); + if(res < 0) + lo = md + 1; + else if(res > 0) + hi = md; + else + return md; + } + + return -(lo + 1); +} + +#define ARR_SET_INSERT(arr, cmp, elm) \ +do { \ + int idx = _arr_bsearch((arr), sizeof((arr)[0]), (cmp), (elm)); \ + if (idx < 0) { \ + idx = -idx - 1; \ + memmove(&(arr)[idx+1], &(arr)[idx], sizeof((arr)[0]) * (_ARR_DESCR((arr))->nelts - idx)); \ + (arr)[idx] = *(elm); \ + ++_ARR_DESCR((arr))->nelts; \ + } \ +} while(0) + +#define ARR_SET_INSERT_EXT(type, arr, cmp, elm) \ +do { \ + int idx = _arr_bsearch((arr), sizeof((arr)[0]), (cmp), (elm)); \ + if (idx < 0) { \ + int len = ARR_LEN(arr); \ + idx = -idx - 1; \ + ARR_EXTO(type, arr, len + 1); \ + memmove(&(arr)[idx+1], &(arr)[idx], sizeof((arr)[0]) * (len - idx)); \ + (arr)[idx] = *(elm); \ + } \ +} while(0) + +#define ARR_SET_REMOVE(arr, cmp, elm) \ +do { \ + int idx = _arr_bsearch((arr), sizeof((arr)[0]), (cmp), (elm)); \ + if (idx >= 0) { \ + --_ARR_DESCR((arr))->nelts; \ + memmove(&(arr)[idx], &(arr)[idx+1], sizeof((arr)[0]) * (_ARR_DESCR((arr))->nelts - idx)); \ + } \ +} while(0) + +/** + * Return the index of an element in an array set. + * To check for containment, use the expression: + * (ARR_SET_GET_IDX(arr, cmp, elm) >= 0) + * + * @return The index or some value < 0 if the element was not in the set. + */ +#define ARR_SET_GET_IDX(arr, cmp, elm) \ + (ARR_VRFY((arr)), _arr_bsearch((arr), sizeof((arr)[0]), cmp, (elm))) + +#ifdef __GNUC__ +#define ARR_SET_GET(arr, cmp, elm) \ + ({ int idx = ARR_SET_GET_IDX(arr, cmp, elm); idx >= 0 ? &(arr)[idx] : NULL; }) +#else +#define ARR_SET_GET(arr, cmp, elm) \ + (ARR_SET_GET_IDX(arr, cmp, elm) >= 0 ? &(arr)[ARR_SET_GET_IDX(arr, cmp, elm)] : NULL) +#endif + + +#define ARR_SET_CONTAINS(arr, cmp, elm) \ + (ARR_SET_GET_IDX((arr), (cmp), (elm)) >= 0) + +/** + * Reset the array set. + * This just initializes the size to zero but does not wipe out any element. + */ +#define ARR_SET_CLEAR(arr) ARR_SHRINKLEN(arr, 0) + #endif /* FIRM_ADT_ARRAY_H */