add isl_ast_build_{access,call}_from_multi_pw_aff
[isl.git] / isl_list_templ.c
blob71024ce5119df8d856527b0e6ddd81f3ad78e682
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2011 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include <isl_sort.h>
16 #include <isl_tarjan.h>
18 #define xCAT(A,B) A ## B
19 #define CAT(A,B) xCAT(A,B)
20 #undef EL
21 #define EL CAT(isl_,BASE)
22 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
23 #define FN(TYPE,NAME) xFN(TYPE,NAME)
24 #define xLIST(EL) EL ## _list
25 #define LIST(EL) xLIST(EL)
26 #define xS(TYPE,NAME) struct TYPE ## _ ## NAME
27 #define S(TYPE,NAME) xS(TYPE,NAME)
29 isl_ctx *FN(LIST(EL),get_ctx)(__isl_keep LIST(EL) *list)
31 return list ? list->ctx : NULL;
34 __isl_give LIST(EL) *FN(LIST(EL),alloc)(isl_ctx *ctx, int n)
36 LIST(EL) *list;
38 if (n < 0)
39 isl_die(ctx, isl_error_invalid,
40 "cannot create list of negative length",
41 return NULL);
42 list = isl_alloc(ctx, LIST(EL),
43 sizeof(LIST(EL)) + (n - 1) * sizeof(struct EL *));
44 if (!list)
45 return NULL;
47 list->ctx = ctx;
48 isl_ctx_ref(ctx);
49 list->ref = 1;
50 list->size = n;
51 list->n = 0;
52 return list;
55 __isl_give LIST(EL) *FN(LIST(EL),copy)(__isl_keep LIST(EL) *list)
57 if (!list)
58 return NULL;
60 list->ref++;
61 return list;
64 __isl_give LIST(EL) *FN(LIST(EL),dup)(__isl_keep LIST(EL) *list)
66 int i;
67 LIST(EL) *dup;
69 if (!list)
70 return NULL;
72 dup = FN(LIST(EL),alloc)(FN(LIST(EL),get_ctx)(list), list->n);
73 if (!dup)
74 return NULL;
75 for (i = 0; i < list->n; ++i)
76 dup = FN(LIST(EL),add)(dup, FN(EL,copy)(list->p[i]));
77 return dup;
80 __isl_give LIST(EL) *FN(LIST(EL),cow)(__isl_take LIST(EL) *list)
82 if (!list)
83 return NULL;
85 if (list->ref == 1)
86 return list;
87 list->ref--;
88 return FN(LIST(EL),dup)(list);
91 /* Make sure "list" has room for at least "n" more pieces.
93 * If there is only one reference to list, we extend it in place.
94 * Otherwise, we create a new LIST(EL) and copy the elements.
96 static __isl_give LIST(EL) *FN(LIST(EL),grow)(__isl_take LIST(EL) *list, int n)
98 isl_ctx *ctx;
99 int i, new_size;
100 LIST(EL) *res;
102 if (!list)
103 return NULL;
104 if (list->n + n <= list->size)
105 return list;
107 ctx = FN(LIST(EL),get_ctx)(list);
108 new_size = ((list->n + n + 1) * 3) / 2;
109 if (list->ref == 1) {
110 res = isl_realloc(ctx, list, LIST(EL),
111 sizeof(LIST(EL)) + (new_size - 1) * sizeof(EL *));
112 if (!res)
113 return FN(LIST(EL),free)(list);
114 res->size = new_size;
115 return res;
118 res = FN(LIST(EL),alloc)(ctx, new_size);
119 if (!res)
120 return FN(LIST(EL),free)(list);
122 for (i = 0; i < list->n; ++i)
123 res = FN(LIST(EL),add)(res, FN(EL,copy)(list->p[i]));
125 FN(LIST(EL),free)(list);
126 return res;
129 __isl_give LIST(EL) *FN(LIST(EL),add)(__isl_take LIST(EL) *list,
130 __isl_take struct EL *el)
132 list = FN(LIST(EL),grow)(list, 1);
133 if (!list || !el)
134 goto error;
135 list->p[list->n] = el;
136 list->n++;
137 return list;
138 error:
139 FN(EL,free)(el);
140 FN(LIST(EL),free)(list);
141 return NULL;
144 /* Remove the "n" elements starting at "first" from "list".
146 __isl_give LIST(EL) *FN(LIST(EL),drop)(__isl_take LIST(EL) *list,
147 unsigned first, unsigned n)
149 int i;
151 if (!list)
152 return NULL;
153 if (first + n > list->n || first + n < first)
154 isl_die(list->ctx, isl_error_invalid,
155 "index out of bounds", return FN(LIST(EL),free)(list));
156 if (n == 0)
157 return list;
158 list = FN(LIST(EL),cow)(list);
159 if (!list)
160 return NULL;
161 for (i = 0; i < n; ++i)
162 FN(EL,free)(list->p[first + i]);
163 for (i = first; i + n < list->n; ++i)
164 list->p[i] = list->p[i + n];
165 list->n -= n;
166 return list;
169 /* Insert "el" at position "pos" in "list".
171 * If there is only one reference to "list" and if it already has space
172 * for one extra element, we insert it directly into "list".
173 * Otherwise, we create a new list consisting of "el" and copied
174 * elements from "list".
176 __isl_give LIST(EL) *FN(LIST(EL),insert)(__isl_take LIST(EL) *list,
177 unsigned pos, __isl_take struct EL *el)
179 int i;
180 isl_ctx *ctx;
181 LIST(EL) *res;
183 if (!list || !el)
184 goto error;
185 ctx = FN(LIST(EL),get_ctx)(list);
186 if (pos > list->n)
187 isl_die(ctx, isl_error_invalid,
188 "index out of bounds", goto error);
190 if (list->ref == 1 && list->size > list->n) {
191 for (i = list->n - 1; i >= pos; --i)
192 list->p[i + 1] = list->p[i];
193 list->n++;
194 list->p[pos] = el;
195 return list;
198 res = FN(LIST(EL),alloc)(ctx, list->n + 1);
199 for (i = 0; i < pos; ++i)
200 res = FN(LIST(EL),add)(res, FN(EL,copy)(list->p[i]));
201 res = FN(LIST(EL),add)(res, el);
202 for (i = pos; i < list->n; ++i)
203 res = FN(LIST(EL),add)(res, FN(EL,copy)(list->p[i]));
204 FN(LIST(EL),free)(list);
206 return res;
207 error:
208 FN(EL,free)(el);
209 FN(LIST(EL),free)(list);
210 return NULL;
213 void *FN(LIST(EL),free)(__isl_take LIST(EL) *list)
215 int i;
217 if (!list)
218 return NULL;
220 if (--list->ref > 0)
221 return NULL;
223 isl_ctx_deref(list->ctx);
224 for (i = 0; i < list->n; ++i)
225 FN(EL,free)(list->p[i]);
226 free(list);
228 return NULL;
231 int FN(FN(LIST(EL),n),BASE)(__isl_keep LIST(EL) *list)
233 return list ? list->n : 0;
236 __isl_give EL *FN(FN(LIST(EL),get),BASE)(__isl_keep LIST(EL) *list, int index)
238 if (!list)
239 return NULL;
240 if (index < 0 || index >= list->n)
241 isl_die(list->ctx, isl_error_invalid,
242 "index out of bounds", return NULL);
243 return FN(EL,copy)(list->p[index]);
246 /* Replace the element at position "index" in "list" by "el".
248 __isl_give LIST(EL) *FN(FN(LIST(EL),set),BASE)(__isl_take LIST(EL) *list,
249 int index, __isl_take EL *el)
251 if (!list || !el)
252 goto error;
253 if (index < 0 || index >= list->n)
254 isl_die(list->ctx, isl_error_invalid,
255 "index out of bounds", goto error);
256 if (list->p[index] == el) {
257 FN(EL,free)(el);
258 return list;
260 list = FN(LIST(EL),cow)(list);
261 if (!list)
262 goto error;
263 FN(EL,free)(list->p[index]);
264 list->p[index] = el;
265 return list;
266 error:
267 FN(EL,free)(el);
268 FN(LIST(EL),free)(list);
269 return NULL;
272 int FN(LIST(EL),foreach)(__isl_keep LIST(EL) *list,
273 int (*fn)(__isl_take EL *el, void *user), void *user)
275 int i;
277 if (!list)
278 return -1;
280 for (i = 0; i < list->n; ++i) {
281 EL *el = FN(EL,copy(list->p[i]));
282 if (!el)
283 return -1;
284 if (fn(el, user) < 0)
285 return -1;
288 return 0;
291 /* Internal data structure for isl_*_list_sort.
293 * "cmp" is the original comparison function.
294 * "user" is a user provided pointer that should be passed to "cmp".
296 S(LIST(EL),sort_data) {
297 int (*cmp)(__isl_keep EL *a, __isl_keep EL *b, void *user);
298 void *user;
301 /* Compare two entries of an isl_*_list based on the user provided
302 * comparison function on pairs of isl_* objects.
304 static int FN(LIST(EL),cmp)(const void *a, const void *b, void *user)
306 S(LIST(EL),sort_data) *data = user;
307 EL * const *el1 = a;
308 EL * const *el2 = b;
310 return data->cmp(*el1, *el2, data->user);
313 /* Sort the elements of "list" in ascending order according to
314 * comparison function "cmp".
316 __isl_give LIST(EL) *FN(LIST(EL),sort)(__isl_take LIST(EL) *list,
317 int (*cmp)(__isl_keep EL *a, __isl_keep EL *b, void *user), void *user)
319 S(LIST(EL),sort_data) data = { cmp, user };
321 if (!list)
322 return NULL;
323 if (list->n <= 1)
324 return list;
325 list = FN(LIST(EL),cow)(list);
326 if (!list)
327 return NULL;
329 if (isl_sort(list->p, list->n, sizeof(list->p[0]),
330 &FN(LIST(EL),cmp), &data) < 0)
331 return FN(LIST(EL),free)(list);
333 return list;
336 /* Internal data structure for isl_*_list_foreach_scc.
338 * "list" is the original list.
339 * "follows" is the user provided callback that defines the edges of the graph.
341 S(LIST(EL),foreach_scc_data) {
342 LIST(EL) *list;
343 int (*follows)(__isl_keep EL *a, __isl_keep EL *b, void *user);
344 void *follows_user;
347 /* Does element i of data->list follow element j?
349 * Use the user provided callback to find out.
351 static int FN(LIST(EL),follows)(int i, int j, void *user)
353 S(LIST(EL),foreach_scc_data) *data = user;
355 return data->follows(data->list->p[i], data->list->p[j],
356 data->follows_user);
359 /* Call "fn" on the sublist of "list" that consists of the elements
360 * with indices specified by the "n" elements of "pos".
362 static int FN(LIST(EL),call_on_scc)(__isl_keep LIST(EL) *list, int *pos, int n,
363 int (*fn)(__isl_take LIST(EL) *scc, void *user), void *user)
365 int i;
366 isl_ctx *ctx;
367 LIST(EL) *slice;
369 ctx = FN(LIST(EL),get_ctx)(list);
370 slice = FN(LIST(EL),alloc)(ctx, n);
371 for (i = 0; i < n; ++i) {
372 EL *el;
374 el = FN(EL,copy)(list->p[pos[i]]);
375 slice = FN(LIST(EL),add)(slice, el);
378 return fn(slice, user);
381 /* Call "fn" on each of the strongly connected components (SCCs) of
382 * the graph with as vertices the elements of "list" and
383 * a directed edge from node b to node a iff follows(a, b)
384 * returns 1. follows should return -1 on error.
386 * If SCC a contains a node i that follows a node j in another SCC b
387 * (i.e., follows(i, j, user) returns 1), then fn will be called on SCC a
388 * after being called on SCC b.
390 * We simply call isl_tarjan_graph_init, extract the SCCs from the result and
391 * call fn on each of them.
393 int FN(LIST(EL),foreach_scc)(__isl_keep LIST(EL) *list,
394 int (*follows)(__isl_keep EL *a, __isl_keep EL *b, void *user),
395 void *follows_user,
396 int (*fn)(__isl_take LIST(EL) *scc, void *user), void *fn_user)
398 S(LIST(EL),foreach_scc_data) data = { list, follows, follows_user };
399 int i, n;
400 isl_ctx *ctx;
401 struct isl_tarjan_graph *g;
403 if (!list)
404 return -1;
405 if (list->n == 0)
406 return 0;
407 if (list->n == 1)
408 return fn(FN(LIST(EL),copy)(list), fn_user);
410 ctx = FN(LIST(EL),get_ctx)(list);
411 n = list->n;
412 g = isl_tarjan_graph_init(ctx, n, &FN(LIST(EL),follows), &data);
413 if (!g)
414 return -1;
416 i = 0;
417 do {
418 int first;
420 if (g->order[i] == -1)
421 isl_die(ctx, isl_error_internal, "cannot happen",
422 break);
423 first = i;
424 while (g->order[i] != -1) {
425 ++i; --n;
427 if (first == 0 && n == 0) {
428 isl_tarjan_graph_free(g);
429 return fn(FN(LIST(EL),copy)(list), fn_user);
431 if (FN(LIST(EL),call_on_scc)(list, g->order + first, i - first,
432 fn, fn_user) < 0)
433 break;
434 ++i;
435 } while (n);
437 isl_tarjan_graph_free(g);
439 return n > 0 ? -1 : 0;
442 __isl_give LIST(EL) *FN(FN(LIST(EL),from),BASE)(__isl_take EL *el)
444 isl_ctx *ctx;
445 LIST(EL) *list;
447 if (!el)
448 return NULL;
449 ctx = FN(EL,get_ctx)(el);
450 list = FN(LIST(EL),alloc)(ctx, 1);
451 if (!list)
452 goto error;
453 list = FN(LIST(EL),add)(list, el);
454 return list;
455 error:
456 FN(EL,free)(el);
457 return NULL;
460 __isl_give LIST(EL) *FN(LIST(EL),concat)(__isl_take LIST(EL) *list1,
461 __isl_take LIST(EL) *list2)
463 int i;
464 isl_ctx *ctx;
465 LIST(EL) *res;
467 if (!list1 || !list2)
468 goto error;
470 ctx = FN(LIST(EL),get_ctx)(list1);
471 res = FN(LIST(EL),alloc)(ctx, list1->n + list2->n);
472 for (i = 0; i < list1->n; ++i)
473 res = FN(LIST(EL),add)(res, FN(EL,copy)(list1->p[i]));
474 for (i = 0; i < list2->n; ++i)
475 res = FN(LIST(EL),add)(res, FN(EL,copy)(list2->p[i]));
477 FN(LIST(EL),free)(list1);
478 FN(LIST(EL),free)(list2);
479 return res;
480 error:
481 FN(LIST(EL),free)(list1);
482 FN(LIST(EL),free)(list2);
483 return NULL;
486 __isl_give isl_printer *CAT(isl_printer_print_,LIST(BASE))(
487 __isl_take isl_printer *p, __isl_keep LIST(EL) *list)
489 int i;
491 if (!p || !list)
492 goto error;
493 p = isl_printer_print_str(p, "(");
494 for (i = 0; i < list->n; ++i) {
495 if (i)
496 p = isl_printer_print_str(p, ",");
497 p = CAT(isl_printer_print_,BASE)(p, list->p[i]);
499 p = isl_printer_print_str(p, ")");
500 return p;
501 error:
502 isl_printer_free(p);
503 return NULL;
506 void FN(LIST(EL),dump)(__isl_keep LIST(EL) *list)
508 isl_printer *printer;
510 if (!list)
511 return;
513 printer = isl_printer_to_file(FN(LIST(EL),get_ctx)(list), stderr);
514 printer = CAT(isl_printer_print_,LIST(BASE))(printer, list);
515 printer = isl_printer_end_line(printer);
517 isl_printer_free(printer);