isl_pw_qpolynomial_gist: drop empty pieces in result
[platform/upstream/isl.git] / isl_pw_templ.c
1 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
2 #define FN(TYPE,NAME) xFN(TYPE,NAME)
3 #define xS(TYPE,NAME) struct TYPE ## _ ## NAME
4 #define S(TYPE,NAME) xS(TYPE,NAME)
5
6 #ifdef HAS_TYPE
7 static __isl_give PW *FN(PW,alloc_)(__isl_take isl_dim *dim,
8         enum isl_fold type, int n)
9 #else
10 static __isl_give PW *FN(PW,alloc_)(__isl_take isl_dim *dim, int n)
11 #endif
12 {
13         struct PW *pw;
14
15         if (!dim)
16                 return NULL;
17         isl_assert(dim->ctx, n >= 0, goto error);
18         pw = isl_alloc(dim->ctx, struct PW,
19                         sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
20         if (!pw)
21                 goto error;
22
23         pw->ref = 1;
24 #ifdef HAS_TYPE
25         pw->type = type;
26 #endif
27         pw->size = n;
28         pw->n = 0;
29         pw->dim = dim;
30         return pw;
31 error:
32         isl_dim_free(dim);
33         return NULL;
34 }
35
36 #ifdef HAS_TYPE
37 __isl_give PW *FN(PW,zero)(__isl_take isl_dim *dim, enum isl_fold type)
38 {
39         return FN(PW,alloc_)(dim, type, 0);
40 }
41 #else
42 __isl_give PW *FN(PW,zero)(__isl_take isl_dim *dim)
43 {
44         return FN(PW,alloc_)(dim, 0);
45 }
46 #endif
47
48 __isl_give PW *FN(PW,add_piece)(__isl_take PW *pw,
49         __isl_take isl_set *set, __isl_take EL *el)
50 {
51         if (!pw || !set || !el)
52                 goto error;
53
54         if (isl_set_fast_is_empty(set) || FN(EL,IS_ZERO)(el)) {
55                 isl_set_free(set);
56                 FN(EL,free)(el);
57                 return pw;
58         }
59
60 #ifdef HAS_TYPE
61         if (pw->type != el->type)
62                 isl_die(set->ctx, isl_error_invalid, "fold types don't match",
63                         goto error);
64 #endif
65         isl_assert(set->ctx, isl_dim_equal(pw->dim, el->dim), goto error);
66         isl_assert(set->ctx, pw->n < pw->size, goto error);
67
68         pw->p[pw->n].set = set;
69         pw->p[pw->n].FIELD = el;
70         pw->n++;
71         
72         return pw;
73 error:
74         FN(PW,free)(pw);
75         isl_set_free(set);
76         FN(EL,free)(el);
77         return NULL;
78 }
79
80 #ifdef HAS_TYPE
81 __isl_give PW *FN(PW,alloc)(enum isl_fold type,
82         __isl_take isl_set *set, __isl_take EL *el)
83 #else
84 __isl_give PW *FN(PW,alloc)(__isl_take isl_set *set, __isl_take EL *el)
85 #endif
86 {
87         PW *pw;
88
89         if (!set || !el)
90                 goto error;
91
92 #ifdef HAS_TYPE
93         pw = FN(PW,alloc_)(isl_set_get_dim(set), type, 1);
94 #else
95         pw = FN(PW,alloc_)(isl_set_get_dim(set), 1);
96 #endif
97
98         return FN(PW,add_piece)(pw, set, el);
99 error:
100         isl_set_free(set);
101         FN(EL,free)(el);
102         return NULL;
103 }
104
105 __isl_give PW *FN(PW,dup)(__isl_keep PW *pw)
106 {
107         int i;
108         PW *dup;
109
110         if (!pw)
111                 return NULL;
112
113 #ifdef HAS_TYPE
114         dup = FN(PW,alloc_)(isl_dim_copy(pw->dim), pw->type, pw->n);
115 #else
116         dup = FN(PW,alloc_)(isl_dim_copy(pw->dim), pw->n);
117 #endif
118         if (!dup)
119                 return NULL;
120
121         for (i = 0; i < pw->n; ++i)
122                 dup = FN(PW,add_piece)(dup, isl_set_copy(pw->p[i].set),
123                                             FN(EL,copy)(pw->p[i].FIELD));
124
125         return dup;
126 error:
127         FN(PW,free)(dup);
128         return NULL;
129 }
130
131 __isl_give PW *FN(PW,cow)(__isl_take PW *pw)
132 {
133         if (!pw)
134                 return NULL;
135
136         if (pw->ref == 1)
137                 return pw;
138         pw->ref--;
139         return FN(PW,dup)(pw);
140 }
141
142 __isl_give PW *FN(PW,copy)(__isl_keep PW *pw)
143 {
144         if (!pw)
145                 return NULL;
146
147         pw->ref++;
148         return pw;
149 }
150
151 void FN(PW,free)(__isl_take PW *pw)
152 {
153         int i;
154
155         if (!pw)
156                 return;
157         if (--pw->ref > 0)
158                 return;
159
160         for (i = 0; i < pw->n; ++i) {
161                 isl_set_free(pw->p[i].set);
162                 FN(EL,free)(pw->p[i].FIELD);
163         }
164         isl_dim_free(pw->dim);
165         free(pw);
166 }
167
168 int FN(PW,is_zero)(__isl_keep PW *pw)
169 {
170         if (!pw)
171                 return -1;
172
173         return pw->n == 0;
174 }
175
176 __isl_give PW *FN(PW,add)(__isl_take PW *pw1, __isl_take PW *pw2)
177 {
178         int i, j, n;
179         struct PW *res;
180         isl_set *set;
181
182         if (!pw1 || !pw2)
183                 goto error;
184
185 #ifdef HAS_TYPE
186         if (pw1->type != pw2->type)
187                 isl_die(pw1->dim->ctx, isl_error_invalid,
188                         "fold types don't match", goto error);
189 #endif
190         isl_assert(pw1->dim->ctx, isl_dim_equal(pw1->dim, pw2->dim), goto error);
191
192         if (FN(PW,is_zero)(pw1)) {
193                 FN(PW,free)(pw1);
194                 return pw2;
195         }
196
197         if (FN(PW,is_zero)(pw2)) {
198                 FN(PW,free)(pw2);
199                 return pw1;
200         }
201
202         n = (pw1->n + 1) * (pw2->n + 1);
203 #ifdef HAS_TYPE
204         res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), pw1->type, n);
205 #else
206         res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), n);
207 #endif
208
209         for (i = 0; i < pw1->n; ++i) {
210                 set = isl_set_copy(pw1->p[i].set);
211                 for (j = 0; j < pw2->n; ++j) {
212                         struct isl_set *common;
213                         EL *sum;
214                         set = isl_set_subtract(set,
215                                         isl_set_copy(pw2->p[j].set));
216                         common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
217                                                 isl_set_copy(pw2->p[j].set));
218                         if (isl_set_fast_is_empty(common)) {
219                                 isl_set_free(common);
220                                 continue;
221                         }
222
223                         sum = FN(EL,add_on_domain)(common,
224                                                    FN(EL,copy)(pw1->p[i].FIELD),
225                                                    FN(EL,copy)(pw2->p[j].FIELD));
226
227                         res = FN(PW,add_piece)(res, common, sum);
228                 }
229                 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw1->p[i].FIELD));
230         }
231
232         for (j = 0; j < pw2->n; ++j) {
233                 set = isl_set_copy(pw2->p[j].set);
234                 for (i = 0; i < pw1->n; ++i)
235                         set = isl_set_subtract(set,
236                                         isl_set_copy(pw1->p[i].set));
237                 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw2->p[j].FIELD));
238         }
239
240         FN(PW,free)(pw1);
241         FN(PW,free)(pw2);
242
243         return res;
244 error:
245         FN(PW,free)(pw1);
246         FN(PW,free)(pw2);
247         return NULL;
248 }
249
250 __isl_give PW *FN(PW,add_disjoint)(__isl_take PW *pw1, __isl_take PW *pw2)
251 {
252         int i;
253         PW *res;
254
255         if (!pw1 || !pw2)
256                 goto error;
257
258 #ifdef HAS_TYPE
259         if (pw1->type != pw2->type)
260                 isl_die(pw1->dim->ctx, isl_error_invalid,
261                         "fold types don't match", goto error);
262 #endif
263         isl_assert(pw1->dim->ctx, isl_dim_equal(pw1->dim, pw2->dim), goto error);
264
265         if (FN(PW,is_zero)(pw1)) {
266                 FN(PW,free)(pw1);
267                 return pw2;
268         }
269
270         if (FN(PW,is_zero)(pw2)) {
271                 FN(PW,free)(pw2);
272                 return pw1;
273         }
274
275 #ifdef HAS_TYPE
276         res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), pw1->type, pw1->n + pw2->n);
277 #else
278         res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), pw1->n + pw2->n);
279 #endif
280
281         for (i = 0; i < pw1->n; ++i)
282                 res = FN(PW,add_piece)(res,
283                                 isl_set_copy(pw1->p[i].set),
284                                 FN(EL,copy)(pw1->p[i].FIELD));
285
286         for (i = 0; i < pw2->n; ++i)
287                 res = FN(PW,add_piece)(res,
288                                 isl_set_copy(pw2->p[i].set),
289                                 FN(EL,copy)(pw2->p[i].FIELD));
290
291         FN(PW,free)(pw1);
292         FN(PW,free)(pw2);
293
294         return res;
295 error:
296         FN(PW,free)(pw1);
297         FN(PW,free)(pw2);
298         return NULL;
299 }
300
301 __isl_give isl_qpolynomial *FN(PW,eval)(__isl_take PW *pw,
302         __isl_take isl_point *pnt)
303 {
304         int i;
305         int found = 0;
306         isl_qpolynomial *qp;
307
308         if (!pw || !pnt)
309                 goto error;
310         isl_assert(pnt->dim->ctx, isl_dim_equal(pnt->dim, pw->dim), goto error);
311
312         for (i = 0; i < pw->n; ++i) {
313                 found = isl_set_contains_point(pw->p[i].set, pnt);
314                 if (found < 0)
315                         goto error;
316                 if (found)
317                         break;
318         }
319         if (found)
320                 qp = FN(EL,eval)(FN(EL,copy)(pw->p[i].FIELD),
321                                             isl_point_copy(pnt));
322         else
323                 qp = isl_qpolynomial_zero(isl_dim_copy(pw->dim));
324         FN(PW,free)(pw);
325         isl_point_free(pnt);
326         return qp;
327 error:
328         FN(PW,free)(pw);
329         isl_point_free(pnt);
330         return NULL;
331 }
332
333 __isl_give isl_set *FN(PW,domain)(__isl_take PW *pw)
334 {
335         int i;
336         isl_set *dom;
337
338         if (!pw)
339                 return NULL;
340
341         dom = isl_set_empty(isl_dim_copy(pw->dim));
342         for (i = 0; i < pw->n; ++i)
343                 dom = isl_set_union_disjoint(dom, isl_set_copy(pw->p[i].set));
344
345         FN(PW,free)(pw);
346
347         return dom;
348 }
349
350 __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw, __isl_take isl_set *set)
351 {
352         int i;
353
354         if (!pw || !set)
355                 goto error;
356
357         if (pw->n == 0) {
358                 isl_set_free(set);
359                 return pw;
360         }
361
362         pw = FN(PW,cow)(pw);
363         if (!pw)
364                 goto error;
365
366         for (i = pw->n - 1; i >= 0; --i) {
367                 isl_basic_set *aff;
368                 pw->p[i].set = isl_set_intersect(pw->p[i].set, isl_set_copy(set));
369                 if (!pw->p[i].set)
370                         goto error;
371                 aff = isl_set_affine_hull(isl_set_copy(pw->p[i].set));
372                 pw->p[i].FIELD = FN(EL,substitute_equalities)(pw->p[i].FIELD,
373                                                                 aff);
374                 if (isl_set_fast_is_empty(pw->p[i].set)) {
375                         isl_set_free(pw->p[i].set);
376                         FN(EL,free)(pw->p[i].FIELD);
377                         if (i != pw->n - 1)
378                                 pw->p[i] = pw->p[pw->n - 1];
379                         pw->n--;
380                 }
381         }
382         
383         isl_set_free(set);
384         return pw;
385 error:
386         isl_set_free(set);
387         FN(PW,free)(pw);
388         return NULL;
389 }
390
391 __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
392 {
393         int i;
394         isl_basic_set *hull = NULL;
395
396         if (!pw || !context)
397                 goto error;
398
399         if (pw->n == 0) {
400                 isl_set_free(context);
401                 return pw;
402         }
403
404         context = isl_set_compute_divs(context);
405         hull = isl_set_simple_hull(isl_set_copy(context));
406
407         pw = FN(PW,cow)(pw);
408         if (!pw)
409                 goto error;
410
411         for (i = pw->n - 1; i >= 0; --i) {
412                 pw->p[i].set = isl_set_gist_basic_set(pw->p[i].set,
413                                                 isl_basic_set_copy(hull));
414                 if (!pw->p[i].set)
415                         goto error;
416                 if (isl_set_fast_is_empty(pw->p[i].set)) {
417                         isl_set_free(pw->p[i].set);
418                         FN(EL,free)(pw->p[i].FIELD);
419                         if (i != pw->n - 1)
420                                 pw->p[i] = pw->p[pw->n - 1];
421                         pw->n--;
422                 }
423         }
424
425         isl_basic_set_free(hull);
426         isl_set_free(context);
427
428         return pw;
429 error:
430         FN(PW,free)(pw);
431         isl_basic_set_free(hull);
432         isl_set_free(context);
433         return NULL;
434 }
435
436 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
437 {
438         int i, j;
439
440         if (!pw)
441                 return NULL;
442         if (pw->n == 0)
443                 return pw;
444
445         for (i = pw->n - 1; i >= 0; --i) {
446                 for (j = i - 1; j >= 0; --j) {
447                         if (!FN(EL,is_equal)(pw->p[i].FIELD, pw->p[j].FIELD))
448                                 continue;
449                         pw->p[j].set = isl_set_union(pw->p[j].set,
450                                                         pw->p[i].set);
451                         FN(EL,free)(pw->p[i].FIELD);
452                         if (i != pw->n - 1)
453                                 pw->p[i] = pw->p[pw->n - 1];
454                         pw->n--;
455                         break;
456                 }
457                 if (j >= 0)
458                         continue;
459                 pw->p[i].set = isl_set_coalesce(pw->p[i].set);
460                 if (!pw->p[i].set)
461                         goto error;
462         }
463
464         return pw;
465 error:
466         FN(PW,free)(pw);
467         return NULL;
468 }
469
470 isl_ctx *FN(PW,get_ctx)(__isl_keep PW *pw)
471 {
472         return pw ? pw->dim->ctx : NULL;
473 }
474
475 int FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
476         unsigned first, unsigned n)
477 {
478         int i;
479
480         if (!pw)
481                 return -1;
482         if (pw->n == 0 || n == 0)
483                 return 0;
484         for (i = 0; i < pw->n; ++i) {
485                 int involves = FN(EL,involves_dims)(pw->p[i].FIELD,
486                                                         type, first, n);
487                 if (involves < 0 || involves)
488                         return involves;
489         }
490         return 0;
491 }
492
493 __isl_give PW *FN(PW,set_dim_name)(__isl_take PW *pw,
494         enum isl_dim_type type, unsigned pos, const char *s)
495 {
496         int i;
497
498         pw = FN(PW,cow)(pw);
499         if (!pw)
500                 return NULL;
501
502         pw->dim = isl_dim_set_name(pw->dim, type, pos, s);
503         if (!pw->dim)
504                 goto error;
505
506         for (i = 0; i < pw->n; ++i) {
507                 pw->p[i].set = isl_set_set_dim_name(pw->p[i].set, type, pos, s);
508                 if (!pw->p[i].set)
509                         goto error;
510                 pw->p[i].FIELD = FN(EL,set_dim_name)(pw->p[i].FIELD, type, pos, s);
511                 if (!pw->p[i].FIELD)
512                         goto error;
513         }
514
515         return pw;
516 error:
517         FN(PW,free)(pw);
518         return NULL;
519 }
520
521 __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
522         enum isl_dim_type type, unsigned first, unsigned n)
523 {
524         int i;
525
526         if (!pw)
527                 return NULL;
528         if (n == 0 && !isl_dim_get_tuple_name(pw->dim, type))
529                 return pw;
530
531         pw = FN(PW,cow)(pw);
532         if (!pw)
533                 return NULL;
534         pw->dim = isl_dim_drop(pw->dim, type, first, n);
535         if (!pw->dim)
536                 goto error;
537         for (i = 0; i < pw->n; ++i) {
538                 pw->p[i].set = isl_set_drop(pw->p[i].set, type, first, n);
539                 if (!pw->p[i].set)
540                         goto error;
541                 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
542                 if (!pw->p[i].FIELD)
543                         goto error;
544         }
545
546         return pw;
547 error:
548         FN(PW,free)(pw);
549         return NULL;
550 }
551
552 __isl_give PW *FN(PW,insert_dims)(__isl_take PW *pw, enum isl_dim_type type,
553         unsigned first, unsigned n)
554 {
555         int i;
556
557         if (!pw)
558                 return NULL;
559         if (n == 0 && !isl_dim_is_named_or_nested(pw->dim, type))
560                 return pw;
561
562         pw = FN(PW,cow)(pw);
563         if (!pw)
564                 return NULL;
565
566         pw->dim = isl_dim_insert(pw->dim, type, first, n);
567         if (!pw->dim)
568                 goto error;
569
570         for (i = 0; i < pw->n; ++i) {
571                 pw->p[i].set = isl_set_insert(pw->p[i].set, type, first, n);
572                 if (!pw->p[i].set)
573                         goto error;
574                 pw->p[i].FIELD = FN(EL,insert_dims)(pw->p[i].FIELD,
575                                                                 type, first, n);
576                 if (!pw->p[i].FIELD)
577                         goto error;
578         }
579
580         return pw;
581 error:
582         FN(PW,free)(pw);
583         return NULL;
584 }
585
586 __isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
587         enum isl_dim_type type, unsigned pos, isl_int v)
588 {
589         int i;
590
591         if (!pw)
592                 return NULL;
593
594         pw = FN(PW,cow)(pw);
595         if (!pw)
596                 return NULL;
597         for (i = 0; i < pw->n; ++i) {
598                 pw->p[i].set = isl_set_fix(pw->p[i].set, type, pos, v);
599                 if (!pw->p[i].set)
600                         goto error;
601         }
602
603         return pw;
604 error:
605         FN(PW,free)(pw);
606         return NULL;
607 }
608
609 unsigned FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
610 {
611         return pw ? isl_dim_size(pw->dim, type) : 0;
612 }
613
614 __isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
615         enum isl_dim_type type, unsigned first, unsigned n)
616 {
617         int i;
618
619         if (!pw)
620                 return NULL;
621         if (n == 0)
622                 return pw;
623
624         pw = FN(PW,cow)(pw);
625         if (!pw)
626                 return NULL;
627         if (!pw->dim)
628                 goto error;
629         for (i = 0; i < pw->n; ++i) {
630                 pw->p[i].set = isl_set_split_dims(pw->p[i].set, type, first, n);
631                 if (!pw->p[i].set)
632                         goto error;
633         }
634
635         return pw;
636 error:
637         FN(PW,free)(pw);
638         return NULL;
639 }
640
641 /* Compute the maximal value attained by the piecewise quasipolynomial
642  * on its domain or zero if the domain is empty.
643  * In the worst case, the domain is scanned completely,
644  * so the domain is assumed to be bounded.
645  */
646 __isl_give isl_qpolynomial *FN(PW,opt)(__isl_take PW *pw, int max)
647 {
648         int i;
649         isl_qpolynomial *opt;
650
651         if (!pw)
652                 return NULL;
653
654         if (pw->n == 0) {
655                 isl_dim *dim = isl_dim_copy(pw->dim);
656                 FN(PW,free)(pw);
657                 return isl_qpolynomial_zero(dim);
658         }
659
660         opt = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[0].FIELD),
661                                         isl_set_copy(pw->p[0].set), max);
662         for (i = 1; i < pw->n; ++i) {
663                 isl_qpolynomial *opt_i;
664                 opt_i = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[i].FIELD),
665                                                 isl_set_copy(pw->p[i].set), max);
666                 if (max)
667                         opt = isl_qpolynomial_max_cst(opt, opt_i);
668                 else
669                         opt = isl_qpolynomial_min_cst(opt, opt_i);
670         }
671
672         FN(PW,free)(pw);
673         return opt;
674 }
675
676 __isl_give isl_qpolynomial *FN(PW,max)(__isl_take PW *pw)
677 {
678         return FN(PW,opt)(pw, 1);
679 }
680
681 __isl_give isl_qpolynomial *FN(PW,min)(__isl_take PW *pw)
682 {
683         return FN(PW,opt)(pw, 0);
684 }
685
686 __isl_give isl_dim *FN(PW,get_dim)(__isl_keep PW *pw)
687 {
688         return pw ? isl_dim_copy(pw->dim) : NULL;
689 }
690
691 __isl_give PW *FN(PW,reset_dim)(__isl_take PW *pw, __isl_take isl_dim *dim)
692 {
693         int i;
694
695         pw = FN(PW,cow)(pw);
696         if (!pw || !dim)
697                 goto error;
698
699         for (i = 0; i < pw->n; ++i) {
700                 pw->p[i].set = isl_set_reset_dim(pw->p[i].set,
701                                                  isl_dim_copy(dim));
702                 if (!pw->p[i].set)
703                         goto error;
704                 pw->p[i].FIELD = FN(EL,reset_dim)(pw->p[i].FIELD,
705                                                   isl_dim_copy(dim));
706                 if (!pw->p[i].FIELD)
707                         goto error;
708         }
709         isl_dim_free(pw->dim);
710         pw->dim = dim;
711
712         return pw;
713 error:
714         isl_dim_free(dim);
715         FN(PW,free)(pw);
716         return NULL;
717 }
718
719 int FN(PW,has_equal_dim)(__isl_keep PW *pw1, __isl_keep PW *pw2)
720 {
721         if (!pw1 || !pw2)
722                 return -1;
723
724         return isl_dim_equal(pw1->dim, pw2->dim);
725 }
726
727 __isl_give PW *FN(PW,morph)(__isl_take PW *pw, __isl_take isl_morph *morph)
728 {
729         int i;
730
731         if (!pw || !morph)
732                 goto error;
733
734         isl_assert(pw->dim->ctx, isl_dim_equal(pw->dim, morph->dom->dim),
735                 goto error);
736
737         pw = FN(PW,cow)(pw);
738         if (!pw)
739                 goto error;
740         isl_dim_free(pw->dim);
741         pw->dim = isl_dim_copy(morph->ran->dim);
742         if (!pw->dim)
743                 goto error;
744
745         for (i = 0; i < pw->n; ++i) {
746                 pw->p[i].set = isl_morph_set(isl_morph_copy(morph), pw->p[i].set);
747                 if (!pw->p[i].set)
748                         goto error;
749                 pw->p[i].FIELD = FN(EL,morph)(pw->p[i].FIELD,
750                                                 isl_morph_copy(morph));
751                 if (!pw->p[i].FIELD)
752                         goto error;
753         }
754
755         isl_morph_free(morph);
756
757         return pw;
758 error:
759         FN(PW,free)(pw);
760         isl_morph_free(morph);
761         return NULL;
762 }
763
764 int FN(PW,foreach_piece)(__isl_keep PW *pw,
765         int (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
766         void *user)
767 {
768         int i;
769
770         if (!pw)
771                 return -1;
772
773         for (i = 0; i < pw->n; ++i)
774                 if (fn(isl_set_copy(pw->p[i].set),
775                                 FN(EL,copy)(pw->p[i].FIELD), user) < 0)
776                         return -1;
777
778         return 0;
779 }
780
781 static int any_divs(__isl_keep isl_set *set)
782 {
783         int i;
784
785         if (!set)
786                 return -1;
787
788         for (i = 0; i < set->n; ++i)
789                 if (set->p[i]->n_div > 0)
790                         return 1;
791
792         return 0;
793 }
794
795 static int foreach_lifted_subset(__isl_take isl_set *set, __isl_take EL *el,
796         int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
797                     void *user), void *user)
798 {
799         int i;
800
801         if (!set || !el)
802                 goto error;
803
804         for (i = 0; i < set->n; ++i) {
805                 isl_set *lift;
806                 EL *copy;
807
808                 lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
809                 lift = isl_set_lift(lift);
810
811                 copy = FN(EL,copy)(el);
812                 copy = FN(EL,lift)(copy, isl_set_get_dim(lift));
813
814                 if (fn(lift, copy, user) < 0)
815                         goto error;
816         }
817
818         isl_set_free(set);
819         FN(EL,free)(el);
820
821         return 0;
822 error:
823         isl_set_free(set);
824         FN(EL,free)(el);
825         return -1;
826 }
827
828 int FN(PW,foreach_lifted_piece)(__isl_keep PW *pw,
829         int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
830                     void *user), void *user)
831 {
832         int i;
833
834         if (!pw)
835                 return -1;
836
837         for (i = 0; i < pw->n; ++i) {
838                 isl_set *set;
839                 EL *el;
840
841                 set = isl_set_copy(pw->p[i].set);
842                 el = FN(EL,copy)(pw->p[i].FIELD);
843                 if (!any_divs(set)) {
844                         if (fn(set, el, user) < 0)
845                                 return -1;
846                         continue;
847                 }
848                 if (foreach_lifted_subset(set, el, fn, user) < 0)
849                         return -1;
850         }
851
852         return 0;
853 }
854
855 __isl_give PW *FN(PW,move_dims)(__isl_take PW *pw,
856         enum isl_dim_type dst_type, unsigned dst_pos,
857         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
858 {
859         int i;
860
861         pw = FN(PW,cow)(pw);
862         if (!pw)
863                 return NULL;
864
865         pw->dim = isl_dim_move(pw->dim, dst_type, dst_pos, src_type, src_pos, n);
866         if (!pw->dim)
867                 goto error;
868
869         for (i = 0; i < pw->n; ++i) {
870                 pw->p[i].set = isl_set_move_dims(pw->p[i].set,
871                                                 dst_type, dst_pos,
872                                                 src_type, src_pos, n);
873                 if (!pw->p[i].set)
874                         goto error;
875                 pw->p[i].FIELD = FN(EL,move_dims)(pw->p[i].FIELD,
876                                         dst_type, dst_pos, src_type, src_pos, n);
877                 if (!pw->p[i].FIELD)
878                         goto error;
879         }
880
881         return pw;
882 error:
883         FN(PW,free)(pw);
884         return NULL;
885 }
886
887 __isl_give PW *FN(PW,realign)(__isl_take PW *pw, __isl_take isl_reordering *exp)
888 {
889         int i;
890
891         pw = FN(PW,cow)(pw);
892         if (!pw || !exp)
893                 return NULL;
894
895         for (i = 0; i < pw->n; ++i) {
896                 pw->p[i].set = isl_set_realign(pw->p[i].set,
897                                                     isl_reordering_copy(exp));
898                 if (!pw->p[i].set)
899                         goto error;
900                 pw->p[i].FIELD = FN(EL,realign)(pw->p[i].FIELD,
901                                                     isl_reordering_copy(exp));
902                 if (!pw->p[i].FIELD)
903                         goto error;
904         }
905
906         pw = FN(PW,reset_dim)(pw, isl_dim_copy(exp->dim));
907
908         isl_reordering_free(exp);
909         return pw;
910 error:
911         isl_reordering_free(exp);
912         FN(PW,free)(pw);
913         return NULL;
914 }