<commit_before>/*
 * Copyright 2008-2009 Katholieke Universiteit Leuven
 * Copyright 2014      INRIA Rocquencourt
 *
 * Use of this software is governed by the MIT license
 *
 * Written by <NAME>, K.U.Leuven, Departement
 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
 * B.P. 105 - 78153 Le Chesnay, France
 */

#include <isl_ctx_private.h>
#include <isl_map_private.h>
#include <isl_lp_private.h>
#include <isl/map.h>
#include <isl_mat_private.h>
#include <isl_vec_private.h>
#include <isl/set.h>
#include <isl_seq.h>
#include <isl_options_private.h>
#include "isl_equalities.h"
#include "isl_tab.h"
#include <isl_sort.h>

#include <bset_to_bmap.c>
#include <bset_from_bmap.c>
#include <set_to_map.c>

static __isl_give isl_basic_set *uset_convex_hull_wrap_bounded(
	__isl_take isl_set *set);

/* Remove redundant
 * constraints.  If the minimal value along the normal of a constraint
 * is the same if the constraint is removed, then the constraint is redundant.
 *
 * Since some constraints may be mutually redundant, sort the constraints
 * first such that constraints that involve existentially quantified
 * variables are considered for removal before those that do not.
 * The sorting is also needed for the use in map_simple_hull.
 *
 * Note that isl_tab_detect_implicit_equalities may also end up
 * marking some constraints as redundant.  Make sure the constraints
 * are preserved and undo those marking such that isl_tab_detect_redundant
 * can consider the constraints in the sorted order.
 *
 * Alternatively, we could have intersected the basic map with the
 * corresponding equality and then checked if the dimension was that
 * of a facet.
 */
__isl_give isl_basic_map *isl_basic_map_remove_redundancies(
	__isl_take isl_basic_map *bmap)
{
	struct isl_tab *tab;

	if (!bmap)
		return NULL;

	bmap = isl_basic_map_gauss(bmap, NULL);
	if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
		return bmap;
	if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_REDUNDANT))
		return bmap;
	if (bmap->n_ineq <= 1)
		return bmap;

	bmap = isl_basic_map_sort_constraints(bmap);
	tab = isl_tab_from_basic_map(bmap, 0);
	if (!tab)
		goto error;
	tab->preserve = 1;
	if (isl_tab_detect_implicit_equalities(tab) < 0)
		goto error;
	if (isl_tab_restore_redundant(tab) < 0)
		goto error;
	tab->preserve = 0;
	if (isl_tab_detect_redundant(tab) < 0)
		goto error;
	bmap = isl_basic_map_update_from_tab(bmap, tab);
	isl_tab_free(tab);
	if (!bmap)
		return NULL;
	ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
	ISL_F_SET(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
	return bmap;
error:
	isl_tab_free(tab);
	isl_basic_map_free(bmap);
	return NULL;
}

__isl_give isl_basic_set *isl_basic_set_remove_redundancies(
	__isl_take isl_basic_set *bset)
{
	return bset_from_bmap(
		isl_basic_map_remove_redundancies(bset_to_bmap(bset)));
}

/* Remove redundant constraints in each of the basic maps.
 */
__isl_give isl_map *isl_map_remove_redundancies(__isl_take isl_map *map)
{
	return isl_map_inline_foreach_basic_map(map,
					    &isl_basic_map_remove_redundancies);
}

__isl_give isl_set *isl_set_remove_redundancies(__isl_take isl_set *set)
{
	return isl_map_remove_redundancies(set);
}

/* Check if the set set is bound in the direction of the affine
 * constraint c and if so, set the constant term such that the
 * resulting constraint is a bounding constraint for the set.
 */
static isl_bool uset_is_bound(__isl_keep isl_set *set, isl_int *c, unsigned len)
{
	int first;
	int j;
	isl_int opt;
	isl_int opt_denom;

	isl_int_init(opt);
	isl_int_init(opt_denom);
	first = 1;
	for (j = 0; j < set->n; ++j) {
		enum isl_lp_result res;

		if (ISL_F_ISSET(set->p[j], ISL_BASIC_SET_EMPTY))
			continue;

		res = isl_basic_set_solve_lp(set->p[j],
				0, c, set->ctx->one, &opt, &opt_denom, NULL);
		if (res == isl_lp_unbounded)
			break;
		if (res == isl_lp_error)
			goto error;
		if (res == isl_lp_empty) {
			set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
			if (!set->p[j])
				goto error;
			continue;
		}
		if (first || isl_int_is_neg(opt)) {
			if (!isl_int_is_one(opt_denom))
				isl_seq_scale(c, c, opt_denom, len);
			isl_int_sub(c[0], c[0], opt);
		}
		first = 0;
	}
	isl_int_clear(opt);
	isl_int_clear(opt_denom);
	return isl_bool_ok(j >= set->n);
error:
	isl_int_clear(opt);
	isl_int_clear(opt_denom);
	return isl_bool_error;
}

static __isl_give isl_set *isl_set_add_basic_set_equality(
	__isl_take isl_set *set, isl_int *c)
{
	int i;

	set = isl_set_cow(set);
	if (!set)
		return NULL;
	for (i = 0; i < set->n; ++i) {
		set->p[i] = isl_basic_set_add_eq(set->p[i], c);
		if (!set->p[i])
			goto error;
	}
	return set;
error:
	isl_set_free(set);
	return NULL;
}

/* Given a union of basic sets, construct the constraints for wrapping
 * a facet around one of its ridges.
 * In particular, if each of n the d-dimensional basic sets i in "set"
 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
 * and is defined by the constraints
 *				    [ 1 ]
 *				A_i [ x ]  >= 0
 *
 * then the resulting set is of dimension n*(1+d) and has as constraints
 *
 *				    [ a_i ]
 *				A_i [ x_i ] >= 0
 *
 *				      a_i   >= 0
 *
 *			\sum_i x_{i,1} = 1
 */
static __isl_give isl_basic_set *wrap_constraints(__isl_keep isl_set *set)
{
	struct isl_basic_set *lp;
	unsigned n_eq;
	unsigned n_ineq;
	int i, j, k;
	isl_size dim, lp_dim;

	dim = isl_set_dim(set, isl_dim_set);
	if (dim < 0)
		return NULL;

	dim += 1;
	n_eq = 1;
	n_ineq = set->n;
	for (i = 0; i < set->n; ++i) {
		n_eq += set->p[i]->n_eq;
		n_ineq += set->p[i]->n_ineq;
	}
	lp = isl_basic_set_alloc(set->ctx, 0, dim * set->n, 0, n_eq, n_ineq);
	lp = isl_basic_set_set_rational(lp);
	if (!lp)
		return NULL;
	lp_dim = isl_basic_set_dim(lp, isl_dim_set);
	if (lp_dim < 0)
		return isl_basic_set_free(lp);
	k = isl_basic_set_alloc_equality(lp);
	isl_int_set_si(lp->eq[k][0], -1);
	for (i = 0; i < set->n; ++i) {
		isl_int_set_si(lp->eq[k][1+dim*i], 0);
		isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
		isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
	}
	for (i = 0; i < set->n; ++i) {
		k = isl_basic_set_alloc_inequality(lp);
		isl_seq_clr(lp->ineq[k], 1+lp_dim);
		isl_int_set_si(lp->ineq[k][1+dim*i], 1);

		for (j = 0; j < set->p[i]->n_eq; ++j) {
			k = isl_basic_set_alloc_equality(lp);
			isl_seq_clr(lp->eq[k], 1+dim*i);
			isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
			isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
		}

		for (j = 0; j < set->p[i]->n_ineq; ++j) {
			k = isl_basic_set_alloc_inequality(lp);
			isl_seq_clr(lp->ineq[k], 1+dim*i);
			isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
			isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
		}
	}
	return lp;
}

/* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
 * of that facet, compute the other facet of the convex hull that contains
 * the ridge.
 *
 * We first transform the set such that the facet constraint becomes
 *
 *			x_1 >= 0
 *
 * I.e., the facet lies in
 *
 *			x_1 = 0
 *
 * and on that facet, the constraint that defines the ridge is
 *
 *			x_2 >= 0
 *
 * (This transformation is not strictly needed, all that is needed is
 * that the ridge contains the origin.)
 *
 * Since the ridge contains the origin, the cone of the convex hull
 * will be of the form
 *
 *			x_1 >= 0
 *			x_2 >= a x_1
 *
 * with this second constraint defining the new facet.
 * The constant a is obtained by settting x_1 in the cone of the
 * convex hull to 1 and minimizing x_2.
 * Now, each element in the cone of the convex hull is the sum
 * of elements in the cones of the basic sets.
 * If a_i is the dilation factor of basic set i, then the problem
 * we need to solve is
 *
 *			min \sum_i x_{i,2}
 *			st
 *				\sum_i x_{i,1} = 1
 *				    a_i   >= 0
 *				  [ a_i ]
 *				A [ x_i ] >= 0
 *
 * with
 *				    [  1  ]
 *				A_i [ x_i ] >= 0
 *
 * the constraints of each (transformed) basic set.
 * If a = n/d, then the constraint defining the new facet (in the transformed
 * space) is
 *
 *			-n x_1 + d x_2 >= 0
 *
 * In the original space, we need to take the same combination of the
 * corresponding constraints "facet" and "ridge".
 *
 * If a = -infty = "-1/0", then we just return the original facet constraint.
 * This means that the facet is unbounded, but has a bounded intersection
 * with the union of sets.
 */
isl_int *isl_set_wrap_facet(__isl_keep isl_set *set,
	isl_int *facet, isl_int *ridge)
{
	int i;
	isl_ctx *ctx;
	struct isl_mat *T = NULL;
	struct isl_basic_set *lp = NULL;
	struct isl_vec *obj;
	enum isl_lp_result res;
	isl_int num, den;
	isl_size dim;

	dim = isl_set_dim(set, isl_dim_set);
	if (dim < 0)
		return NULL;
	ctx = set->ctx;
	set = isl_set_copy(set);
	set = isl_set_set_rational(set);

	dim += 1;
	T = isl_mat_alloc(ctx, 3, dim);
	if (!T)
		goto error;
	isl_int_set_si(T->row[0][0], 1);
	isl_seq_clr(T->row[0]+1, dim - 1);
	isl_seq_cpy(T->row[1], facet, dim);
	isl_seq_cpy(T->row[2], ridge, dim);
	T = isl_mat_right_inverse(T);
	set = isl_set_preimage(set, T);
	T = NULL;
	if (!set)
		goto error;
	lp = wrap_constraints(set);
	obj = isl_vec_alloc(ctx, 1 + dim*set->n);
	if (!obj)
		goto error;
	isl_int_set_si(obj->block.data[0], 0);
	for (i = 0; i < set->n; ++i) {
		isl_seq_clr(obj->block.data + 1 + dim*i, 2);
		isl_int_set_si(obj->block.data[1 + dim*i+2], 1);
		isl_seq_clr(obj->block.data + 1 + dim*i+3, dim-3);
	}
	isl_int_init(num);
	isl_int_init(den);
	res = isl_basic_set_solve_lp(lp, 0,
			    obj->block.data, ctx->one, &num, &den, NULL);
	if (res == isl_lp_ok) {
		isl_int_neg(num, num);
		isl_seq_combine(facet, num, facet, den, ridge, dim);
		isl_seq_normalize(ctx, facet, dim);
	}
	isl_int_clear(num);
	isl_int_clear(den);
	isl_vec_free(obj);
	isl_basic_set_free(lp);
	isl_set_free(set);
	if (res == isl_lp_error)
		return NULL;
	isl_assert(ctx, res == isl_lp_ok || res == isl_lp_unbounded, 
		   return NULL);
	return facet;
error:
	isl_basic_set_free(lp);
	isl_mat_free(T);
	isl_set_free(set);
	return NULL;
}

/* Compute the constraint of a facet of "set".
 *
 * We first compute the intersection with a bounding constraint
 * that is orthogonal to one of the coordinate axes.
 * If the affine hull of this intersection has only one equality,
 * we have found a facet.
 * Otherwise, we wrap the current bounding constraint around
 * one of the equalities of the face (one that is not equal to
 * the current bounding constraint).
 * This process continues until we have found a facet.
 * The dimension of the intersection increases by at least
 * one on each iteration, so termination is guaranteed.
 */
static __isl_give isl_mat *initial_facet_constraint(__isl_keep isl_set *set)
{
	struct isl_set *slice = NULL;
	struct isl_basic_set *face = NULL;
	int i;
	isl_size dim = isl_set_dim(set, isl_dim_set);
	isl_bool is_bound;
	isl_mat *bounds = NULL;

	if (dim < 0)
		return NULL;
	isl_assert(set->ctx, set->n > 0, goto error);
	bounds = isl_mat_alloc(set->ctx, 1, 1 + dim);
	if (!bounds)
		return NULL;

	isl_seq_clr(bounds->row[0], dim);
	isl_int_set_si(bounds->row[0][1 + dim - 1], 1);
	is_bound = uset_is_bound(set, bounds->row[0], 1 + dim);
	if (is_bound < 0)
		goto error;
	isl_assert(set->ctx, is_bound, goto error);
	isl_seq_normalize(set->ctx, bounds->row[0], 1 + dim);
	bounds->n_row = 1;

	for (;;) {
		slice = isl_set_copy(set);
		slice = isl_set_add_basic_set_equality(slice, bounds->row[0]);
		face = isl_set_affine_hull(slice);
		if (!face)
			goto error;
		if (face->n_eq == 1) {
			isl_basic_set_free(face);
			break;
		}
		for (i = 0; i < face->n_eq; ++i)
			if (!isl_seq_eq(bounds->row[0], face->eq[i], 1 + dim) &&
			    !isl_seq_is_neg(bounds->row[0],
						face->eq[i], 1 + dim))
				break;
		isl_assert(set->ctx, i < face->n_eq, goto error);
		if (!isl_set_wrap_facet(set, bounds->row[0], face->eq[i]))
			goto error;
		isl_seq_normalize(set->ctx, bounds->row[0], bounds->n_col);
		isl_basic_set_free(face);
	}

	return bounds;
error:
	isl_basic_set_free(face);
	isl_mat_free(bounds);
	return NULL;
}

/* Given the bounding constraint "c" of a facet of the convex hull of "set",
 * compute a hyperplane description of the facet, i.e., compute the facets
 * of the facet.
 *
 * We compute an affine transformation that transforms the constraint
 *
 *			  [ 1 ]
 *			c [ x ] = 0
 *
 * to the constraint
 *
 *			   z_1  = 0
 *
 * by computing the right inverse U of a matrix that starts with the rows
 *
 *			[ 1 0 ]
 *			[  c  ]
 *
 * Then
 *			[ 1 ]     [ 1 ]
 *			[ x ] = U [ z ]
 * and
 *			[ 1 ]     [ 1 ]
 *			[ z ] = Q [ x ]
 *
 * with Q = U^{-1}
 * Since z_1 is zero, we can drop this variable as well as the corresponding
 * column of U to obtain
 *
 *			[ 1 ]      [ 1  ]
 *			[ x ] = U' [ z' ]
 * and
 *			[ 1  ]      [ 1 ]
 *			[ z' ] = Q' [ x ]
 *
 * with Q' equal to Q, but without the corresponding row.
 * After computing the facets of the facet in the z' space,
 * we convert them back to the x space through Q.
 */
static __isl_give isl_basic_set *compute_facet(__isl_keep isl_set *set,
	isl_int *c)
{
	struct isl_mat *m, *U, *Q;
	struct isl_basic_set *facet = NULL;
	struct isl_ctx *ctx;
	isl_size dim;

	dim = isl_set_dim(set, isl_dim_set);
	if (dim < 0)
		return NULL;
	ctx = set->ctx;
	set = isl_set_copy(set);
	m = isl_mat_alloc(set->ctx, 2, 1 + dim);
	if (!m)
		goto error;
	isl_int_set_si(m->row[0][0], 1);
	isl_seq_clr(m->row[0]+1, dim);
	isl_seq_cpy(m->row[1], c, 1+dim);
	U = isl_mat_right_inverse(m);
	Q = isl_mat_right_inverse(isl_mat_copy(U));
	U = isl_mat_drop_cols(U, 1, 1);
	Q = isl_mat_drop_rows(Q, 1, 1);
	set = isl_set_preimage(set, U);
	facet = uset_convex_hull_wrap_bounded(set);
	facet = isl_basic_set_preimage(facet, Q);
	if (facet && facet->n_eq != 0)
		isl_die(ctx, isl_error_internal, "unexpected equality",
			return isl_basic_set_free(facet));
	return facet;
error:
	isl_basic_set_free(facet);
	isl_set_free(set);
	return NULL;
}

/* Given an initial facet constraint, compute the remaining facets.
 * We do this by running through all facets found so far and computing
 * the adjacent facets through wrapping, adding those facets that we
 * hadn't already found before.
 *
 * For each facet we have found so far, we first compute its facets
 * in the resulting convex hull.  That is, we compute the ridges
 * of the resulting convex hull contained in the facet.
 * We also compute the corresponding facet in the current approximation
 * of the convex hull.  There is no need to wrap around the ridges
 * in this facet since that would result in a facet that is already
 * present in the current approximation.
 *
 * This function can still be significantly optimized by checking which of
 * the facets of the basic sets are also facets of the convex hull and
 * using all the facets so far to help in constructing the facets of the
 * facets
 * and/or
 * using the technique in section "3.1 Ridge Generation" of
 * "Extended Convex Hull" by Fukuda et al.
 */
static __isl_give isl_basic_set *extend(__isl_take isl_basic_set *hull,
	__isl_keep isl_set *set)
{
	int i, j, f;
	int k;
	struct isl_basic_set *facet = NULL;
	struct isl_basic_set *hull_facet = NULL;
	isl_size dim;

	dim = isl_set_dim(set, isl_dim_set);
	if (dim < 0 || !hull)
		return isl_basic_set_free(hull);

	isl_assert(set->ctx, set->n > 0, goto error);

	for (i = 0; i < hull->n_ineq; ++i) {
		facet = compute_facet(set, hull->ineq[i]);
		facet = isl_basic_set_add_eq(facet, hull->ineq[i]);
		facet = isl_basic_set_gauss(facet, NULL);
		facet = isl_basic_set_normalize_constraints(facet);
		hull_facet = isl_basic_set_copy(hull);
		hull_facet = isl_basic_set_add_eq(hull_facet, hull->ineq[i]);
		hull_facet = isl_basic_set_gauss(hull_facet, NULL);
		hull_facet = isl_basic_set_normalize_constraints(hull_facet);
		if (!facet || !hull_facet)
			goto error;
		hull = isl_basic_set_cow(hull);
		hull = isl_basic_set_extend_space(hull,
			isl_space_copy(hull->dim), 0, 0, facet->n_ineq);
		if (!hull)
			goto error;
		for (j = 0; j < facet->n_ineq; ++j) {
			for (f = 0; f < hull_facet->n_ineq; ++f)
				if (isl_seq_eq(facet->ineq[j],
						hull_facet->ineq[f], 1 + dim))
					break;
			if (f < hull_facet->n_ineq)
				continue;
			k = isl_basic_set_alloc_inequality(hull);
			if (k < 0)
				goto error;
			isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+dim);
			if (!isl_set_wrap_facet(set, hull->ineq[k], facet->ineq[j]))
				goto error;
		}
		isl_basic_set_free(hull_facet);
		isl_basic_set_free(facet);
	}
	hull = isl_basic_set_simplify(hull);
	hull = isl_basic_set_finalize(hull);
	return hull;
error:
	isl_basic_set_free(hull_facet);
	isl_basic_set_free(facet);
	isl_basic_set_free(hull);
	return NULL;
}

/* Special case for computing the convex hull of a one dimensional set.
 * We simply collect the lower and upper bounds of each basic set
 * and the biggest of those.
 */
static __isl_give isl_basic_set *convex_hull_1d(__isl_take isl_set *set)
{
	struct isl_mat *c = NULL;
	isl_int *lower = NULL;
	isl_int *upper = NULL;
	int i, j, k;
	isl_int a, b;
	struct isl_basic_set *hull;

	for (i = 0; i < set->n; ++i) {
		set->p[i] = isl_basic_set_simplify(set->p[i]);
		if (!set->p[i])
			goto error;
	}
	set = isl_set_remove_empty_parts(set);
	if (!set)
		goto error;
	isl_assert(set->ctx, set->n > 0, goto error);
	c = isl_mat_alloc(set->ctx, 2, 2);
	if (!c)
		goto error;

	if (set->p[0]->n_eq > 0) {
		isl_assert(set->ctx, set->p[0]->n_eq == 1, goto error);
		lower = c->row[0];
		upper = c->row[1];
		if (isl_int_is_pos(set->p[0]->eq[0][1])) {
			isl_seq_cpy(lower, set->p[0]->eq[0], 2);
			isl_seq_neg(upper, set->p[0]->eq[0], 2);
		} else {
			isl_seq_neg(lower, set->p[0]->eq[0], 2);
			isl_seq_cpy(upper, set->p[0]->eq[0], 2);
		}
	} else {
		for (j = 0; j < set->p[0]->n_ineq; ++j) {
			if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
				lower = c->row[0];
				isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
			} else {
				upper = c->row[1];
				isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
			}
		}
	}

	isl_int_init(a);
	isl_int_init(b);
	for (i = 0; i < set->n; ++i) {
		struct isl_basic_set *bset = set->p[i];
		int has_lower = 0;
		int has_upper = 0;

		for (j = 0; j < bset->n_eq; ++j) {
			has_lower = 1;
			has_upper = 1;
			if (lower) {
				isl_int_mul(a, lower[0], bset->eq[j][1]);
				isl_int_mul(b, lower[1], bset->eq[j][0]);
				if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
					isl_seq_cpy(lower, bset->eq[j], 2);
				if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
					isl_seq_neg(lower, bset->eq[j], 2);
			}
			if (upper) {
				isl_int_mul(a, upper[0], bset->eq[j][1]);
				isl_int_mul(b, upper[1], bset->eq[j][0]);
				if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
					isl_seq_neg(upper, bset->eq[j], 2);
				if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
					isl_seq_cpy(upper, bset->eq[j], 2);
			}
		}
		for (j = 0; j < bset->n_ineq; ++j) {
			if (isl_int_is_pos(bset->ineq[j][1]))
				has_lower = 1;
			if (isl_int_is_neg(bset->ineq[j][1]))
				has_upper = 1;
			if (lower && isl_int_is_pos(bset->ineq[j][1])) {
				isl_int_mul(a, lower[0], bset->ineq[j][1]);
				isl_int_mul(b, lower[1], bset->ineq[j][0]);
				if (isl_int_lt(a, b))
					isl_seq_cpy(lower, bset->ineq[j], 2);
			}
			if (upper && isl_int_is_neg(bset->ineq[j][1])) {
				isl_int_mul(a, upper[0], bset->ineq[j][1]);
				isl_int_mul(b, upper[1], bset->ineq[j][0]);
				if (isl_int_gt(a, b))
					isl_seq_cpy(upper, bset->ineq[j], 2);
			}
		}
		if (!has_lower)
			lower = NULL;
		if (!has_upper)
			upper = NULL;
	}
	isl_int_clear(a);
	isl_int_clear(b);

	hull = isl_basic_set_alloc(set->ctx, 0, 1, 0, 0, 2);
	hull = isl_basic_set_set_rational(hull);
	if (!hull)
		goto error;
	if (lower) {
		k = isl_basic_set_alloc_inequality(hull);
		isl_seq_cpy(hull->ineq[k], lower, 2);
	}
	if (upper) {
		k = isl_basic_set_alloc_inequality(hull);
		isl_seq_cpy(hull->ineq[k], upper, 2);
	}
	hull = isl_basic_set_finalize(hull);
	isl_set_free(set);
	isl_mat_free(c);
	return hull;
error:
	isl_set_free(set);
	isl_mat_free(c);
	return NULL;
}

static __isl_give isl_basic_set *convex_hull_0d(__isl_take isl_set *set)
{
	struct isl_basic_set *convex_hull;

	if (!set)
		return NULL;

	if (isl_set_is_empty(set))
		convex_hull = isl_basic_set_empty(isl_space_copy(set->dim));
	else
		convex_hull = isl_basic_set_universe(isl_space_copy(set->dim));
	isl_set_free(set);
	return convex_hull;
}

/* Compute the convex hull of a pair of basic sets without any parameters or
 * integer divisions using Fourier-Motzkin elimination.
 * The convex hull is the set of all points that can be written as
 * the sum of points from both basic sets (in homogeneous coordinates).
 * We set up the constraints in a space with dimensions for each of
 * the three sets and then project out the dimensions corresponding
 * to the two original basic sets, retaining only those corresponding
 * to the convex hull.
 */
static __isl_give isl_basic_set *convex_hull_pair_elim(
	__isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
{
	int i, j, k;
	struct isl_basic_set *bset[2];
	struct isl_basic_set *hull = NULL;
	isl_size dim;

	dim = isl_basic_set_dim(bset1, isl_dim_set);
	if (dim < 0 || !bset2)
		goto error;

	hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * dim, 0,
				1 + dim + bset1->n_eq + bset2->n_eq,
				2 + bset1->n_ineq + bset2->n_ineq);
	bset[0] = bset1;
	bset[1] = bset2;
	for (i = 0; i < 2; ++i) {
		for (j = 0; j < bset[i]->n_eq; ++j) {
			k = isl_basic_set_alloc_equality(hull);
			if (k < 0)
				goto error;
			isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
			isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
			isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
					1+dim);
		}
		for (j = 0; j < bset[i]->n_ineq; ++j) {
			k = isl_basic_set_alloc_inequality(hull);
			if (k < 0)
				goto error;
			isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
			isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
			isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
					bset[i]->ineq[j], 1+dim);
		}
		k = isl_basic_set_alloc_inequality(hull);
		if (k < 0)
			goto error;
		isl_seq_clr(hull->ineq[k], 1+2+3*dim);
		isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
	}
	for (j = 0; j < 1+dim; ++j) {
		k = isl_basic_set_alloc_equality(hull);
		if (k < 0)
			goto error;
		isl_seq_clr(hull->eq[k], 1+2+3*dim);
		isl_int_set_si(hull->eq[k][j], -1);
		isl_int_set_si(hull->eq[k][1+dim+j], 1);
		isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
	}
	hull = isl_basic_set_set_rational(hull);
	hull = isl_basic_set_remove_dims(hull, isl_dim_set, dim, 2*(1+dim));
	hull = isl_basic_set_remove_redundancies(hull);
	isl_basic_set_free(bset1);
	isl_basic_set_free(bset2);
	return hull;
error:
	isl_basic_set_free(bset1);
	isl_basic_set_free(bset2);
	isl_basic_set_free(hull);
	return NULL;
}

/* Is the set bounded for each value of the parameters?
 */
isl_bool isl_basic_set_is_bounded(__isl_keep isl_basic_set *bset)
{
	struct isl_tab *tab;
	isl_bool bounded;

	if (!bset)
		return isl_bool_error;
	if (isl_basic_set_plain_is_empty(bset))
		return isl_bool_true;

	tab = isl_tab_from_recession_cone(bset, 1);
	bounded = isl_tab_cone_is_bounded(tab);
	isl_tab_free(tab);
	return bounded;
}

/* Is the image bounded for each value of the parameters and
 * the domain variables?
 */
isl_bool isl_basic_map_image_is_bounded(__isl_keep isl_basic_map *bmap)
{
	isl_size nparam = isl_basic_map_dim(bmap, isl_dim_param);
	isl_size n_in = isl_basic_map_dim(bmap, isl_dim_in);
	isl_bool bounded;

	if (nparam < 0 || n_in < 0)
		return isl_bool_error;

	bmap = isl_basic_map_copy(bmap);
	bmap = isl_basic_map_cow(bmap);
	bmap = isl_basic_map_move_dims(bmap, isl_dim_param, nparam,
					isl_dim_in, 0, n_in);
	bounded = isl_basic_set_is_bounded(bset_from_bmap(bmap));
	isl_basic_map_free(bmap);

	return bounded;
}

/* Is the set bounded for each value of the parameters?
 */
isl_bool isl_set_is_bounded(__isl_keep isl_set *set)
{
	int i;

	if (!set)
		return isl_bool_error;

	for (i = 0; i < set->n; ++i) {
		isl_bool bounded = isl_basic_set_is_bounded(set->p[i]);
		if (!bounded || bounded < 0)
			return bounded;
	}
	return isl_bool_true;
}

/* Compute the lineality space of the convex hull of bset1 and bset2.
 *
 * We first compute the intersection of the recession cone of bset1
 * with the negative of the recession cone of bset2 and then compute
 * the linear hull of the resulting cone.
 */
static __isl_give isl_basic_set *induced_lineality_space(
	__isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
{
	int i, k;
	struct isl_basic_set *lin = NULL;
	isl_size dim;

	dim = isl_basic_set_dim(bset1, isl_dim_all);
	if (dim < 0 || !bset2)
		goto error;

	lin = isl_basic_set_alloc_space(isl_basic_set_get_space(bset1), 0,
					bset1->n_eq + bset2->n_eq,
					bset1->n_ineq + bset2->n_ineq);
	lin = isl_basic_set_set_rational(lin);
	if (!lin)
		goto error;
	for (i = 0; i < bset1->n_eq; ++i) {
		k = isl_basic_set_alloc_equality(lin);
		if (k < 0)
			goto error;
		isl_int_set_si(lin->eq[k][0], 0);
		isl_seq_cpy(lin->eq[k] + 1, bset1->eq[i] + 1, dim);
	}
	for (i = 0; i < bset1->n_ineq; ++i) {
		k = isl_basic_set_alloc_inequality(lin);
		if (k < 0)
			goto error;
		isl_int_set_si(lin->ineq[k][0], 0);
		isl_seq_cpy(lin->ineq[k] + 1, bset1->ineq[i] + 1, dim);
	}
	for (i = 0; i < bset2->n_eq; ++i) {
		k = isl_basic_set_alloc_equality(lin);
		if (k < 0)
			goto error;
		isl_int_set_si(lin->eq[k][0], 0);
		isl_seq_neg(lin->eq[k] + 1, bset2->eq[i] + 1, dim);
	}
	for (i = 0; i < bset2->n_ineq; ++i) {
		k = isl_basic_set_alloc_inequality(lin);
		if (k < 0)
			goto error;
		isl_int_set_si(lin->ineq[k][0], 0);
		isl_seq_neg(lin->ineq[k] + 1, bset2->ineq[i] + 1, dim);
	}

	isl_basic_set_free(bset1);
	isl_basic_set_free(bset2);
	return isl_basic_set_affine_hull(lin);
error:
	isl_basic_set_free(lin);
	isl_basic_set_free(bset1);
	isl_basic_set_free(bset2);
	return NULL;
}

static __isl_give isl_basic_set *uset_convex_hull(__isl_take isl_set *set);

/* Given a set and a linear space "lin" of dimension n > 0,
 * project the linear space from the set, compute the convex hull
 * and then map the set back to the original space.
 *
 * Let
 *
 *	M x = 0
 *
 * describe the linear space.  We first compute the Hermite normal
 * form H = M U of M = H Q, to obtain
 *
 *	H Q x = 0
 *
 * The last n rows of H will be zero, so the last n variables of x' = Q x
 * are the one we want to project out.  We do this by transforming each
 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
 * we transform the hull back to the original space as A' Q_1 x >= b',
 * with Q_1 all but the last n rows of Q.
 */
static __isl_give isl_basic_set *modulo_lineality(__isl_take isl_set *set,
	__isl_take isl_basic_set *lin)
{
	isl_size total = isl_basic_set_dim(lin, isl_dim_all);
	unsigned lin_dim;
	struct isl_basic_set *hull;
	struct isl_mat *M, *U, *Q;

	if (!set || total < 0)
		goto error;
	lin_dim = total - lin->n_eq;
	M = isl_mat_sub_alloc6(set->ctx, lin->eq, 0, lin->n_eq, 1, total);
	M = isl_mat_left_hermite(M, 0, &U, &Q);
	if (!M)
		goto error;
	isl_mat_free(M);
	isl_basic_set_free(lin);

	Q = isl_mat_drop_rows(Q, Q->n_row - lin_dim, lin_dim);

	U = isl_mat_lin_to_aff(U);
	Q = isl_mat_lin_to_aff(Q);

	set = isl_set_preimage(set, U);
	set = isl_set_remove_dims(set, isl_dim_set, total - lin_dim, lin_dim);
	hull = uset_convex_hull(set);
	hull = isl_basic_set_preimage(hull, Q);

	return hull;
error:
	isl_basic_set_free(lin);
	isl_set_free(set);
	return NULL;
}

/* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
 * set up an LP for solving
 *
 *	\sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
 *
 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
 * The next \alpha{ij} correspond to the equalities and come in pairs.
 * The final \alpha{ij} correspond to the inequalities.
 */
static __isl_give isl_basic_set *valid_direction_lp(
	__isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
{
	isl_space *dim;
	struct isl_basic_set *lp;
	unsigned d;
	int n;
	int i, j, k;
	isl_size total;

	total = isl_basic_set_dim(bset1, isl_dim_all);
	if (total < 0 || !bset2)
		goto error;
	d = 1 + total;
	n = 2 +
	    2 * bset1->n_eq + bset1->n_ineq + 2 * bset2->n_eq + bset2->n_ineq;
	dim = isl_space_set_alloc(bset1->ctx, 0, n);
	lp = isl_basic_set_alloc_space(dim, 0, d, n);
	if (!lp)
		goto error;
	for (i = 0; i < n; ++i) {
		k = isl_basic_set_alloc_inequality(lp);
		if (k < 0)
			goto error;
		isl_seq_clr(lp->ineq[k] + 1, n);
		isl_int_set_si(lp->ineq[k][0], -1);
		isl_int_set_si(lp->ineq[k][1 + i], 1);
	}
	for (i = 0; i < d; ++i) {
		k = isl_basic_set_alloc_equality(lp);
		if (k < 0)
			goto error;
		n = 0;
		isl_int_set_si(lp->eq[k][n], 0); n++;
		/* positivity constraint 1 >= 0 */
		isl_int_set_si(lp->eq[k][n], i == 0); n++;
		for (j = 0; j < bset1->n_eq; ++j) {
			isl_int_set(lp->eq[k][n], bset1->eq[j][i]); n++;
			isl_int_neg(lp->eq[k][n], bset1->eq[j][i]); n++;
		}
		for (j = 0; j < bset1->n_ineq; ++j) {
			isl_int_set(lp->eq[k][n], bset1->ineq[j][i]); n++;
		}
		/* positivity constraint 1 >= 0 */
		isl_int_set_si(lp->eq[k][n], -(i == 0)); n++;
		for (j = 0; j < bset2->n_eq; ++j) {
			isl_int_neg(lp->eq[k][n], bset2->eq[j][i]); n++;
			isl_int_set(lp->eq[k][n], bset2->eq[j][i]); n++;
		}
		for (j = 0; j < bset2->n_ineq; ++j) {
			isl_int_neg(lp->eq[k][n], bset2->ineq[j][i]); n++;
		}
	}
	lp = isl_basic_set_gauss(lp, NULL);
	isl_basic_set_free(bset1);
	isl_basic_set_free(bset2);
	return lp;
error:
	isl_basic_set_free(bset1);
	isl_basic_set_free(bset2);
	return NULL;
}

/* Compute a vector s in the homogeneous space such that <s, r> > 0
 * for all rays in the homogeneous space of the two cones that correspond
 * to the input polyhedra bset1 and bset2.
 *
 * We compute s as a vector that satisfies
 *
 *	s = \sum_j \alpha_{ij} h_{ij}	for i = 1,2			(*)
 *
 * with h_{ij} the normals of the facets of polyhedron i
 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
 * strictly positive numbers.  For simplicity we impose \alpha_{ij} >= 1.
 * We first set up an LP with as variables the \alpha{ij}.
 * In this formulation, for each polyhedron i,
 * the first constraint is the positivity constraint, followed by pairs
 * of variables for the equalities, followed by variables for the inequalities.
 * We then simply pick a feasible solution and compute s using (*).
 *
 * Note that we simply pick any valid direction and make no attempt
 * to pick a "good" or even the "best" valid direction.
 */
static __isl_give isl_vec *valid_direction(
	__isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
{
	struct isl_basic_set *lp;
	struct isl_tab *tab;
	struct isl_vec *sample = NULL;
	struct isl_vec *dir;
	isl_size d;
	int i;
	int n;

	if (!bset1 || !bset2)
		goto error;
	lp = valid_direction_lp(isl_basic_set_copy(bset1),
				isl_basic_set_copy(bset2));
	tab = isl_tab_from_basic_set(lp, 0);
	sample = isl_tab_get_sample_value(tab);
	isl_tab_free(tab);
	isl_basic_set_free(lp);
	if (!sample)
		goto error;
	d = isl_basic_set_dim(bset1, isl_dim_all);
	if (d < 0)
		goto error;
	dir = isl_vec_alloc(bset1->ctx, 1 + d);
	if (!dir)
		goto error;
	isl_seq_clr(dir->block.data + 1, dir->size - 1);
	n = 1;
	/* positivity constraint 1 >= 0 */
	isl_int_set(dir->block.data[0], sample->block.data[n]); n++;
	for (i = 0; i < bset1->n_eq; ++i) {
		isl_int_sub(sample->block.data[n],
			    sample->block.data[n], sample->block.data[n+1]);
		isl_seq_combine(dir->block.data,
				bset1->ctx->one, dir->block.data,
				sample->block.data[n], bset1->eq[i], 1 + d);

		n += 2;
	}
	for (i = 0; i < bset1->n_ineq; ++i)
		isl_seq_combine(dir->block.data,
				bset1->ctx->one, dir->block.data,
				sample->block.data[n++], bset1->ineq[i], 1 + d);
	isl_vec_free(sample);
	isl_seq_normalize(bset1->ctx, dir->el, dir->size);
	isl_basic_set_free(bset1);
	isl_basic_set_free(bset2);
	return dir;
error:
	isl_vec_free(sample);
	isl_basic_set_free(bset1);
	isl_basic_set_free(bset2);
	return NULL;
}

/* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
 * compute b_i' + A_i' x' >= 0, with
 *
 *	[ b_i A_i ]        [ y' ]		              [ y' ]
 *	[  1   0  ] S^{-1} [ x' ] >= 0	or	[ b_i' A_i' ] [ x' ] >= 0
 *
 * In particular, add the "positivity constraint" and then perform
 * the mapping.
 */
static __isl_give isl_basic_set *homogeneous_map(__isl_take isl_basic_set *bset,
	__isl_take isl_mat *T)
{
	int k;
	isl_size total;

	total = isl_basic_set_dim(bset, isl_dim_all);
	if (total < 0)
		goto error;
	bset = isl_basic_set_extend_constraints(bset, 0, 1);
	k = isl_basic_set_alloc_inequality(bset);
	if (k < 0)
		goto error;
	isl_seq_clr(bset->ineq[k] + 1, total);
	isl_int_set_si(bset->ineq[k][0], 1);
	bset = isl_basic_set_preimage(bset, T);
	return bset;
error:
	isl_mat_free(T);
	isl_basic_set_free(bset);
	return NULL;
}

/* Compute the convex hull of a pair of basic sets without any parameters or
 * integer divisions, where the convex hull is known to be pointed,
 * but the basic sets may be unbounded.
 *
 * We turn this problem into the computation of a convex hull of a pair
 * _bounded_ polyhedra by "changing the direction of the homogeneous
 * dimension".  This idea is due to <NAME>.
 *
 * Consider the cones in homogeneous space that correspond to the
 * input polyhedra.  The rays of these cones are also rays of the
 * polyhedra if the coordinate that corresponds to the homogeneous
 * dimension is zero.  That is, if the inner product of the rays
 * with the homogeneous direction is zero.
 * The cones in the homogeneous space can also be considered to
 * correspond to other pairs of polyhedra by chosing a different
 * homogeneous direction.  To ensure that both of these polyhedra
 * are bounded, we need to make sure that all rays of the cones
 * correspond to vertices and not to rays.
 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
 * The vector s is computed in valid_direction.
 *
 * Note that we need to consider _all_ rays of the cones and not just
 * the rays that correspond to rays in the polyhedra.  If we were to
 * only consider those rays and turn them into vertices, then we
 * may inadvertently turn some vertices into rays.
 *
 * The standard homogeneous direction is the unit vector in the 0th coordinate.
 * We therefore transform the two polyhedra such that the selected
 * direction is mapped onto this standard direction and then proceed
 * with the normal computation.
 * Let S be a non-singular square matrix with s as its first row,
 * then we want to map the polyhedra to the space
 *
 *	[ y' ]     [ y ]		[ y ]          [ y' ]
 *	[ x' ] = S [ x ]	i.e.,	[ x ] = S^{-1} [ x' ]
 *
 * We take S to be the unimodular completion of s to limit the growth
 * of the coefficients in the following computations.
 *
 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
 * We first move to the homogeneous dimension
 *
 *	b_i y + A_i x >= 0		[ b_i A_i ] [ y ]    [ 0 ]
 *	    y         >= 0	or	[  1   0  ] [ x ] >= [ 0 ]
 *
 * Then we change directoin
 *
 *	[ b_i A_i ]        [ y' ]		              [ y' ]
 *	[  1   0  ] S^{-1} [ x' ] >= 0	or	[ b_i' A_i' ] [ x' ] >= 0
 *
 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
 * resulting in b' + A' x' >= 0, which we then convert back
 *
 *	            [ y ]		        [ y ]
 *	[ b' A' ] S [ x ] >= 0	or	[ b A ] [ x ] >= 0
 *
 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
 */
static __isl_give isl_basic_set *convex_hull_pair_pointed(
	__isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
{
	struct isl_ctx *ctx = NULL;
	struct isl_vec *dir = NULL;
	struct isl_mat *T = NULL;
	struct isl_mat *T2 = NULL;
	struct isl_basic_set *hull;
	struct isl_set *set;

	if (!bset1 || !bset2)
		goto error;
	ctx = isl_basic_set_get_ctx(bset1);
	dir = valid_direction(isl_basic_set_copy(bset1),
				isl_basic_set_copy(bset2));
	if (!dir)
		goto error;
	T = isl_mat_alloc(ctx, dir->size, dir->size);
	if (!T)
		goto error;
	isl_seq_cpy(T->row[0], dir->block.data, dir->size);
	T = isl_mat_unimodular_complete(T, 1);
	T2 = isl_mat_right_inverse(isl_mat_copy(T));

	bset1 = homogeneous_map(bset1, isl_mat_copy(T2));
	bset2 = homogeneous_map(bset2, T2);
	set = isl_set_alloc_space(isl_basic_set_get_space(bset1), 2, 0);
	set = isl_set_add_basic_set(set, bset1);
	set = isl_set_add_basic_set(set, bset2);
	hull = uset_convex_hull(set);
	hull = isl_basic_set_preimage(hull, T);
	 
	isl_vec_free(dir);

	return hull;
error:
	isl_vec_free(dir);
	isl_basic_set_free(bset1);
	isl_basic_set_free(bset2);
	return NULL;
}

static __isl_give isl_basic_set *uset_convex_hull_wrap(__isl_take isl_set *set);
static __isl_give isl_basic_set *modulo_affine_hull(
	__isl_take isl_set *set, __isl_take isl_basic_set *affine_hull);

/* Compute the convex hull of a pair of basic sets without any parameters or
 * integer divisions.
 *
 * This function is called from uset_convex_hull_unbounded, which
 * means that the complete convex hull is unbounded.  Some pairs
 * of basic sets may still be bounded, though.
 * They may even lie inside a lower dimensional space, in which
 * case they need to be handled inside their affine hull since
 * the main algorithm assumes that the result is full-dimensional.
 *
 * If the convex hull of the two basic sets would have a non-trivial
 * lineality space, we first project out this lineality space.
 */
static __isl_give isl_basic_set *convex_hull_pair(
	__isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
{
	isl_basic_set *lin, *aff;
	isl_bool bounded1, bounded2;
	isl_size total;

	if (bset1->ctx->opt->convex == ISL_CONVEX_HULL_FM)
		return convex_hull_pair_elim(bset1, bset2);

	aff = isl_set_affine_hull(isl_basic_set_union(isl_basic_set_copy(bset1),
						    isl_basic_set_copy(bset2)));
	if (!aff)
		goto error;
	if (aff->n_eq != 0) 
		return modulo_affine_hull(isl_basic_set_union(bset1, bset2), aff);
	isl_basic_set_free(aff);

	bounded1 = isl_basic_set_is_bounded(bset1);
	bounded2 = isl_basic_set_is_bounded(bset2);

	if (bounded1 < 0 || bounded2 < 0)
		goto error;

	if (bounded1 && bounded2)
		return uset_convex_hull_wrap(isl_basic_set_union(bset1, bset2));

	if (bounded1 || bounded2)
		return convex_hull_pair_pointed(bset1, bset2);

	lin = induced_lineality_space(isl_basic_set_copy(bset1),
				      isl_basic_set_copy(bset2));
	if (!lin)
		goto error;
	if (isl_basic_set_plain_is_universe(lin)) {
		isl_basic_set_free(bset1);
		isl_basic_set_free(bset2);
		return lin;
	}
	total = isl_basic_set_dim(lin, isl_dim_all);
	if (lin->n_eq < total) {
		struct isl_set *set;
		set = isl_set_alloc_space(isl_basic_set_get_space(bset1), 2, 0);
		set = isl_set_add_basic_set(set, bset1);
		set = isl_set_add_basic_set(set, bset2);
		return modulo_lineality(set, lin);
	}
	isl_basic_set_free(lin);
	if (total < 0)
		goto error;

	return convex_hull_pair_pointed(bset1, bset2);
error:
	isl_basic_set_free(bset1);
	isl_basic_set_free(bset2);
	return NULL;
}

/* Compute the lineality space of a basic set.
 * We basically just drop the constants and turn every inequality
 * into an equality.
 * Any explicit representations of local variables are removed
 * because they may no longer be valid representations
 * in the lineality space.
 */
__isl_give isl_basic_set *isl_basic_set_lineality_space(
	__isl_take isl_basic_set *bset)
{
	int i, k;
	struct isl_basic_set *lin = NULL;
	isl_size n_div, dim;

	n_div = isl_basic_set_dim(bset, isl_dim_div);
	dim = isl_basic_set_dim(bset, isl_dim_all);
	if (n_div < 0 || dim < 0)
		return isl_basic_set_free(bset);

	lin = isl_basic_set_alloc_space(isl_basic_set_get_space(bset),
					n_div, dim, 0);
	for (i = 0; i < n_div; ++i)
		if (isl_basic_set_alloc_div(lin) < 0)
			goto error;
	if (!lin)
		goto error;
	for (i = 0; i < bset->n_eq; ++i) {
		k = isl_basic_set_alloc_equality(lin);
		if (k < 0)
			goto error;
		isl_int_set_si(lin->eq[k][0], 0);
		isl_seq_cpy(lin->eq[k] + 1, bset->eq[i] + 1, dim);
	}
	lin = isl_basic_set_gauss(lin, NULL);
	if (!lin)
		goto error;
	for (i = 0; i < bset->n_ineq && lin->n_eq < dim; ++i) {
		k = isl_basic_set_alloc_equality(lin);
		if (k < 0)
			goto error;
		isl_int_set_si(lin->eq[k][0], 0);
		isl_seq_cpy(lin->eq[k] + 1, bset->ineq[i] + 1, dim);
		lin = isl_basic_set_gauss(lin, NULL);
		if (!lin)
			goto error;
	}
	isl_basic_set_free(bset);
	return lin;
error:
	isl_basic_set_free(lin);
	isl_basic_set_free(bset);
	return NULL;
}

/* Compute the (linear) hull of the lineality spaces of the basic sets in the
 * set "set".
 */
__isl_give isl_basic_set *isl_set_combined_lineality_space(
	__isl_take isl_set *set)
{
	int i;
	struct isl_set *lin = NULL;

	if (!set)
		return NULL;
	if (set->n == 0) {
		isl_space *space = isl_set_get_space(set);
		isl_set_free(set);
		return isl_basic_set_empty(space);
	}

	lin = isl_set_alloc_space(isl_set_get_space(set), set->n, 0);
	for (i = 0; i < set->n; ++i)
		lin = isl_set_add_basic_set(lin,
		    isl_basic_set_lineality_space(isl_basic_set_copy(set->p[i])));
	isl_set_free(set);
	return isl_set_affine_hull(lin);
}

/* Compute the convex hull of a set without any parameters or
 * integer divisions.
 * In each step, we combined two basic sets until only one
 * basic set is left.
 * The input basic sets are assumed not to have a non-trivial
 * lineality space.  If any of the intermediate results has
 * a non-trivial lineality space, it is projected out.
 */
static __isl_give isl_basic_set *uset_convex_hull_unbounded(
	__isl_take isl_set *set)
{
	isl_basic_set_list *list;

	list = isl_set_get_basic_set_list(set);
	isl_set_free(set);

	while (list) {
		isl_size n, total;
		struct isl_basic_set *t;
		isl_basic_set *bset1, *bset2;

		n = isl_basic_set_list_n_basic_set(list);
		if (n < 0)
			goto error;
		if (n < 2)
			isl_die(isl_basic_set_list_get_ctx(list),
				isl_error_internal,
				"expecting at least two elements", goto error);
		bset1 = isl_basic_set_list_get_basic_set(list, n - 1);
		bset2 = isl_basic_set_list_get_basic_set(list, n - 2);
		bset1 = convex_hull_pair(bset1, bset2);
		if (n == 2) {
			isl_basic_set_list_free(list);
			return bset1;
		}
		bset1 = isl_basic_set_underlying_set(bset1);
		list = isl_basic_set_list_drop(list, n - 2, 2);
		list = isl_basic_set_list_add(list, bset1);

		t = isl_basic_set_list_get_basic_set(list, n - 2);
		t = isl_basic_set_lineality_space(t);
		if (!t)
			goto error;
		if (isl_basic_set_plain_is_universe(t)) {
			isl_basic_set_list_free(list);
			return t;
		}
		total = isl_basic_set_dim(t, isl_dim_all);
		if (t->n_eq < total) {
			set = isl_basic_set_list_union(list);
			return modulo_lineality(set, t);
		}
		isl_basic_set_free(t);
		if (total < 0)
			goto error;
	}

	return NULL;
error:
	isl_basic_set_list_free(list);
	return NULL;
}

/* Compute an initial hull for wrapping containing a single initial
 * facet.
 * This function assumes that the given set is bounded.
 */
static __isl_give isl_basic_set *initial_hull(__isl_take isl_basic_set *hull,
	__isl_keep isl_set *set)
{
	struct isl_mat *bounds = NULL;
	isl_size dim;
	int k;

	if (!hull)
		goto error;
	bounds = initial_facet_constraint(set);
	if (!bounds)
		goto error;
	k = isl_basic_set_alloc_inequality(hull);
	if (k < 0)
		goto error;
	dim = isl_set_dim(set, isl_dim_set);
	if (dim < 0)
		goto error;
	isl_assert(set->ctx, 1 + dim == bounds->n_col, goto error);
	isl_seq_cpy(hull->ineq[k], bounds->row[0], bounds->n_col);
	isl_mat_free(bounds);

	return hull;
error:
	isl_basic_set_free(hull);
	isl_mat_free(bounds);
	return NULL;
}

struct max_constraint {
	struct isl_mat *c;
	int	 	count;
	int		ineq;
};

static int max_constraint_equal(const void *entry, const void *val)
{
	struct max_constraint *a = (struct max_constraint *)entry;
	isl_int *b = (isl_int *)val;

	return isl_seq_eq(a->c->row[0] + 1, b, a->c->n_col - 1);
}

static isl_stat update_constraint(struct isl_ctx *ctx,
	struct isl_hash_table *table,
	isl_int *con, unsigned len, int n, int ineq)
{
	struct isl_hash_table_entry *entry;
	struct max_constraint *c;
	uint32_t c_hash;

	c_hash = isl_seq_get_hash(con + 1, len);
	entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
			con + 1, 0);
	if (!entry)
		return isl_stat_ok;
	c = entry->data;
	if (c->count < n) {
		isl_hash_table_remove(ctx, table, entry);
		return isl_stat_ok;
	}
	c->count++;
	if (isl_int_gt(c->c->row[0][0], con[0]))
		return isl_stat_ok;
	if (isl_int_eq(c->c->row[0][0], con[0])) {
		if (ineq)
			c->ineq = ineq;
		return isl_stat_ok;
	}
	c->c = isl_mat_cow(c->c);
	isl_int_set(c->c->row[0][0], con[0]);
	c->ineq = ineq;

	return isl_stat_ok;
}

/* Check whether the constraint hash table "table" contains the constraint
 * "con".
 */
static int has_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
	isl_int *con, unsigned len, int n)
{
	struct isl_hash_table_entry *entry;
	struct max_constraint *c;
	uint32_t c_hash;

	c_hash = isl_seq_get_hash(con + 1, len);
	entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
			con + 1, 0);
	if (!entry)
		return 0;
	c = entry->data;
	if (c->count < n)
		return 0;
	return isl_int_eq(c->c->row[0][0], con[0]);
}

/* Are the constraints of "bset" known to be facets?
 * If there are any equality constraints, then they are not.
 * If there may be redundant constraints, then those
 * redundant constraints are not facets.
 */
static isl_bool has_facets(__isl_keep isl_basic_set *bset)
{
	int n_eq;

	n_eq = isl_basic_set_n_equality(bset);
	if (n_eq < 0)
		return isl_bool_error;
	if (n_eq != 0)
		return isl_bool_false;
	return ISL_F_ISSET(bset, ISL_BASIC_SET_NO_REDUNDANT);
}

/* Check for inequality constraints of a basic set without equalities
 * or redundant constraints
 * such that the same or more stringent copies of the constraint appear
 * in all of the basic sets.  Such constraints are necessarily facet
 * constraints of the convex hull.
 *
 * If the resulting basic set is by chance identical to one of
 * the basic sets in "set", then we know that this basic set contains
 * all other basic sets and is therefore the convex hull of set.
 * In this case we set *is_hull to 1.
 */
static __isl_give isl_basic_set *common_constraints(
	__isl_take isl_basic_set *hull, __isl_keep isl_set *set, int *is_hull)
{
	int i, j, s, n;
	int min_constraints;
	int best;
	struct max_constraint *constraints = NULL;
	struct isl_hash_table *table = NULL;
	isl_size total;

	*is_hull = 0;

	for (i = 0; i < set->n; ++i) {
		isl_bool facets = has_facets(set->p[i]);
		if (facets < 0)
			return isl_basic_set_free(hull);
		if (facets)
			break;
	}
	if (i >= set->n)
		return hull;
	min_constraints = set->p[i]->n_ineq;
	best = i;
	for (i = best + 1; i < set->n; ++i) {
		isl_bool facets = has_facets(set->p[i]);
		if (facets < 0)
			return isl_basic_set_free(hull);
		if (!facets)
			continue;
		if (set->p[i]->n_ineq >= min_constraints)
			continue;
		min_constraints = set->p[i]->n_ineq;
		best = i;
	}
	constraints = isl_calloc_array(hull->ctx, struct max_constraint,
					min_constraints);
	if (!constraints)
		return hull;
	table = isl_alloc_type(hull->ctx, struct isl_hash_table);
	if (isl_hash_table_init(hull->ctx, table, min_constraints))
		goto error;

	total = isl_set_dim(set, isl_dim_all);
	if (total < 0)
		goto error;
	for (i = 0; i < set->p[best]->n_ineq; ++i) {
		constraints[i].c = isl_mat_sub_alloc6(hull->ctx,
			set->p[best]->ineq + i, 0, 1, 0, 1 + total);
		if (!constraints[i].c)
			goto error;
		constraints[i].ineq = 1;
	}
	for (i = 0; i < min_constraints; ++i) {
		struct isl_hash_table_entry *entry;
		uint32_t c_hash;
		c_hash = isl_seq_get_hash(constraints[i].c->row[0] + 1, total);
		entry = isl_hash_table_find(hull->ctx, table, c_hash,
			max_constraint_equal, constraints[i].c->row[0] + 1, 1);
		if (!entry)
			goto error;
		isl_assert(hull->ctx, !entry->data, goto error);
		entry->data = &constraints[i];
	}

	n = 0;
	for (s = 0; s < set->n; ++s) {
		if (s == best)
			continue;

		for (i = 0; i < set->p[s]->n_eq; ++i) {
			isl_int *eq = set->p[s]->eq[i];
			for (j = 0; j < 2; ++j) {
				isl_seq_neg(eq, eq, 1 + total);
				if (update_constraint(hull->ctx, table,
						    eq, total, n, 0) < 0)
					goto error;
			}
		}
		for (i = 0; i < set->p[s]->n_ineq; ++i) {
			isl_int *ineq = set->p[s]->ineq[i];
			if (update_constraint(hull->ctx, table, ineq, total, n,
					set->p[s]->n_eq == 0) < 0)
				goto error;
		}
		++n;
	}

	for (i = 0; i < min_constraints; ++i) {
		if (constraints[i].count < n)
			continue;
		if (!constraints[i].ineq)
			continue;
		j = isl_basic_set_alloc_inequality(hull);
		if (j < 0)
			goto error;
		isl_seq_cpy(hull->ineq[j], constraints[i].c->row[0], 1 + total);
	}

	for (s = 0; s < set->n; ++s) {
		if (set->p[s]->n_eq)
			continue;
		if (set->p[s]->n_ineq != hull->n_ineq)
			continue;
		for (i = 0; i < set->p[s]->n_ineq; ++i) {
			isl_int *ineq = set->p[s]->ineq[i];
			if (!has_constraint(hull->ctx, table, ineq, total, n))
				break;
		}
		if (i == set->p[s]->n_ineq)
			*is_hull = 1;
	}

	isl_hash_table_clear(table);
	for (i = 0; i < min_constraints; ++i)
		isl_mat_free(constraints[i].c);
	free(constraints);
	free(table);
	return hull;
error:
	isl_hash_table_clear(table);
	free(table);
	if (constraints)
		for (i = 0; i < min_constraints; ++i)
			isl_mat_free(constraints[i].c);
	free(constraints);
	return hull;
}

/* Create a template for the convex hull of "set" and fill it up
 * obvious facet constraints, if any.  If the result happens to
 * be the convex hull of "set" then *is_hull is set to 1.<commit_msg>isl_convex_hull.c: has_constraint: handle error conditions<commit_after>/*
 * Copyright 2008-2009 <NAME>
 * Copyright 2014      INRIA Rocquencourt
 *
 * Use of this software is governed by the MIT license
 *
 * Written by <NAME>, K.U.Leuven, Departement
 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
 * B.P. 105 - 78153 Le Chesnay, France
 */

#include <isl_ctx_private.h>
#include <isl_map_private.h>
#include <isl_lp_private.h>
#include <isl/map.h>
#include <isl_mat_private.h>
#include <isl_vec_private.h>
#include <isl/set.h>
#include <isl_seq.h>
#include <isl_options_private.h>
#include "isl_equalities.h"
#include "isl_tab.h"
#include <isl_sort.h>

#include <bset_to_bmap.c>
#include <bset_from_bmap.c>
#include <set_to_map.c>

static __isl_give isl_basic_set *uset_convex_hull_wrap_bounded(
	__isl_take isl_set *set);

/* Remove redundant
 * constraints.  If the minimal value along the normal of a constraint
 * is the same if the constraint is removed, then the constraint is redundant.
 *
 * Since some constraints may be mutually redundant, sort the constraints
 * first such that constraints that involve existentially quantified
 * variables are considered for removal before those that do not.
 * The sorting is also needed for the use in map_simple_hull.
 *
 * Note that isl_tab_detect_implicit_equalities may also end up
 * marking some constraints as redundant.  Make sure the constraints
 * are preserved and undo those marking such that isl_tab_detect_redundant
 * can consider the constraints in the sorted order.
 *
 * Alternatively, we could have intersected the basic map with the
 * corresponding equality and then checked if the dimension was that
 * of a facet.
 */
__isl_give isl_basic_map *isl_basic_map_remove_redundancies(
	__isl_take isl_basic_map *bmap)
{
	struct isl_tab *tab;

	if (!bmap)
		return NULL;

	bmap = isl_basic_map_gauss(bmap, NULL);
	if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
		return bmap;
	if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_REDUNDANT))
		return bmap;
	if (bmap->n_ineq <= 1)
		return bmap;

	bmap = isl_basic_map_sort_constraints(bmap);
	tab = isl_tab_from_basic_map(bmap, 0);
	if (!tab)
		goto error;
	tab->preserve = 1;
	if (isl_tab_detect_implicit_equalities(tab) < 0)
		goto error;
	if (isl_tab_restore_redundant(tab) < 0)
		goto error;
	tab->preserve = 0;
	if (isl_tab_detect_redundant(tab) < 0)
		goto error;
	bmap = isl_basic_map_update_from_tab(bmap, tab);
	isl_tab_free(tab);
	if (!bmap)
		return NULL;
	ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
	ISL_F_SET(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
	return bmap;
error:
	isl_tab_free(tab);
	isl_basic_map_free(bmap);
	return NULL;
}

__isl_give isl_basic_set *isl_basic_set_remove_redundancies(
	__isl_take isl_basic_set *bset)
{
	return bset_from_bmap(
		isl_basic_map_remove_redundancies(bset_to_bmap(bset)));
}

/* Remove redundant constraints in each of the basic maps.
 */
__isl_give isl_map *isl_map_remove_redundancies(__isl_take isl_map *map)
{
	return isl_map_inline_foreach_basic_map(map,
					    &isl_basic_map_remove_redundancies);
}

__isl_give isl_set *isl_set_remove_redundancies(__isl_take isl_set *set)
{
	return isl_map_remove_redundancies(set);
}

/* Check if the set set is bound in the direction of the affine
 * constraint c and if so, set the constant term such that the
 * resulting constraint is a bounding constraint for the set.
 */
static isl_bool uset_is_bound(__isl_keep isl_set *set, isl_int *c, unsigned len)
{
	int first;
	int j;
	isl_int opt;
	isl_int opt_denom;

	isl_int_init(opt);
	isl_int_init(opt_denom);
	first = 1;
	for (j = 0; j < set->n; ++j) {
		enum isl_lp_result res;

		if (ISL_F_ISSET(set->p[j], ISL_BASIC_SET_EMPTY))
			continue;

		res = isl_basic_set_solve_lp(set->p[j],
				0, c, set->ctx->one, &opt, &opt_denom, NULL);
		if (res == isl_lp_unbounded)
			break;
		if (res == isl_lp_error)
			goto error;
		if (res == isl_lp_empty) {
			set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
			if (!set->p[j])
				goto error;
			continue;
		}
		if (first || isl_int_is_neg(opt)) {
			if (!isl_int_is_one(opt_denom))
				isl_seq_scale(c, c, opt_denom, len);
			isl_int_sub(c[0], c[0], opt);
		}
		first = 0;
	}
	isl_int_clear(opt);
	isl_int_clear(opt_denom);
	return isl_bool_ok(j >= set->n);
error:
	isl_int_clear(opt);
	isl_int_clear(opt_denom);
	return isl_bool_error;
}

static __isl_give isl_set *isl_set_add_basic_set_equality(
	__isl_take isl_set *set, isl_int *c)
{
	int i;

	set = isl_set_cow(set);
	if (!set)
		return NULL;
	for (i = 0; i < set->n; ++i) {
		set->p[i] = isl_basic_set_add_eq(set->p[i], c);
		if (!set->p[i])
			goto error;
	}
	return set;
error:
	isl_set_free(set);
	return NULL;
}

/* Given a union of basic sets, construct the constraints for wrapping
 * a facet around one of its ridges.
 * In particular, if each of n the d-dimensional basic sets i in "set"
 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
 * and is defined by the constraints
 *				    [ 1 ]
 *				A_i [ x ]  >= 0
 *
 * then the resulting set is of dimension n*(1+d) and has as constraints
 *
 *				    [ a_i ]
 *				A_i [ x_i ] >= 0
 *
 *				      a_i   >= 0
 *
 *			\sum_i x_{i,1} = 1
 */
static __isl_give isl_basic_set *wrap_constraints(__isl_keep isl_set *set)
{
	struct isl_basic_set *lp;
	unsigned n_eq;
	unsigned n_ineq;
	int i, j, k;
	isl_size dim, lp_dim;

	dim = isl_set_dim(set, isl_dim_set);
	if (dim < 0)
		return NULL;

	dim += 1;
	n_eq = 1;
	n_ineq = set->n;
	for (i = 0; i < set->n; ++i) {
		n_eq += set->p[i]->n_eq;
		n_ineq += set->p[i]->n_ineq;
	}
	lp = isl_basic_set_alloc(set->ctx, 0, dim * set->n, 0, n_eq, n_ineq);
	lp = isl_basic_set_set_rational(lp);
	if (!lp)
		return NULL;
	lp_dim = isl_basic_set_dim(lp, isl_dim_set);
	if (lp_dim < 0)
		return isl_basic_set_free(lp);
	k = isl_basic_set_alloc_equality(lp);
	isl_int_set_si(lp->eq[k][0], -1);
	for (i = 0; i < set->n; ++i) {
		isl_int_set_si(lp->eq[k][1+dim*i], 0);
		isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
		isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
	}
	for (i = 0; i < set->n; ++i) {
		k = isl_basic_set_alloc_inequality(lp);
		isl_seq_clr(lp->ineq[k], 1+lp_dim);
		isl_int_set_si(lp->ineq[k][1+dim*i], 1);

		for (j = 0; j < set->p[i]->n_eq; ++j) {
			k = isl_basic_set_alloc_equality(lp);
			isl_seq_clr(lp->eq[k], 1+dim*i);
			isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
			isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
		}

		for (j = 0; j < set->p[i]->n_ineq; ++j) {
			k = isl_basic_set_alloc_inequality(lp);
			isl_seq_clr(lp->ineq[k], 1+dim*i);
			isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
			isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
		}
	}
	return lp;
}

/* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
 * of that facet, compute the other facet of the convex hull that contains
 * the ridge.
 *
 * We first transform the set such that the facet constraint becomes
 *
 *			x_1 >= 0
 *
 * I.e., the facet lies in
 *
 *			x_1 = 0
 *
 * and on that facet, the constraint that defines the ridge is
 *
 *			x_2 >= 0
 *
 * (This transformation is not strictly needed, all that is needed is
 * that the ridge contains the origin.)
 *
 * Since the ridge contains the origin, the cone of the convex hull
 * will be of the form
 *
 *			x_1 >= 0
 *			x_2 >= a x_1
 *
 * with this second constraint defining the new facet.
 * The constant a is obtained by settting x_1 in the cone of the
 * convex hull to 1 and minimizing x_2.
 * Now, each element in the cone of the convex hull is the sum
 * of elements in the cones of the basic sets.
 * If a_i is the dilation factor of basic set i, then the problem
 * we need to solve is
 *
 *			min \sum_i x_{i,2}
 *			st
 *				\sum_i x_{i,1} = 1
 *				    a_i   >= 0
 *				  [ a_i ]
 *				A [ x_i ] >= 0
 *
 * with
 *				    [  1  ]
 *				A_i [ x_i ] >= 0
 *
 * the constraints of each (transformed) basic set.
 * If a = n/d, then the constraint defining the new facet (in the transformed
 * space) is
 *
 *			-n x_1 + d x_2 >= 0
 *
 * In the original space, we need to take the same combination of the
 * corresponding constraints "facet" and "ridge".
 *
 * If a = -infty = "-1/0", then we just return the original facet constraint.
 * This means that the facet is unbounded, but has a bounded intersection
 * with the union of sets.
 */
isl_int *isl_set_wrap_facet(__isl_keep isl_set *set,
	isl_int *facet, isl_int *ridge)
{
	int i;
	isl_ctx *ctx;
	struct isl_mat *T = NULL;
	struct isl_basic_set *lp = NULL;
	struct isl_vec *obj;
	enum isl_lp_result res;
	isl_int num, den;
	isl_size dim;

	dim = isl_set_dim(set, isl_dim_set);
	if (dim < 0)
		return NULL;
	ctx = set->ctx;
	set = isl_set_copy(set);
	set = isl_set_set_rational(set);

	dim += 1;
	T = isl_mat_alloc(ctx, 3, dim);
	if (!T)
		goto error;
	isl_int_set_si(T->row[0][0], 1);
	isl_seq_clr(T->row[0]+1, dim - 1);
	isl_seq_cpy(T->row[1], facet, dim);
	isl_seq_cpy(T->row[2], ridge, dim);
	T = isl_mat_right_inverse(T);
	set = isl_set_preimage(set, T);
	T = NULL;
	if (!set)
		goto error;
	lp = wrap_constraints(set);
	obj = isl_vec_alloc(ctx, 1 + dim*set->n);
	if (!obj)
		goto error;
	isl_int_set_si(obj->block.data[0], 0);
	for (i = 0; i < set->n; ++i) {
		isl_seq_clr(obj->block.data + 1 + dim*i, 2);
		isl_int_set_si(obj->block.data[1 + dim*i+2], 1);
		isl_seq_clr(obj->block.data + 1 + dim*i+3, dim-3);
	}
	isl_int_init(num);
	isl_int_init(den);
	res = isl_basic_set_solve_lp(lp, 0,
			    obj->block.data, ctx->one, &num, &den, NULL);
	if (res == isl_lp_ok) {
		isl_int_neg(num, num);
		isl_seq_combine(facet, num, facet, den, ridge, dim);
		isl_seq_normalize(ctx, facet, dim);
	}
	isl_int_clear(num);
	isl_int_clear(den);
	isl_vec_free(obj);
	isl_basic_set_free(lp);
	isl_set_free(set);
	if (res == isl_lp_error)
		return NULL;
	isl_assert(ctx, res == isl_lp_ok || res == isl_lp_unbounded, 
		   return NULL);
	return facet;
error:
	isl_basic_set_free(lp);
	isl_mat_free(T);
	isl_set_free(set);
	return NULL;
}

/* Compute the constraint of a facet of "set".
 *
 * We first compute the intersection with a bounding constraint
 * that is orthogonal to one of the coordinate axes.
 * If the affine hull of this intersection has only one equality,
 * we have found a facet.
 * Otherwise, we wrap the current bounding constraint around
 * one of the equalities of the face (one that is not equal to
 * the current bounding constraint).
 * This process continues until we have found a facet.
 * The dimension of the intersection increases by at least
 * one on each iteration, so termination is guaranteed.
 */
static __isl_give isl_mat *initial_facet_constraint(__isl_keep isl_set *set)
{
	struct isl_set *slice = NULL;
	struct isl_basic_set *face = NULL;
	int i;
	isl_size dim = isl_set_dim(set, isl_dim_set);
	isl_bool is_bound;
	isl_mat *bounds = NULL;

	if (dim < 0)
		return NULL;
	isl_assert(set->ctx, set->n > 0, goto error);
	bounds = isl_mat_alloc(set->ctx, 1, 1 + dim);
	if (!bounds)
		return NULL;

	isl_seq_clr(bounds->row[0], dim);
	isl_int_set_si(bounds->row[0][1 + dim - 1], 1);
	is_bound = uset_is_bound(set, bounds->row[0], 1 + dim);
	if (is_bound < 0)
		goto error;
	isl_assert(set->ctx, is_bound, goto error);
	isl_seq_normalize(set->ctx, bounds->row[0], 1 + dim);
	bounds->n_row = 1;

	for (;;) {
		slice = isl_set_copy(set);
		slice = isl_set_add_basic_set_equality(slice, bounds->row[0]);
		face = isl_set_affine_hull(slice);
		if (!face)
			goto error;
		if (face->n_eq == 1) {
			isl_basic_set_free(face);
			break;
		}
		for (i = 0; i < face->n_eq; ++i)
			if (!isl_seq_eq(bounds->row[0], face->eq[i], 1 + dim) &&
			    !isl_seq_is_neg(bounds->row[0],
						face->eq[i], 1 + dim))
				break;
		isl_assert(set->ctx, i < face->n_eq, goto error);
		if (!isl_set_wrap_facet(set, bounds->row[0], face->eq[i]))
			goto error;
		isl_seq_normalize(set->ctx, bounds->row[0], bounds->n_col);
		isl_basic_set_free(face);
	}

	return bounds;
error:
	isl_basic_set_free(face);
	isl_mat_free(bounds);
	return NULL;
}

/* Given the bounding constraint "c" of a facet of the convex hull of "set",
 * compute a hyperplane description of the facet, i.e., compute the facets
 * of the facet.
 *
 * We compute an affine transformation that transforms the constraint
 *
 *			  [ 1 ]
 *			c [ x ] = 0
 *
 * to the constraint
 *
 *			   z_1  = 0
 *
 * by computing the right inverse U of a matrix that starts with the rows
 *
 *			[ 1 0 ]
 *			[  c  ]
 *
 * Then
 *			[ 1 ]     [ 1 ]
 *			[ x ] = U [ z ]
 * and
 *			[ 1 ]     [ 1 ]
 *			[ z ] = Q [ x ]
 *
 * with Q = U^{-1}
 * Since z_1 is zero, we can drop this variable as well as the corresponding
 * column of U to obtain
 *
 *			[ 1 ]      [ 1  ]
 *			[ x ] = U' [ z' ]
 * and
 *			[ 1  ]      [ 1 ]
 *			[ z' ] = Q' [ x ]
 *
 * with Q' equal to Q, but without the corresponding row.
 * After computing the facets of the facet in the z' space,
 * we convert them back to the x space through Q.
 */
static __isl_give isl_basic_set *compute_facet(__isl_keep isl_set *set,
	isl_int *c)
{
	struct isl_mat *m, *U, *Q;
	struct isl_basic_set *facet = NULL;
	struct isl_ctx *ctx;
	isl_size dim;

	dim = isl_set_dim(set, isl_dim_set);
	if (dim < 0)
		return NULL;
	ctx = set->ctx;
	set = isl_set_copy(set);
	m = isl_mat_alloc(set->ctx, 2, 1 + dim);
	if (!m)
		goto error;
	isl_int_set_si(m->row[0][0], 1);
	isl_seq_clr(m->row[0]+1, dim);
	isl_seq_cpy(m->row[1], c, 1+dim);
	U = isl_mat_right_inverse(m);
	Q = isl_mat_right_inverse(isl_mat_copy(U));
	U = isl_mat_drop_cols(U, 1, 1);
	Q = isl_mat_drop_rows(Q, 1, 1);
	set = isl_set_preimage(set, U);
	facet = uset_convex_hull_wrap_bounded(set);
	facet = isl_basic_set_preimage(facet, Q);
	if (facet && facet->n_eq != 0)
		isl_die(ctx, isl_error_internal, "unexpected equality",
			return isl_basic_set_free(facet));
	return facet;
error:
	isl_basic_set_free(facet);
	isl_set_free(set);
	return NULL;
}

/* Given an initial facet constraint, compute the remaining facets.
 * We do this by running through all facets found so far and computing
 * the adjacent facets through wrapping, adding those facets that we
 * hadn't already found before.
 *
 * For each facet we have found so far, we first compute its facets
 * in the resulting convex hull.  That is, we compute the ridges
 * of the resulting convex hull contained in the facet.
 * We also compute the corresponding facet in the current approximation
 * of the convex hull.  There is no need to wrap around the ridges
 * in this facet since that would result in a facet that is already
 * present in the current approximation.
 *
 * This function can still be significantly optimized by checking which of
 * the facets of the basic sets are also facets of the convex hull and
 * using all the facets so far to help in constructing the facets of the
 * facets
 * and/or
 * using the technique in section "3.1 Ridge Generation" of
 * "Extended Convex Hull" by Fukuda et al.
 */
static __isl_give isl_basic_set *extend(__isl_take isl_basic_set *hull,
	__isl_keep isl_set *set)
{
	int i, j, f;
	int k;
	struct isl_basic_set *facet = NULL;
	struct isl_basic_set *hull_facet = NULL;
	isl_size dim;

	dim = isl_set_dim(set, isl_dim_set);
	if (dim < 0 || !hull)
		return isl_basic_set_free(hull);

	isl_assert(set->ctx, set->n > 0, goto error);

	for (i = 0; i < hull->n_ineq; ++i) {
		facet = compute_facet(set, hull->ineq[i]);
		facet = isl_basic_set_add_eq(facet, hull->ineq[i]);
		facet = isl_basic_set_gauss(facet, NULL);
		facet = isl_basic_set_normalize_constraints(facet);
		hull_facet = isl_basic_set_copy(hull);
		hull_facet = isl_basic_set_add_eq(hull_facet, hull->ineq[i]);
		hull_facet = isl_basic_set_gauss(hull_facet, NULL);
		hull_facet = isl_basic_set_normalize_constraints(hull_facet);
		if (!facet || !hull_facet)
			goto error;
		hull = isl_basic_set_cow(hull);
		hull = isl_basic_set_extend_space(hull,
			isl_space_copy(hull->dim), 0, 0, facet->n_ineq);
		if (!hull)
			goto error;
		for (j = 0; j < facet->n_ineq; ++j) {
			for (f = 0; f < hull_facet->n_ineq; ++f)
				if (isl_seq_eq(facet->ineq[j],
						hull_facet->ineq[f], 1 + dim))
					break;
			if (f < hull_facet->n_ineq)
				continue;
			k = isl_basic_set_alloc_inequality(hull);
			if (k < 0)
				goto error;
			isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+dim);
			if (!isl_set_wrap_facet(set, hull->ineq[k], facet->ineq[j]))
				goto error;
		}
		isl_basic_set_free(hull_facet);
		isl_basic_set_free(facet);
	}
	hull = isl_basic_set_simplify(hull);
	hull = isl_basic_set_finalize(hull);
	return hull;
error:
	isl_basic_set_free(hull_facet);
	isl_basic_set_free(facet);
	isl_basic_set_free(hull);
	return NULL;
}

/* Special case for computing the convex hull of a one dimensional set.
 * We simply collect the lower and upper bounds of each basic set
 * and the biggest of those.
 */
static __isl_give isl_basic_set *convex_hull_1d(__isl_take isl_set *set)
{
	struct isl_mat *c = NULL;
	isl_int *lower = NULL;
	isl_int *upper = NULL;
	int i, j, k;
	isl_int a, b;
	struct isl_basic_set *hull;

	for (i = 0; i < set->n; ++i) {
		set->p[i] = isl_basic_set_simplify(set->p[i]);
		if (!set->p[i])
			goto error;
	}
	set = isl_set_remove_empty_parts(set);
	if (!set)
		goto error;
	isl_assert(set->ctx, set->n > 0, goto error);
	c = isl_mat_alloc(set->ctx, 2, 2);
	if (!c)
		goto error;

	if (set->p[0]->n_eq > 0) {
		isl_assert(set->ctx, set->p[0]->n_eq == 1, goto error);
		lower = c->row[0];
		upper = c->row[1];
		if (isl_int_is_pos(set->p[0]->eq[0][1])) {
			isl_seq_cpy(lower, set->p[0]->eq[0], 2);
			isl_seq_neg(upper, set->p[0]->eq[0], 2);
		} else {
			isl_seq_neg(lower, set->p[0]->eq[0], 2);
			isl_seq_cpy(upper, set->p[0]->eq[0], 2);
		}
	} else {
		for (j = 0; j < set->p[0]->n_ineq; ++j) {
			if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
				lower = c->row[0];
				isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
			} else {
				upper = c->row[1];
				isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
			}
		}
	}

	isl_int_init(a);
	isl_int_init(b);
	for (i = 0; i < set->n; ++i) {
		struct isl_basic_set *bset = set->p[i];
		int has_lower = 0;
		int has_upper = 0;

		for (j = 0; j < bset->n_eq; ++j) {
			has_lower = 1;
			has_upper = 1;
			if (lower) {
				isl_int_mul(a, lower[0], bset->eq[j][1]);
				isl_int_mul(b, lower[1], bset->eq[j][0]);
				if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
					isl_seq_cpy(lower, bset->eq[j], 2);
				if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
					isl_seq_neg(lower, bset->eq[j], 2);
			}
			if (upper) {
				isl_int_mul(a, upper[0], bset->eq[j][1]);
				isl_int_mul(b, upper[1], bset->eq[j][0]);
				if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
					isl_seq_neg(upper, bset->eq[j], 2);
				if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
					isl_seq_cpy(upper, bset->eq[j], 2);
			}
		}
		for (j = 0; j < bset->n_ineq; ++j) {
			if (isl_int_is_pos(bset->ineq[j][1]))
				has_lower = 1;
			if (isl_int_is_neg(bset->ineq[j][1]))
				has_upper = 1;
			if (lower && isl_int_is_pos(bset->ineq[j][1])) {
				isl_int_mul(a, lower[0], bset->ineq[j][1]);
				isl_int_mul(b, lower[1], bset->ineq[j][0]);
				if (isl_int_lt(a, b))
					isl_seq_cpy(lower, bset->ineq[j], 2);
			}
			if (upper && isl_int_is_neg(bset->ineq[j][1])) {
				isl_int_mul(a, upper[0], bset->ineq[j][1]);
				isl_int_mul(b, upper[1], bset->ineq[j][0]);
				if (isl_int_gt(a, b))
					isl_seq_cpy(upper, bset->ineq[j], 2);
			}
		}
		if (!has_lower)
			lower = NULL;
		if (!has_upper)
			upper = NULL;
	}
	isl_int_clear(a);
	isl_int_clear(b);

	hull = isl_basic_set_alloc(set->ctx, 0, 1, 0, 0, 2);
	hull = isl_basic_set_set_rational(hull);
	if (!hull)
		goto error;
	if (lower) {
		k = isl_basic_set_alloc_inequality(hull);
		isl_seq_cpy(hull->ineq[k], lower, 2);
	}
	if (upper) {
		k = isl_basic_set_alloc_inequality(hull);
		isl_seq_cpy(hull->ineq[k], upper, 2);
	}
	hull = isl_basic_set_finalize(hull);
	isl_set_free(set);
	isl_mat_free(c);
	return hull;
error:
	isl_set_free(set);
	isl_mat_free(c);
	return NULL;
}

static __isl_give isl_basic_set *convex_hull_0d(__isl_take isl_set *set)
{
	struct isl_basic_set *convex_hull;

	if (!set)
		return NULL;

	if (isl_set_is_empty(set))
		convex_hull = isl_basic_set_empty(isl_space_copy(set->dim));
	else
		convex_hull = isl_basic_set_universe(isl_space_copy(set->dim));
	isl_set_free(set);
	return convex_hull;
}

/* Compute the convex hull of a pair of basic sets without any parameters or
 * integer divisions using Fourier-Motzkin elimination.
 * The convex hull is the set of all points that can be written as
 * the sum of points from both basic sets (in homogeneous coordinates).
 * We set up the constraints in a space with dimensions for each of
 * the three sets and then project out the dimensions corresponding
 * to the two original basic sets, retaining only those corresponding
 * to the convex hull.
 */
static __isl_give isl_basic_set *convex_hull_pair_elim(
	__isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
{
	int i, j, k;
	struct isl_basic_set *bset[2];
	struct isl_basic_set *hull = NULL;
	isl_size dim;

	dim = isl_basic_set_dim(bset1, isl_dim_set);
	if (dim < 0 || !bset2)
		goto error;

	hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * dim, 0,
				1 + dim + bset1->n_eq + bset2->n_eq,
				2 + bset1->n_ineq + bset2->n_ineq);
	bset[0] = bset1;
	bset[1] = bset2;
	for (i = 0; i < 2; ++i) {
		for (j = 0; j < bset[i]->n_eq; ++j) {
			k = isl_basic_set_alloc_equality(hull);
			if (k < 0)
				goto error;
			isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
			isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
			isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
					1+dim);
		}
		for (j = 0; j < bset[i]->n_ineq; ++j) {
			k = isl_basic_set_alloc_inequality(hull);
			if (k < 0)
				goto error;
			isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
			isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
			isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
					bset[i]->ineq[j], 1+dim);
		}
		k = isl_basic_set_alloc_inequality(hull);
		if (k < 0)
			goto error;
		isl_seq_clr(hull->ineq[k], 1+2+3*dim);
		isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
	}
	for (j = 0; j < 1+dim; ++j) {
		k = isl_basic_set_alloc_equality(hull);
		if (k < 0)
			goto error;
		isl_seq_clr(hull->eq[k], 1+2+3*dim);
		isl_int_set_si(hull->eq[k][j], -1);
		isl_int_set_si(hull->eq[k][1+dim+j], 1);
		isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
	}
	hull = isl_basic_set_set_rational(hull);
	hull = isl_basic_set_remove_dims(hull, isl_dim_set, dim, 2*(1+dim));
	hull = isl_basic_set_remove_redundancies(hull);
	isl_basic_set_free(bset1);
	isl_basic_set_free(bset2);
	return hull;
error:
	isl_basic_set_free(bset1);
	isl_basic_set_free(bset2);
	isl_basic_set_free(hull);
	return NULL;
}

/* Is the set bounded for each value of the parameters?
 */
isl_bool isl_basic_set_is_bounded(__isl_keep isl_basic_set *bset)
{
	struct isl_tab *tab;
	isl_bool bounded;

	if (!bset)
		return isl_bool_error;
	if (isl_basic_set_plain_is_empty(bset))
		return isl_bool_true;

	tab = isl_tab_from_recession_cone(bset, 1);
	bounded = isl_tab_cone_is_bounded(tab);
	isl_tab_free(tab);
	return bounded;
}

/* Is the image bounded for each value of the parameters and
 * the domain variables?
 */
isl_bool isl_basic_map_image_is_bounded(__isl_keep isl_basic_map *bmap)
{
	isl_size nparam = isl_basic_map_dim(bmap, isl_dim_param);
	isl_size n_in = isl_basic_map_dim(bmap, isl_dim_in);
	isl_bool bounded;

	if (nparam < 0 || n_in < 0)
		return isl_bool_error;

	bmap = isl_basic_map_copy(bmap);
	bmap = isl_basic_map_cow(bmap);
	bmap = isl_basic_map_move_dims(bmap, isl_dim_param, nparam,
					isl_dim_in, 0, n_in);
	bounded = isl_basic_set_is_bounded(bset_from_bmap(bmap));
	isl_basic_map_free(bmap);

	return bounded;
}

/* Is the set bounded for each value of the parameters?
 */
isl_bool isl_set_is_bounded(__isl_keep isl_set *set)
{
	int i;

	if (!set)
		return isl_bool_error;

	for (i = 0; i < set->n; ++i) {
		isl_bool bounded = isl_basic_set_is_bounded(set->p[i]);
		if (!bounded || bounded < 0)
			return bounded;
	}
	return isl_bool_true;
}

/* Compute the lineality space of the convex hull of bset1 and bset2.
 *
 * We first compute the intersection of the recession cone of bset1
 * with the negative of the recession cone of bset2 and then compute
 * the linear hull of the resulting cone.
 */
static __isl_give isl_basic_set *induced_lineality_space(
	__isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
{
	int i, k;
	struct isl_basic_set *lin = NULL;
	isl_size dim;

	dim = isl_basic_set_dim(bset1, isl_dim_all);
	if (dim < 0 || !bset2)
		goto error;

	lin = isl_basic_set_alloc_space(isl_basic_set_get_space(bset1), 0,
					bset1->n_eq + bset2->n_eq,
					bset1->n_ineq + bset2->n_ineq);
	lin = isl_basic_set_set_rational(lin);
	if (!lin)
		goto error;
	for (i = 0; i < bset1->n_eq; ++i) {
		k = isl_basic_set_alloc_equality(lin);
		if (k < 0)
			goto error;
		isl_int_set_si(lin->eq[k][0], 0);
		isl_seq_cpy(lin->eq[k] + 1, bset1->eq[i] + 1, dim);
	}
	for (i = 0; i < bset1->n_ineq; ++i) {
		k = isl_basic_set_alloc_inequality(lin);
		if (k < 0)
			goto error;
		isl_int_set_si(lin->ineq[k][0], 0);
		isl_seq_cpy(lin->ineq[k] + 1, bset1->ineq[i] + 1, dim);
	}
	for (i = 0; i < bset2->n_eq; ++i) {
		k = isl_basic_set_alloc_equality(lin);
		if (k < 0)
			goto error;
		isl_int_set_si(lin->eq[k][0], 0);
		isl_seq_neg(lin->eq[k] + 1, bset2->eq[i] + 1, dim);
	}
	for (i = 0; i < bset2->n_ineq; ++i) {
		k = isl_basic_set_alloc_inequality(lin);
		if (k < 0)
			goto error;
		isl_int_set_si(lin->ineq[k][0], 0);
		isl_seq_neg(lin->ineq[k] + 1, bset2->ineq[i] + 1, dim);
	}

	isl_basic_set_free(bset1);
	isl_basic_set_free(bset2);
	return isl_basic_set_affine_hull(lin);
error:
	isl_basic_set_free(lin);
	isl_basic_set_free(bset1);
	isl_basic_set_free(bset2);
	return NULL;
}

static __isl_give isl_basic_set *uset_convex_hull(__isl_take isl_set *set);

/* Given a set and a linear space "lin" of dimension n > 0,
 * project the linear space from the set, compute the convex hull
 * and then map the set back to the original space.
 *
 * Let
 *
 *	M x = 0
 *
 * describe the linear space.  We first compute the Hermite normal
 * form H = M U of M = H Q, to obtain
 *
 *	H Q x = 0
 *
 * The last n rows of H will be zero, so the last n variables of x' = Q x
 * are the one we want to project out.  We do this by transforming each
 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
 * we transform the hull back to the original space as A' Q_1 x >= b',
 * with Q_1 all but the last n rows of Q.
 */
static __isl_give isl_basic_set *modulo_lineality(__isl_take isl_set *set,
	__isl_take isl_basic_set *lin)
{
	isl_size total = isl_basic_set_dim(lin, isl_dim_all);
	unsigned lin_dim;
	struct isl_basic_set *hull;
	struct isl_mat *M, *U, *Q;

	if (!set || total < 0)
		goto error;
	lin_dim = total - lin->n_eq;
	M = isl_mat_sub_alloc6(set->ctx, lin->eq, 0, lin->n_eq, 1, total);
	M = isl_mat_left_hermite(M, 0, &U, &Q);
	if (!M)
		goto error;
	isl_mat_free(M);
	isl_basic_set_free(lin);

	Q = isl_mat_drop_rows(Q, Q->n_row - lin_dim, lin_dim);

	U = isl_mat_lin_to_aff(U);
	Q = isl_mat_lin_to_aff(Q);

	set = isl_set_preimage(set, U);
	set = isl_set_remove_dims(set, isl_dim_set, total - lin_dim, lin_dim);
	hull = uset_convex_hull(set);
	hull = isl_basic_set_preimage(hull, Q);

	return hull;
error:
	isl_basic_set_free(lin);
	isl_set_free(set);
	return NULL;
}

/* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
 * set up an LP for solving
 *
 *	\sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
 *
 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
 * The next \alpha{ij} correspond to the equalities and come in pairs.
 * The final \alpha{ij} correspond to the inequalities.
 */
static __isl_give isl_basic_set *valid_direction_lp(
	__isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
{
	isl_space *dim;
	struct isl_basic_set *lp;
	unsigned d;
	int n;
	int i, j, k;
	isl_size total;

	total = isl_basic_set_dim(bset1, isl_dim_all);
	if (total < 0 || !bset2)
		goto error;
	d = 1 + total;
	n = 2 +
	    2 * bset1->n_eq + bset1->n_ineq + 2 * bset2->n_eq + bset2->n_ineq;
	dim = isl_space_set_alloc(bset1->ctx, 0, n);
	lp = isl_basic_set_alloc_space(dim, 0, d, n);
	if (!lp)
		goto error;
	for (i = 0; i < n; ++i) {
		k = isl_basic_set_alloc_inequality(lp);
		if (k < 0)
			goto error;
		isl_seq_clr(lp->ineq[k] + 1, n);
		isl_int_set_si(lp->ineq[k][0], -1);
		isl_int_set_si(lp->ineq[k][1 + i], 1);
	}
	for (i = 0; i < d; ++i) {
		k = isl_basic_set_alloc_equality(lp);
		if (k < 0)
			goto error;
		n = 0;
		isl_int_set_si(lp->eq[k][n], 0); n++;
		/* positivity constraint 1 >= 0 */
		isl_int_set_si(lp->eq[k][n], i == 0); n++;
		for (j = 0; j < bset1->n_eq; ++j) {
			isl_int_set(lp->eq[k][n], bset1->eq[j][i]); n++;
			isl_int_neg(lp->eq[k][n], bset1->eq[j][i]); n++;
		}
		for (j = 0; j < bset1->n_ineq; ++j) {
			isl_int_set(lp->eq[k][n], bset1->ineq[j][i]); n++;
		}
		/* positivity constraint 1 >= 0 */
		isl_int_set_si(lp->eq[k][n], -(i == 0)); n++;
		for (j = 0; j < bset2->n_eq; ++j) {
			isl_int_neg(lp->eq[k][n], bset2->eq[j][i]); n++;
			isl_int_set(lp->eq[k][n], bset2->eq[j][i]); n++;
		}
		for (j = 0; j < bset2->n_ineq; ++j) {
			isl_int_neg(lp->eq[k][n], bset2->ineq[j][i]); n++;
		}
	}
	lp = isl_basic_set_gauss(lp, NULL);
	isl_basic_set_free(bset1);
	isl_basic_set_free(bset2);
	return lp;
error:
	isl_basic_set_free(bset1);
	isl_basic_set_free(bset2);
	return NULL;
}

/* Compute a vector s in the homogeneous space such that <s, r> > 0
 * for all rays in the homogeneous space of the two cones that correspond
 * to the input polyhedra bset1 and bset2.
 *
 * We compute s as a vector that satisfies
 *
 *	s = \sum_j \alpha_{ij} h_{ij}	for i = 1,2			(*)
 *
 * with h_{ij} the normals of the facets of polyhedron i
 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
 * strictly positive numbers.  For simplicity we impose \alpha_{ij} >= 1.
 * We first set up an LP with as variables the \alpha{ij}.
 * In this formulation, for each polyhedron i,
 * the first constraint is the positivity constraint, followed by pairs
 * of variables for the equalities, followed by variables for the inequalities.
 * We then simply pick a feasible solution and compute s using (*).
 *
 * Note that we simply pick any valid direction and make no attempt
 * to pick a "good" or even the "best" valid direction.
 */
static __isl_give isl_vec *valid_direction(
	__isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
{
	struct isl_basic_set *lp;
	struct isl_tab *tab;
	struct isl_vec *sample = NULL;
	struct isl_vec *dir;
	isl_size d;
	int i;
	int n;

	if (!bset1 || !bset2)
		goto error;
	lp = valid_direction_lp(isl_basic_set_copy(bset1),
				isl_basic_set_copy(bset2));
	tab = isl_tab_from_basic_set(lp, 0);
	sample = isl_tab_get_sample_value(tab);
	isl_tab_free(tab);
	isl_basic_set_free(lp);
	if (!sample)
		goto error;
	d = isl_basic_set_dim(bset1, isl_dim_all);
	if (d < 0)
		goto error;
	dir = isl_vec_alloc(bset1->ctx, 1 + d);
	if (!dir)
		goto error;
	isl_seq_clr(dir->block.data + 1, dir->size - 1);
	n = 1;
	/* positivity constraint 1 >= 0 */
	isl_int_set(dir->block.data[0], sample->block.data[n]); n++;
	for (i = 0; i < bset1->n_eq; ++i) {
		isl_int_sub(sample->block.data[n],
			    sample->block.data[n], sample->block.data[n+1]);
		isl_seq_combine(dir->block.data,
				bset1->ctx->one, dir->block.data,
				sample->block.data[n], bset1->eq[i], 1 + d);

		n += 2;
	}
	for (i = 0; i < bset1->n_ineq; ++i)
		isl_seq_combine(dir->block.data,
				bset1->ctx->one, dir->block.data,
				sample->block.data[n++], bset1->ineq[i], 1 + d);
	isl_vec_free(sample);
	isl_seq_normalize(bset1->ctx, dir->el, dir->size);
	isl_basic_set_free(bset1);
	isl_basic_set_free(bset2);
	return dir;
error:
	isl_vec_free(sample);
	isl_basic_set_free(bset1);
	isl_basic_set_free(bset2);
	return NULL;
}

/* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
 * compute b_i' + A_i' x' >= 0, with
 *
 *	[ b_i A_i ]        [ y' ]		              [ y' ]
 *	[  1   0  ] S^{-1} [ x' ] >= 0	or	[ b_i' A_i' ] [ x' ] >= 0
 *
 * In particular, add the "positivity constraint" and then perform
 * the mapping.
 */
static __isl_give isl_basic_set *homogeneous_map(__isl_take isl_basic_set *bset,
	__isl_take isl_mat *T)
{
	int k;
	isl_size total;

	total = isl_basic_set_dim(bset, isl_dim_all);
	if (total < 0)
		goto error;
	bset = isl_basic_set_extend_constraints(bset, 0, 1);
	k = isl_basic_set_alloc_inequality(bset);
	if (k < 0)
		goto error;
	isl_seq_clr(bset->ineq[k] + 1, total);
	isl_int_set_si(bset->ineq[k][0], 1);
	bset = isl_basic_set_preimage(bset, T);
	return bset;
error:
	isl_mat_free(T);
	isl_basic_set_free(bset);
	return NULL;
}

/* Compute the convex hull of a pair of basic sets without any parameters or
 * integer divisions, where the convex hull is known to be pointed,
 * but the basic sets may be unbounded.
 *
 * We turn this problem into the computation of a convex hull of a pair
 * _bounded_ polyhedra by "changing the direction of the homogeneous
 * dimension".  This idea is due to <NAME>.
 *
 * Consider the cones in homogeneous space that correspond to the
 * input polyhedra.  The rays of these cones are also rays of the
 * polyhedra if the coordinate that corresponds to the homogeneous
 * dimension is zero.  That is, if the inner product of the rays
 * with the homogeneous direction is zero.
 * The cones in the homogeneous space can also be considered to
 * correspond to other pairs of polyhedra by chosing a different
 * homogeneous direction.  To ensure that both of these polyhedra
 * are bounded, we need to make sure that all rays of the cones
 * correspond to vertices and not to rays.
 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
 * The vector s is computed in valid_direction.
 *
 * Note that we need to consider _all_ rays of the cones and not just
 * the rays that correspond to rays in the polyhedra.  If we were to
 * only consider those rays and turn them into vertices, then we
 * may inadvertently turn some vertices into rays.
 *
 * The standard homogeneous direction is the unit vector in the 0th coordinate.
 * We therefore transform the two polyhedra such that the selected
 * direction is mapped onto this standard direction and then proceed
 * with the normal computation.
 * Let S be a non-singular square matrix with s as its first row,
 * then we want to map the polyhedra to the space
 *
 *	[ y' ]     [ y ]		[ y ]          [ y' ]
 *	[ x' ] = S [ x ]	i.e.,	[ x ] = S^{-1} [ x' ]
 *
 * We take S to be the unimodular completion of s to limit the growth
 * of the coefficients in the following computations.
 *
 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
 * We first move to the homogeneous dimension
 *
 *	b_i y + A_i x >= 0		[ b_i A_i ] [ y ]    [ 0 ]
 *	    y         >= 0	or	[  1   0  ] [ x ] >= [ 0 ]
 *
 * Then we change directoin
 *
 *	[ b_i A_i ]        [ y' ]		              [ y' ]
 *	[  1   0  ] S^{-1} [ x' ] >= 0	or	[ b_i' A_i' ] [ x' ] >= 0
 *
 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
 * resulting in b' + A' x' >= 0, which we then convert back
 *
 *	            [ y ]		        [ y ]
 *	[ b' A' ] S [ x ] >= 0	or	[ b A ] [ x ] >= 0
 *
 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
 */
static __isl_give isl_basic_set *convex_hull_pair_pointed(
	__isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
{
	struct isl_ctx *ctx = NULL;
	struct isl_vec *dir = NULL;
	struct isl_mat *T = NULL;
	struct isl_mat *T2 = NULL;
	struct isl_basic_set *hull;
	struct isl_set *set;

	if (!bset1 || !bset2)
		goto error;
	ctx = isl_basic_set_get_ctx(bset1);
	dir = valid_direction(isl_basic_set_copy(bset1),
				isl_basic_set_copy(bset2));
	if (!dir)
		goto error;
	T = isl_mat_alloc(ctx, dir->size, dir->size);
	if (!T)
		goto error;
	isl_seq_cpy(T->row[0], dir->block.data, dir->size);
	T = isl_mat_unimodular_complete(T, 1);
	T2 = isl_mat_right_inverse(isl_mat_copy(T));

	bset1 = homogeneous_map(bset1, isl_mat_copy(T2));
	bset2 = homogeneous_map(bset2, T2);
	set = isl_set_alloc_space(isl_basic_set_get_space(bset1), 2, 0);
	set = isl_set_add_basic_set(set, bset1);
	set = isl_set_add_basic_set(set, bset2);
	hull = uset_convex_hull(set);
	hull = isl_basic_set_preimage(hull, T);
	 
	isl_vec_free(dir);

	return hull;
error:
	isl_vec_free(dir);
	isl_basic_set_free(bset1);
	isl_basic_set_free(bset2);
	return NULL;
}

static __isl_give isl_basic_set *uset_convex_hull_wrap(__isl_take isl_set *set);
static __isl_give isl_basic_set *modulo_affine_hull(
	__isl_take isl_set *set, __isl_take isl_basic_set *affine_hull);

/* Compute the convex hull of a pair of basic sets without any parameters or
 * integer divisions.
 *
 * This function is called from uset_convex_hull_unbounded, which
 * means that the complete convex hull is unbounded.  Some pairs
 * of basic sets may still be bounded, though.
 * They may even lie inside a lower dimensional space, in which
 * case they need to be handled inside their affine hull since
 * the main algorithm assumes that the result is full-dimensional.
 *
 * If the convex hull of the two basic sets would have a non-trivial
 * lineality space, we first project out this lineality space.
 */
static __isl_give isl_basic_set *convex_hull_pair(
	__isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
{
	isl_basic_set *lin, *aff;
	isl_bool bounded1, bounded2;
	isl_size total;

	if (bset1->ctx->opt->convex == ISL_CONVEX_HULL_FM)
		return convex_hull_pair_elim(bset1, bset2);

	aff = isl_set_affine_hull(isl_basic_set_union(isl_basic_set_copy(bset1),
						    isl_basic_set_copy(bset2)));
	if (!aff)
		goto error;
	if (aff->n_eq != 0) 
		return modulo_affine_hull(isl_basic_set_union(bset1, bset2), aff);
	isl_basic_set_free(aff);

	bounded1 = isl_basic_set_is_bounded(bset1);
	bounded2 = isl_basic_set_is_bounded(bset2);

	if (bounded1 < 0 || bounded2 < 0)
		goto error;

	if (bounded1 && bounded2)
		return uset_convex_hull_wrap(isl_basic_set_union(bset1, bset2));

	if (bounded1 || bounded2)
		return convex_hull_pair_pointed(bset1, bset2);

	lin = induced_lineality_space(isl_basic_set_copy(bset1),
				      isl_basic_set_copy(bset2));
	if (!lin)
		goto error;
	if (isl_basic_set_plain_is_universe(lin)) {
		isl_basic_set_free(bset1);
		isl_basic_set_free(bset2);
		return lin;
	}
	total = isl_basic_set_dim(lin, isl_dim_all);
	if (lin->n_eq < total) {
		struct isl_set *set;
		set = isl_set_alloc_space(isl_basic_set_get_space(bset1), 2, 0);
		set = isl_set_add_basic_set(set, bset1);
		set = isl_set_add_basic_set(set, bset2);
		return modulo_lineality(set, lin);
	}
	isl_basic_set_free(lin);
	if (total < 0)
		goto error;

	return convex_hull_pair_pointed(bset1, bset2);
error:
	isl_basic_set_free(bset1);
	isl_basic_set_free(bset2);
	return NULL;
}

/* Compute the lineality space of a basic set.
 * We basically just drop the constants and turn every inequality
 * into an equality.
 * Any explicit representations of local variables are removed
 * because they may no longer be valid representations
 * in the lineality space.
 */
__isl_give isl_basic_set *isl_basic_set_lineality_space(
	__isl_take isl_basic_set *bset)
{
	int i, k;
	struct isl_basic_set *lin = NULL;
	isl_size n_div, dim;

	n_div = isl_basic_set_dim(bset, isl_dim_div);
	dim = isl_basic_set_dim(bset, isl_dim_all);
	if (n_div < 0 || dim < 0)
		return isl_basic_set_free(bset);

	lin = isl_basic_set_alloc_space(isl_basic_set_get_space(bset),
					n_div, dim, 0);
	for (i = 0; i < n_div; ++i)
		if (isl_basic_set_alloc_div(lin) < 0)
			goto error;
	if (!lin)
		goto error;
	for (i = 0; i < bset->n_eq; ++i) {
		k = isl_basic_set_alloc_equality(lin);
		if (k < 0)
			goto error;
		isl_int_set_si(lin->eq[k][0], 0);
		isl_seq_cpy(lin->eq[k] + 1, bset->eq[i] + 1, dim);
	}
	lin = isl_basic_set_gauss(lin, NULL);
	if (!lin)
		goto error;
	for (i = 0; i < bset->n_ineq && lin->n_eq < dim; ++i) {
		k = isl_basic_set_alloc_equality(lin);
		if (k < 0)
			goto error;
		isl_int_set_si(lin->eq[k][0], 0);
		isl_seq_cpy(lin->eq[k] + 1, bset->ineq[i] + 1, dim);
		lin = isl_basic_set_gauss(lin, NULL);
		if (!lin)
			goto error;
	}
	isl_basic_set_free(bset);
	return lin;
error:
	isl_basic_set_free(lin);
	isl_basic_set_free(bset);
	return NULL;
}

/* Compute the (linear) hull of the lineality spaces of the basic sets in the
 * set "set".
 */
__isl_give isl_basic_set *isl_set_combined_lineality_space(
	__isl_take isl_set *set)
{
	int i;
	struct isl_set *lin = NULL;

	if (!set)
		return NULL;
	if (set->n == 0) {
		isl_space *space = isl_set_get_space(set);
		isl_set_free(set);
		return isl_basic_set_empty(space);
	}

	lin = isl_set_alloc_space(isl_set_get_space(set), set->n, 0);
	for (i = 0; i < set->n; ++i)
		lin = isl_set_add_basic_set(lin,
		    isl_basic_set_lineality_space(isl_basic_set_copy(set->p[i])));
	isl_set_free(set);
	return isl_set_affine_hull(lin);
}

/* Compute the convex hull of a set without any parameters or
 * integer divisions.
 * In each step, we combined two basic sets until only one
 * basic set is left.
 * The input basic sets are assumed not to have a non-trivial
 * lineality space.  If any of the intermediate results has
 * a non-trivial lineality space, it is projected out.
 */
static __isl_give isl_basic_set *uset_convex_hull_unbounded(
	__isl_take isl_set *set)
{
	isl_basic_set_list *list;

	list = isl_set_get_basic_set_list(set);
	isl_set_free(set);

	while (list) {
		isl_size n, total;
		struct isl_basic_set *t;
		isl_basic_set *bset1, *bset2;

		n = isl_basic_set_list_n_basic_set(list);
		if (n < 0)
			goto error;
		if (n < 2)
			isl_die(isl_basic_set_list_get_ctx(list),
				isl_error_internal,
				"expecting at least two elements", goto error);
		bset1 = isl_basic_set_list_get_basic_set(list, n - 1);
		bset2 = isl_basic_set_list_get_basic_set(list, n - 2);
		bset1 = convex_hull_pair(bset1, bset2);
		if (n == 2) {
			isl_basic_set_list_free(list);
			return bset1;
		}
		bset1 = isl_basic_set_underlying_set(bset1);
		list = isl_basic_set_list_drop(list, n - 2, 2);
		list = isl_basic_set_list_add(list, bset1);

		t = isl_basic_set_list_get_basic_set(list, n - 2);
		t = isl_basic_set_lineality_space(t);
		if (!t)
			goto error;
		if (isl_basic_set_plain_is_universe(t)) {
			isl_basic_set_list_free(list);
			return t;
		}
		total = isl_basic_set_dim(t, isl_dim_all);
		if (t->n_eq < total) {
			set = isl_basic_set_list_union(list);
			return modulo_lineality(set, t);
		}
		isl_basic_set_free(t);
		if (total < 0)
			goto error;
	}

	return NULL;
error:
	isl_basic_set_list_free(list);
	return NULL;
}

/* Compute an initial hull for wrapping containing a single initial
 * facet.
 * This function assumes that the given set is bounded.
 */
static __isl_give isl_basic_set *initial_hull(__isl_take isl_basic_set *hull,
	__isl_keep isl_set *set)
{
	struct isl_mat *bounds = NULL;
	isl_size dim;
	int k;

	if (!hull)
		goto error;
	bounds = initial_facet_constraint(set);
	if (!bounds)
		goto error;
	k = isl_basic_set_alloc_inequality(hull);
	if (k < 0)
		goto error;
	dim = isl_set_dim(set, isl_dim_set);
	if (dim < 0)
		goto error;
	isl_assert(set->ctx, 1 + dim == bounds->n_col, goto error);
	isl_seq_cpy(hull->ineq[k], bounds->row[0], bounds->n_col);
	isl_mat_free(bounds);

	return hull;
error:
	isl_basic_set_free(hull);
	isl_mat_free(bounds);
	return NULL;
}

struct max_constraint {
	struct isl_mat *c;
	int	 	count;
	int		ineq;
};

static int max_constraint_equal(const void *entry, const void *val)
{
	struct max_constraint *a = (struct max_constraint *)entry;
	isl_int *b = (isl_int *)val;

	return isl_seq_eq(a->c->row[0] + 1, b, a->c->n_col - 1);
}

static isl_stat update_constraint(struct isl_ctx *ctx,
	struct isl_hash_table *table,
	isl_int *con, unsigned len, int n, int ineq)
{
	struct isl_hash_table_entry *entry;
	struct max_constraint *c;
	uint32_t c_hash;

	c_hash = isl_seq_get_hash(con + 1, len);
	entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
			con + 1, 0);
	if (!entry)
		return isl_stat_ok;
	c = entry->data;
	if (c->count < n) {
		isl_hash_table_remove(ctx, table, entry);
		return isl_stat_ok;
	}
	c->count++;
	if (isl_int_gt(c->c->row[0][0], con[0]))
		return isl_stat_ok;
	if (isl_int_eq(c->c->row[0][0], con[0])) {
		if (ineq)
			c->ineq = ineq;
		return isl_stat_ok;
	}
	c->c = isl_mat_cow(c->c);
	isl_int_set(c->c->row[0][0], con[0]);
	c->ineq = ineq;

	return isl_stat_ok;
}

/* Check whether the constraint hash table "table" contains the constraint
 * "con".
 */
static isl_bool has_constraint(struct isl_ctx *ctx,
	struct isl_hash_table *table, isl_int *con, unsigned len, int n)
{
	struct isl_hash_table_entry *entry;
	struct max_constraint *c;
	uint32_t c_hash;

	c_hash = isl_seq_get_hash(con + 1, len);
	entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
			con + 1, 0);
	if (!entry)
		return isl_bool_false;
	c = entry->data;
	if (c->count < n)
		return isl_bool_false;
	return isl_bool_ok(isl_int_eq(c->c->row[0][0], con[0]));
}

/* Are the constraints of "bset" known to be facets?
 * If there are any equality constraints, then they are not.
 * If there may be redundant constraints, then those
 * redundant constraints are not facets.
 */
static isl_bool has_facets(__isl_keep isl_basic_set *bset)
{
	int n_eq;

	n_eq = isl_basic_set_n_equality(bset);
	if (n_eq < 0)
		return isl_bool_error;
	if (n_eq != 0)
		return isl_bool_false;
	return ISL_F_ISSET(bset, ISL_BASIC_SET_NO_REDUNDANT);
}

/* Check for inequality constraints of a basic set without equalities
 * or redundant constraints
 * such that the same or more stringent copies of the constraint appear
 * in all of the basic sets.  Such constraints are necessarily facet
 * constraints of the convex hull.
 *
 * If the resulting basic set is by chance identical to one of
 * the basic sets in "set", then we know that this basic set contains
 * all other basic sets and is therefore the convex hull of set.
 * In this case we set *is_hull to 1.
 */
static __isl_give isl_basic_set *common_constraints(
	__isl_take isl_basic_set *hull, __isl_keep isl_set *set, int *is_hull)
{
	int i, j, s, n;
	int min_constraints;
	int best;
	struct max_constraint *constraints = NULL;
	struct isl_hash_table *table = NULL;
	isl_size total;

	*is_hull = 0;

	for (i = 0; i < set->n; ++i) {
		isl_bool facets = has_facets(set->p[i]);
		if (facets < 0)
			return isl_basic_set_free(hull);
		if (facets)
			break;
	}
	if (i >= set->n)
		return hull;
	min_constraints = set->p[i]->n_ineq;
	best = i;
	for (i = best + 1; i < set->n; ++i) {
		isl_bool facets = has_facets(set->p[i]);
		if (facets < 0)
			return isl_basic_set_free(hull);
		if (!facets)
			continue;
		if (set->p[i]->n_ineq >= min_constraints)
			continue;
		min_constraints = set->p[i]->n_ineq;
		best = i;
	}
	constraints = isl_calloc_array(hull->ctx, struct max_constraint,
					min_constraints);
	if (!constraints)
		return hull;
	table = isl_alloc_type(hull->ctx, struct isl_hash_table);
	if (isl_hash_table_init(hull->ctx, table, min_constraints))
		goto error;

	total = isl_set_dim(set, isl_dim_all);
	if (total < 0)
		goto error;
	for (i = 0; i < set->p[best]->n_ineq; ++i) {
		constraints[i].c = isl_mat_sub_alloc6(hull->ctx,
			set->p[best]->ineq + i, 0, 1, 0, 1 + total);
		if (!constraints[i].c)
			goto error;
		constraints[i].ineq = 1;
	}
	for (i = 0; i < min_constraints; ++i) {
		struct isl_hash_table_entry *entry;
		uint32_t c_hash;
		c_hash = isl_seq_get_hash(constraints[i].c->row[0] + 1, total);
		entry = isl_hash_table_find(hull->ctx, table, c_hash,
			max_constraint_equal, constraints[i].c->row[0] + 1, 1);
		if (!entry)
			goto error;
		isl_assert(hull->ctx, !entry->data, goto error);
		entry->data = &constraints[i];
	}

	n = 0;
	for (s = 0; s < set->n; ++s) {
		if (s == best)
			continue;

		for (i = 0; i < set->p[s]->n_eq; ++i) {
			isl_int *eq = set->p[s]->eq[i];
			for (j = 0; j < 2; ++j) {
				isl_seq_neg(eq, eq, 1 + total);
				if (update_constraint(hull->ctx, table,
						    eq, total, n, 0) < 0)
					goto error;
			}
		}
		for (i = 0; i < set->p[s]->n_ineq; ++i) {
			isl_int *ineq = set->p[s]->ineq[i];
			if (update_constraint(hull->ctx, table, ineq, total, n,
					set->p[s]->n_eq == 0) < 0)
				goto error;
		}
		++n;
	}

	for (i = 0; i < min_constraints; ++i) {
		if (constraints[i].count < n)
			continue;
		if (!constraints[i].ineq)
			continue;
		j = isl_basic_set_alloc_inequality(hull);
		if (j < 0)
			goto error;
		isl_seq_cpy(hull->ineq[j], constraints[i].c->row[0], 1 + total);
	}

	for (s = 0; s < set->n; ++s) {
		if (set->p[s]->n_eq)
			continue;
		if (set->p[s]->n_ineq != hull->n_ineq)
			continue;
		for (i = 0; i < set->p[s]->n_ineq; ++i) {
			isl_bool has;
			isl_int *ineq = set->p[s]->ineq[i];
			has = has_constraint(hull->ctx, table, ineq, total, n);
			if (has < 0)
				goto error;
			if (!has)
				break;
		}
		if (i == set->p[s]->n_ineq)
			*is_hull = 1;
	}

	isl_hash_table_clear(table);
	for (i = 0; i < min_constraints; ++i)
		isl_mat_free(constraints[i].c);
	free(constraints);
	free(table);
	return hull;
error:
	isl_hash_table_clear(table);
	free(table);
	if (constraints)
		for (i = 0; i < min_constraints; ++i)
			isl_mat_free(constraints[i].c);
	free(constraints);
	return hull;
}

/* Create a template for the convex hull of "set" and fill it up
 * obvious facet constraints, if any.  If the result happens to
 * be the convex hull of "set" then *is_hull is set to 1.
{'fraction_non_alphanumeric': 0.1347624798711755, 'fraction_numerical': 0.014925523349436393, 'mean_word_length': 3.9611044537647295, 'pattern_counts': {'":': 0, '<': 261, '<?xml version=': 0, '>': 892, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1601278', 'n_tokens_mistral': 44478, 'n_tokens_neox': 41129, 'n_words': 10688}