+
+/* For each 0 <= i < "n", replace variable "first" + i of type "type"
+ * in fold->qp[k] by subs[i].
+ */
+__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute(
+ __isl_take isl_qpolynomial_fold *fold,
+ enum isl_dim_type type, unsigned first, unsigned n,
+ __isl_keep isl_qpolynomial **subs)
+{
+ int i;
+
+ if (n == 0)
+ return fold;
+
+ fold = isl_qpolynomial_fold_cow(fold);
+ if (!fold)
+ return NULL;
+
+ for (i = 0; i < fold->n; ++i) {
+ fold->qp[i] = isl_qpolynomial_substitute(fold->qp[i],
+ type, first, n, subs);
+ if (!fold->qp[i])
+ goto error;
+ }
+
+ return fold;
+error:
+ isl_qpolynomial_fold_free(fold);
+ return NULL;
+}
+
+static int add_pwqp(__isl_take isl_pw_qpolynomial *pwqp, void *user)
+{
+ isl_ctx *ctx;
+ isl_pw_qpolynomial_fold *pwf;
+ isl_union_pw_qpolynomial_fold **upwf;
+ uint32_t hash;
+ struct isl_hash_table_entry *entry;
+
+ upwf = (isl_union_pw_qpolynomial_fold **)user;
+
+ ctx = pwqp->dim->ctx;
+ hash = isl_space_get_hash(pwqp->dim);
+ entry = isl_hash_table_find(ctx, &(*upwf)->table,
+ hash, &has_dim, pwqp->dim, 1);
+ if (!entry)
+ goto error;
+
+ pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial((*upwf)->type, pwqp);
+ if (!entry->data)
+ entry->data = pwf;
+ else {
+ entry->data = isl_pw_qpolynomial_fold_add(entry->data, pwf);
+ if (!entry->data)
+ return -1;
+ if (isl_pw_qpolynomial_fold_is_zero(entry->data)) {
+ isl_pw_qpolynomial_fold_free(entry->data);
+ isl_hash_table_remove(ctx, &(*upwf)->table, entry);
+ }
+ }
+
+ return 0;
+error:
+ isl_pw_qpolynomial_free(pwqp);
+ return -1;
+}
+
+__isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(
+ __isl_take isl_union_pw_qpolynomial_fold *upwf,
+ __isl_take isl_union_pw_qpolynomial *upwqp)
+{
+ upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
+ isl_union_pw_qpolynomial_get_space(upwqp));
+ upwqp = isl_union_pw_qpolynomial_align_params(upwqp,
+ isl_union_pw_qpolynomial_fold_get_space(upwf));
+
+ upwf = isl_union_pw_qpolynomial_fold_cow(upwf);
+ if (!upwf || !upwqp)
+ goto error;
+
+ if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &add_pwqp,
+ &upwf) < 0)
+ goto error;
+
+ isl_union_pw_qpolynomial_free(upwqp);
+
+ return upwf;
+error:
+ isl_union_pw_qpolynomial_fold_free(upwf);
+ isl_union_pw_qpolynomial_free(upwqp);
+ return NULL;
+}
+
+static int join_compatible(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
+{
+ int m;
+ m = isl_space_match(dim1, isl_dim_param, dim2, isl_dim_param);
+ if (m < 0 || !m)
+ return m;
+ return isl_space_tuple_match(dim1, isl_dim_out, dim2, isl_dim_in);
+}
+
+/* Compute the intersection of the range of the map and the domain
+ * of the piecewise quasipolynomial reduction and then compute a bound
+ * on the associated quasipolynomial reduction over all elements
+ * in this intersection.
+ *
+ * We first introduce some unconstrained dimensions in the
+ * piecewise quasipolynomial, intersect the resulting domain
+ * with the wrapped map and the compute the sum.
+ */
+__isl_give isl_pw_qpolynomial_fold *isl_map_apply_pw_qpolynomial_fold(
+ __isl_take isl_map *map, __isl_take isl_pw_qpolynomial_fold *pwf,
+ int *tight)
+{
+ isl_ctx *ctx;
+ isl_set *dom;
+ isl_space *map_dim;
+ isl_space *pwf_dim;
+ unsigned n_in;
+ int ok;
+
+ ctx = isl_map_get_ctx(map);
+ if (!ctx)
+ goto error;
+
+ map_dim = isl_map_get_space(map);
+ pwf_dim = isl_pw_qpolynomial_fold_get_space(pwf);
+ ok = join_compatible(map_dim, pwf_dim);
+ isl_space_free(map_dim);
+ isl_space_free(pwf_dim);
+ if (!ok)
+ isl_die(ctx, isl_error_invalid, "incompatible dimensions",
+ goto error);
+
+ n_in = isl_map_dim(map, isl_dim_in);
+ pwf = isl_pw_qpolynomial_fold_insert_dims(pwf, isl_dim_in, 0, n_in);
+
+ dom = isl_map_wrap(map);
+ pwf = isl_pw_qpolynomial_fold_reset_domain_space(pwf,
+ isl_set_get_space(dom));
+
+ pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, dom);
+ pwf = isl_pw_qpolynomial_fold_bound(pwf, tight);
+
+ return pwf;
+error:
+ isl_map_free(map);
+ isl_pw_qpolynomial_fold_free(pwf);
+ return NULL;
+}
+
+__isl_give isl_pw_qpolynomial_fold *isl_set_apply_pw_qpolynomial_fold(
+ __isl_take isl_set *set, __isl_take isl_pw_qpolynomial_fold *pwf,
+ int *tight)
+{
+ return isl_map_apply_pw_qpolynomial_fold(set, pwf, tight);
+}
+
+struct isl_apply_fold_data {
+ isl_union_pw_qpolynomial_fold *upwf;
+ isl_union_pw_qpolynomial_fold *res;
+ isl_map *map;
+ int tight;
+};
+
+static int pw_qpolynomial_fold_apply(__isl_take isl_pw_qpolynomial_fold *pwf,
+ void *user)
+{
+ isl_space *map_dim;
+ isl_space *pwf_dim;
+ struct isl_apply_fold_data *data = user;
+ int ok;
+
+ map_dim = isl_map_get_space(data->map);
+ pwf_dim = isl_pw_qpolynomial_fold_get_space(pwf);
+ ok = join_compatible(map_dim, pwf_dim);
+ isl_space_free(map_dim);
+ isl_space_free(pwf_dim);
+
+ if (ok) {
+ pwf = isl_map_apply_pw_qpolynomial_fold(isl_map_copy(data->map),
+ pwf, data->tight ? &data->tight : NULL);
+ data->res = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
+ data->res, pwf);
+ } else
+ isl_pw_qpolynomial_fold_free(pwf);
+
+ return 0;
+}
+
+static int map_apply(__isl_take isl_map *map, void *user)
+{
+ struct isl_apply_fold_data *data = user;
+ int r;
+
+ data->map = map;
+ r = isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(
+ data->upwf, &pw_qpolynomial_fold_apply, data);
+
+ isl_map_free(map);
+ return r;
+}
+
+__isl_give isl_union_pw_qpolynomial_fold *isl_union_map_apply_union_pw_qpolynomial_fold(
+ __isl_take isl_union_map *umap,
+ __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
+{
+ isl_space *dim;
+ enum isl_fold type;
+ struct isl_apply_fold_data data;
+
+ upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
+ isl_union_map_get_space(umap));
+ umap = isl_union_map_align_params(umap,
+ isl_union_pw_qpolynomial_fold_get_space(upwf));
+
+ data.upwf = upwf;
+ data.tight = tight ? 1 : 0;
+ dim = isl_union_pw_qpolynomial_fold_get_space(upwf);
+ type = isl_union_pw_qpolynomial_fold_get_type(upwf);
+ data.res = isl_union_pw_qpolynomial_fold_zero(dim, type);
+ if (isl_union_map_foreach_map(umap, &map_apply, &data) < 0)
+ goto error;
+
+ isl_union_map_free(umap);
+ isl_union_pw_qpolynomial_fold_free(upwf);
+
+ if (tight)
+ *tight = data.tight;
+
+ return data.res;
+error:
+ isl_union_map_free(umap);
+ isl_union_pw_qpolynomial_fold_free(upwf);
+ isl_union_pw_qpolynomial_fold_free(data.res);
+ return NULL;
+}
+
+__isl_give isl_union_pw_qpolynomial_fold *isl_union_set_apply_union_pw_qpolynomial_fold(
+ __isl_take isl_union_set *uset,
+ __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
+{
+ return isl_union_map_apply_union_pw_qpolynomial_fold(uset, upwf, tight);
+}
+
+/* Reorder the dimension of "fold" according to the given reordering.
+ */
+__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_realign_domain(
+ __isl_take isl_qpolynomial_fold *fold, __isl_take isl_reordering *r)
+{
+ int i;
+
+ fold = isl_qpolynomial_fold_cow(fold);
+ if (!fold || !r)
+ goto error;
+
+ for (i = 0; i < fold->n; ++i) {
+ fold->qp[i] = isl_qpolynomial_realign_domain(fold->qp[i],
+ isl_reordering_copy(r));
+ if (!fold->qp[i])
+ goto error;
+ }
+
+ fold = isl_qpolynomial_fold_reset_domain_space(fold,
+ isl_space_copy(r->dim));
+
+ isl_reordering_free(r);
+
+ return fold;
+error:
+ isl_qpolynomial_fold_free(fold);
+ isl_reordering_free(r);
+ return NULL;
+}
+
+__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_mul_isl_int(
+ __isl_take isl_qpolynomial_fold *fold, isl_int v)
+{
+ int i;
+
+ if (isl_int_is_one(v))
+ return fold;
+ if (fold && isl_int_is_zero(v)) {
+ isl_qpolynomial_fold *zero;
+ isl_space *dim = isl_space_copy(fold->dim);
+ zero = isl_qpolynomial_fold_empty(fold->type, dim);
+ isl_qpolynomial_fold_free(fold);
+ return zero;
+ }
+
+ fold = isl_qpolynomial_fold_cow(fold);
+ if (!fold)
+ return NULL;
+
+ if (isl_int_is_neg(v))
+ fold->type = isl_fold_type_negate(fold->type);
+ for (i = 0; i < fold->n; ++i) {
+ fold->qp[i] = isl_qpolynomial_mul_isl_int(fold->qp[i], v);
+ if (!fold->qp[i])
+ goto error;
+ }
+
+ return fold;
+error:
+ isl_qpolynomial_fold_free(fold);
+ return NULL;
+}
+
+__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale(
+ __isl_take isl_qpolynomial_fold *fold, isl_int v)
+{
+ return isl_qpolynomial_fold_mul_isl_int(fold, v);
+}
+
+/* Multiply "fold" by "v".
+ */
+__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale_val(
+ __isl_take isl_qpolynomial_fold *fold, __isl_take isl_val *v)
+{
+ int i;
+
+ if (!fold || !v)
+ goto error;
+
+ if (isl_val_is_one(v)) {
+ isl_val_free(v);
+ return fold;
+ }
+ if (isl_val_is_zero(v)) {
+ isl_qpolynomial_fold *zero;
+ isl_space *space = isl_qpolynomial_fold_get_domain_space(fold);
+ zero = isl_qpolynomial_fold_empty(fold->type, space);
+ isl_qpolynomial_fold_free(fold);
+ isl_val_free(v);
+ return zero;
+ }
+ if (!isl_val_is_rat(v))
+ isl_die(isl_qpolynomial_fold_get_ctx(fold), isl_error_invalid,
+ "expecting rational factor", goto error);
+
+ fold = isl_qpolynomial_fold_cow(fold);
+ if (!fold)
+ goto error;
+
+ if (isl_val_is_neg(v))
+ fold->type = isl_fold_type_negate(fold->type);
+ for (i = 0; i < fold->n; ++i) {
+ fold->qp[i] = isl_qpolynomial_scale_val(fold->qp[i],
+ isl_val_copy(v));
+ if (!fold->qp[i])
+ goto error;
+ }
+
+ isl_val_free(v);
+ return fold;
+error:
+ isl_val_free(v);
+ isl_qpolynomial_fold_free(fold);
+ return NULL;
+}