2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 #include <isl_ctx_private.h>
11 #include <isl_map_private.h>
12 #include "isl_basis_reduction.h"
16 #include <isl_val_private.h>
19 struct isl_scan_callback callback;
24 static int increment_counter(struct isl_scan_callback *cb,
25 __isl_take isl_vec *sample)
27 struct isl_counter *cnt = (struct isl_counter *)cb;
29 isl_int_add_ui(cnt->count, cnt->count, 1);
33 if (isl_int_is_zero(cnt->max) || isl_int_lt(cnt->count, cnt->max))
38 static int increment_range(struct isl_scan_callback *cb, isl_int min, isl_int max)
40 struct isl_counter *cnt = (struct isl_counter *)cb;
42 isl_int_add(cnt->count, cnt->count, max);
43 isl_int_sub(cnt->count, cnt->count, min);
44 isl_int_add_ui(cnt->count, cnt->count, 1);
46 if (isl_int_is_zero(cnt->max) || isl_int_lt(cnt->count, cnt->max))
48 isl_int_set(cnt->count, cnt->max);
52 /* Call callback->add with the current sample value of the tableau "tab".
54 static int add_solution(struct isl_tab *tab, struct isl_scan_callback *callback)
56 struct isl_vec *sample;
60 sample = isl_tab_get_sample_value(tab);
64 return callback->add(callback, sample);
67 static int scan_0D(struct isl_basic_set *bset,
68 struct isl_scan_callback *callback)
70 struct isl_vec *sample;
72 sample = isl_vec_alloc(bset->ctx, 1);
73 isl_basic_set_free(bset);
78 isl_int_set_si(sample->el[0], 1);
80 return callback->add(callback, sample);
83 /* Look for all integer points in "bset", which is assumed to be bounded,
84 * and call callback->add on each of them.
86 * We first compute a reduced basis for the set and then scan
87 * the set in the directions of this basis.
88 * We basically perform a depth first search, where in each level i
89 * we compute the range in the i-th basis vector direction, given
90 * fixed values in the directions of the previous basis vector.
91 * We then add an equality to the tableau fixing the value in the
92 * direction of the current basis vector to each value in the range
93 * in turn and then continue to the next level.
95 * The search is implemented iteratively. "level" identifies the current
96 * basis vector. "init" is true if we want the first value at the current
97 * level and false if we want the next value.
98 * Solutions are added in the leaves of the search tree, i.e., after
99 * we have fixed a value in each direction of the basis.
101 int isl_basic_set_scan(struct isl_basic_set *bset,
102 struct isl_scan_callback *callback)
105 struct isl_mat *B = NULL;
106 struct isl_tab *tab = NULL;
109 struct isl_tab_undo **snap;
112 enum isl_lp_result res;
117 dim = isl_basic_set_total_dim(bset);
119 return scan_0D(bset, callback);
121 min = isl_vec_alloc(bset->ctx, dim);
122 max = isl_vec_alloc(bset->ctx, dim);
123 snap = isl_alloc_array(bset->ctx, struct isl_tab_undo *, dim);
125 if (!min || !max || !snap)
128 tab = isl_tab_from_basic_set(bset, 0);
131 if (isl_tab_extend_cons(tab, dim + 1) < 0)
134 tab->basis = isl_mat_identity(bset->ctx, 1 + dim);
136 tab = isl_tab_compute_reduced_basis(tab);
139 B = isl_mat_copy(tab->basis);
149 res = isl_tab_min(tab, B->row[1 + level],
150 bset->ctx->one, &min->el[level], NULL, 0);
151 if (res == isl_lp_empty)
153 if (res == isl_lp_error || res == isl_lp_unbounded)
155 isl_seq_neg(B->row[1 + level] + 1,
156 B->row[1 + level] + 1, dim);
157 res = isl_tab_min(tab, B->row[1 + level],
158 bset->ctx->one, &max->el[level], NULL, 0);
159 isl_seq_neg(B->row[1 + level] + 1,
160 B->row[1 + level] + 1, dim);
161 isl_int_neg(max->el[level], max->el[level]);
162 if (res == isl_lp_empty)
164 if (res == isl_lp_error || res == isl_lp_unbounded)
166 snap[level] = isl_tab_snap(tab);
168 isl_int_add_ui(min->el[level], min->el[level], 1);
170 if (empty || isl_int_gt(min->el[level], max->el[level])) {
174 if (isl_tab_rollback(tab, snap[level]) < 0)
178 if (level == dim - 1 && callback->add == increment_counter) {
179 if (increment_range(callback,
180 min->el[level], max->el[level]))
185 if (isl_tab_rollback(tab, snap[level]) < 0)
189 isl_int_neg(B->row[1 + level][0], min->el[level]);
190 if (isl_tab_add_valid_eq(tab, B->row[1 + level]) < 0)
192 isl_int_set_si(B->row[1 + level][0], 0);
193 if (level < dim - 1) {
198 if (add_solution(tab, callback) < 0)
201 if (isl_tab_rollback(tab, snap[level]) < 0)
209 isl_basic_set_free(bset);
217 isl_basic_set_free(bset);
222 int isl_set_scan(__isl_take isl_set *set, struct isl_scan_callback *callback)
226 if (!set || !callback)
229 set = isl_set_cow(set);
230 set = isl_set_make_disjoint(set);
231 set = isl_set_compute_divs(set);
235 for (i = 0; i < set->n; ++i)
236 if (isl_basic_set_scan(isl_basic_set_copy(set->p[i]),
247 int isl_basic_set_count_upto(__isl_keep isl_basic_set *bset,
248 isl_int max, isl_int *count)
250 struct isl_counter cnt = { { &increment_counter } };
255 isl_int_init(cnt.count);
256 isl_int_init(cnt.max);
258 isl_int_set_si(cnt.count, 0);
259 isl_int_set(cnt.max, max);
260 if (isl_basic_set_scan(isl_basic_set_copy(bset), &cnt.callback) < 0 &&
261 isl_int_lt(cnt.count, cnt.max))
264 isl_int_set(*count, cnt.count);
265 isl_int_clear(cnt.max);
266 isl_int_clear(cnt.count);
270 isl_int_clear(cnt.count);
274 int isl_set_count_upto(__isl_keep isl_set *set, isl_int max, isl_int *count)
276 struct isl_counter cnt = { { &increment_counter } };
281 isl_int_init(cnt.count);
282 isl_int_init(cnt.max);
284 isl_int_set_si(cnt.count, 0);
285 isl_int_set(cnt.max, max);
286 if (isl_set_scan(isl_set_copy(set), &cnt.callback) < 0 &&
287 isl_int_lt(cnt.count, cnt.max))
290 isl_int_set(*count, cnt.count);
291 isl_int_clear(cnt.max);
292 isl_int_clear(cnt.count);
296 isl_int_clear(cnt.count);
300 int isl_set_count(__isl_keep isl_set *set, isl_int *count)
304 return isl_set_count_upto(set, set->ctx->zero, count);
307 /* Count the total number of elements in "set" (in an inefficient way) and
310 __isl_give isl_val *isl_set_count_val(__isl_keep isl_set *set)
316 v = isl_val_zero(isl_set_get_ctx(set));
320 if (isl_set_count(set, &v->n) < 0)