+#endif
+
+int FN(PW,n_piece)(__isl_keep PW *pw)
+{
+ return pw ? pw->n : 0;
+}
+
+int FN(PW,foreach_piece)(__isl_keep PW *pw,
+ int (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
+ void *user)
+{
+ int i;
+
+ if (!pw)
+ return -1;
+
+ for (i = 0; i < pw->n; ++i)
+ if (fn(isl_set_copy(pw->p[i].set),
+ FN(EL,copy)(pw->p[i].FIELD), user) < 0)
+ return -1;
+
+ return 0;
+}
+
+#ifndef NO_LIFT
+static int any_divs(__isl_keep isl_set *set)
+{
+ int i;
+
+ if (!set)
+ return -1;
+
+ for (i = 0; i < set->n; ++i)
+ if (set->p[i]->n_div > 0)
+ return 1;
+
+ return 0;
+}
+
+static int foreach_lifted_subset(__isl_take isl_set *set, __isl_take EL *el,
+ int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
+ void *user), void *user)
+{
+ int i;
+
+ if (!set || !el)
+ goto error;
+
+ for (i = 0; i < set->n; ++i) {
+ isl_set *lift;
+ EL *copy;
+
+ lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
+ lift = isl_set_lift(lift);
+
+ copy = FN(EL,copy)(el);
+ copy = FN(EL,lift)(copy, isl_set_get_space(lift));
+
+ if (fn(lift, copy, user) < 0)
+ goto error;
+ }
+
+ isl_set_free(set);
+ FN(EL,free)(el);
+
+ return 0;
+error:
+ isl_set_free(set);
+ FN(EL,free)(el);
+ return -1;
+}
+
+int FN(PW,foreach_lifted_piece)(__isl_keep PW *pw,
+ int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
+ void *user), void *user)
+{
+ int i;
+
+ if (!pw)
+ return -1;
+
+ for (i = 0; i < pw->n; ++i) {
+ isl_set *set;
+ EL *el;
+
+ set = isl_set_copy(pw->p[i].set);
+ el = FN(EL,copy)(pw->p[i].FIELD);
+ if (!any_divs(set)) {
+ if (fn(set, el, user) < 0)
+ return -1;
+ continue;
+ }
+ if (foreach_lifted_subset(set, el, fn, user) < 0)
+ return -1;
+ }
+
+ return 0;
+}
+#endif
+
+#ifndef NO_MOVE_DIMS
+__isl_give PW *FN(PW,move_dims)(__isl_take PW *pw,
+ enum isl_dim_type dst_type, unsigned dst_pos,
+ enum isl_dim_type src_type, unsigned src_pos, unsigned n)
+{
+ int i;
+
+ pw = FN(PW,cow)(pw);
+ if (!pw)
+ return NULL;
+
+ pw->dim = isl_space_move_dims(pw->dim, dst_type, dst_pos, src_type, src_pos, n);
+ if (!pw->dim)
+ goto error;
+
+ for (i = 0; i < pw->n; ++i) {
+ pw->p[i].FIELD = FN(EL,move_dims)(pw->p[i].FIELD,
+ dst_type, dst_pos, src_type, src_pos, n);
+ if (!pw->p[i].FIELD)
+ goto error;
+ }
+
+ if (dst_type == isl_dim_in)
+ dst_type = isl_dim_set;
+ if (src_type == isl_dim_in)
+ src_type = isl_dim_set;
+
+ for (i = 0; i < pw->n; ++i) {
+ pw->p[i].set = isl_set_move_dims(pw->p[i].set,
+ dst_type, dst_pos,
+ src_type, src_pos, n);
+ if (!pw->p[i].set)
+ goto error;
+ }
+
+ return pw;
+error:
+ FN(PW,free)(pw);
+ return NULL;
+}
+#endif
+
+__isl_give PW *FN(PW,mul_isl_int)(__isl_take PW *pw, isl_int v)
+{
+ int i;
+
+ if (isl_int_is_one(v))
+ return pw;
+ if (pw && DEFAULT_IS_ZERO && isl_int_is_zero(v)) {
+ PW *zero;
+ isl_space *dim = FN(PW,get_space)(pw);
+#ifdef HAS_TYPE
+ zero = FN(PW,ZERO)(dim, pw->type);
+#else
+ zero = FN(PW,ZERO)(dim);
+#endif
+ FN(PW,free)(pw);
+ return zero;
+ }
+ pw = FN(PW,cow)(pw);
+ if (!pw)
+ return NULL;
+ if (pw->n == 0)
+ return pw;
+
+#ifdef HAS_TYPE
+ if (isl_int_is_neg(v))
+ pw->type = isl_fold_type_negate(pw->type);
+#endif
+ for (i = 0; i < pw->n; ++i) {
+ pw->p[i].FIELD = FN(EL,scale)(pw->p[i].FIELD, v);
+ if (!pw->p[i].FIELD)
+ goto error;
+ }
+
+ return pw;
+error:
+ FN(PW,free)(pw);
+ return NULL;
+}
+
+__isl_give PW *FN(PW,scale)(__isl_take PW *pw, isl_int v)
+{
+ return FN(PW,mul_isl_int)(pw, v);
+}
+
+static int FN(PW,qsort_set_cmp)(const void *p1, const void *p2)
+{
+ isl_set *set1 = *(isl_set * const *)p1;
+ isl_set *set2 = *(isl_set * const *)p2;
+
+ return isl_set_plain_cmp(set1, set2);
+}
+
+/* We normalize in place, but if anything goes wrong we need
+ * to return NULL, so we need to make sure we don't change the
+ * meaning of any possible other copies of map.
+ */
+__isl_give PW *FN(PW,normalize)(__isl_take PW *pw)
+{
+ int i, j;
+ isl_set *set;
+
+ if (!pw)
+ return NULL;
+ for (i = 0; i < pw->n; ++i) {
+ set = isl_set_normalize(isl_set_copy(pw->p[i].set));
+ if (!set)
+ return FN(PW,free)(pw);
+ isl_set_free(pw->p[i].set);
+ pw->p[i].set = set;
+ }
+ qsort(pw->p, pw->n, sizeof(pw->p[0]), &FN(PW,qsort_set_cmp));
+ for (i = pw->n - 1; i >= 1; --i) {
+ if (!isl_set_plain_is_equal(pw->p[i - 1].set, pw->p[i].set))
+ continue;
+ if (!FN(EL,plain_is_equal)(pw->p[i - 1].FIELD, pw->p[i].FIELD))
+ continue;
+ set = isl_set_union(isl_set_copy(pw->p[i - 1].set),
+ isl_set_copy(pw->p[i].set));
+ if (!set)
+ return FN(PW,free)(pw);
+ isl_set_free(pw->p[i].set);
+ FN(EL,free)(pw->p[i].FIELD);
+ isl_set_free(pw->p[i - 1].set);
+ pw->p[i - 1].set = set;
+ for (j = i + 1; j < pw->n; ++j)
+ pw->p[j - 1] = pw->p[j];
+ pw->n--;
+ }
+
+ return pw;
+}
+
+/* Is pw1 obviously equal to pw2?
+ * That is, do they have obviously identical cells and obviously identical
+ * elements on each cell?
+ */
+int FN(PW,plain_is_equal)(__isl_keep PW *pw1, __isl_keep PW *pw2)
+{
+ int i;
+ int equal;
+
+ if (!pw1 || !pw2)
+ return -1;
+
+ if (pw1 == pw2)
+ return 1;
+ if (!isl_space_is_equal(pw1->dim, pw2->dim))
+ return 0;
+
+ pw1 = FN(PW,copy)(pw1);
+ pw2 = FN(PW,copy)(pw2);
+ pw1 = FN(PW,normalize)(pw1);
+ pw2 = FN(PW,normalize)(pw2);
+ if (!pw1 || !pw2)
+ goto error;
+
+ equal = pw1->n == pw2->n;
+ for (i = 0; equal && i < pw1->n; ++i) {
+ equal = isl_set_plain_is_equal(pw1->p[i].set, pw2->p[i].set);
+ if (equal < 0)
+ goto error;
+ if (!equal)
+ break;
+ equal = FN(EL,plain_is_equal)(pw1->p[i].FIELD, pw2->p[i].FIELD);
+ if (equal < 0)
+ goto error;
+ }
+
+ FN(PW,free)(pw1);
+ FN(PW,free)(pw2);
+ return equal;
+error:
+ FN(PW,free)(pw1);
+ FN(PW,free)(pw2);
+ return -1;
+}
+
+#ifndef NO_PULLBACK
+static __isl_give PW *FN(PW,align_params_pw_multi_aff_and)(__isl_take PW *pw,
+ __isl_take isl_multi_aff *ma,
+ __isl_give PW *(*fn)(__isl_take PW *pw, __isl_take isl_multi_aff *ma))
+{
+ isl_ctx *ctx;
+ isl_space *ma_space;
+
+ ma_space = isl_multi_aff_get_space(ma);
+ if (!pw || !ma || !ma_space)
+ goto error;
+ if (isl_space_match(pw->dim, isl_dim_param, ma_space, isl_dim_param)) {
+ isl_space_free(ma_space);
+ return fn(pw, ma);
+ }
+ ctx = FN(PW,get_ctx)(pw);
+ if (!isl_space_has_named_params(pw->dim) ||
+ !isl_space_has_named_params(ma_space))
+ isl_die(ctx, isl_error_invalid,
+ "unaligned unnamed parameters", goto error);
+ pw = FN(PW,align_params)(pw, ma_space);
+ ma = isl_multi_aff_align_params(ma, FN(PW,get_space)(pw));
+ return fn(pw, ma);
+error:
+ isl_space_free(ma_space);
+ FN(PW,free)(pw);
+ isl_multi_aff_free(ma);
+ return NULL;
+}
+
+static __isl_give PW *FN(PW,align_params_pw_pw_multi_aff_and)(__isl_take PW *pw,
+ __isl_take isl_pw_multi_aff *pma,
+ __isl_give PW *(*fn)(__isl_take PW *pw,
+ __isl_take isl_pw_multi_aff *ma))
+{
+ isl_ctx *ctx;
+ isl_space *pma_space;
+
+ pma_space = isl_pw_multi_aff_get_space(pma);
+ if (!pw || !pma || !pma_space)
+ goto error;
+ if (isl_space_match(pw->dim, isl_dim_param, pma_space, isl_dim_param)) {
+ isl_space_free(pma_space);
+ return fn(pw, pma);
+ }
+ ctx = FN(PW,get_ctx)(pw);
+ if (!isl_space_has_named_params(pw->dim) ||
+ !isl_space_has_named_params(pma_space))
+ isl_die(ctx, isl_error_invalid,
+ "unaligned unnamed parameters", goto error);
+ pw = FN(PW,align_params)(pw, pma_space);
+ pma = isl_pw_multi_aff_align_params(pma, FN(PW,get_space)(pw));
+ return fn(pw, pma);
+error:
+ isl_space_free(pma_space);
+ FN(PW,free)(pw);
+ isl_pw_multi_aff_free(pma);
+ return NULL;
+}
+
+/* Compute the pullback of "pw" by the function represented by "ma".
+ * In other words, plug in "ma" in "pw".
+ */
+static __isl_give PW *FN(PW,pullback_multi_aff_aligned)(__isl_take PW *pw,
+ __isl_take isl_multi_aff *ma)
+{
+ int i;
+ isl_space *space = NULL;
+
+ ma = isl_multi_aff_align_divs(ma);
+ pw = FN(PW,cow)(pw);
+ if (!pw || !ma)
+ goto error;
+
+ space = isl_space_join(isl_multi_aff_get_space(ma),
+ FN(PW,get_space)(pw));
+
+ for (i = 0; i < pw->n; ++i) {
+ pw->p[i].set = isl_set_preimage_multi_aff(pw->p[i].set,
+ isl_multi_aff_copy(ma));
+ if (!pw->p[i].set)
+ goto error;
+ pw->p[i].FIELD = FN(EL,pullback_multi_aff)(pw->p[i].FIELD,
+ isl_multi_aff_copy(ma));
+ if (!pw->p[i].FIELD)
+ goto error;
+ }
+
+ pw = FN(PW,reset_space)(pw, space);
+ isl_multi_aff_free(ma);
+ return pw;
+error:
+ isl_space_free(space);
+ isl_multi_aff_free(ma);
+ FN(PW,free)(pw);
+ return NULL;
+}
+
+__isl_give PW *FN(PW,pullback_multi_aff)(__isl_take PW *pw,
+ __isl_take isl_multi_aff *ma)
+{
+ return FN(PW,align_params_pw_multi_aff_and)(pw, ma,
+ &FN(PW,pullback_multi_aff_aligned));
+}
+
+/* Compute the pullback of "pw" by the function represented by "pma".
+ * In other words, plug in "pma" in "pw".
+ */
+static __isl_give PW *FN(PW,pullback_pw_multi_aff_aligned)(__isl_take PW *pw,
+ __isl_take isl_pw_multi_aff *pma)
+{
+ int i;
+ PW *res;
+
+ if (!pma)
+ goto error;
+
+ if (pma->n == 0) {
+ isl_pw_multi_aff_free(pma);
+ res = FN(PW,empty)(FN(PW,get_space)(pw));
+ FN(PW,free)(pw);
+ return res;
+ }
+
+ res = FN(PW,pullback_multi_aff)(FN(PW,copy)(pw),
+ isl_multi_aff_copy(pma->p[0].maff));
+ res = FN(PW,intersect_domain)(res, isl_set_copy(pma->p[0].set));
+
+ for (i = 1; i < pma->n; ++i) {
+ PW *res_i;
+
+ res_i = FN(PW,pullback_multi_aff)(FN(PW,copy)(pw),
+ isl_multi_aff_copy(pma->p[i].maff));
+ res_i = FN(PW,intersect_domain)(res_i,
+ isl_set_copy(pma->p[i].set));
+ res = FN(PW,add_disjoint)(res, res_i);
+ }
+
+ isl_pw_multi_aff_free(pma);
+ FN(PW,free)(pw);
+ return res;
+error:
+ isl_pw_multi_aff_free(pma);
+ FN(PW,free)(pw);
+ return NULL;
+}
+
+__isl_give PW *FN(PW,pullback_pw_multi_aff)(__isl_take PW *pw,
+ __isl_take isl_pw_multi_aff *pma)
+{
+ return FN(PW,align_params_pw_pw_multi_aff_and)(pw, pma,
+ &FN(PW,pullback_pw_multi_aff_aligned));
+}
+#endif