From fb13782b7ff5f63b5a6d6b5c229fa0796c6c33aa Mon Sep 17 00:00:00 2001 From: Sven Verdoolaege Date: Thu, 11 Nov 2010 13:04:22 +0100 Subject: [PATCH] add isl_set_foreach_orthant Signed-off-by: Sven Verdoolaege --- isl_map.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ isl_map_private.h | 3 +++ 2 files changed, 67 insertions(+) diff --git a/isl_map.c b/isl_map.c index 44f213d..d2b64c8 100644 --- a/isl_map.c +++ b/isl_map.c @@ -5642,6 +5642,70 @@ error: return NULL; } +static int foreach_orthant(__isl_take isl_set *set, int *signs, int first, + int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user), + void *user) +{ + isl_set *half; + + if (!set) + return -1; + if (isl_set_fast_is_empty(set)) { + isl_set_free(set); + return 0; + } + if (first == len) + return fn(set, signs, user); + + signs[first] = 1; + half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_dim(set), + 1 + first)); + half = isl_set_intersect(half, isl_set_copy(set)); + if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0) + goto error; + + signs[first] = -1; + half = isl_set_from_basic_set(neg_halfspace(isl_set_get_dim(set), + 1 + first)); + half = isl_set_intersect(half, set); + return foreach_orthant(half, signs, first + 1, len, fn, user); +error: + isl_set_free(set); + return -1; +} + +/* Call "fn" on the intersections of "set" with each of the orthants + * (except for obviously empty intersections). The orthant is identified + * by the signs array, with each entry having value 1 or -1 according + * to the sign of the corresponding variable. + */ +int isl_set_foreach_orthant(__isl_keep isl_set *set, + int (*fn)(__isl_take isl_set *orthant, int *signs, void *user), + void *user) +{ + unsigned nparam; + unsigned nvar; + int *signs; + int r; + + if (!set) + return -1; + if (isl_set_fast_is_empty(set)) + return 0; + + nparam = isl_set_dim(set, isl_dim_param); + nvar = isl_set_dim(set, isl_dim_set); + + signs = isl_alloc_array(set->ctx, int, nparam + nvar); + + r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar, + fn, user); + + free(signs); + + return r; +} + int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2) { return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2); diff --git a/isl_map_private.h b/isl_map_private.h index 409025c..5066be2 100644 --- a/isl_map_private.h +++ b/isl_map_private.h @@ -193,6 +193,9 @@ int isl_set_contains_point(__isl_keep isl_set *set, __isl_keep isl_point *point) int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset, unsigned first, unsigned n, int *signs); +int isl_set_foreach_orthant(__isl_keep isl_set *set, + int (*fn)(__isl_take isl_set *orthant, int *signs, void *user), + void *user); int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset, unsigned pos, isl_int *div); -- 2.7.4