* add some DEBUG capability in the pseudo axes auto file to ease the debugging.
[hkl.git] / hkl / hkl-list.h
blob597230eb6b2888280bd43e43db92e453afed1a56
1 /* This file is part of the hkl library.
3 * The hkl library is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * The hkl library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with the hkl library. If not, see <http://www.gnu.org/licenses/>.
16 * Copyright (C) 2003-2010 Synchrotron SOLEIL
17 * L'Orme des Merisiers Saint-Aubin
18 * BP 48 91192 GIF-sur-YVETTE CEDEX
20 * Authors: Picca Frédéric-Emmanuel <picca@synchrotron-soleil.fr>
22 #ifndef __HKL_LIST_H__
23 #define __HKL_LIST_H__
25 #include <stdlib.h>
26 #include <hkl/hkl-macros.h>
28 HKL_BEGIN_DECLS
30 #define HKL_LIST_LEN(array) array ## _len
32 #define HKL_LIST(type, name) type *name; size_t HKL_LIST_LEN(name)
34 #define HKL_LIST_INIT(array) array = NULL, HKL_LIST_LEN(array) = 0
36 #define HKL_LIST_ALLOC(array, len) do{ \
37 array = malloc((len) * sizeof(*array)); \
38 HKL_LIST_LEN(array) = (len); \
39 }while(0)
41 #define HKL_LIST_COPY(dst, src) memcpy(dst, src, HKL_LIST_LEN(src) * sizeof(*src))
43 #define HKL_LIST_FREE(array) free(array), HKL_LIST_INIT(array)
45 #define HKL_LIST_FREE_DESTRUCTOR(array, destructor) do{ \
46 if(HKL_LIST_LEN(array)){ \
47 size_t i; \
48 for(i=0; i<HKL_LIST_LEN(array); ++i) \
49 destructor(array[i]); \
50 } \
51 HKL_LIST_FREE(array); \
52 }while(0)
54 #define HKL_LIST_RESIZE(array, len) do{ \
55 array = realloc(array, (len) * sizeof(*array)); \
56 HKL_LIST_LEN(array) = (len); \
57 }while(0)
59 #define HKL_LIST_ADD_VALUE(array, value) do{ \
60 size_t len = HKL_LIST_LEN(array); \
61 HKL_LIST_RESIZE(array, len + 1); \
62 array[len] = value; \
63 }while(0)
65 #define HKL_LIST_DEL(array, idx) do{ \
66 HKL_LIST_LEN(array) = HKL_LIST_LEN(array) - 1; \
67 if (idx < HKL_LIST_LEN(array)) \
68 memmove(&array[idx], &array[idx] + 1, sizeof(*array) * (HKL_LIST_LEN(array) - idx)); \
69 }while(0)
71 #define HKL_LIST_DEL_DESTRUCTOR(array, idx, destructor) do{ \
72 destructor(array[idx]); \
73 HKL_LIST_DEL(array, idx); \
74 }while(0)
77 #define HKL_LIST_DEL_ITEM_DESTRUCTOR(array, item, destructor) do{ \
78 size_t i; \
79 for(i=0; i<HKL_LIST_LEN(array); ++i) \
80 if(array[i] == item){ \
81 destructor(array[i]); \
82 HKL_LIST_DEL(array, i); \
83 } \
84 }while(0)
86 HKL_END_DECLS
88 #endif