2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 #include "isl_basis_reduction.h"
14 #include <isl_map_private.h>
17 struct isl_scan_callback callback;
21 static int increment_counter(struct isl_scan_callback *cb,
22 __isl_take isl_vec *sample)
24 struct isl_counter *cnt = (struct isl_counter *)cb;
26 isl_int_add_ui(cnt->count, cnt->count, 1);
33 static int increment_range(struct isl_scan_callback *cb, isl_int min, isl_int max)
35 struct isl_counter *cnt = (struct isl_counter *)cb;
37 isl_int_add(cnt->count, cnt->count, max);
38 isl_int_sub(cnt->count, cnt->count, min);
39 isl_int_add_ui(cnt->count, cnt->count, 1);
44 /* Call callback->add with the current sample value of the tableau "tab".
46 static int add_solution(struct isl_tab *tab, struct isl_scan_callback *callback)
48 struct isl_vec *sample;
52 sample = isl_tab_get_sample_value(tab);
56 return callback->add(callback, sample);
59 static int scan_0D(struct isl_basic_set *bset,
60 struct isl_scan_callback *callback)
62 struct isl_vec *sample;
64 sample = isl_vec_alloc(bset->ctx, 1);
65 isl_basic_set_free(bset);
70 isl_int_set_si(sample->el[0], 1);
72 return callback->add(callback, sample);
75 /* Look for all integer points in "bset", which is assumed to be unbounded,
76 * and call callback->add on each of them.
78 * We first compute a reduced basis for the set and then scan
79 * the set in the directions of this basis.
80 * We basically perform a depth first search, where in each level i
81 * we compute the range in the i-th basis vector direction, given
82 * fixed values in the directions of the previous basis vector.
83 * We then add an equality to the tableau fixing the value in the
84 * direction of the current basis vector to each value in the range
85 * in turn and then continue to the next level.
87 * The search is implemented iteratively. "level" identifies the current
88 * basis vector. "init" is true if we want the first value at the current
89 * level and false if we want the next value.
90 * Solutions are added in the leaves of the search tree, i.e., after
91 * we have fixed a value in each direction of the basis.
93 int isl_basic_set_scan(struct isl_basic_set *bset,
94 struct isl_scan_callback *callback)
97 struct isl_mat *B = NULL;
98 struct isl_tab *tab = NULL;
101 struct isl_tab_undo **snap;
104 enum isl_lp_result res;
109 dim = isl_basic_set_total_dim(bset);
111 return scan_0D(bset, callback);
113 min = isl_vec_alloc(bset->ctx, dim);
114 max = isl_vec_alloc(bset->ctx, dim);
115 snap = isl_alloc_array(bset->ctx, struct isl_tab_undo *, dim);
117 if (!min || !max || !snap)
120 tab = isl_tab_from_basic_set(bset);
123 if (isl_tab_extend_cons(tab, dim + 1) < 0)
126 tab->basis = isl_mat_identity(bset->ctx, 1 + dim);
128 tab = isl_tab_compute_reduced_basis(tab);
131 B = isl_mat_copy(tab->basis);
141 res = isl_tab_min(tab, B->row[1 + level],
142 bset->ctx->one, &min->el[level], NULL, 0);
143 if (res == isl_lp_empty)
145 if (res == isl_lp_error || res == isl_lp_unbounded)
147 isl_seq_neg(B->row[1 + level] + 1,
148 B->row[1 + level] + 1, dim);
149 res = isl_tab_min(tab, B->row[1 + level],
150 bset->ctx->one, &max->el[level], NULL, 0);
151 isl_seq_neg(B->row[1 + level] + 1,
152 B->row[1 + level] + 1, dim);
153 isl_int_neg(max->el[level], max->el[level]);
154 if (res == isl_lp_empty)
156 if (res == isl_lp_error || res == isl_lp_unbounded)
158 snap[level] = isl_tab_snap(tab);
160 isl_int_add_ui(min->el[level], min->el[level], 1);
162 if (empty || isl_int_gt(min->el[level], max->el[level])) {
166 if (isl_tab_rollback(tab, snap[level]) < 0)
170 if (level == dim - 1 && callback->add == increment_counter) {
171 if (increment_range(callback,
172 min->el[level], max->el[level]))
177 if (isl_tab_rollback(tab, snap[level]) < 0)
181 isl_int_neg(B->row[1 + level][0], min->el[level]);
182 tab = isl_tab_add_valid_eq(tab, B->row[1 + level]);
183 isl_int_set_si(B->row[1 + level][0], 0);
184 if (level < dim - 1) {
189 if (add_solution(tab, callback) < 0)
192 if (isl_tab_rollback(tab, snap[level]) < 0)
200 isl_basic_set_free(bset);
208 isl_basic_set_free(bset);
213 int isl_set_scan(__isl_take isl_set *set, struct isl_scan_callback *callback)
217 if (!set || !callback)
220 set = isl_set_cow(set);
221 set = isl_set_make_disjoint(set);
222 set = isl_set_compute_divs(set);
226 for (i = 0; i < set->n; ++i)
227 if (isl_basic_set_scan(isl_basic_set_copy(set->p[i]),
238 int isl_set_count(__isl_keep isl_set *set, isl_int *count)
240 struct isl_counter cnt = { { &increment_counter } };
245 isl_int_init(cnt.count);
247 isl_int_set_si(cnt.count, 0);
248 if (isl_set_scan(isl_set_copy(set), &cnt.callback) < 0)
251 isl_int_set(*count, cnt.count);
252 isl_int_clear(cnt.count);
256 isl_int_clear(cnt.count);