2
2
* Copyright 2008-2009 Katholieke Universiteit Leuven
3
3
* Copyright 2011 INRIA Saclay
4
* Copyright 2012 Ecole Normale Superieure
5
* Use of this software is governed by the GNU LGPLv2.1 license
6
* Use of this software is governed by the MIT license
7
8
* Written by Sven Verdoolaege, K.U.Leuven, Departement
8
9
* Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9
10
* and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10
11
* ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12
* and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13
15
#define xCAT(A,B) A ## B
75
__isl_give LIST(EL) *FN(LIST(EL),cow)(__isl_take LIST(EL) *list)
83
return FN(LIST(EL),dup)(list);
86
/* Make sure "list" has room for at least "n" more pieces.
88
* If there is only one reference to list, we extend it in place.
89
* Otherwise, we create a new LIST(EL) and copy the elements.
91
static __isl_give LIST(EL) *FN(LIST(EL),grow)(__isl_take LIST(EL) *list, int n)
99
if (list->n + n <= list->size)
102
ctx = FN(LIST(EL),get_ctx)(list);
103
new_size = ((list->n + n + 1) * 3) / 2;
104
if (list->ref == 1) {
105
res = isl_realloc(ctx, list, LIST(EL),
106
sizeof(LIST(EL)) + (new_size - 1) * sizeof(EL *));
108
return FN(LIST(EL),free)(list);
109
res->size = new_size;
113
res = FN(LIST(EL),alloc)(ctx, new_size);
115
return FN(LIST(EL),free)(list);
117
for (i = 0; i < list->n; ++i)
118
res = FN(LIST(EL),add)(res, FN(EL,copy)(list->p[i]));
120
FN(LIST(EL),free)(list);
73
124
__isl_give LIST(EL) *FN(LIST(EL),add)(__isl_take LIST(EL) *list,
74
125
__isl_take struct EL *el)
127
list = FN(LIST(EL),grow)(list, 1);
78
isl_assert(list->ctx, list->n < list->size, goto error);
79
130
list->p[list->n] = el;
139
/* Remove the "n" elements starting at "first" from "list".
141
__isl_give LIST(EL) *FN(LIST(EL),drop)(__isl_take LIST(EL) *list,
142
unsigned first, unsigned n)
148
if (first + n > list->n || first + n < first)
149
isl_die(list->ctx, isl_error_invalid,
150
"index out of bounds", return FN(LIST(EL),free)(list));
153
list = FN(LIST(EL),cow)(list);
156
for (i = 0; i < n; ++i)
157
FN(EL,free)(list->p[first + i]);
158
for (i = first; i + n < list->n; ++i)
159
list->p[i] = list->p[i + n];
164
/* Insert "el" at position "pos" in "list".
166
* If there is only one reference to "list" and if it already has space
167
* for one extra element, we insert it directly into "list".
168
* Otherwise, we create a new list consisting of "el" and copied
169
* elements from "list".
171
__isl_give LIST(EL) *FN(LIST(EL),insert)(__isl_take LIST(EL) *list,
172
unsigned pos, __isl_take struct EL *el)
180
ctx = FN(LIST(EL),get_ctx)(list);
182
isl_die(ctx, isl_error_invalid,
183
"index out of bounds", goto error);
185
if (list->ref == 1 && list->size > list->n) {
186
for (i = list->n - 1; i >= pos; --i)
187
list->p[i + 1] = list->p[i];
193
res = FN(LIST(EL),alloc)(ctx, list->n + 1);
194
for (i = 0; i < pos; ++i)
195
res = FN(LIST(EL),add)(res, FN(EL,copy)(list->p[i]));
196
res = FN(LIST(EL),add)(res, el);
197
for (i = pos; i < list->n; ++i)
198
res = FN(LIST(EL),add)(res, FN(EL,copy)(list->p[i]));
199
FN(LIST(EL),free)(list);
204
FN(LIST(EL),free)(list);
88
208
void *FN(LIST(EL),free)(__isl_take LIST(EL) *list)
118
238
return FN(EL,copy)(list->p[index]);
241
/* Replace the element at position "index" in "list" by "el".
243
__isl_give LIST(EL) *FN(FN(LIST(EL),set),BASE)(__isl_take LIST(EL) *list,
244
int index, __isl_take EL *el)
248
if (index < 0 || index >= list->n)
249
isl_die(list->ctx, isl_error_invalid,
250
"index out of bounds", goto error);
251
if (list->p[index] == el) {
255
list = FN(LIST(EL),cow)(list);
258
FN(EL,free)(list->p[index]);
263
FN(LIST(EL),free)(list);
121
267
int FN(LIST(EL),foreach)(__isl_keep LIST(EL) *list,
122
268
int (*fn)(__isl_take EL *el, void *user), void *user)