isl_scan.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright 2008-2009 Katholieke Universiteit Leuven
  3. *
  4. * Use of this software is governed by the MIT license
  5. *
  6. * Written by Sven Verdoolaege, K.U.Leuven, Departement
  7. * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
  8. */
  9. #include <isl_ctx_private.h>
  10. #include <isl_map_private.h>
  11. #include "isl_basis_reduction.h"
  12. #include "isl_scan.h"
  13. #include <isl_seq.h>
  14. #include "isl_tab.h"
  15. #include <isl_val_private.h>
  16. #include <isl_vec_private.h>
  17. struct isl_counter {
  18. struct isl_scan_callback callback;
  19. isl_int count;
  20. isl_int max;
  21. };
  22. static isl_stat increment_counter(struct isl_scan_callback *cb,
  23. __isl_take isl_vec *sample)
  24. {
  25. struct isl_counter *cnt = (struct isl_counter *)cb;
  26. isl_int_add_ui(cnt->count, cnt->count, 1);
  27. isl_vec_free(sample);
  28. if (isl_int_is_zero(cnt->max) || isl_int_lt(cnt->count, cnt->max))
  29. return isl_stat_ok;
  30. return isl_stat_error;
  31. }
  32. static int increment_range(struct isl_scan_callback *cb, isl_int min, isl_int max)
  33. {
  34. struct isl_counter *cnt = (struct isl_counter *)cb;
  35. isl_int_add(cnt->count, cnt->count, max);
  36. isl_int_sub(cnt->count, cnt->count, min);
  37. isl_int_add_ui(cnt->count, cnt->count, 1);
  38. if (isl_int_is_zero(cnt->max) || isl_int_lt(cnt->count, cnt->max))
  39. return 0;
  40. isl_int_set(cnt->count, cnt->max);
  41. return -1;
  42. }
  43. /* Call callback->add with the current sample value of the tableau "tab".
  44. */
  45. static int add_solution(struct isl_tab *tab, struct isl_scan_callback *callback)
  46. {
  47. struct isl_vec *sample;
  48. if (!tab)
  49. return -1;
  50. sample = isl_tab_get_sample_value(tab);
  51. if (!sample)
  52. return -1;
  53. return callback->add(callback, sample);
  54. }
  55. static isl_stat scan_0D(__isl_take isl_basic_set *bset,
  56. struct isl_scan_callback *callback)
  57. {
  58. struct isl_vec *sample;
  59. sample = isl_vec_alloc(bset->ctx, 1);
  60. isl_basic_set_free(bset);
  61. if (!sample)
  62. return isl_stat_error;
  63. isl_int_set_si(sample->el[0], 1);
  64. return callback->add(callback, sample);
  65. }
  66. /* Look for all integer points in "bset", which is assumed to be bounded,
  67. * and call callback->add on each of them.
  68. *
  69. * We first compute a reduced basis for the set and then scan
  70. * the set in the directions of this basis.
  71. * We basically perform a depth first search, where in each level i
  72. * we compute the range in the i-th basis vector direction, given
  73. * fixed values in the directions of the previous basis vector.
  74. * We then add an equality to the tableau fixing the value in the
  75. * direction of the current basis vector to each value in the range
  76. * in turn and then continue to the next level.
  77. *
  78. * The search is implemented iteratively. "level" identifies the current
  79. * basis vector. "init" is true if we want the first value at the current
  80. * level and false if we want the next value.
  81. * Solutions are added in the leaves of the search tree, i.e., after
  82. * we have fixed a value in each direction of the basis.
  83. */
  84. isl_stat isl_basic_set_scan(__isl_take isl_basic_set *bset,
  85. struct isl_scan_callback *callback)
  86. {
  87. isl_size dim;
  88. struct isl_mat *B = NULL;
  89. struct isl_tab *tab = NULL;
  90. struct isl_vec *min;
  91. struct isl_vec *max;
  92. struct isl_tab_undo **snap;
  93. int level;
  94. int init;
  95. enum isl_lp_result res;
  96. dim = isl_basic_set_dim(bset, isl_dim_all);
  97. if (dim < 0) {
  98. bset = isl_basic_set_free(bset);
  99. return isl_stat_error;
  100. }
  101. if (dim == 0)
  102. return scan_0D(bset, callback);
  103. min = isl_vec_alloc(bset->ctx, dim);
  104. max = isl_vec_alloc(bset->ctx, dim);
  105. snap = isl_alloc_array(bset->ctx, struct isl_tab_undo *, dim);
  106. if (!min || !max || !snap)
  107. goto error;
  108. tab = isl_tab_from_basic_set(bset, 0);
  109. if (!tab)
  110. goto error;
  111. if (isl_tab_extend_cons(tab, dim + 1) < 0)
  112. goto error;
  113. tab->basis = isl_mat_identity(bset->ctx, 1 + dim);
  114. if (1)
  115. tab = isl_tab_compute_reduced_basis(tab);
  116. if (!tab)
  117. goto error;
  118. B = isl_mat_copy(tab->basis);
  119. if (!B)
  120. goto error;
  121. level = 0;
  122. init = 1;
  123. while (level >= 0) {
  124. int empty = 0;
  125. if (init) {
  126. res = isl_tab_min(tab, B->row[1 + level],
  127. bset->ctx->one, &min->el[level], NULL, 0);
  128. if (res == isl_lp_empty)
  129. empty = 1;
  130. if (res == isl_lp_error || res == isl_lp_unbounded)
  131. goto error;
  132. isl_seq_neg(B->row[1 + level] + 1,
  133. B->row[1 + level] + 1, dim);
  134. res = isl_tab_min(tab, B->row[1 + level],
  135. bset->ctx->one, &max->el[level], NULL, 0);
  136. isl_seq_neg(B->row[1 + level] + 1,
  137. B->row[1 + level] + 1, dim);
  138. isl_int_neg(max->el[level], max->el[level]);
  139. if (res == isl_lp_empty)
  140. empty = 1;
  141. if (res == isl_lp_error || res == isl_lp_unbounded)
  142. goto error;
  143. snap[level] = isl_tab_snap(tab);
  144. } else
  145. isl_int_add_ui(min->el[level], min->el[level], 1);
  146. if (empty || isl_int_gt(min->el[level], max->el[level])) {
  147. level--;
  148. init = 0;
  149. if (level >= 0)
  150. if (isl_tab_rollback(tab, snap[level]) < 0)
  151. goto error;
  152. continue;
  153. }
  154. if (level == dim - 1 && callback->add == increment_counter) {
  155. if (increment_range(callback,
  156. min->el[level], max->el[level]))
  157. goto error;
  158. level--;
  159. init = 0;
  160. if (level >= 0)
  161. if (isl_tab_rollback(tab, snap[level]) < 0)
  162. goto error;
  163. continue;
  164. }
  165. isl_int_neg(B->row[1 + level][0], min->el[level]);
  166. if (isl_tab_add_valid_eq(tab, B->row[1 + level]) < 0)
  167. goto error;
  168. isl_int_set_si(B->row[1 + level][0], 0);
  169. if (level < dim - 1) {
  170. ++level;
  171. init = 1;
  172. continue;
  173. }
  174. if (add_solution(tab, callback) < 0)
  175. goto error;
  176. init = 0;
  177. if (isl_tab_rollback(tab, snap[level]) < 0)
  178. goto error;
  179. }
  180. isl_tab_free(tab);
  181. free(snap);
  182. isl_vec_free(min);
  183. isl_vec_free(max);
  184. isl_basic_set_free(bset);
  185. isl_mat_free(B);
  186. return isl_stat_ok;
  187. error:
  188. isl_tab_free(tab);
  189. free(snap);
  190. isl_vec_free(min);
  191. isl_vec_free(max);
  192. isl_basic_set_free(bset);
  193. isl_mat_free(B);
  194. return isl_stat_error;
  195. }
  196. isl_stat isl_set_scan(__isl_take isl_set *set,
  197. struct isl_scan_callback *callback)
  198. {
  199. int i;
  200. if (!set || !callback)
  201. goto error;
  202. set = isl_set_cow(set);
  203. set = isl_set_make_disjoint(set);
  204. set = isl_set_compute_divs(set);
  205. if (!set)
  206. goto error;
  207. for (i = 0; i < set->n; ++i)
  208. if (isl_basic_set_scan(isl_basic_set_copy(set->p[i]),
  209. callback) < 0)
  210. goto error;
  211. isl_set_free(set);
  212. return isl_stat_ok;
  213. error:
  214. isl_set_free(set);
  215. return isl_stat_error;
  216. }
  217. int isl_basic_set_count_upto(__isl_keep isl_basic_set *bset,
  218. isl_int max, isl_int *count)
  219. {
  220. struct isl_counter cnt = { { &increment_counter } };
  221. if (!bset)
  222. return -1;
  223. isl_int_init(cnt.count);
  224. isl_int_init(cnt.max);
  225. isl_int_set_si(cnt.count, 0);
  226. isl_int_set(cnt.max, max);
  227. if (isl_basic_set_scan(isl_basic_set_copy(bset), &cnt.callback) < 0 &&
  228. isl_int_lt(cnt.count, cnt.max))
  229. goto error;
  230. isl_int_set(*count, cnt.count);
  231. isl_int_clear(cnt.max);
  232. isl_int_clear(cnt.count);
  233. return 0;
  234. error:
  235. isl_int_clear(cnt.count);
  236. return -1;
  237. }
  238. int isl_set_count_upto(__isl_keep isl_set *set, isl_int max, isl_int *count)
  239. {
  240. struct isl_counter cnt = { { &increment_counter } };
  241. if (!set)
  242. return -1;
  243. isl_int_init(cnt.count);
  244. isl_int_init(cnt.max);
  245. isl_int_set_si(cnt.count, 0);
  246. isl_int_set(cnt.max, max);
  247. if (isl_set_scan(isl_set_copy(set), &cnt.callback) < 0 &&
  248. isl_int_lt(cnt.count, cnt.max))
  249. goto error;
  250. isl_int_set(*count, cnt.count);
  251. isl_int_clear(cnt.max);
  252. isl_int_clear(cnt.count);
  253. return 0;
  254. error:
  255. isl_int_clear(cnt.count);
  256. return -1;
  257. }
  258. int isl_set_count(__isl_keep isl_set *set, isl_int *count)
  259. {
  260. if (!set)
  261. return -1;
  262. return isl_set_count_upto(set, set->ctx->zero, count);
  263. }
  264. /* Count the total number of elements in "set" (in an inefficient way) and
  265. * return the result.
  266. */
  267. __isl_give isl_val *isl_set_count_val(__isl_keep isl_set *set)
  268. {
  269. isl_val *v;
  270. if (!set)
  271. return NULL;
  272. v = isl_val_zero(isl_set_get_ctx(set));
  273. v = isl_val_cow(v);
  274. if (!v)
  275. return NULL;
  276. if (isl_set_count(set, &v->n) < 0)
  277. v = isl_val_free(v);
  278. return v;
  279. }