*/
#include <isl_map_private.h>
-#include <isl/constraint.h>
+#include <isl_constraint_private.h>
#include <isl_dim_private.h>
#include <isl_div_private.h>
#include <isl/seq.h>
constraint);
}
+__isl_give isl_map *isl_map_add_constraint(__isl_take isl_map *map,
+ __isl_take isl_constraint *constraint)
+{
+ isl_basic_map *bmap;
+
+ bmap = isl_basic_map_from_constraint(constraint);
+ map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
+
+ return map;
+}
+
+__isl_give isl_set *isl_set_add_constraint(__isl_take isl_set *set,
+ __isl_take isl_constraint *constraint)
+{
+ return isl_map_add_constraint(set, constraint);
+}
+
struct isl_constraint *isl_constraint_add_div(struct isl_constraint *constraint,
struct isl_div *div, int *pos)
{
isl_seq_clr(constraint->line[0], 1 + total);
}
+/* Check whether the two basic maps have identical divs in the same order.
+ */
+static int equal_divs(__isl_keep isl_basic_map *bmap1,
+ __isl_keep isl_basic_map *bmap2)
+{
+ int i;
+ unsigned total;
+
+ if (!isl_basic_map_divs_known(bmap1))
+ return 0;
+ if (!isl_basic_map_divs_known(bmap2))
+ return 0;
+ if (bmap1->n_div != bmap2->n_div)
+ return 0;
+
+ total = isl_basic_map_total_dim(bmap1);
+ for (i = 0; i < bmap1->n_div; ++i)
+ if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
+ return 0;
+
+ return 1;
+}
+
+/* Drop any constraint from "bset" that is identical to "constraint".
+ * In particular, this means that the local spaces of "bset" and
+ * "constraint" need to be the same.
+ *
+ * Since the given constraint may actually be a pointer into the bset,
+ * we have to be careful not to reorder the constraints as the user
+ * may be holding on to other constraints from the same bset.
+ * This should be cleaned up when the internal representation of
+ * isl_constraint is changed to use isl_aff.
+ */
+__isl_give isl_basic_set *isl_basic_set_drop_constraint(
+ __isl_take isl_basic_set *bset, __isl_take isl_constraint *constraint)
+{
+ int i;
+ unsigned n;
+ isl_int **row;
+ unsigned total;
+
+ if (!bset || !constraint)
+ goto error;
+
+ if (!isl_dim_equal(bset->dim, constraint->bmap->dim))
+ isl_die(bset->ctx, isl_error_invalid,
+ "spaces don't match", goto error);
+
+ if (bset != constraint->bmap && !equal_divs(bset, constraint->bmap)) {
+ isl_constraint_free(constraint);
+ return bset;
+ }
+
+ if (isl_constraint_is_equality(constraint)) {
+ n = bset->n_eq;
+ row = bset->eq;
+ } else {
+ n = bset->n_ineq;
+ row = bset->ineq;
+ }
+
+ total = isl_basic_map_total_dim(constraint->bmap);
+ for (i = 0; i < n; ++i)
+ if (isl_seq_eq(row[i], constraint->line[0], 1 + total))
+ isl_seq_clr(row[i], 1 + total);
+
+ isl_constraint_free(constraint);
+ return bset;
+error:
+ isl_constraint_free(constraint);
+ isl_basic_set_free(bset);
+ return NULL;
+}
+
struct isl_constraint *isl_constraint_negate(struct isl_constraint *constraint)
{
unsigned total;
return aff;
}
+
+/* For an inequality constraint
+ *
+ * f >= 0
+ *
+ * or an equality constraint
+ *
+ * f = 0
+ *
+ * return the affine expression f.
+ */
+__isl_give isl_aff *isl_constraint_get_aff(
+ __isl_keep isl_constraint *constraint)
+{
+ isl_aff *aff;
+ isl_local_space *ls;
+
+ if (!constraint)
+ return NULL;
+
+ ls = isl_basic_set_get_local_space(constraint->bmap);
+ aff = isl_aff_alloc(ls);
+ if (!aff)
+ return NULL;
+
+ isl_seq_cpy(aff->v->el + 1, constraint->line[0], aff->v->size - 1);
+ isl_int_set_si(aff->v->el[0], 1);
+
+ return aff;
+}
+
+/* Construct an equality constraint equating the given affine expression
+ * to zero.
+ */
+__isl_give isl_constraint *isl_equality_from_aff(__isl_take isl_aff *aff)
+{
+ int k;
+ isl_basic_set *bset;
+
+ if (!aff)
+ return NULL;
+
+ bset = isl_basic_set_from_local_space(isl_aff_get_local_space(aff));
+ bset = isl_basic_set_extend_constraints(bset, 1, 0);
+ k = isl_basic_set_alloc_equality(bset);
+ if (k < 0)
+ goto error;
+
+ isl_seq_cpy(bset->eq[k], aff->v->el + 1, aff->v->size - 1);
+ isl_aff_free(aff);
+
+ return isl_basic_set_constraint(bset, &bset->eq[k]);
+error:
+ isl_aff_free(aff);
+ isl_basic_set_free(bset);
+ return NULL;
+}
+
+/* Construct an inequality constraint enforcing the given affine expression
+ * to be non-negative.
+ */
+__isl_give isl_constraint *isl_inequality_from_aff(__isl_take isl_aff *aff)
+{
+ int k;
+ isl_basic_set *bset;
+
+ if (!aff)
+ return NULL;
+
+ bset = isl_basic_set_from_local_space(isl_aff_get_local_space(aff));
+ bset = isl_basic_set_extend_constraints(bset, 0, 1);
+ k = isl_basic_set_alloc_inequality(bset);
+ if (k < 0)
+ goto error;
+
+ isl_seq_cpy(bset->ineq[k], aff->v->el + 1, aff->v->size - 1);
+ isl_aff_free(aff);
+
+ return isl_basic_set_constraint(bset, &bset->ineq[k]);
+error:
+ isl_aff_free(aff);
+ isl_basic_set_free(bset);
+ return NULL;
+}