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 #define xCAT(A,B) A ## B
21 #define CAT(A,B) xCAT(A,B)
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
)
41 isl_die(ctx
, isl_error_invalid
,
42 "cannot create list of negative length",
44 list
= isl_alloc(ctx
, LIST(EL
),
45 sizeof(LIST(EL
)) + (n
- 1) * sizeof(struct EL
*));
57 __isl_give
LIST(EL
) *FN(LIST(EL
),copy
)(__isl_keep
LIST(EL
) *list
)
66 __isl_give
LIST(EL
) *FN(LIST(EL
),dup
)(__isl_keep
LIST(EL
) *list
)
74 dup
= FN(LIST(EL
),alloc
)(FN(LIST(EL
),get_ctx
)(list
), list
->n
);
77 for (i
= 0; i
< list
->n
; ++i
)
78 dup
= FN(LIST(EL
),add
)(dup
, FN(EL
,copy
)(list
->p
[i
]));
82 __isl_give
LIST(EL
) *FN(LIST(EL
),cow
)(__isl_take
LIST(EL
) *list
)
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
)
107 if (list
->ref
== 1 && list
->n
+ n
<= list
->size
)
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
*));
116 return FN(LIST(EL
),free
)(list
);
117 res
->size
= new_size
;
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
);
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
);
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
)
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
);
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);
153 list
->p
[list
->n
] = el
;
158 FN(LIST(EL
),free
)(list
);
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
)
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
));
176 list
= FN(LIST(EL
),cow
)(list
);
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
];
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
)
203 ctx
= FN(LIST(EL
),get_ctx
)(list
);
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];
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
);
227 FN(LIST(EL
),free
)(list
);
231 __isl_null
LIST(EL
) *FN(LIST(EL
),free
)(__isl_take
LIST(EL
) *list
)
241 isl_ctx_deref(list
->ctx
);
242 for (i
= 0; i
< list
->n
; ++i
)
243 FN(EL
,free
)(list
->p
[i
]);
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)
260 return list
->p
[index
];
263 /* Return a copy of the element at position "index" in "list".
265 __isl_give EL
*FN(FN(LIST(EL
),get
),BASE
)(__isl_keep
LIST(EL
) *list
, int index
)
267 return FN(EL
,copy
)(FN(LIST(EL
),peek
)(list
, index
));
270 /* Replace the element at position "index" in "list" by "el".
272 __isl_give
LIST(EL
) *FN(FN(LIST(EL
),set
),BASE
)(__isl_take
LIST(EL
) *list
,
273 int index
, __isl_take EL
*el
)
277 if (FN(LIST(EL
),check_index
)(list
, index
) < 0)
279 if (list
->p
[index
] == el
) {
283 list
= FN(LIST(EL
),cow
)(list
);
286 FN(EL
,free
)(list
->p
[index
]);
291 FN(LIST(EL
),free
)(list
);
295 /* Return the element at position "index" of "list".
296 * This may be either a copy or the element itself
297 * if there is only one reference to "list".
298 * This allows the element to be modified inplace
299 * if both the list and the element have only a single reference.
300 * The caller is not allowed to modify "list" between
301 * this call to isl_list_*_take_* and a subsequent call
302 * to isl_list_*_restore_*.
303 * The only exception is that isl_list_*_free can be called instead.
305 static __isl_give EL
*FN(FN(LIST(EL
),take
),BASE
)(__isl_keep
LIST(EL
) *list
,
310 if (FN(LIST(EL
),check_index
)(list
, index
) < 0)
313 return FN(FN(LIST(EL
),get
),BASE
)(list
, index
);
315 list
->p
[index
] = NULL
;
319 /* Set the element at position "index" of "list" to "el",
320 * where the position may be empty due to a previous call
321 * to isl_list_*_take_*.
323 static __isl_give
LIST(EL
) *FN(FN(LIST(EL
),restore
),BASE
)(
324 __isl_take
LIST(EL
) *list
, int index
, __isl_take EL
*el
)
326 return FN(FN(LIST(EL
),set
),BASE
)(list
, index
, el
);
329 isl_stat
FN(LIST(EL
),foreach
)(__isl_keep
LIST(EL
) *list
,
330 isl_stat (*fn
)(__isl_take EL
*el
, void *user
), void *user
)
335 return isl_stat_error
;
337 for (i
= 0; i
< list
->n
; ++i
) {
338 EL
*el
= FN(EL
,copy
)(list
->p
[i
]);
340 return isl_stat_error
;
341 if (fn(el
, user
) < 0)
342 return isl_stat_error
;
348 /* Replace each element in "list" by the result of calling "fn"
351 __isl_give
LIST(EL
) *FN(LIST(EL
),map
)(__isl_keep
LIST(EL
) *list
,
352 __isl_give EL
*(*fn
)(__isl_take EL
*el
, void *user
), void *user
)
360 for (i
= 0; i
< n
; ++i
) {
361 EL
*el
= FN(FN(LIST(EL
),take
),BASE
)(list
, i
);
363 return FN(LIST(EL
),free
)(list
);
365 list
= FN(FN(LIST(EL
),restore
),BASE
)(list
, i
, el
);
371 /* Internal data structure for isl_*_list_sort.
373 * "cmp" is the original comparison function.
374 * "user" is a user provided pointer that should be passed to "cmp".
376 S(LIST(EL
),sort_data
) {
377 int (*cmp
)(__isl_keep EL
*a
, __isl_keep EL
*b
, void *user
);
381 /* Compare two entries of an isl_*_list based on the user provided
382 * comparison function on pairs of isl_* objects.
384 static int FN(LIST(EL
),cmp
)(const void *a
, const void *b
, void *user
)
386 S(LIST(EL
),sort_data
) *data
= user
;
390 return data
->cmp(*el1
, *el2
, data
->user
);
393 /* Sort the elements of "list" in ascending order according to
394 * comparison function "cmp".
396 __isl_give
LIST(EL
) *FN(LIST(EL
),sort
)(__isl_take
LIST(EL
) *list
,
397 int (*cmp
)(__isl_keep EL
*a
, __isl_keep EL
*b
, void *user
), void *user
)
399 S(LIST(EL
),sort_data
) data
= { cmp
, user
};
405 list
= FN(LIST(EL
),cow
)(list
);
409 if (isl_sort(list
->p
, list
->n
, sizeof(list
->p
[0]),
410 &FN(LIST(EL
),cmp
), &data
) < 0)
411 return FN(LIST(EL
),free
)(list
);
416 /* Internal data structure for isl_*_list_foreach_scc.
418 * "list" is the original list.
419 * "follows" is the user provided callback that defines the edges of the graph.
421 S(LIST(EL
),foreach_scc_data
) {
423 isl_bool (*follows
)(__isl_keep EL
*a
, __isl_keep EL
*b
, void *user
);
427 /* Does element i of data->list follow element j?
429 * Use the user provided callback to find out.
431 static isl_bool
FN(LIST(EL
),follows
)(int i
, int j
, void *user
)
433 S(LIST(EL
),foreach_scc_data
) *data
= user
;
435 return data
->follows(data
->list
->p
[i
], data
->list
->p
[j
],
439 /* Call "fn" on the sublist of "list" that consists of the elements
440 * with indices specified by the "n" elements of "pos".
442 static isl_stat
FN(LIST(EL
),call_on_scc
)(__isl_keep
LIST(EL
) *list
, int *pos
,
443 int n
, isl_stat (*fn
)(__isl_take
LIST(EL
) *scc
, void *user
), void *user
)
449 ctx
= FN(LIST(EL
),get_ctx
)(list
);
450 slice
= FN(LIST(EL
),alloc
)(ctx
, n
);
451 for (i
= 0; i
< n
; ++i
) {
454 el
= FN(EL
,copy
)(list
->p
[pos
[i
]]);
455 slice
= FN(LIST(EL
),add
)(slice
, el
);
458 return fn(slice
, user
);
461 /* Call "fn" on each of the strongly connected components (SCCs) of
462 * the graph with as vertices the elements of "list" and
463 * a directed edge from node b to node a iff follows(a, b)
464 * returns 1. follows should return -1 on error.
466 * If SCC a contains a node i that follows a node j in another SCC b
467 * (i.e., follows(i, j, user) returns 1), then fn will be called on SCC a
468 * after being called on SCC b.
470 * We simply call isl_tarjan_graph_init, extract the SCCs from the result and
471 * call fn on each of them.
473 isl_stat
FN(LIST(EL
),foreach_scc
)(__isl_keep
LIST(EL
) *list
,
474 isl_bool (*follows
)(__isl_keep EL
*a
, __isl_keep EL
*b
, void *user
),
476 isl_stat (*fn
)(__isl_take
LIST(EL
) *scc
, void *user
), void *fn_user
)
478 S(LIST(EL
),foreach_scc_data
) data
= { list
, follows
, follows_user
};
481 struct isl_tarjan_graph
*g
;
484 return isl_stat_error
;
488 return fn(FN(LIST(EL
),copy
)(list
), fn_user
);
490 ctx
= FN(LIST(EL
),get_ctx
)(list
);
492 g
= isl_tarjan_graph_init(ctx
, n
, &FN(LIST(EL
),follows
), &data
);
494 return isl_stat_error
;
500 if (g
->order
[i
] == -1)
501 isl_die(ctx
, isl_error_internal
, "cannot happen",
504 while (g
->order
[i
] != -1) {
507 if (first
== 0 && n
== 0) {
508 isl_tarjan_graph_free(g
);
509 return fn(FN(LIST(EL
),copy
)(list
), fn_user
);
511 if (FN(LIST(EL
),call_on_scc
)(list
, g
->order
+ first
, i
- first
,
517 isl_tarjan_graph_free(g
);
519 return n
> 0 ? isl_stat_error
: isl_stat_ok
;
522 __isl_give
LIST(EL
) *FN(FN(LIST(EL
),from
),BASE
)(__isl_take EL
*el
)
529 ctx
= FN(EL
,get_ctx
)(el
);
530 list
= FN(LIST(EL
),alloc
)(ctx
, 1);
533 list
= FN(LIST(EL
),add
)(list
, el
);
540 /* Append the elements of "list2" to "list1", where "list1" is known
541 * to have only a single reference and enough room to hold
542 * the extra elements.
544 static __isl_give
LIST(EL
) *FN(LIST(EL
),concat_inplace
)(
545 __isl_take
LIST(EL
) *list1
, __isl_take
LIST(EL
) *list2
)
549 for (i
= 0; i
< list2
->n
; ++i
)
550 list1
= FN(LIST(EL
),add
)(list1
, FN(EL
,copy
)(list2
->p
[i
]));
551 FN(LIST(EL
),free
)(list2
);
555 /* Concatenate "list1" and "list2".
556 * If "list1" has only one reference and has enough room
557 * for the elements of "list2", the add the elements to "list1" itself.
558 * Otherwise, create a new list to store the result.
560 __isl_give
LIST(EL
) *FN(LIST(EL
),concat
)(__isl_take
LIST(EL
) *list1
,
561 __isl_take
LIST(EL
) *list2
)
567 if (!list1
|| !list2
)
570 if (list1
->ref
== 1 && list1
->n
+ list2
->n
<= list1
->size
)
571 return FN(LIST(EL
),concat_inplace
)(list1
, list2
);
573 ctx
= FN(LIST(EL
),get_ctx
)(list1
);
574 res
= FN(LIST(EL
),alloc
)(ctx
, list1
->n
+ list2
->n
);
575 for (i
= 0; i
< list1
->n
; ++i
)
576 res
= FN(LIST(EL
),add
)(res
, FN(EL
,copy
)(list1
->p
[i
]));
577 for (i
= 0; i
< list2
->n
; ++i
)
578 res
= FN(LIST(EL
),add
)(res
, FN(EL
,copy
)(list2
->p
[i
]));
580 FN(LIST(EL
),free
)(list1
);
581 FN(LIST(EL
),free
)(list2
);
584 FN(LIST(EL
),free
)(list1
);
585 FN(LIST(EL
),free
)(list2
);
589 __isl_give isl_printer
*CAT(isl_printer_print_
,LIST(BASE
))(
590 __isl_take isl_printer
*p
, __isl_keep
LIST(EL
) *list
)
596 p
= isl_printer_print_str(p
, "(");
597 for (i
= 0; i
< list
->n
; ++i
) {
599 p
= isl_printer_print_str(p
, ",");
600 p
= CAT(isl_printer_print_
,BASE
)(p
, list
->p
[i
]);
602 p
= isl_printer_print_str(p
, ")");
609 void FN(LIST(EL
),dump
)(__isl_keep
LIST(EL
) *list
)
611 isl_printer
*printer
;
616 printer
= isl_printer_to_file(FN(LIST(EL
),get_ctx
)(list
), stderr
);
617 printer
= CAT(isl_printer_print_
,LIST(BASE
))(printer
, list
);
618 printer
= isl_printer_end_line(printer
);
620 isl_printer_free(printer
);