add isl_set_count
[isl.git] / isl_scan.c
blobe70892387767bbfd354d3614b508e26999c654f3
1 /*
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
8 */
10 #include "isl_basis_reduction.h"
11 #include "isl_scan.h"
12 #include "isl_seq.h"
13 #include "isl_tab.h"
14 #include <isl_map_private.h>
16 struct isl_counter {
17 struct isl_scan_callback callback;
18 isl_int count;
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);
28 isl_vec_free(sample);
30 return 0;
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);
41 return 0;
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;
50 if (!tab)
51 return -1;
52 sample = isl_tab_get_sample_value(tab);
53 if (!sample)
54 return -1;
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);
67 if (!sample)
68 return -1;
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)
96 unsigned dim;
97 struct isl_mat *B = NULL;
98 struct isl_tab *tab = NULL;
99 struct isl_vec *min;
100 struct isl_vec *max;
101 struct isl_tab_undo **snap;
102 int level;
103 int init;
104 enum isl_lp_result res;
106 if (!bset)
107 return -1;
109 dim = isl_basic_set_total_dim(bset);
110 if (dim == 0)
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)
118 goto error;
120 tab = isl_tab_from_basic_set(bset);
121 if (!tab)
122 goto error;
123 if (isl_tab_extend_cons(tab, dim + 1) < 0)
124 goto error;
126 tab->basis = isl_mat_identity(bset->ctx, 1 + dim);
127 if (1)
128 tab = isl_tab_compute_reduced_basis(tab);
129 if (!tab)
130 goto error;
131 B = isl_mat_copy(tab->basis);
132 if (!B)
133 goto error;
135 level = 0;
136 init = 1;
138 while (level >= 0) {
139 int empty = 0;
140 if (init) {
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)
144 empty = 1;
145 if (res == isl_lp_error || res == isl_lp_unbounded)
146 goto error;
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)
155 empty = 1;
156 if (res == isl_lp_error || res == isl_lp_unbounded)
157 goto error;
158 snap[level] = isl_tab_snap(tab);
159 } else
160 isl_int_add_ui(min->el[level], min->el[level], 1);
162 if (empty || isl_int_gt(min->el[level], max->el[level])) {
163 level--;
164 init = 0;
165 if (level >= 0)
166 if (isl_tab_rollback(tab, snap[level]) < 0)
167 goto error;
168 continue;
170 if (level == dim - 1 && callback->add == increment_counter) {
171 if (increment_range(callback,
172 min->el[level], max->el[level]))
173 goto error;
174 level--;
175 init = 0;
176 if (level >= 0)
177 if (isl_tab_rollback(tab, snap[level]) < 0)
178 goto error;
179 continue;
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) {
185 ++level;
186 init = 1;
187 continue;
189 if (add_solution(tab, callback) < 0)
190 goto error;
191 init = 0;
192 if (isl_tab_rollback(tab, snap[level]) < 0)
193 goto error;
196 isl_tab_free(tab);
197 free(snap);
198 isl_vec_free(min);
199 isl_vec_free(max);
200 isl_basic_set_free(bset);
201 isl_mat_free(B);
202 return 0;
203 error:
204 isl_tab_free(tab);
205 free(snap);
206 isl_vec_free(min);
207 isl_vec_free(max);
208 isl_basic_set_free(bset);
209 isl_mat_free(B);
210 return -1;
213 int isl_set_count(__isl_keep isl_set *set, isl_int *count)
215 int i;
216 struct isl_counter cnt = { { &increment_counter } };
218 if (!set)
219 return -1;
221 isl_int_init(cnt.count);
223 set = isl_set_copy(set);
224 set = isl_set_cow(set);
225 set = isl_set_make_disjoint(set);
226 set = isl_set_compute_divs(set);
227 if (!set)
228 goto error;
230 isl_int_set_si(cnt.count, 0);
231 for (i = 0; i < set->n; ++i)
232 if (isl_basic_set_scan(isl_basic_set_copy(set->p[i]),
233 &cnt.callback) < 0)
234 goto error;
236 isl_int_set(*count, cnt.count);
238 isl_set_free(set);
239 isl_int_clear(cnt.count);
241 return 0;
242 error:
243 isl_set_free(set);
244 isl_int_clear(cnt.count);
245 return -1;