add isl_*_list_get_at as an alternative name for isl_*_list_get_*
[isl.git] / isl_list_templ.c
blob20a25f89e57402becf51042db581993779f2c33c
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2011 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2017 Sven Verdoolaege
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 #include <isl_sort.h>
17 #include <isl_tarjan.h>
18 #include <isl/printer.h>
20 #define xCAT(A,B) A ## B
21 #define CAT(A,B) xCAT(A,B)
22 #undef EL
23 #define EL CAT(isl_,BASE)
24 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
25 #define FN(TYPE,NAME) xFN(TYPE,NAME)
26 #define xLIST(EL) EL ## _list
27 #define LIST(EL) xLIST(EL)
28 #define xS(TYPE,NAME) struct TYPE ## _ ## NAME
29 #define S(TYPE,NAME) xS(TYPE,NAME)
31 isl_ctx *FN(LIST(EL),get_ctx)(__isl_keep LIST(EL) *list)
33 return list ? list->ctx : NULL;
36 __isl_give LIST(EL) *FN(LIST(EL),alloc)(isl_ctx *ctx, int n)
38 LIST(EL) *list;
40 if (n < 0)
41 isl_die(ctx, isl_error_invalid,
42 "cannot create list of negative length",
43 return NULL);
44 list = isl_alloc(ctx, LIST(EL),
45 sizeof(LIST(EL)) + (n - 1) * sizeof(struct EL *));
46 if (!list)
47 return NULL;
49 list->ctx = ctx;
50 isl_ctx_ref(ctx);
51 list->ref = 1;
52 list->size = n;
53 list->n = 0;
54 return list;
57 __isl_give LIST(EL) *FN(LIST(EL),copy)(__isl_keep LIST(EL) *list)
59 if (!list)
60 return NULL;
62 list->ref++;
63 return list;
66 __isl_give LIST(EL) *FN(LIST(EL),dup)(__isl_keep LIST(EL) *list)
68 int i;
69 LIST(EL) *dup;
71 if (!list)
72 return NULL;
74 dup = FN(LIST(EL),alloc)(FN(LIST(EL),get_ctx)(list), list->n);
75 if (!dup)
76 return NULL;
77 for (i = 0; i < list->n; ++i)
78 dup = FN(LIST(EL),add)(dup, FN(EL,copy)(list->p[i]));
79 return dup;
82 __isl_give LIST(EL) *FN(LIST(EL),cow)(__isl_take LIST(EL) *list)
84 if (!list)
85 return NULL;
87 if (list->ref == 1)
88 return list;
89 list->ref--;
90 return FN(LIST(EL),dup)(list);
93 /* Make sure "list" has room for at least "n" more pieces.
94 * Always return a list with a single reference.
96 * If there is only one reference to list, we extend it in place.
97 * Otherwise, we create a new LIST(EL) and copy the elements.
99 static __isl_give LIST(EL) *FN(LIST(EL),grow)(__isl_take LIST(EL) *list, int n)
101 isl_ctx *ctx;
102 int i, new_size;
103 LIST(EL) *res;
105 if (!list)
106 return NULL;
107 if (list->ref == 1 && list->n + n <= list->size)
108 return list;
110 ctx = FN(LIST(EL),get_ctx)(list);
111 new_size = ((list->n + n + 1) * 3) / 2;
112 if (list->ref == 1) {
113 res = isl_realloc(ctx, list, LIST(EL),
114 sizeof(LIST(EL)) + (new_size - 1) * sizeof(EL *));
115 if (!res)
116 return FN(LIST(EL),free)(list);
117 res->size = new_size;
118 return res;
121 if (list->n + n <= list->size && list->size < new_size)
122 new_size = list->size;
124 res = FN(LIST(EL),alloc)(ctx, new_size);
125 if (!res)
126 return FN(LIST(EL),free)(list);
128 for (i = 0; i < list->n; ++i)
129 res = FN(LIST(EL),add)(res, FN(EL,copy)(list->p[i]));
131 FN(LIST(EL),free)(list);
132 return res;
135 /* Check that "index" is a valid position in "list".
137 static isl_stat FN(LIST(EL),check_index)(__isl_keep LIST(EL) *list, int index)
139 if (!list)
140 return isl_stat_error;
141 if (index < 0 || index >= list->n)
142 isl_die(FN(LIST(EL),get_ctx)(list), isl_error_invalid,
143 "index out of bounds", return isl_stat_error);
144 return isl_stat_ok;
147 __isl_give LIST(EL) *FN(LIST(EL),add)(__isl_take LIST(EL) *list,
148 __isl_take struct EL *el)
150 list = FN(LIST(EL),grow)(list, 1);
151 if (!list || !el)
152 goto error;
153 list->p[list->n] = el;
154 list->n++;
155 return list;
156 error:
157 FN(EL,free)(el);
158 FN(LIST(EL),free)(list);
159 return NULL;
162 /* Remove the "n" elements starting at "first" from "list".
164 __isl_give LIST(EL) *FN(LIST(EL),drop)(__isl_take LIST(EL) *list,
165 unsigned first, unsigned n)
167 int i;
169 if (!list)
170 return NULL;
171 if (first + n > list->n || first + n < first)
172 isl_die(list->ctx, isl_error_invalid,
173 "index out of bounds", return FN(LIST(EL),free)(list));
174 if (n == 0)
175 return list;
176 list = FN(LIST(EL),cow)(list);
177 if (!list)
178 return NULL;
179 for (i = 0; i < n; ++i)
180 FN(EL,free)(list->p[first + i]);
181 for (i = first; i + n < list->n; ++i)
182 list->p[i] = list->p[i + n];
183 list->n -= n;
184 return list;
187 /* Insert "el" at position "pos" in "list".
189 * If there is only one reference to "list" and if it already has space
190 * for one extra element, we insert it directly into "list".
191 * Otherwise, we create a new list consisting of "el" and copied
192 * elements from "list".
194 __isl_give LIST(EL) *FN(LIST(EL),insert)(__isl_take LIST(EL) *list,
195 unsigned pos, __isl_take struct EL *el)
197 int i;
198 isl_ctx *ctx;
199 LIST(EL) *res;
201 if (!list || !el)
202 goto error;
203 ctx = FN(LIST(EL),get_ctx)(list);
204 if (pos > list->n)
205 isl_die(ctx, isl_error_invalid,
206 "index out of bounds", goto error);
208 if (list->ref == 1 && list->size > list->n) {
209 for (i = list->n; i > pos; --i)
210 list->p[i] = list->p[i - 1];
211 list->n++;
212 list->p[pos] = el;
213 return list;
216 res = FN(LIST(EL),alloc)(ctx, list->n + 1);
217 for (i = 0; i < pos; ++i)
218 res = FN(LIST(EL),add)(res, FN(EL,copy)(list->p[i]));
219 res = FN(LIST(EL),add)(res, el);
220 for (i = pos; i < list->n; ++i)
221 res = FN(LIST(EL),add)(res, FN(EL,copy)(list->p[i]));
222 FN(LIST(EL),free)(list);
224 return res;
225 error:
226 FN(EL,free)(el);
227 FN(LIST(EL),free)(list);
228 return NULL;
231 __isl_null LIST(EL) *FN(LIST(EL),free)(__isl_take LIST(EL) *list)
233 int i;
235 if (!list)
236 return NULL;
238 if (--list->ref > 0)
239 return NULL;
241 isl_ctx_deref(list->ctx);
242 for (i = 0; i < list->n; ++i)
243 FN(EL,free)(list->p[i]);
244 free(list);
246 return NULL;
249 int FN(FN(LIST(EL),n),BASE)(__isl_keep LIST(EL) *list)
251 return list ? list->n : 0;
254 /* Return the element at position "index" in "list".
256 static __isl_keep EL *FN(LIST(EL),peek)(__isl_keep LIST(EL) *list, int index)
258 if (FN(LIST(EL),check_index)(list, index) < 0)
259 return NULL;
260 return list->p[index];
263 /* Return a copy of the element at position "index" in "list".
265 __isl_give EL *FN(LIST(EL),get_at)(__isl_keep LIST(EL) *list, int index)
267 return FN(EL,copy)(FN(LIST(EL),peek)(list, index));
270 /* This is an alternative name for the function above.
272 __isl_give EL *FN(FN(LIST(EL),get),BASE)(__isl_keep LIST(EL) *list, int index)
274 return FN(LIST(EL),get_at)(list, index);
277 /* Replace the element at position "index" in "list" by "el".
279 __isl_give LIST(EL) *FN(FN(LIST(EL),set),BASE)(__isl_take LIST(EL) *list,
280 int index, __isl_take EL *el)
282 if (!list || !el)
283 goto error;
284 if (FN(LIST(EL),check_index)(list, index) < 0)
285 goto error;
286 if (list->p[index] == el) {
287 FN(EL,free)(el);
288 return list;
290 list = FN(LIST(EL),cow)(list);
291 if (!list)
292 goto error;
293 FN(EL,free)(list->p[index]);
294 list->p[index] = el;
295 return list;
296 error:
297 FN(EL,free)(el);
298 FN(LIST(EL),free)(list);
299 return NULL;
302 /* Return the element at position "index" of "list".
303 * This may be either a copy or the element itself
304 * if there is only one reference to "list".
305 * This allows the element to be modified inplace
306 * if both the list and the element have only a single reference.
307 * The caller is not allowed to modify "list" between
308 * this call to isl_list_*_take_* and a subsequent call
309 * to isl_list_*_restore_*.
310 * The only exception is that isl_list_*_free can be called instead.
312 static __isl_give EL *FN(FN(LIST(EL),take),BASE)(__isl_keep LIST(EL) *list,
313 int index)
315 EL *el;
317 if (FN(LIST(EL),check_index)(list, index) < 0)
318 return NULL;
319 if (list->ref != 1)
320 return FN(FN(LIST(EL),get),BASE)(list, index);
321 el = list->p[index];
322 list->p[index] = NULL;
323 return el;
326 /* Set the element at position "index" of "list" to "el",
327 * where the position may be empty due to a previous call
328 * to isl_list_*_take_*.
330 static __isl_give LIST(EL) *FN(FN(LIST(EL),restore),BASE)(
331 __isl_take LIST(EL) *list, int index, __isl_take EL *el)
333 return FN(FN(LIST(EL),set),BASE)(list, index, el);
336 isl_stat FN(LIST(EL),foreach)(__isl_keep LIST(EL) *list,
337 isl_stat (*fn)(__isl_take EL *el, void *user), void *user)
339 int i;
341 if (!list)
342 return isl_stat_error;
344 for (i = 0; i < list->n; ++i) {
345 EL *el = FN(EL,copy)(list->p[i]);
346 if (!el)
347 return isl_stat_error;
348 if (fn(el, user) < 0)
349 return isl_stat_error;
352 return isl_stat_ok;
355 /* Replace each element in "list" by the result of calling "fn"
356 * on the element.
358 __isl_give LIST(EL) *FN(LIST(EL),map)(__isl_keep LIST(EL) *list,
359 __isl_give EL *(*fn)(__isl_take EL *el, void *user), void *user)
361 int i, n;
363 if (!list)
364 return NULL;
366 n = list->n;
367 for (i = 0; i < n; ++i) {
368 EL *el = FN(FN(LIST(EL),take),BASE)(list, i);
369 if (!el)
370 return FN(LIST(EL),free)(list);
371 el = fn(el, user);
372 list = FN(FN(LIST(EL),restore),BASE)(list, i, el);
375 return list;
378 /* Internal data structure for isl_*_list_sort.
380 * "cmp" is the original comparison function.
381 * "user" is a user provided pointer that should be passed to "cmp".
383 S(LIST(EL),sort_data) {
384 int (*cmp)(__isl_keep EL *a, __isl_keep EL *b, void *user);
385 void *user;
388 /* Compare two entries of an isl_*_list based on the user provided
389 * comparison function on pairs of isl_* objects.
391 static int FN(LIST(EL),cmp)(const void *a, const void *b, void *user)
393 S(LIST(EL),sort_data) *data = user;
394 EL * const *el1 = a;
395 EL * const *el2 = b;
397 return data->cmp(*el1, *el2, data->user);
400 /* Sort the elements of "list" in ascending order according to
401 * comparison function "cmp".
403 __isl_give LIST(EL) *FN(LIST(EL),sort)(__isl_take LIST(EL) *list,
404 int (*cmp)(__isl_keep EL *a, __isl_keep EL *b, void *user), void *user)
406 S(LIST(EL),sort_data) data = { cmp, user };
408 if (!list)
409 return NULL;
410 if (list->n <= 1)
411 return list;
412 list = FN(LIST(EL),cow)(list);
413 if (!list)
414 return NULL;
416 if (isl_sort(list->p, list->n, sizeof(list->p[0]),
417 &FN(LIST(EL),cmp), &data) < 0)
418 return FN(LIST(EL),free)(list);
420 return list;
423 /* Internal data structure for isl_*_list_foreach_scc.
425 * "list" is the original list.
426 * "follows" is the user provided callback that defines the edges of the graph.
428 S(LIST(EL),foreach_scc_data) {
429 LIST(EL) *list;
430 isl_bool (*follows)(__isl_keep EL *a, __isl_keep EL *b, void *user);
431 void *follows_user;
434 /* Does element i of data->list follow element j?
436 * Use the user provided callback to find out.
438 static isl_bool FN(LIST(EL),follows)(int i, int j, void *user)
440 S(LIST(EL),foreach_scc_data) *data = user;
442 return data->follows(data->list->p[i], data->list->p[j],
443 data->follows_user);
446 /* Call "fn" on the sublist of "list" that consists of the elements
447 * with indices specified by the "n" elements of "pos".
449 static isl_stat FN(LIST(EL),call_on_scc)(__isl_keep LIST(EL) *list, int *pos,
450 int n, isl_stat (*fn)(__isl_take LIST(EL) *scc, void *user), void *user)
452 int i;
453 isl_ctx *ctx;
454 LIST(EL) *slice;
456 ctx = FN(LIST(EL),get_ctx)(list);
457 slice = FN(LIST(EL),alloc)(ctx, n);
458 for (i = 0; i < n; ++i) {
459 EL *el;
461 el = FN(EL,copy)(list->p[pos[i]]);
462 slice = FN(LIST(EL),add)(slice, el);
465 return fn(slice, user);
468 /* Call "fn" on each of the strongly connected components (SCCs) of
469 * the graph with as vertices the elements of "list" and
470 * a directed edge from node b to node a iff follows(a, b)
471 * returns 1. follows should return -1 on error.
473 * If SCC a contains a node i that follows a node j in another SCC b
474 * (i.e., follows(i, j, user) returns 1), then fn will be called on SCC a
475 * after being called on SCC b.
477 * We simply call isl_tarjan_graph_init, extract the SCCs from the result and
478 * call fn on each of them.
480 isl_stat FN(LIST(EL),foreach_scc)(__isl_keep LIST(EL) *list,
481 isl_bool (*follows)(__isl_keep EL *a, __isl_keep EL *b, void *user),
482 void *follows_user,
483 isl_stat (*fn)(__isl_take LIST(EL) *scc, void *user), void *fn_user)
485 S(LIST(EL),foreach_scc_data) data = { list, follows, follows_user };
486 int i, n;
487 isl_ctx *ctx;
488 struct isl_tarjan_graph *g;
490 if (!list)
491 return isl_stat_error;
492 if (list->n == 0)
493 return isl_stat_ok;
494 if (list->n == 1)
495 return fn(FN(LIST(EL),copy)(list), fn_user);
497 ctx = FN(LIST(EL),get_ctx)(list);
498 n = list->n;
499 g = isl_tarjan_graph_init(ctx, n, &FN(LIST(EL),follows), &data);
500 if (!g)
501 return isl_stat_error;
503 i = 0;
504 do {
505 int first;
507 if (g->order[i] == -1)
508 isl_die(ctx, isl_error_internal, "cannot happen",
509 break);
510 first = i;
511 while (g->order[i] != -1) {
512 ++i; --n;
514 if (first == 0 && n == 0) {
515 isl_tarjan_graph_free(g);
516 return fn(FN(LIST(EL),copy)(list), fn_user);
518 if (FN(LIST(EL),call_on_scc)(list, g->order + first, i - first,
519 fn, fn_user) < 0)
520 break;
521 ++i;
522 } while (n);
524 isl_tarjan_graph_free(g);
526 return n > 0 ? isl_stat_error : isl_stat_ok;
529 __isl_give LIST(EL) *FN(FN(LIST(EL),from),BASE)(__isl_take EL *el)
531 isl_ctx *ctx;
532 LIST(EL) *list;
534 if (!el)
535 return NULL;
536 ctx = FN(EL,get_ctx)(el);
537 list = FN(LIST(EL),alloc)(ctx, 1);
538 if (!list)
539 goto error;
540 list = FN(LIST(EL),add)(list, el);
541 return list;
542 error:
543 FN(EL,free)(el);
544 return NULL;
547 /* Append the elements of "list2" to "list1", where "list1" is known
548 * to have only a single reference and enough room to hold
549 * the extra elements.
551 static __isl_give LIST(EL) *FN(LIST(EL),concat_inplace)(
552 __isl_take LIST(EL) *list1, __isl_take LIST(EL) *list2)
554 int i;
556 for (i = 0; i < list2->n; ++i)
557 list1 = FN(LIST(EL),add)(list1, FN(EL,copy)(list2->p[i]));
558 FN(LIST(EL),free)(list2);
559 return list1;
562 /* Concatenate "list1" and "list2".
563 * If "list1" has only one reference and has enough room
564 * for the elements of "list2", the add the elements to "list1" itself.
565 * Otherwise, create a new list to store the result.
567 __isl_give LIST(EL) *FN(LIST(EL),concat)(__isl_take LIST(EL) *list1,
568 __isl_take LIST(EL) *list2)
570 int i;
571 isl_ctx *ctx;
572 LIST(EL) *res;
574 if (!list1 || !list2)
575 goto error;
577 if (list1->ref == 1 && list1->n + list2->n <= list1->size)
578 return FN(LIST(EL),concat_inplace)(list1, list2);
580 ctx = FN(LIST(EL),get_ctx)(list1);
581 res = FN(LIST(EL),alloc)(ctx, list1->n + list2->n);
582 for (i = 0; i < list1->n; ++i)
583 res = FN(LIST(EL),add)(res, FN(EL,copy)(list1->p[i]));
584 for (i = 0; i < list2->n; ++i)
585 res = FN(LIST(EL),add)(res, FN(EL,copy)(list2->p[i]));
587 FN(LIST(EL),free)(list1);
588 FN(LIST(EL),free)(list2);
589 return res;
590 error:
591 FN(LIST(EL),free)(list1);
592 FN(LIST(EL),free)(list2);
593 return NULL;
596 __isl_give isl_printer *CAT(isl_printer_print_,LIST(BASE))(
597 __isl_take isl_printer *p, __isl_keep LIST(EL) *list)
599 int i;
601 if (!p || !list)
602 goto error;
603 p = isl_printer_print_str(p, "(");
604 for (i = 0; i < list->n; ++i) {
605 if (i)
606 p = isl_printer_print_str(p, ",");
607 p = CAT(isl_printer_print_,BASE)(p, list->p[i]);
609 p = isl_printer_print_str(p, ")");
610 return p;
611 error:
612 isl_printer_free(p);
613 return NULL;
616 void FN(LIST(EL),dump)(__isl_keep LIST(EL) *list)
618 isl_printer *printer;
620 if (!list)
621 return;
623 printer = isl_printer_to_file(FN(LIST(EL),get_ctx)(list), stderr);
624 printer = CAT(isl_printer_print_,LIST(BASE))(printer, list);
625 printer = isl_printer_end_line(printer);
627 isl_printer_free(printer);