+
+/* Project the domain of pw onto its parameter space.
+ */
+__isl_give PW *FN(PW,project_domain_on_params)(__isl_take PW *pw)
+{
+ isl_space *space;
+ unsigned n;
+
+ n = FN(PW,dim)(pw, isl_dim_in);
+ pw = FN(PW,project_out)(pw, isl_dim_in, 0, n);
+ space = FN(PW,get_domain_space)(pw);
+ space = isl_space_params(space);
+ pw = FN(PW,reset_domain_space)(pw, space);
+ return pw;
+}
+#endif
+
+#ifndef NO_INSERT_DIMS
+__isl_give PW *FN(PW,insert_dims)(__isl_take PW *pw, enum isl_dim_type type,
+ unsigned first, unsigned n)
+{
+ int i;
+ enum isl_dim_type set_type;
+
+ if (!pw)
+ return NULL;
+ if (n == 0 && !isl_space_is_named_or_nested(pw->dim, type))
+ return pw;
+
+ set_type = type == isl_dim_in ? isl_dim_set : type;
+
+ pw = FN(PW,cow)(pw);
+ if (!pw)
+ return NULL;
+
+ pw->dim = isl_space_insert_dims(pw->dim, type, first, n);
+ if (!pw->dim)
+ goto error;
+
+ for (i = 0; i < pw->n; ++i) {
+ pw->p[i].set = isl_set_insert_dims(pw->p[i].set,
+ set_type, first, n);
+ if (!pw->p[i].set)
+ goto error;
+ pw->p[i].FIELD = FN(EL,insert_dims)(pw->p[i].FIELD,
+ type, first, n);
+ if (!pw->p[i].FIELD)
+ goto error;
+ }
+
+ return pw;
+error:
+ FN(PW,free)(pw);
+ return NULL;
+}
+#endif
+
+__isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
+ enum isl_dim_type type, unsigned pos, isl_int v)
+{
+ int i;
+
+ if (!pw)
+ return NULL;
+
+ if (type == isl_dim_in)
+ type = isl_dim_set;
+
+ pw = FN(PW,cow)(pw);
+ if (!pw)
+ return NULL;
+ for (i = 0; i < pw->n; ++i) {
+ pw->p[i].set = isl_set_fix(pw->p[i].set, type, pos, v);
+ if (!pw->p[i].set)
+ goto error;
+ }
+
+ return pw;
+error:
+ FN(PW,free)(pw);
+ return NULL;
+}
+
+unsigned FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
+{
+ return pw ? isl_space_dim(pw->dim, type) : 0;
+}
+
+__isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
+ enum isl_dim_type type, unsigned first, unsigned n)
+{
+ int i;
+
+ if (!pw)
+ return NULL;
+ if (n == 0)
+ return pw;
+
+ if (type == isl_dim_in)
+ type = isl_dim_set;
+
+ pw = FN(PW,cow)(pw);
+ if (!pw)
+ return NULL;
+ if (!pw->dim)
+ goto error;
+ for (i = 0; i < pw->n; ++i) {
+ pw->p[i].set = isl_set_split_dims(pw->p[i].set, type, first, n);
+ if (!pw->p[i].set)
+ goto error;
+ }
+
+ return pw;
+error:
+ FN(PW,free)(pw);
+ return NULL;
+}
+
+#ifndef NO_OPT
+/* Compute the maximal value attained by the piecewise quasipolynomial
+ * on its domain or zero if the domain is empty.
+ * In the worst case, the domain is scanned completely,
+ * so the domain is assumed to be bounded.
+ */
+__isl_give isl_qpolynomial *FN(PW,opt)(__isl_take PW *pw, int max)
+{
+ int i;
+ isl_qpolynomial *opt;
+
+ if (!pw)
+ return NULL;
+
+ if (pw->n == 0) {
+ isl_space *dim = isl_space_copy(pw->dim);
+ FN(PW,free)(pw);
+ return isl_qpolynomial_zero_on_domain(dim);
+ }
+
+ opt = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[0].FIELD),
+ isl_set_copy(pw->p[0].set), max);
+ for (i = 1; i < pw->n; ++i) {
+ isl_qpolynomial *opt_i;
+ opt_i = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[i].FIELD),
+ isl_set_copy(pw->p[i].set), max);
+ if (max)
+ opt = isl_qpolynomial_max_cst(opt, opt_i);
+ else
+ opt = isl_qpolynomial_min_cst(opt, opt_i);
+ }
+
+ FN(PW,free)(pw);
+ return opt;
+}
+
+__isl_give isl_qpolynomial *FN(PW,max)(__isl_take PW *pw)
+{
+ return FN(PW,opt)(pw, 1);
+}
+
+__isl_give isl_qpolynomial *FN(PW,min)(__isl_take PW *pw)
+{
+ return FN(PW,opt)(pw, 0);
+}
+#endif
+
+__isl_give isl_space *FN(PW,get_space)(__isl_keep PW *pw)
+{
+ return pw ? isl_space_copy(pw->dim) : NULL;
+}
+
+__isl_give isl_space *FN(PW,get_domain_space)(__isl_keep PW *pw)
+{
+ return pw ? isl_space_domain(isl_space_copy(pw->dim)) : NULL;
+}
+
+#ifndef NO_RESET_DIM
+/* Reset the space of "pw". Since we don't know if the elements
+ * represent the spaces themselves or their domains, we pass along
+ * both when we call their reset_space_and_domain.
+ */
+static __isl_give PW *FN(PW,reset_space_and_domain)(__isl_take PW *pw,
+ __isl_take isl_space *space, __isl_take isl_space *domain)
+{
+ int i;
+
+ pw = FN(PW,cow)(pw);
+ if (!pw || !space || !domain)
+ goto error;
+
+ for (i = 0; i < pw->n; ++i) {
+ pw->p[i].set = isl_set_reset_space(pw->p[i].set,
+ isl_space_copy(domain));
+ if (!pw->p[i].set)
+ goto error;
+ pw->p[i].FIELD = FN(EL,reset_space_and_domain)(pw->p[i].FIELD,
+ isl_space_copy(space), isl_space_copy(domain));
+ if (!pw->p[i].FIELD)
+ goto error;
+ }
+
+ isl_space_free(domain);
+
+ isl_space_free(pw->dim);
+ pw->dim = space;
+
+ return pw;
+error:
+ isl_space_free(domain);
+ isl_space_free(space);
+ FN(PW,free)(pw);
+ return NULL;
+}
+
+__isl_give PW *FN(PW,reset_domain_space)(__isl_take PW *pw,
+ __isl_take isl_space *domain)
+{
+ isl_space *space;
+
+ space = isl_space_extend_domain_with_range(isl_space_copy(domain),
+ FN(PW,get_space)(pw));
+ return FN(PW,reset_space_and_domain)(pw, space, domain);
+}
+
+__isl_give PW *FN(PW,reset_space)(__isl_take PW *pw, __isl_take isl_space *dim)
+{
+ isl_space *domain;
+
+ domain = isl_space_domain(isl_space_copy(dim));
+ return FN(PW,reset_space_and_domain)(pw, dim, domain);
+}
+
+__isl_give PW *FN(PW,set_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type,
+ __isl_take isl_id *id)
+{
+ isl_space *space;
+
+ pw = FN(PW,cow)(pw);
+ if (!pw)
+ return isl_id_free(id);
+
+ space = FN(PW,get_space)(pw);
+ space = isl_space_set_tuple_id(space, type, id);
+
+ return FN(PW,reset_space)(pw, space);
+}
+#endif
+
+int FN(PW,has_equal_space)(__isl_keep PW *pw1, __isl_keep PW *pw2)
+{
+ if (!pw1 || !pw2)
+ return -1;
+
+ return isl_space_is_equal(pw1->dim, pw2->dim);
+}
+
+#ifndef NO_MORPH
+__isl_give PW *FN(PW,morph_domain)(__isl_take PW *pw,
+ __isl_take isl_morph *morph)
+{
+ int i;
+ isl_ctx *ctx;
+
+ if (!pw || !morph)
+ goto error;
+
+ ctx = isl_space_get_ctx(pw->dim);
+ isl_assert(ctx, isl_space_is_domain(morph->dom->dim, pw->dim),
+ goto error);
+
+ pw = FN(PW,cow)(pw);
+ if (!pw)
+ goto error;
+ pw->dim = isl_space_extend_domain_with_range(
+ isl_space_copy(morph->ran->dim), pw->dim);
+ if (!pw->dim)
+ goto error;
+
+ for (i = 0; i < pw->n; ++i) {
+ pw->p[i].set = isl_morph_set(isl_morph_copy(morph), pw->p[i].set);
+ if (!pw->p[i].set)
+ goto error;
+ pw->p[i].FIELD = FN(EL,morph_domain)(pw->p[i].FIELD,
+ isl_morph_copy(morph));
+ if (!pw->p[i].FIELD)
+ goto error;
+ }
+
+ isl_morph_free(morph);
+
+ return pw;
+error:
+ FN(PW,free)(pw);
+ isl_morph_free(morph);
+ return NULL;
+}
+#endif
+
+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 && 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)
+{
+ const isl_set *set1 = *(const isl_set **)p1;
+ const isl_set *set2 = *(const isl_set **)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;
+}