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
17 #include <isl_tarjan.h>
18 #include <isl/printer.h>
20 #include <isl_list_macro.h>
22 #define xS(TYPE,NAME) struct TYPE ## _ ## NAME
23 #define S(TYPE,NAME) xS(TYPE,NAME)
25 isl_ctx
*FN(LIST(EL
),get_ctx
)(__isl_keep
LIST(EL
) *list
)
27 return list
? list
->ctx
: NULL
;
30 __isl_give
LIST(EL
) *FN(LIST(EL
),alloc
)(isl_ctx
*ctx
, int n
)
35 isl_die(ctx
, isl_error_invalid
,
36 "cannot create list of negative length",
38 list
= isl_alloc(ctx
, LIST(EL
),
39 sizeof(LIST(EL
)) + (n
- 1) * sizeof(struct EL
*));
51 __isl_give
LIST(EL
) *FN(LIST(EL
),copy
)(__isl_keep
LIST(EL
) *list
)
60 __isl_give
LIST(EL
) *FN(LIST(EL
),dup
)(__isl_keep
LIST(EL
) *list
)
68 dup
= FN(LIST(EL
),alloc
)(FN(LIST(EL
),get_ctx
)(list
), list
->n
);
71 for (i
= 0; i
< list
->n
; ++i
)
72 dup
= FN(LIST(EL
),add
)(dup
, FN(EL
,copy
)(list
->p
[i
]));
76 __isl_give
LIST(EL
) *FN(LIST(EL
),cow
)(__isl_take
LIST(EL
) *list
)
84 return FN(LIST(EL
),dup
)(list
);
87 /* Make sure "list" has room for at least "n" more pieces.
88 * Always return a list with a single reference.
90 * If there is only one reference to list, we extend it in place.
91 * Otherwise, we create a new LIST(EL) and copy the elements.
93 static __isl_give
LIST(EL
) *FN(LIST(EL
),grow
)(__isl_take
LIST(EL
) *list
, int n
)
101 if (list
->ref
== 1 && list
->n
+ n
<= list
->size
)
104 ctx
= FN(LIST(EL
),get_ctx
)(list
);
105 new_size
= ((list
->n
+ n
+ 1) * 3) / 2;
106 if (list
->ref
== 1) {
107 res
= isl_realloc(ctx
, list
, LIST(EL
),
108 sizeof(LIST(EL
)) + (new_size
- 1) * sizeof(EL
*));
110 return FN(LIST(EL
),free
)(list
);
111 res
->size
= new_size
;
115 if (list
->n
+ n
<= list
->size
&& list
->size
< new_size
)
116 new_size
= list
->size
;
118 res
= FN(LIST(EL
),alloc
)(ctx
, new_size
);
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
);
129 /* Check that "index" is a valid position in "list".
131 static isl_stat
FN(LIST(EL
),check_index
)(__isl_keep
LIST(EL
) *list
, int index
)
134 return isl_stat_error
;
135 if (index
< 0 || index
>= list
->n
)
136 isl_die(FN(LIST(EL
),get_ctx
)(list
), isl_error_invalid
,
137 "index out of bounds", return isl_stat_error
);
141 __isl_give
LIST(EL
) *FN(LIST(EL
),add
)(__isl_take
LIST(EL
) *list
,
142 __isl_take
struct EL
*el
)
144 list
= FN(LIST(EL
),grow
)(list
, 1);
147 list
->p
[list
->n
] = el
;
152 FN(LIST(EL
),free
)(list
);
156 /* Remove the "n" elements starting at "first" from "list".
158 __isl_give
LIST(EL
) *FN(LIST(EL
),drop
)(__isl_take
LIST(EL
) *list
,
159 unsigned first
, unsigned n
)
165 if (first
+ n
> list
->n
|| first
+ n
< first
)
166 isl_die(list
->ctx
, isl_error_invalid
,
167 "index out of bounds", return FN(LIST(EL
),free
)(list
));
170 list
= FN(LIST(EL
),cow
)(list
);
173 for (i
= 0; i
< n
; ++i
)
174 FN(EL
,free
)(list
->p
[first
+ i
]);
175 for (i
= first
; i
+ n
< list
->n
; ++i
)
176 list
->p
[i
] = list
->p
[i
+ n
];
181 /* Remove all elements from "list".
183 __isl_give
LIST(EL
) *FN(LIST(EL
),clear
)(__isl_take
LIST(EL
) *list
)
187 return FN(LIST(EL
),drop
)(list
, 0, list
->n
);
190 /* Insert "el" at position "pos" in "list".
192 * If there is only one reference to "list" and if it already has space
193 * for one extra element, we insert it directly into "list".
194 * Otherwise, we create a new list consisting of "el" and copied
195 * elements from "list".
197 __isl_give
LIST(EL
) *FN(LIST(EL
),insert
)(__isl_take
LIST(EL
) *list
,
198 unsigned pos
, __isl_take
struct EL
*el
)
206 ctx
= FN(LIST(EL
),get_ctx
)(list
);
208 isl_die(ctx
, isl_error_invalid
,
209 "index out of bounds", goto error
);
211 if (list
->ref
== 1 && list
->size
> list
->n
) {
212 for (i
= list
->n
; i
> pos
; --i
)
213 list
->p
[i
] = list
->p
[i
- 1];
219 res
= FN(LIST(EL
),alloc
)(ctx
, list
->n
+ 1);
220 for (i
= 0; i
< pos
; ++i
)
221 res
= FN(LIST(EL
),add
)(res
, FN(EL
,copy
)(list
->p
[i
]));
222 res
= FN(LIST(EL
),add
)(res
, el
);
223 for (i
= pos
; i
< list
->n
; ++i
)
224 res
= FN(LIST(EL
),add
)(res
, FN(EL
,copy
)(list
->p
[i
]));
225 FN(LIST(EL
),free
)(list
);
230 FN(LIST(EL
),free
)(list
);
234 __isl_null
LIST(EL
) *FN(LIST(EL
),free
)(__isl_take
LIST(EL
) *list
)
244 isl_ctx_deref(list
->ctx
);
245 for (i
= 0; i
< list
->n
; ++i
)
246 FN(EL
,free
)(list
->p
[i
]);
252 /* Return the number of elements in "list".
254 isl_size
FN(LIST(EL
),size
)(__isl_keep
LIST(EL
) *list
)
256 return list
? list
->n
: isl_size_error
;
259 /* This is an alternative name for the function above.
261 isl_size
FN(FN(LIST(EL
),n
),EL_BASE
)(__isl_keep
LIST(EL
) *list
)
263 return FN(LIST(EL
),size
)(list
);
266 /* Return the element at position "index" in "list".
268 __isl_keep EL
*FN(LIST(EL
),peek
)(__isl_keep
LIST(EL
) *list
, int index
)
270 if (FN(LIST(EL
),check_index
)(list
, index
) < 0)
272 return list
->p
[index
];
275 /* Return a copy of the element at position "index" in "list".
277 __isl_give EL
*FN(LIST(EL
),get_at
)(__isl_keep
LIST(EL
) *list
, int index
)
279 return FN(EL
,copy
)(FN(LIST(EL
),peek
)(list
, index
));
282 /* This is an alternative name for the function above.
284 __isl_give EL
*FN(FN(LIST(EL
),get
),EL_BASE
)(__isl_keep
LIST(EL
) *list
,
287 return FN(LIST(EL
),get_at
)(list
, index
);
290 /* Replace the element at position "index" in "list" by "el".
292 __isl_give
LIST(EL
) *FN(FN(LIST(EL
),set
),EL_BASE
)(__isl_take
LIST(EL
) *list
,
293 int index
, __isl_take EL
*el
)
297 if (FN(LIST(EL
),check_index
)(list
, index
) < 0)
299 if (list
->p
[index
] == el
) {
303 list
= FN(LIST(EL
),cow
)(list
);
306 FN(EL
,free
)(list
->p
[index
]);
311 FN(LIST(EL
),free
)(list
);
315 /* Return the element at position "index" of "list".
316 * This may be either a copy or the element itself
317 * if there is only one reference to "list".
318 * This allows the element to be modified inplace
319 * if both the list and the element have only a single reference.
320 * The caller is not allowed to modify "list" between
321 * this call to isl_list_*_take_* and a subsequent call
322 * to isl_list_*_restore_*.
323 * The only exception is that isl_list_*_free can be called instead.
325 static __isl_give EL
*FN(FN(LIST(EL
),take
),EL_BASE
)(__isl_keep
LIST(EL
) *list
,
330 if (FN(LIST(EL
),check_index
)(list
, index
) < 0)
333 return FN(FN(LIST(EL
),get
),EL_BASE
)(list
, index
);
335 list
->p
[index
] = NULL
;
339 /* Set the element at position "index" of "list" to "el",
340 * where the position may be empty due to a previous call
341 * to isl_list_*_take_*.
343 static __isl_give
LIST(EL
) *FN(FN(LIST(EL
),restore
),EL_BASE
)(
344 __isl_take
LIST(EL
) *list
, int index
, __isl_take EL
*el
)
346 return FN(FN(LIST(EL
),set
),EL_BASE
)(list
, index
, el
);
349 /* Swap the elements of "list" in positions "pos1" and "pos2".
351 __isl_give
LIST(EL
) *FN(LIST(EL
),swap
)(__isl_take
LIST(EL
) *list
,
352 unsigned pos1
, unsigned pos2
)
358 el1
= FN(FN(LIST(EL
),take
),EL_BASE
)(list
, pos1
);
359 el2
= FN(FN(LIST(EL
),take
),EL_BASE
)(list
, pos2
);
360 list
= FN(FN(LIST(EL
),restore
),EL_BASE
)(list
, pos1
, el2
);
361 list
= FN(FN(LIST(EL
),restore
),EL_BASE
)(list
, pos2
, el1
);
365 /* Reverse the elements of "list".
367 __isl_give
LIST(EL
) *FN(LIST(EL
),reverse
)(__isl_take
LIST(EL
) *list
)
371 n
= FN(LIST(EL
),size
)(list
);
372 for (i
= 0; i
< n
- 1 - i
; ++i
)
373 list
= FN(LIST(EL
),swap
)(list
, i
, n
- 1 - i
);
377 isl_stat
FN(LIST(EL
),foreach
)(__isl_keep
LIST(EL
) *list
,
378 isl_stat (*fn
)(__isl_take EL
*el
, void *user
), void *user
)
383 return isl_stat_error
;
385 for (i
= 0; i
< list
->n
; ++i
) {
386 EL
*el
= FN(EL
,copy
)(list
->p
[i
]);
388 return isl_stat_error
;
389 if (fn(el
, user
) < 0)
390 return isl_stat_error
;
396 /* Does "test" succeed on every element of "list"?
398 isl_bool
FN(LIST(EL
),every
)(__isl_keep
LIST(EL
) *list
,
399 isl_bool (*test
)(__isl_keep EL
*el
, void *user
), void *user
)
404 return isl_bool_error
;
406 for (i
= 0; i
< list
->n
; ++i
) {
409 r
= test(list
->p
[i
], user
);
414 return isl_bool_true
;
417 /* Replace each element in "list" by the result of calling "fn"
420 __isl_give
LIST(EL
) *FN(LIST(EL
),map
)(__isl_keep
LIST(EL
) *list
,
421 __isl_give EL
*(*fn
)(__isl_take EL
*el
, void *user
), void *user
)
429 for (i
= 0; i
< n
; ++i
) {
430 EL
*el
= FN(FN(LIST(EL
),take
),EL_BASE
)(list
, i
);
432 return FN(LIST(EL
),free
)(list
);
434 list
= FN(FN(LIST(EL
),restore
),EL_BASE
)(list
, i
, el
);
440 /* Internal data structure for isl_*_list_sort.
442 * "cmp" is the original comparison function.
443 * "user" is a user provided pointer that should be passed to "cmp".
445 S(LIST(EL
),sort_data
) {
446 int (*cmp
)(__isl_keep EL
*a
, __isl_keep EL
*b
, void *user
);
450 /* Compare two entries of an isl_*_list based on the user provided
451 * comparison function on pairs of isl_* objects.
453 static int FN(LIST(EL
),cmp
)(const void *a
, const void *b
, void *user
)
455 S(LIST(EL
),sort_data
) *data
= user
;
459 return data
->cmp(*el1
, *el2
, data
->user
);
462 /* Sort the elements of "list" in ascending order according to
463 * comparison function "cmp".
465 __isl_give
LIST(EL
) *FN(LIST(EL
),sort
)(__isl_take
LIST(EL
) *list
,
466 int (*cmp
)(__isl_keep EL
*a
, __isl_keep EL
*b
, void *user
), void *user
)
468 S(LIST(EL
),sort_data
) data
= { cmp
, user
};
474 list
= FN(LIST(EL
),cow
)(list
);
478 if (isl_sort(list
->p
, list
->n
, sizeof(list
->p
[0]),
479 &FN(LIST(EL
),cmp
), &data
) < 0)
480 return FN(LIST(EL
),free
)(list
);
485 /* Internal data structure for isl_*_list_foreach_scc.
487 * "list" is the original list.
488 * "follows" is the user provided callback that defines the edges of the graph.
490 S(LIST(EL
),foreach_scc_data
) {
492 isl_bool (*follows
)(__isl_keep EL
*a
, __isl_keep EL
*b
, void *user
);
496 /* Does element i of data->list follow element j?
498 * Use the user provided callback to find out.
500 static isl_bool
FN(LIST(EL
),follows
)(int i
, int j
, void *user
)
502 S(LIST(EL
),foreach_scc_data
) *data
= user
;
504 return data
->follows(data
->list
->p
[i
], data
->list
->p
[j
],
508 /* Call "fn" on the sublist of "list" that consists of the elements
509 * with indices specified by the "n" elements of "pos".
511 static isl_stat
FN(LIST(EL
),call_on_scc
)(__isl_keep
LIST(EL
) *list
, int *pos
,
512 int n
, isl_stat (*fn
)(__isl_take
LIST(EL
) *scc
, void *user
), void *user
)
518 ctx
= FN(LIST(EL
),get_ctx
)(list
);
519 slice
= FN(LIST(EL
),alloc
)(ctx
, n
);
520 for (i
= 0; i
< n
; ++i
) {
523 el
= FN(EL
,copy
)(list
->p
[pos
[i
]]);
524 slice
= FN(LIST(EL
),add
)(slice
, el
);
527 return fn(slice
, user
);
530 /* Call "fn" on each of the strongly connected components (SCCs) of
531 * the graph with as vertices the elements of "list" and
532 * a directed edge from node b to node a iff follows(a, b)
533 * returns 1. follows should return -1 on error.
535 * If SCC a contains a node i that follows a node j in another SCC b
536 * (i.e., follows(i, j, user) returns 1), then fn will be called on SCC a
537 * after being called on SCC b.
539 * We simply call isl_tarjan_graph_init, extract the SCCs from the result and
540 * call fn on each of them.
542 isl_stat
FN(LIST(EL
),foreach_scc
)(__isl_keep
LIST(EL
) *list
,
543 isl_bool (*follows
)(__isl_keep EL
*a
, __isl_keep EL
*b
, void *user
),
545 isl_stat (*fn
)(__isl_take
LIST(EL
) *scc
, void *user
), void *fn_user
)
547 S(LIST(EL
),foreach_scc_data
) data
= { list
, follows
, follows_user
};
550 struct isl_tarjan_graph
*g
;
553 return isl_stat_error
;
557 return fn(FN(LIST(EL
),copy
)(list
), fn_user
);
559 ctx
= FN(LIST(EL
),get_ctx
)(list
);
561 g
= isl_tarjan_graph_init(ctx
, n
, &FN(LIST(EL
),follows
), &data
);
563 return isl_stat_error
;
569 if (g
->order
[i
] == -1)
570 isl_die(ctx
, isl_error_internal
, "cannot happen",
573 while (g
->order
[i
] != -1) {
576 if (first
== 0 && n
== 0) {
577 isl_tarjan_graph_free(g
);
578 return fn(FN(LIST(EL
),copy
)(list
), fn_user
);
580 if (FN(LIST(EL
),call_on_scc
)(list
, g
->order
+ first
, i
- first
,
586 isl_tarjan_graph_free(g
);
588 return n
> 0 ? isl_stat_error
: isl_stat_ok
;
591 __isl_give
LIST(EL
) *FN(FN(LIST(EL
),from
),EL_BASE
)(__isl_take EL
*el
)
598 ctx
= FN(EL
,get_ctx
)(el
);
599 list
= FN(LIST(EL
),alloc
)(ctx
, 1);
602 list
= FN(LIST(EL
),add
)(list
, el
);
609 /* This function performs the same operation as isl_*_list_from_*,
610 * but is considered as a function on the element when exported.
612 __isl_give
LIST(EL
) *FN(EL
,to_list
)(__isl_take EL
*el
)
614 return FN(FN(LIST(EL
),from
),EL_BASE
)(el
);
617 /* Append the elements of "list2" to "list1", where "list1" is known
618 * to have only a single reference and enough room to hold
619 * the extra elements.
621 static __isl_give
LIST(EL
) *FN(LIST(EL
),concat_inplace
)(
622 __isl_take
LIST(EL
) *list1
, __isl_take
LIST(EL
) *list2
)
626 for (i
= 0; i
< list2
->n
; ++i
)
627 list1
= FN(LIST(EL
),add
)(list1
, FN(EL
,copy
)(list2
->p
[i
]));
628 FN(LIST(EL
),free
)(list2
);
632 /* Concatenate "list1" and "list2".
633 * If "list1" has only one reference and has enough room
634 * for the elements of "list2", the add the elements to "list1" itself.
635 * Otherwise, create a new list to store the result.
637 __isl_give
LIST(EL
) *FN(LIST(EL
),concat
)(__isl_take
LIST(EL
) *list1
,
638 __isl_take
LIST(EL
) *list2
)
644 if (!list1
|| !list2
)
647 if (list1
->ref
== 1 && list1
->n
+ list2
->n
<= list1
->size
)
648 return FN(LIST(EL
),concat_inplace
)(list1
, list2
);
650 ctx
= FN(LIST(EL
),get_ctx
)(list1
);
651 res
= FN(LIST(EL
),alloc
)(ctx
, list1
->n
+ list2
->n
);
652 for (i
= 0; i
< list1
->n
; ++i
)
653 res
= FN(LIST(EL
),add
)(res
, FN(EL
,copy
)(list1
->p
[i
]));
654 for (i
= 0; i
< list2
->n
; ++i
)
655 res
= FN(LIST(EL
),add
)(res
, FN(EL
,copy
)(list2
->p
[i
]));
657 FN(LIST(EL
),free
)(list1
);
658 FN(LIST(EL
),free
)(list2
);
661 FN(LIST(EL
),free
)(list1
);
662 FN(LIST(EL
),free
)(list2
);
666 __isl_give isl_printer
*CAT(isl_printer_print_
,LIST(EL_BASE
))(
667 __isl_take isl_printer
*p
, __isl_keep
LIST(EL
) *list
)
673 p
= isl_printer_print_str(p
, "(");
674 for (i
= 0; i
< list
->n
; ++i
) {
676 p
= isl_printer_print_str(p
, ",");
677 p
= CAT(isl_printer_print_
,EL_BASE
)(p
, list
->p
[i
]);
679 p
= isl_printer_print_str(p
, ")");
687 #define BASE LIST(EL_BASE)
689 #define PRINT_DUMP_DEFAULT 0
690 #include "print_templ.c"
691 #undef PRINT_DUMP_DEFAULT