加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
isl_list_templ.c 16.16 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
/*
* Copyright 2008-2009 Katholieke Universiteit Leuven
* Copyright 2011 INRIA Saclay
* Copyright 2012-2013 Ecole Normale Superieure
* Copyright 2017 Sven Verdoolaege
*
* Use of this software is governed by the MIT license
*
* Written by Sven Verdoolaege, K.U.Leuven, Departement
* Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
* and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
* ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
* and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
*/
#include <isl_sort.h>
#include <isl_tarjan.h>
#include <isl/printer.h>
#include <isl_list_macro.h>
#define xS(TYPE,NAME) struct TYPE ## _ ## NAME
#define S(TYPE,NAME) xS(TYPE,NAME)
isl_ctx *FN(LIST(EL),get_ctx)(__isl_keep LIST(EL) *list)
{
return list ? list->ctx : NULL;
}
__isl_give LIST(EL) *FN(LIST(EL),alloc)(isl_ctx *ctx, int n)
{
LIST(EL) *list;
if (n < 0)
isl_die(ctx, isl_error_invalid,
"cannot create list of negative length",
return NULL);
list = isl_alloc(ctx, LIST(EL),
sizeof(LIST(EL)) + (n - 1) * sizeof(struct EL *));
if (!list)
return NULL;
list->ctx = ctx;
isl_ctx_ref(ctx);
list->ref = 1;
list->size = n;
list->n = 0;
return list;
}
__isl_give LIST(EL) *FN(LIST(EL),copy)(__isl_keep LIST(EL) *list)
{
if (!list)
return NULL;
list->ref++;
return list;
}
__isl_give LIST(EL) *FN(LIST(EL),dup)(__isl_keep LIST(EL) *list)
{
int i;
LIST(EL) *dup;
if (!list)
return NULL;
dup = FN(LIST(EL),alloc)(FN(LIST(EL),get_ctx)(list), list->n);
if (!dup)
return NULL;
for (i = 0; i < list->n; ++i)
dup = FN(LIST(EL),add)(dup, FN(EL,copy)(list->p[i]));
return dup;
}
__isl_give LIST(EL) *FN(LIST(EL),cow)(__isl_take LIST(EL) *list)
{
if (!list)
return NULL;
if (list->ref == 1)
return list;
list->ref--;
return FN(LIST(EL),dup)(list);
}
/* Make sure "list" has room for at least "n" more pieces.
* Always return a list with a single reference.
*
* If there is only one reference to list, we extend it in place.
* Otherwise, we create a new LIST(EL) and copy the elements.
*/
static __isl_give LIST(EL) *FN(LIST(EL),grow)(__isl_take LIST(EL) *list, int n)
{
isl_ctx *ctx;
int i, new_size;
LIST(EL) *res;
if (!list)
return NULL;
if (list->ref == 1 && list->n + n <= list->size)
return list;
ctx = FN(LIST(EL),get_ctx)(list);
new_size = ((list->n + n + 1) * 3) / 2;
if (list->ref == 1) {
res = isl_realloc(ctx, list, LIST(EL),
sizeof(LIST(EL)) + (new_size - 1) * sizeof(EL *));
if (!res)
return FN(LIST(EL),free)(list);
res->size = new_size;
return res;
}
if (list->n + n <= list->size && list->size < new_size)
new_size = list->size;
res = FN(LIST(EL),alloc)(ctx, new_size);
if (!res)
return FN(LIST(EL),free)(list);
for (i = 0; i < list->n; ++i)
res = FN(LIST(EL),add)(res, FN(EL,copy)(list->p[i]));
FN(LIST(EL),free)(list);
return res;
}
/* Check that "index" is a valid position in "list".
*/
static isl_stat FN(LIST(EL),check_index)(__isl_keep LIST(EL) *list, int index)
{
if (!list)
return isl_stat_error;
if (index < 0 || index >= list->n)
isl_die(FN(LIST(EL),get_ctx)(list), isl_error_invalid,
"index out of bounds", return isl_stat_error);
return isl_stat_ok;
}
__isl_give LIST(EL) *FN(LIST(EL),add)(__isl_take LIST(EL) *list,
__isl_take struct EL *el)
{
list = FN(LIST(EL),grow)(list, 1);
if (!list || !el)
goto error;
list->p[list->n] = el;
list->n++;
return list;
error:
FN(EL,free)(el);
FN(LIST(EL),free)(list);
return NULL;
}
/* Remove the "n" elements starting at "first" from "list".
*/
__isl_give LIST(EL) *FN(LIST(EL),drop)(__isl_take LIST(EL) *list,
unsigned first, unsigned n)
{
int i;
if (!list)
return NULL;
if (first + n > list->n || first + n < first)
isl_die(list->ctx, isl_error_invalid,
"index out of bounds", return FN(LIST(EL),free)(list));
if (n == 0)
return list;
list = FN(LIST(EL),cow)(list);
if (!list)
return NULL;
for (i = 0; i < n; ++i)
FN(EL,free)(list->p[first + i]);
for (i = first; i + n < list->n; ++i)
list->p[i] = list->p[i + n];
list->n -= n;
return list;
}
/* Remove all elements from "list".
*/
__isl_give LIST(EL) *FN(LIST(EL),clear)(__isl_take LIST(EL) *list)
{
if (!list)
return NULL;
return FN(LIST(EL),drop)(list, 0, list->n);
}
/* Insert "el" at position "pos" in "list".
*
* If there is only one reference to "list" and if it already has space
* for one extra element, we insert it directly into "list".
* Otherwise, we create a new list consisting of "el" and copied
* elements from "list".
*/
__isl_give LIST(EL) *FN(LIST(EL),insert)(__isl_take LIST(EL) *list,
unsigned pos, __isl_take struct EL *el)
{
int i;
isl_ctx *ctx;
LIST(EL) *res;
if (!list || !el)
goto error;
ctx = FN(LIST(EL),get_ctx)(list);
if (pos > list->n)
isl_die(ctx, isl_error_invalid,
"index out of bounds", goto error);
if (list->ref == 1 && list->size > list->n) {
for (i = list->n; i > pos; --i)
list->p[i] = list->p[i - 1];
list->n++;
list->p[pos] = el;
return list;
}
res = FN(LIST(EL),alloc)(ctx, list->n + 1);
for (i = 0; i < pos; ++i)
res = FN(LIST(EL),add)(res, FN(EL,copy)(list->p[i]));
res = FN(LIST(EL),add)(res, el);
for (i = pos; i < list->n; ++i)
res = FN(LIST(EL),add)(res, FN(EL,copy)(list->p[i]));
FN(LIST(EL),free)(list);
return res;
error:
FN(EL,free)(el);
FN(LIST(EL),free)(list);
return NULL;
}
__isl_null LIST(EL) *FN(LIST(EL),free)(__isl_take LIST(EL) *list)
{
int i;
if (!list)
return NULL;
if (--list->ref > 0)
return NULL;
isl_ctx_deref(list->ctx);
for (i = 0; i < list->n; ++i)
FN(EL,free)(list->p[i]);
free(list);
return NULL;
}
/* Return the number of elements in "list".
*/
isl_size FN(LIST(EL),size)(__isl_keep LIST(EL) *list)
{
return list ? list->n : isl_size_error;
}
/* This is an alternative name for the function above.
*/
isl_size FN(FN(LIST(EL),n),EL_BASE)(__isl_keep LIST(EL) *list)
{
return FN(LIST(EL),size)(list);
}
/* Return the element at position "index" in "list".
*/
__isl_keep EL *FN(LIST(EL),peek)(__isl_keep LIST(EL) *list, int index)
{
if (FN(LIST(EL),check_index)(list, index) < 0)
return NULL;
return list->p[index];
}
/* Return a copy of the element at position "index" in "list".
*/
__isl_give EL *FN(LIST(EL),get_at)(__isl_keep LIST(EL) *list, int index)
{
return FN(EL,copy)(FN(LIST(EL),peek)(list, index));
}
/* This is an alternative name for the function above.
*/
__isl_give EL *FN(FN(LIST(EL),get),EL_BASE)(__isl_keep LIST(EL) *list,
int index)
{
return FN(LIST(EL),get_at)(list, index);
}
/* Replace the element at position "index" in "list" by "el".
*/
__isl_give LIST(EL) *FN(FN(LIST(EL),set),EL_BASE)(__isl_take LIST(EL) *list,
int index, __isl_take EL *el)
{
if (!list || !el)
goto error;
if (FN(LIST(EL),check_index)(list, index) < 0)
goto error;
if (list->p[index] == el) {
FN(EL,free)(el);
return list;
}
list = FN(LIST(EL),cow)(list);
if (!list)
goto error;
FN(EL,free)(list->p[index]);
list->p[index] = el;
return list;
error:
FN(EL,free)(el);
FN(LIST(EL),free)(list);
return NULL;
}
/* Return the element at position "index" of "list".
* This may be either a copy or the element itself
* if there is only one reference to "list".
* This allows the element to be modified inplace
* if both the list and the element have only a single reference.
* The caller is not allowed to modify "list" between
* this call to isl_list_*_take_* and a subsequent call
* to isl_list_*_restore_*.
* The only exception is that isl_list_*_free can be called instead.
*/
static __isl_give EL *FN(FN(LIST(EL),take),EL_BASE)(__isl_keep LIST(EL) *list,
int index)
{
EL *el;
if (FN(LIST(EL),check_index)(list, index) < 0)
return NULL;
if (list->ref != 1)
return FN(FN(LIST(EL),get),EL_BASE)(list, index);
el = list->p[index];
list->p[index] = NULL;
return el;
}
/* Set the element at position "index" of "list" to "el",
* where the position may be empty due to a previous call
* to isl_list_*_take_*.
*/
static __isl_give LIST(EL) *FN(FN(LIST(EL),restore),EL_BASE)(
__isl_take LIST(EL) *list, int index, __isl_take EL *el)
{
return FN(FN(LIST(EL),set),EL_BASE)(list, index, el);
}
/* Swap the elements of "list" in positions "pos1" and "pos2".
*/
__isl_give LIST(EL) *FN(LIST(EL),swap)(__isl_take LIST(EL) *list,
unsigned pos1, unsigned pos2)
{
EL *el1, *el2;
if (pos1 == pos2)
return list;
el1 = FN(FN(LIST(EL),take),EL_BASE)(list, pos1);
el2 = FN(FN(LIST(EL),take),EL_BASE)(list, pos2);
list = FN(FN(LIST(EL),restore),EL_BASE)(list, pos1, el2);
list = FN(FN(LIST(EL),restore),EL_BASE)(list, pos2, el1);
return list;
}
/* Reverse the elements of "list".
*/
__isl_give LIST(EL) *FN(LIST(EL),reverse)(__isl_take LIST(EL) *list)
{
int i, n;
n = FN(LIST(EL),size)(list);
for (i = 0; i < n - 1 - i; ++i)
list = FN(LIST(EL),swap)(list, i, n - 1 - i);
return list;
}
isl_stat FN(LIST(EL),foreach)(__isl_keep LIST(EL) *list,
isl_stat (*fn)(__isl_take EL *el, void *user), void *user)
{
int i;
if (!list)
return isl_stat_error;
for (i = 0; i < list->n; ++i) {
EL *el = FN(EL,copy)(list->p[i]);
if (!el)
return isl_stat_error;
if (fn(el, user) < 0)
return isl_stat_error;
}
return isl_stat_ok;
}
/* Does "test" succeed on every element of "list"?
*/
isl_bool FN(LIST(EL),every)(__isl_keep LIST(EL) *list,
isl_bool (*test)(__isl_keep EL *el, void *user), void *user)
{
int i;
if (!list)
return isl_bool_error;
for (i = 0; i < list->n; ++i) {
isl_bool r;
r = test(list->p[i], user);
if (r < 0 || !r)
return r;
}
return isl_bool_true;
}
/* Replace each element in "list" by the result of calling "fn"
* on the element.
*/
__isl_give LIST(EL) *FN(LIST(EL),map)(__isl_keep LIST(EL) *list,
__isl_give EL *(*fn)(__isl_take EL *el, void *user), void *user)
{
int i, n;
if (!list)
return NULL;
n = list->n;
for (i = 0; i < n; ++i) {
EL *el = FN(FN(LIST(EL),take),EL_BASE)(list, i);
if (!el)
return FN(LIST(EL),free)(list);
el = fn(el, user);
list = FN(FN(LIST(EL),restore),EL_BASE)(list, i, el);
}
return list;
}
/* Internal data structure for isl_*_list_sort.
*
* "cmp" is the original comparison function.
* "user" is a user provided pointer that should be passed to "cmp".
*/
S(LIST(EL),sort_data) {
int (*cmp)(__isl_keep EL *a, __isl_keep EL *b, void *user);
void *user;
};
/* Compare two entries of an isl_*_list based on the user provided
* comparison function on pairs of isl_* objects.
*/
static int FN(LIST(EL),cmp)(const void *a, const void *b, void *user)
{
S(LIST(EL),sort_data) *data = user;
EL * const *el1 = a;
EL * const *el2 = b;
return data->cmp(*el1, *el2, data->user);
}
/* Sort the elements of "list" in ascending order according to
* comparison function "cmp".
*/
__isl_give LIST(EL) *FN(LIST(EL),sort)(__isl_take LIST(EL) *list,
int (*cmp)(__isl_keep EL *a, __isl_keep EL *b, void *user), void *user)
{
S(LIST(EL),sort_data) data = { cmp, user };
if (!list)
return NULL;
if (list->n <= 1)
return list;
list = FN(LIST(EL),cow)(list);
if (!list)
return NULL;
if (isl_sort(list->p, list->n, sizeof(list->p[0]),
&FN(LIST(EL),cmp), &data) < 0)
return FN(LIST(EL),free)(list);
return list;
}
/* Internal data structure for isl_*_list_foreach_scc.
*
* "list" is the original list.
* "follows" is the user provided callback that defines the edges of the graph.
*/
S(LIST(EL),foreach_scc_data) {
LIST(EL) *list;
isl_bool (*follows)(__isl_keep EL *a, __isl_keep EL *b, void *user);
void *follows_user;
};
/* Does element i of data->list follow element j?
*
* Use the user provided callback to find out.
*/
static isl_bool FN(LIST(EL),follows)(int i, int j, void *user)
{
S(LIST(EL),foreach_scc_data) *data = user;
return data->follows(data->list->p[i], data->list->p[j],
data->follows_user);
}
/* Call "fn" on the sublist of "list" that consists of the elements
* with indices specified by the "n" elements of "pos".
*/
static isl_stat FN(LIST(EL),call_on_scc)(__isl_keep LIST(EL) *list, int *pos,
int n, isl_stat (*fn)(__isl_take LIST(EL) *scc, void *user), void *user)
{
int i;
isl_ctx *ctx;
LIST(EL) *slice;
ctx = FN(LIST(EL),get_ctx)(list);
slice = FN(LIST(EL),alloc)(ctx, n);
for (i = 0; i < n; ++i) {
EL *el;
el = FN(EL,copy)(list->p[pos[i]]);
slice = FN(LIST(EL),add)(slice, el);
}
return fn(slice, user);
}
/* Call "fn" on each of the strongly connected components (SCCs) of
* the graph with as vertices the elements of "list" and
* a directed edge from node b to node a iff follows(a, b)
* returns 1. follows should return -1 on error.
*
* If SCC a contains a node i that follows a node j in another SCC b
* (i.e., follows(i, j, user) returns 1), then fn will be called on SCC a
* after being called on SCC b.
*
* We simply call isl_tarjan_graph_init, extract the SCCs from the result and
* call fn on each of them.
*/
isl_stat FN(LIST(EL),foreach_scc)(__isl_keep LIST(EL) *list,
isl_bool (*follows)(__isl_keep EL *a, __isl_keep EL *b, void *user),
void *follows_user,
isl_stat (*fn)(__isl_take LIST(EL) *scc, void *user), void *fn_user)
{
S(LIST(EL),foreach_scc_data) data = { list, follows, follows_user };
int i, n;
isl_ctx *ctx;
struct isl_tarjan_graph *g;
if (!list)
return isl_stat_error;
if (list->n == 0)
return isl_stat_ok;
if (list->n == 1)
return fn(FN(LIST(EL),copy)(list), fn_user);
ctx = FN(LIST(EL),get_ctx)(list);
n = list->n;
g = isl_tarjan_graph_init(ctx, n, &FN(LIST(EL),follows), &data);
if (!g)
return isl_stat_error;
i = 0;
do {
int first;
if (g->order[i] == -1)
isl_die(ctx, isl_error_internal, "cannot happen",
break);
first = i;
while (g->order[i] != -1) {
++i; --n;
}
if (first == 0 && n == 0) {
isl_tarjan_graph_free(g);
return fn(FN(LIST(EL),copy)(list), fn_user);
}
if (FN(LIST(EL),call_on_scc)(list, g->order + first, i - first,
fn, fn_user) < 0)
break;
++i;
} while (n);
isl_tarjan_graph_free(g);
return n > 0 ? isl_stat_error : isl_stat_ok;
}
__isl_give LIST(EL) *FN(FN(LIST(EL),from),EL_BASE)(__isl_take EL *el)
{
isl_ctx *ctx;
LIST(EL) *list;
if (!el)
return NULL;
ctx = FN(EL,get_ctx)(el);
list = FN(LIST(EL),alloc)(ctx, 1);
if (!list)
goto error;
list = FN(LIST(EL),add)(list, el);
return list;
error:
FN(EL,free)(el);
return NULL;
}
/* This function performs the same operation as isl_*_list_from_*,
* but is considered as a function on the element when exported.
*/
__isl_give LIST(EL) *FN(EL,to_list)(__isl_take EL *el)
{
return FN(FN(LIST(EL),from),EL_BASE)(el);
}
/* Append the elements of "list2" to "list1", where "list1" is known
* to have only a single reference and enough room to hold
* the extra elements.
*/
static __isl_give LIST(EL) *FN(LIST(EL),concat_inplace)(
__isl_take LIST(EL) *list1, __isl_take LIST(EL) *list2)
{
int i;
for (i = 0; i < list2->n; ++i)
list1 = FN(LIST(EL),add)(list1, FN(EL,copy)(list2->p[i]));
FN(LIST(EL),free)(list2);
return list1;
}
/* Concatenate "list1" and "list2".
* If "list1" has only one reference and has enough room
* for the elements of "list2", the add the elements to "list1" itself.
* Otherwise, create a new list to store the result.
*/
__isl_give LIST(EL) *FN(LIST(EL),concat)(__isl_take LIST(EL) *list1,
__isl_take LIST(EL) *list2)
{
int i;
isl_ctx *ctx;
LIST(EL) *res;
if (!list1 || !list2)
goto error;
if (list1->ref == 1 && list1->n + list2->n <= list1->size)
return FN(LIST(EL),concat_inplace)(list1, list2);
ctx = FN(LIST(EL),get_ctx)(list1);
res = FN(LIST(EL),alloc)(ctx, list1->n + list2->n);
for (i = 0; i < list1->n; ++i)
res = FN(LIST(EL),add)(res, FN(EL,copy)(list1->p[i]));
for (i = 0; i < list2->n; ++i)
res = FN(LIST(EL),add)(res, FN(EL,copy)(list2->p[i]));
FN(LIST(EL),free)(list1);
FN(LIST(EL),free)(list2);
return res;
error:
FN(LIST(EL),free)(list1);
FN(LIST(EL),free)(list2);
return NULL;
}
__isl_give isl_printer *CAT(isl_printer_print_,LIST(EL_BASE))(
__isl_take isl_printer *p, __isl_keep LIST(EL) *list)
{
int i;
if (!p || !list)
goto error;
p = isl_printer_print_str(p, "(");
for (i = 0; i < list->n; ++i) {
if (i)
p = isl_printer_print_str(p, ",");
p = CAT(isl_printer_print_,EL_BASE)(p, list->p[i]);
}
p = isl_printer_print_str(p, ")");
return p;
error:
isl_printer_free(p);
return NULL;
}
#undef BASE
#define BASE LIST(EL_BASE)
#define PRINT_DUMP_DEFAULT 0
#include "print_templ.c"
#undef PRINT_DUMP_DEFAULT
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化