isl_*_list_add: dynamically grow list to accomodate extra element
[platform/upstream/isl.git] / isl_list_templ.c
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  * Copyright 2011      INRIA Saclay
4  *
5  * Use of this software is governed by the GNU LGPLv2.1 license
6  *
7  * Written by Sven Verdoolaege, K.U.Leuven, Departement
8  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9  * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10  * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
11  */
12
13 #define xCAT(A,B) A ## B
14 #define CAT(A,B) xCAT(A,B)
15 #undef EL
16 #define EL CAT(isl_,BASE)
17 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
18 #define FN(TYPE,NAME) xFN(TYPE,NAME)
19 #define xLIST(EL) EL ## _list
20 #define LIST(EL) xLIST(EL)
21
22 isl_ctx *FN(LIST(EL),get_ctx)(__isl_keep LIST(EL) *list)
23 {
24         return list ? list->ctx : NULL;
25 }
26
27 __isl_give LIST(EL) *FN(LIST(EL),alloc)(isl_ctx *ctx, int n)
28 {
29         LIST(EL) *list;
30
31         if (n < 0)
32                 isl_die(ctx, isl_error_invalid,
33                         "cannot create list of negative length",
34                         return NULL);
35         list = isl_alloc(ctx, LIST(EL),
36                          sizeof(LIST(EL)) + (n - 1) * sizeof(struct EL *));
37         if (!list)
38                 return NULL;
39
40         list->ctx = ctx;
41         isl_ctx_ref(ctx);
42         list->ref = 1;
43         list->size = n;
44         list->n = 0;
45         return list;
46 }
47
48 __isl_give LIST(EL) *FN(LIST(EL),copy)(__isl_keep LIST(EL) *list)
49 {
50         if (!list)
51                 return NULL;
52
53         list->ref++;
54         return list;
55 }
56
57 __isl_give LIST(EL) *FN(LIST(EL),dup)(__isl_keep LIST(EL) *list)
58 {
59         int i;
60         LIST(EL) *dup;
61
62         if (!list)
63                 return NULL;
64
65         dup = FN(LIST(EL),alloc)(FN(LIST(EL),get_ctx)(list), list->n);
66         if (!dup)
67                 return NULL;
68         for (i = 0; i < list->n; ++i)
69                 dup = FN(LIST(EL),add)(dup, FN(EL,copy)(list->p[i]));
70         return dup;
71 }
72
73 __isl_give LIST(EL) *FN(LIST(EL),cow)(__isl_take LIST(EL) *list)
74 {
75         if (!list)
76                 return NULL;
77
78         if (list->ref == 1)
79                 return list;
80         list->ref--;
81         return FN(LIST(EL),dup)(list);
82 }
83
84 /* Make sure "list" has room for at least "n" more pieces.
85  *
86  * If there is only one reference to list, we extend it in place.
87  * Otherwise, we create a new LIST(EL) and copy the elements.
88  */
89 static __isl_give LIST(EL) *FN(LIST(EL),grow)(__isl_take LIST(EL) *list, int n)
90 {
91         isl_ctx *ctx;
92         int i, new_size;
93         LIST(EL) *res;
94
95         if (!list)
96                 return NULL;
97         if (list->n + n <= list->size)
98                 return list;
99
100         ctx = FN(LIST(EL),get_ctx)(list);
101         new_size = ((list->n + n + 1) * 3) / 2;
102         if (list->ref == 1) {
103                 res = isl_realloc(ctx, list, LIST(EL),
104                             sizeof(LIST(EL)) + (new_size - 1) * sizeof(EL *));
105                 if (!res)
106                         return FN(LIST(EL),free)(list);
107                 res->size = new_size;
108                 return res;
109         }
110
111         res = FN(LIST(EL),alloc)(ctx, new_size);
112         if (!res)
113                 return FN(LIST(EL),free)(list);
114
115         for (i = 0; i < list->n; ++i)
116                 res = FN(LIST(EL),add)(res, FN(EL,copy)(list->p[i]));
117
118         FN(LIST(EL),free)(list);
119         return res;
120 }
121
122 __isl_give LIST(EL) *FN(LIST(EL),add)(__isl_take LIST(EL) *list,
123         __isl_take struct EL *el)
124 {
125         list = FN(LIST(EL),grow)(list, 1);
126         if (!list || !el)
127                 goto error;
128         list->p[list->n] = el;
129         list->n++;
130         return list;
131 error:
132         FN(EL,free)(el);
133         FN(LIST(EL),free)(list);
134         return NULL;
135 }
136
137 void *FN(LIST(EL),free)(__isl_take LIST(EL) *list)
138 {
139         int i;
140
141         if (!list)
142                 return NULL;
143
144         if (--list->ref > 0)
145                 return NULL;
146
147         isl_ctx_deref(list->ctx);
148         for (i = 0; i < list->n; ++i)
149                 FN(EL,free)(list->p[i]);
150         free(list);
151
152         return NULL;
153 }
154
155 int FN(FN(LIST(EL),n),BASE)(__isl_keep LIST(EL) *list)
156 {
157         return list ? list->n : 0;
158 }
159
160 __isl_give EL *FN(FN(LIST(EL),get),BASE)(__isl_keep LIST(EL) *list, int index)
161 {
162         if (!list)
163                 return NULL;
164         if (index < 0 || index >= list->n)
165                 isl_die(list->ctx, isl_error_invalid,
166                         "index out of bounds", return NULL);
167         return FN(EL,copy)(list->p[index]);
168 }
169
170 /* Replace the element at position "index" in "list" by "el".
171  */
172 __isl_give LIST(EL) *FN(FN(LIST(EL),set),BASE)(__isl_take LIST(EL) *list,
173         int index, __isl_take EL *el)
174 {
175         if (!list || !el)
176                 goto error;
177         if (index < 0 || index >= list->n)
178                 isl_die(list->ctx, isl_error_invalid,
179                         "index out of bounds", goto error);
180         if (list->p[index] == el) {
181                 FN(EL,free)(el);
182                 return list;
183         }
184         list = FN(LIST(EL),cow)(list);
185         if (!list)
186                 goto error;
187         FN(EL,free)(list->p[index]);
188         list->p[index] = el;
189         return list;
190 error:
191         FN(EL,free)(el);
192         FN(LIST(EL),free)(list);
193         return NULL;
194 }
195
196 int FN(LIST(EL),foreach)(__isl_keep LIST(EL) *list,
197         int (*fn)(__isl_take EL *el, void *user), void *user)
198 {
199         int i;
200
201         if (!list)
202                 return -1;
203
204         for (i = 0; i < list->n; ++i) {
205                 EL *el = FN(EL,copy(list->p[i]));
206                 if (!el)
207                         return -1;
208                 if (fn(el, user) < 0)
209                         return -1;
210         }
211
212         return 0;
213 }
214
215 __isl_give LIST(EL) *FN(FN(LIST(EL),from),BASE)(__isl_take EL *el)
216 {
217         isl_ctx *ctx;
218         LIST(EL) *list;
219
220         if (!el)
221                 return NULL;
222         ctx = FN(EL,get_ctx)(el);
223         list = FN(LIST(EL),alloc)(ctx, 1);
224         if (!list)
225                 goto error;
226         list = FN(LIST(EL),add)(list, el);
227         return list;
228 error:
229         FN(EL,free)(el);
230         return NULL;
231 }
232
233 __isl_give LIST(EL) *FN(LIST(EL),concat)(__isl_take LIST(EL) *list1,
234         __isl_take LIST(EL) *list2)
235 {
236         int i;
237         isl_ctx *ctx;
238         LIST(EL) *res;
239
240         if (!list1 || !list2)
241                 goto error;
242
243         ctx = FN(LIST(EL),get_ctx)(list1);
244         res = FN(LIST(EL),alloc)(ctx, list1->n + list2->n);
245         for (i = 0; i < list1->n; ++i)
246                 res = FN(LIST(EL),add)(res, FN(EL,copy)(list1->p[i]));
247         for (i = 0; i < list2->n; ++i)
248                 res = FN(LIST(EL),add)(res, FN(EL,copy)(list2->p[i]));
249
250         FN(LIST(EL),free)(list1);
251         FN(LIST(EL),free)(list2);
252         return res;
253 error:
254         FN(LIST(EL),free)(list1);
255         FN(LIST(EL),free)(list2);
256         return NULL;
257 }
258
259 __isl_give isl_printer *CAT(isl_printer_print_,LIST(BASE))(
260         __isl_take isl_printer *p, __isl_keep LIST(EL) *list)
261 {
262         int i;
263
264         if (!p || !list)
265                 goto error;
266         p = isl_printer_print_str(p, "(");
267         for (i = 0; i < list->n; ++i) {
268                 if (i)
269                         p = isl_printer_print_str(p, ",");
270                 p = CAT(isl_printer_print_,BASE)(p, list->p[i]);
271         }
272         p = isl_printer_print_str(p, ")");
273         return p;
274 error:
275         isl_printer_free(p);
276         return NULL;
277 }
278
279 void FN(LIST(EL),dump)(__isl_keep LIST(EL) *list)
280 {
281         isl_printer *printer;
282
283         if (!list)
284                 return;
285
286         printer = isl_printer_to_file(FN(LIST(EL),get_ctx)(list), stderr);
287         printer = CAT(isl_printer_print_,LIST(BASE))(printer, list);
288         printer = isl_printer_end_line(printer);
289
290         isl_printer_free(printer);
291 }