properly remove piplib submodule
[isl.git] / isl_scan.c
blobb0aba4092120e3f58c84393873d6c8aaea9adb07
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"
15 /* Call callback->add with the current sample value of the tableau "tab".
17 static int add_solution(struct isl_tab *tab, struct isl_scan_callback *callback)
19 struct isl_vec *sample;
21 if (!tab)
22 return -1;
23 sample = isl_tab_get_sample_value(tab);
24 if (!sample)
25 return -1;
27 return callback->add(callback, sample);
30 static int scan_0D(struct isl_basic_set *bset,
31 struct isl_scan_callback *callback)
33 struct isl_vec *sample;
35 sample = isl_vec_alloc(bset->ctx, 1);
36 isl_basic_set_free(bset);
38 if (!sample)
39 return -1;
41 isl_int_set_si(sample->el[0], 1);
43 return callback->add(callback, sample);
46 /* Look for all integer points in "bset", which is assumed to be unbounded,
47 * and call callback->add on each of them.
49 * We first compute a reduced basis for the set and then scan
50 * the set in the directions of this basis.
51 * We basically perform a depth first search, where in each level i
52 * we compute the range in the i-th basis vector direction, given
53 * fixed values in the directions of the previous basis vector.
54 * We then add an equality to the tableau fixing the value in the
55 * direction of the current basis vector to each value in the range
56 * in turn and then continue to the next level.
58 * The search is implemented iteratively. "level" identifies the current
59 * basis vector. "init" is true if we want the first value at the current
60 * level and false if we want the next value.
61 * Solutions are added in the leaves of the search tree, i.e., after
62 * we have fixed a value in each direction of the basis.
64 int isl_basic_set_scan(struct isl_basic_set *bset,
65 struct isl_scan_callback *callback)
67 unsigned dim;
68 struct isl_mat *B = NULL;
69 struct isl_tab *tab = NULL;
70 struct isl_vec *min;
71 struct isl_vec *max;
72 struct isl_tab_undo **snap;
73 int level;
74 int init;
75 enum isl_lp_result res;
77 if (!bset)
78 return -1;
80 dim = isl_basic_set_total_dim(bset);
81 if (dim == 0)
82 return scan_0D(bset, callback);
84 min = isl_vec_alloc(bset->ctx, dim);
85 max = isl_vec_alloc(bset->ctx, dim);
86 snap = isl_alloc_array(bset->ctx, struct isl_tab_undo *, dim);
88 if (!min || !max || !snap)
89 goto error;
91 tab = isl_tab_from_basic_set(bset);
92 if (!tab)
93 goto error;
95 tab->basis = isl_mat_identity(bset->ctx, 1 + dim);
96 if (1)
97 tab = isl_tab_compute_reduced_basis(tab);
98 if (!tab)
99 goto error;
100 B = isl_mat_copy(tab->basis);
101 if (!B)
102 goto error;
104 level = 0;
105 init = 1;
107 while (level >= 0) {
108 int empty = 0;
109 if (init) {
110 res = isl_tab_min(tab, B->row[1 + level],
111 bset->ctx->one, &min->el[level], NULL, 0);
112 if (res == isl_lp_empty)
113 empty = 1;
114 if (res == isl_lp_error || res == isl_lp_unbounded)
115 goto error;
116 isl_seq_neg(B->row[1 + level] + 1,
117 B->row[1 + level] + 1, dim);
118 res = isl_tab_min(tab, B->row[1 + level],
119 bset->ctx->one, &max->el[level], NULL, 0);
120 isl_seq_neg(B->row[1 + level] + 1,
121 B->row[1 + level] + 1, dim);
122 isl_int_neg(max->el[level], max->el[level]);
123 if (res == isl_lp_empty)
124 empty = 1;
125 if (res == isl_lp_error || res == isl_lp_unbounded)
126 goto error;
127 snap[level] = isl_tab_snap(tab);
128 } else
129 isl_int_add_ui(min->el[level], min->el[level], 1);
131 if (empty || isl_int_gt(min->el[level], max->el[level])) {
132 level--;
133 init = 0;
134 if (level >= 0)
135 if (isl_tab_rollback(tab, snap[level]) < 0)
136 goto error;
137 continue;
139 isl_int_neg(B->row[1 + level][0], min->el[level]);
140 tab = isl_tab_add_valid_eq(tab, B->row[1 + level]);
141 isl_int_set_si(B->row[1 + level][0], 0);
142 if (level < dim - 1) {
143 ++level;
144 init = 1;
145 continue;
147 if (add_solution(tab, callback) < 0)
148 goto error;
149 init = 0;
150 if (isl_tab_rollback(tab, snap[level]) < 0)
151 goto error;
154 isl_tab_free(tab);
155 free(snap);
156 isl_vec_free(min);
157 isl_vec_free(max);
158 isl_basic_set_free(bset);
159 isl_mat_free(B);
160 return 0;
161 error:
162 isl_tab_free(tab);
163 free(snap);
164 isl_vec_free(min);
165 isl_vec_free(max);
166 isl_basic_set_free(bset);
167 isl_mat_free(B);
168 return -1;