~ubuntu-branches/ubuntu/trusty/cloog/trusty

« back to all changes in this revision

Viewing changes to isl/isl_list_templ.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-01-13 04:29:53 UTC
  • mfrom: (3.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20130113042953-yffow2nvsub33dcd
New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * Copyright 2008-2009 Katholieke Universiteit Leuven
3
3
 * Copyright 2011      INRIA Saclay
 
4
 * Copyright 2012      Ecole Normale Superieure
4
5
 *
5
 
 * Use of this software is governed by the GNU LGPLv2.1 license
 
6
 * Use of this software is governed by the MIT license
6
7
 *
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
11
13
 */
12
14
 
13
15
#define xCAT(A,B) A ## B
70
72
        return dup;
71
73
}
72
74
 
 
75
__isl_give LIST(EL) *FN(LIST(EL),cow)(__isl_take LIST(EL) *list)
 
76
{
 
77
        if (!list)
 
78
                return NULL;
 
79
 
 
80
        if (list->ref == 1)
 
81
                return list;
 
82
        list->ref--;
 
83
        return FN(LIST(EL),dup)(list);
 
84
}
 
85
 
 
86
/* Make sure "list" has room for at least "n" more pieces.
 
87
 *
 
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.
 
90
 */
 
91
static __isl_give LIST(EL) *FN(LIST(EL),grow)(__isl_take LIST(EL) *list, int n)
 
92
{
 
93
        isl_ctx *ctx;
 
94
        int i, new_size;
 
95
        LIST(EL) *res;
 
96
 
 
97
        if (!list)
 
98
                return NULL;
 
99
        if (list->n + n <= list->size)
 
100
                return list;
 
101
 
 
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 *));
 
107
                if (!res)
 
108
                        return FN(LIST(EL),free)(list);
 
109
                res->size = new_size;
 
110
                return res;
 
111
        }
 
112
 
 
113
        res = FN(LIST(EL),alloc)(ctx, new_size);
 
114
        if (!res)
 
115
                return FN(LIST(EL),free)(list);
 
116
 
 
117
        for (i = 0; i < list->n; ++i)
 
118
                res = FN(LIST(EL),add)(res, FN(EL,copy)(list->p[i]));
 
119
 
 
120
        FN(LIST(EL),free)(list);
 
121
        return res;
 
122
}
 
123
 
73
124
__isl_give LIST(EL) *FN(LIST(EL),add)(__isl_take LIST(EL) *list,
74
125
        __isl_take struct EL *el)
75
126
{
 
127
        list = FN(LIST(EL),grow)(list, 1);
76
128
        if (!list || !el)
77
129
                goto error;
78
 
        isl_assert(list->ctx, list->n < list->size, goto error);
79
130
        list->p[list->n] = el;
80
131
        list->n++;
81
132
        return list;
85
136
        return NULL;
86
137
}
87
138
 
 
139
/* Remove the "n" elements starting at "first" from "list".
 
140
 */
 
141
__isl_give LIST(EL) *FN(LIST(EL),drop)(__isl_take LIST(EL) *list,
 
142
        unsigned first, unsigned n)
 
143
{
 
144
        int i;
 
145
 
 
146
        if (!list)
 
147
                return NULL;
 
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));
 
151
        if (n == 0)
 
152
                return list;
 
153
        list = FN(LIST(EL),cow)(list);
 
154
        if (!list)
 
155
                return NULL;
 
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];
 
160
        list->n -= n;
 
161
        return list;
 
162
}
 
163
 
 
164
/* Insert "el" at position "pos" in "list".
 
165
 *
 
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".
 
170
 */
 
171
__isl_give LIST(EL) *FN(LIST(EL),insert)(__isl_take LIST(EL) *list,
 
172
        unsigned pos, __isl_take struct EL *el)
 
173
{
 
174
        int i;
 
175
        isl_ctx *ctx;
 
176
        LIST(EL) *res;
 
177
 
 
178
        if (!list || !el)
 
179
                goto error;
 
180
        ctx = FN(LIST(EL),get_ctx)(list);
 
181
        if (pos > list->n)
 
182
                isl_die(ctx, isl_error_invalid,
 
183
                        "index out of bounds", goto error);
 
184
 
 
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];
 
188
                list->n++;
 
189
                list->p[pos] = el;
 
190
                return list;
 
191
        }
 
192
 
 
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);
 
200
 
 
201
        return res;
 
202
error:
 
203
        FN(EL,free)(el);
 
204
        FN(LIST(EL),free)(list);
 
205
        return NULL;
 
206
}
 
207
 
88
208
void *FN(LIST(EL),free)(__isl_take LIST(EL) *list)
89
209
{
90
210
        int i;
118
238
        return FN(EL,copy)(list->p[index]);
119
239
}
120
240
 
 
241
/* Replace the element at position "index" in "list" by "el".
 
242
 */
 
243
__isl_give LIST(EL) *FN(FN(LIST(EL),set),BASE)(__isl_take LIST(EL) *list,
 
244
        int index, __isl_take EL *el)
 
245
{
 
246
        if (!list || !el)
 
247
                goto error;
 
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) {
 
252
                FN(EL,free)(el);
 
253
                return list;
 
254
        }
 
255
        list = FN(LIST(EL),cow)(list);
 
256
        if (!list)
 
257
                goto error;
 
258
        FN(EL,free)(list->p[index]);
 
259
        list->p[index] = el;
 
260
        return list;
 
261
error:
 
262
        FN(EL,free)(el);
 
263
        FN(LIST(EL),free)(list);
 
264
        return NULL;
 
265
}
 
266
 
121
267
int FN(LIST(EL),foreach)(__isl_keep LIST(EL) *list,
122
268
        int (*fn)(__isl_take EL *el, void *user), void *user)
123
269
{