#include "isl_scan.h"
#include "isl_seq.h"
#include "isl_tab.h"
+#include <isl_map_private.h>
+
+struct isl_counter {
+ struct isl_scan_callback callback;
+ isl_int count;
+ isl_int max;
+};
+
+static int increment_counter(struct isl_scan_callback *cb,
+ __isl_take isl_vec *sample)
+{
+ struct isl_counter *cnt = (struct isl_counter *)cb;
+
+ isl_int_add_ui(cnt->count, cnt->count, 1);
+
+ isl_vec_free(sample);
+
+ if (isl_int_is_zero(cnt->max) || isl_int_lt(cnt->count, cnt->max))
+ return 0;
+ return -1;
+}
+
+static int increment_range(struct isl_scan_callback *cb, isl_int min, isl_int max)
+{
+ struct isl_counter *cnt = (struct isl_counter *)cb;
+
+ isl_int_add(cnt->count, cnt->count, max);
+ isl_int_sub(cnt->count, cnt->count, min);
+ isl_int_add_ui(cnt->count, cnt->count, 1);
+
+ if (isl_int_is_zero(cnt->max) || isl_int_lt(cnt->count, cnt->max))
+ return 0;
+ isl_int_set(cnt->count, cnt->max);
+ return -1;
+}
/* Call callback->add with the current sample value of the tableau "tab".
*/
goto error;
continue;
}
+ if (level == dim - 1 && callback->add == increment_counter) {
+ if (increment_range(callback,
+ min->el[level], max->el[level]))
+ goto error;
+ level--;
+ init = 0;
+ if (level >= 0)
+ if (isl_tab_rollback(tab, snap[level]) < 0)
+ goto error;
+ continue;
+ }
isl_int_neg(B->row[1 + level][0], min->el[level]);
- tab = isl_tab_add_valid_eq(tab, B->row[1 + level]);
+ if (isl_tab_add_valid_eq(tab, B->row[1 + level]) < 0)
+ goto error;
isl_int_set_si(B->row[1 + level][0], 0);
if (level < dim - 1) {
++level;
isl_mat_free(B);
return -1;
}
+
+int isl_set_scan(__isl_take isl_set *set, struct isl_scan_callback *callback)
+{
+ int i;
+
+ if (!set || !callback)
+ goto error;
+
+ set = isl_set_cow(set);
+ set = isl_set_make_disjoint(set);
+ set = isl_set_compute_divs(set);
+ if (!set)
+ goto error;
+
+ for (i = 0; i < set->n; ++i)
+ if (isl_basic_set_scan(isl_basic_set_copy(set->p[i]),
+ callback) < 0)
+ goto error;
+
+ isl_set_free(set);
+ return 0;
+error:
+ isl_set_free(set);
+ return -1;
+}
+
+int isl_set_count_upto(__isl_keep isl_set *set, isl_int max, isl_int *count)
+{
+ struct isl_counter cnt = { { &increment_counter } };
+
+ if (!set)
+ return -1;
+
+ isl_int_init(cnt.count);
+ isl_int_init(cnt.max);
+
+ isl_int_set_si(cnt.count, 0);
+ isl_int_set(cnt.max, max);
+ if (isl_set_scan(isl_set_copy(set), &cnt.callback) < 0 &&
+ isl_int_lt(cnt.count, cnt.max))
+ goto error;
+
+ isl_int_set(*count, cnt.count);
+ isl_int_clear(cnt.max);
+ isl_int_clear(cnt.count);
+
+ return 0;
+error:
+ isl_int_clear(cnt.count);
+ return -1;
+}
+
+int isl_set_count(__isl_keep isl_set *set, isl_int *count)
+{
+ if (!set)
+ return -1;
+ return isl_set_count_upto(set, set->ctx->zero, count);
+}