#include <strings.h>
#include "isl_ctx.h"
#include "isl_blk.h"
-#include "isl_dim.h"
+#include "isl_dim_private.h"
#include "isl_equalities.h"
#include "isl_list.h"
#include "isl_lp.h"
unsigned isl_basic_map_dim(const struct isl_basic_map *bmap,
enum isl_dim_type type)
{
+ if (!bmap)
+ return 0;
switch (type) {
case isl_dim_param:
case isl_dim_in:
unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
{
- return bset->dim->n_out;
+ return isl_basic_set_dim(bset, isl_dim_set);
}
unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
{
- return bset->dim->nparam;
+ return isl_basic_set_dim(bset, isl_dim_param);
}
unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
unsigned isl_set_n_dim(const struct isl_set *set)
{
- return set->dim->n_out;
+ return isl_set_dim(set, isl_dim_set);
}
unsigned isl_set_n_param(const struct isl_set *set)
{
- return set->dim->nparam;
+ return isl_set_dim(set, isl_dim_param);
}
unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
{
- return isl_dim_total(bmap->dim) + bmap->n_div;
+ return bmap ? isl_dim_total(bmap->dim) + bmap->n_div : 0;
}
unsigned isl_map_n_in(const struct isl_map *map)
bmap->dim->nparam == bset->dim->nparam;
}
+isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
+{
+ return map ? map->ctx : NULL;
+}
+
struct isl_dim *isl_basic_map_get_dim(struct isl_basic_map *bmap)
{
if (!bmap)
return isl_dim_copy(set->dim);
}
+__isl_give isl_basic_map *isl_basic_map_set_dim_name(
+ __isl_take isl_basic_map *bmap,
+ enum isl_dim_type type, unsigned pos, const char *s)
+{
+ if (!bmap)
+ return NULL;
+ bmap->dim = isl_dim_set_name(bmap->dim, type, pos, s);
+ if (!bmap->dim)
+ goto error;
+ return bmap;
+error:
+ isl_basic_map_free(bmap);
+ return NULL;
+}
+
+__isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
+ enum isl_dim_type type, unsigned pos, const char *s)
+{
+ int i;
+
+ if (!map)
+ return NULL;
+
+ map->dim = isl_dim_set_name(map->dim, type, pos, s);
+ if (!map->dim)
+ goto error;
+
+ for (i = 0; i < map->n; ++i) {
+ map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
+ if (!map->p[i])
+ goto error;
+ }
+
+ return map;
+error:
+ isl_map_free(map);
+ return NULL;
+}
+
+__isl_give isl_basic_set *isl_basic_set_set_dim_name(
+ __isl_take isl_basic_set *bset,
+ enum isl_dim_type type, unsigned pos, const char *s)
+{
+ return (isl_basic_set *)isl_basic_map_set_dim_name(
+ (isl_basic_map *)bset, type, pos, s);
+}
+
+__isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
+ enum isl_dim_type type, unsigned pos, const char *s)
+{
+ return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
+}
+
+int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
+{
+ if (!bmap)
+ return -1;
+ return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
+}
+
static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
struct isl_basic_map *bmap, unsigned extra,
unsigned n_eq, unsigned n_ineq)
int i;
size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
+ bmap->ctx = ctx;
+ isl_ctx_ref(ctx);
+
bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
- if (isl_blk_is_error(bmap->block)) {
- free(bmap);
- return NULL;
- }
+ if (isl_blk_is_error(bmap->block))
+ goto error;
bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
- if (!bmap->ineq) {
- isl_blk_free(ctx, bmap->block);
- free(bmap);
- return NULL;
- }
+ if (!bmap->ineq)
+ goto error;
if (extra == 0) {
bmap->block2 = isl_blk_empty();
bmap->div = NULL;
} else {
bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
- if (isl_blk_is_error(bmap->block2)) {
- free(bmap->ineq);
- isl_blk_free(ctx, bmap->block);
- free(bmap);
- return NULL;
- }
+ if (isl_blk_is_error(bmap->block2))
+ goto error;
bmap->div = isl_alloc_array(ctx, isl_int *, extra);
- if (!bmap->div) {
- isl_blk_free(ctx, bmap->block2);
- free(bmap->ineq);
- isl_blk_free(ctx, bmap->block);
- free(bmap);
- return NULL;
- }
+ if (!bmap->div)
+ goto error;
}
for (i = 0; i < n_ineq + n_eq; ++i)
for (i = 0; i < extra; ++i)
bmap->div[i] = bmap->block2.data + i * (1 + row_size);
- bmap->ctx = ctx;
- isl_ctx_ref(ctx);
bmap->ref = 1;
bmap->flags = 0;
bmap->c_size = n_eq + n_ineq;
bmap->sample = NULL;
return bmap;
+error:
+ isl_basic_map_free(bmap);
+ return NULL;
}
struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
if (!dim)
return NULL;
- bmap = isl_alloc_type(dim->ctx, struct isl_basic_map);
+ bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
if (!bmap)
goto error;
bmap->dim = dim;
return NULL;
dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
if (!dim)
- return NULL;
+ goto error;
bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
return bmap;
+error:
+ isl_basic_map_free(base);
+ return NULL;
}
struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
bmap->ref--;
bmap = isl_basic_map_dup(bmap);
}
- ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
+ if (bmap)
+ ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
return bmap;
}
isl_basic_map_compatible_domain(bmap, bset), goto error);
bmap = isl_basic_map_cow(bmap);
- bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
- bset->n_div, bset->n_eq, bset->n_ineq);
if (!bmap)
goto error;
+ bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
+ bset->n_div, bset->n_eq, bset->n_ineq);
dim = isl_dim_reverse(isl_dim_copy(bset->dim));
bmap_domain = isl_basic_map_from_basic_set(bset, dim);
bmap = add_constraints(bmap, bmap_domain, 0, 0);
isl_basic_map_compatible_range(bmap, bset), goto error);
bmap = isl_basic_map_cow(bmap);
- bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
- bset->n_div, bset->n_eq, bset->n_ineq);
if (!bmap)
goto error;
+ bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
+ bset->n_div, bset->n_eq, bset->n_ineq);
bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
bmap = add_constraints(bmap, bmap_range, 0, 0);
sample = isl_vec_copy(bmap2->sample);
bmap1 = isl_basic_map_cow(bmap1);
- bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
- bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
if (!bmap1)
goto error;
+ bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
+ bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
bmap1 = add_constraints(bmap1, bmap2, 0, 0);
- if (sample) {
+ if (!bmap1)
+ isl_vec_free(sample);
+ else if (sample) {
isl_vec_free(bmap1->sample);
bmap1->sample = sample;
}
off += size;
}
}
- isl_dim_map_div(dim_map, bmap, off + n);
+ isl_dim_map_div(dim_map, bmap, off);
res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
bmap->n_div, bmap->n_eq, bmap->n_ineq);
bmap = move_last(bmap, type, first, n);
bmap = isl_basic_map_cow(bmap);
+ if (!bmap)
+ return NULL;
row_size = 1 + isl_dim_total(bmap->dim) + bmap->extra;
old = bmap->block2.data;
(bmap->extra + n) * (1 + row_size));
if (!bmap->block2.data)
goto error;
- new_div = isl_alloc_array(ctx, isl_int *, bmap->extra + n);
+ new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
if (!new_div)
goto error;
for (i = 0; i < n; ++i) {
return map;
error:
isl_map_free(map);
- return map;
+ return NULL;
}
/* Turn the n dimensions of type type, starting at first
struct isl_map *map;
int i;
+ if (n == 0 && equal)
+ return isl_map_universe(dims);
+
map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
for (i = 0; i + 1 < n; ++i)
struct isl_map *map;
int i;
+ if (n == 0 && equal)
+ return isl_map_universe(dims);
+
map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
for (i = 0; i + 1 < n; ++i)
return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
}
+static __isl_give isl_basic_map *isl_basic_map_reset_dim(
+ __isl_take isl_basic_map *bmap, __isl_take isl_dim *dim)
+{
+ bmap = isl_basic_map_cow(bmap);
+ if (!bmap || !dim)
+ goto error;
+
+ isl_dim_free(bmap->dim);
+ bmap->dim = dim;
+
+ return bmap;
+error:
+ isl_basic_map_free(bmap);
+ isl_dim_free(dim);
+ return NULL;
+}
+
+static __isl_give isl_basic_set *isl_basic_set_reset_dim(
+ __isl_take isl_basic_set *bset, __isl_take isl_dim *dim)
+{
+ return (isl_basic_set *)isl_basic_map_reset_dim((isl_basic_map *)bset,
+ dim);
+}
+
+__isl_give isl_map *isl_map_reset_dim(__isl_take isl_map *map,
+ __isl_take isl_dim *dim)
+{
+ int i;
+
+ map = isl_map_cow(map);
+ if (!map || !dim)
+ goto error;
+
+ for (i = 0; i < map->n; ++i) {
+ map->p[i] = isl_basic_map_reset_dim(map->p[i],
+ isl_dim_copy(dim));
+ if (!map->p[i])
+ goto error;
+ }
+ isl_dim_free(map->dim);
+ map->dim = dim;
+
+ return map;
+error:
+ isl_map_free(map);
+ isl_dim_free(dim);
+ return NULL;
+}
+
+__isl_give isl_set *isl_set_reset_dim(__isl_take isl_set *set,
+ __isl_take isl_dim *dim)
+{
+ return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
+}
+
struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
{
+ isl_dim *dim;
struct isl_basic_set *domain;
unsigned n_in;
unsigned n_out;
+
if (!bmap)
return NULL;
+ dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
+
n_in = isl_basic_map_n_in(bmap);
n_out = isl_basic_map_n_out(bmap);
domain = isl_basic_set_from_basic_map(bmap);
- return isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
+ domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
+
+ domain = isl_basic_set_reset_dim(domain, dim);
+
+ return domain;
}
struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
__isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
{
- return isl_map_reverse(isl_map_from_range(set));;
+ return isl_map_reverse(isl_map_from_range(set));
}
__isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
__isl_take isl_set *range)
{
- return isl_map_product(isl_map_from_domain(domain),
- isl_map_from_range(range));
+ return isl_map_apply_range(isl_map_from_domain(domain),
+ isl_map_from_range(range));
}
struct isl_set *isl_set_from_map(struct isl_map *map)
return (isl_set *)isl_map_lexmax((isl_map *)set);
}
-static struct isl_map *isl_map_reset_dim(struct isl_map *map,
- struct isl_dim *dim)
-{
- int i;
-
- if (!map || !dim)
- goto error;
-
- for (i = 0; i < map->n; ++i) {
- isl_dim_free(map->p[i]->dim);
- map->p[i]->dim = isl_dim_copy(dim);
- }
- isl_dim_free(map->dim);
- map->dim = dim;
-
- return map;
-error:
- isl_map_free(map);
- isl_dim_free(dim);
- return NULL;
-}
-
-static struct isl_set *isl_set_reset_dim(struct isl_set *set,
- struct isl_dim *dim)
-{
- return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
-}
-
/* Apply a preimage specified by "mat" on the parameters of "bset".
* bset is assumed to have only parameters and divs.
*/
int isl_map_fast_is_empty(struct isl_map *map)
{
- return map->n == 0;
+ return map ? map->n == 0 : -1;
}
int isl_set_fast_is_empty(struct isl_set *set)
{
- return set->n == 0;
+ return set ? set->n == 0 : -1;
}
int isl_set_is_empty(struct isl_set *set)
int isl_map_fast_is_universe(__isl_keep isl_map *map)
{
+ int i;
+
if (!map)
return -1;
- return map->n == 1 && isl_basic_map_is_universe(map->p[0]);
+ for (i = 0; i < map->n; ++i) {
+ int r = isl_basic_map_is_universe(map->p[i]);
+ if (r < 0 || r)
+ return r;
+ }
+
+ return 0;
}
int isl_set_fast_is_universe(__isl_keep isl_set *set)
if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
struct isl_basic_map *copy = isl_basic_map_copy(bmap);
- copy = isl_basic_map_convex_hull(copy);
+ copy = isl_basic_map_remove_redundancies(copy);
empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
isl_basic_map_free(copy);
return empty;
struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
{
int i;
- unsigned off = isl_dim_total(bmap->dim);
+ unsigned off;
+
+ if (!bmap)
+ return NULL;
+
+ off = isl_dim_total(bmap->dim);
for (i = 0; i < bmap->n_div; ++i) {
int pos;
return NULL;
}
+__isl_give isl_basic_set *isl_basic_set_sort_constraints(
+ __isl_take isl_basic_set *bset)
+{
+ return (struct isl_basic_set *)isl_basic_map_sort_constraints(
+ (struct isl_basic_map *)bset);
+}
+
struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
{
if (!bmap)
return NULL;
if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
return bmap;
- bmap = isl_basic_map_convex_hull(bmap);
+ bmap = isl_basic_map_remove_redundancies(bmap);
bmap = isl_basic_map_sort_constraints(bmap);
ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
return bmap;
(struct isl_basic_map *)bset);
}
-static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
- const struct isl_basic_map *bmap2)
+int isl_basic_map_fast_cmp(const __isl_keep isl_basic_map *bmap1,
+ const __isl_keep isl_basic_map *bmap2)
{
int i, cmp;
unsigned total;
return 0;
}
-static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
- struct isl_basic_map *bmap2)
+int isl_basic_map_fast_is_equal(__isl_keep isl_basic_map *bmap1,
+ __isl_keep isl_basic_map *bmap2)
{
return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
}
+int isl_basic_set_fast_is_equal(__isl_keep isl_basic_set *bset1,
+ __isl_keep isl_basic_set *bset2)
+{
+ return isl_basic_map_fast_is_equal((isl_basic_map *)bset1,
+ (isl_basic_map *)bset2);
+}
+
static int qsort_bmap_cmp(const void *p1, const void *p2)
{
const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
first += pos(bset->dim, type) - 1;
return isl_basic_set_vars_get_sign(bset, first, n, signs);
}
+
+/* Check if the given map is single-valued.
+ * We simply compute
+ *
+ * M \circ M^-1
+ *
+ * and check if the result is a subset of the identity mapping.
+ */
+int isl_map_is_single_valued(__isl_keep isl_map *map)
+{
+ isl_map *test;
+ isl_map *id;
+ int sv;
+
+ test = isl_map_reverse(isl_map_copy(map));
+ test = isl_map_apply_range(test, isl_map_copy(map));
+
+ id = isl_map_identity(isl_dim_range(isl_map_get_dim(map)));
+
+ sv = isl_map_is_subset(test, id);
+
+ isl_map_free(test);
+ isl_map_free(id);
+
+ return sv;
+}
+
+int isl_map_is_bijective(__isl_keep isl_map *map)
+{
+ int sv;
+
+ sv = isl_map_is_single_valued(map);
+ if (sv < 0 || !sv)
+ return sv;
+
+ map = isl_map_copy(map);
+ map = isl_map_reverse(map);
+ sv = isl_map_is_single_valued(map);
+ isl_map_free(map);
+
+ return sv;
+}
+
+int isl_set_is_singleton(__isl_keep isl_set *set)
+{
+ return isl_map_is_single_valued((isl_map *)set);
+}
+
+int isl_map_is_translation(__isl_keep isl_map *map)
+{
+ int ok;
+ isl_set *delta;
+
+ delta = isl_map_deltas(isl_map_copy(map));
+ ok = isl_set_is_singleton(delta);
+ isl_set_free(delta);
+
+ return ok;
+}
+
+static int unique(isl_int *p, unsigned pos, unsigned len)
+{
+ if (isl_seq_first_non_zero(p, pos) != -1)
+ return 0;
+ if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
+ return 0;
+ return 1;
+}
+
+int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
+{
+ int i, j;
+ unsigned nvar;
+ unsigned ovar;
+
+ if (!bset)
+ return -1;
+
+ if (isl_basic_set_dim(bset, isl_dim_div) != 0)
+ return 0;
+
+ nvar = isl_basic_set_dim(bset, isl_dim_set);
+ ovar = isl_dim_offset(bset->dim, isl_dim_set);
+ for (j = 0; j < nvar; ++j) {
+ int lower = 0, upper = 0;
+ for (i = 0; i < bset->n_eq; ++i) {
+ if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
+ continue;
+ if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
+ return 0;
+ break;
+ }
+ if (i < bset->n_eq)
+ continue;
+ for (i = 0; i < bset->n_ineq; ++i) {
+ if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
+ continue;
+ if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
+ return 0;
+ if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
+ lower = 1;
+ else
+ upper = 1;
+ }
+ if (!lower || !upper)
+ return 0;
+ }
+
+ return 1;
+}
+
+int isl_set_is_box(__isl_keep isl_set *set)
+{
+ if (!set)
+ return -1;
+ if (set->n != 1)
+ return 0;
+
+ return isl_basic_set_is_box(set->p[0]);
+}