add isl_constraint_get_ctx
[isl.git] / polytope_scan.c
blob60ac3e50e4eaf0912fe36f0a0ac69e39e5eb84e2
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 <assert.h>
11 #include <isl_map_private.h>
12 #include "isl_equalities.h"
13 #include <isl/seq.h>
14 #include "isl_scan.h"
15 #include <isl_mat_private.h>
17 /* The input of this program is the same as that of the "polytope_scan"
18 * program from the barvinok distribution.
20 * Constraints of set is PolyLib format.
22 * The input set is assumed to be bounded.
25 struct scan_samples {
26 struct isl_scan_callback callback;
27 struct isl_mat *samples;
30 static int scan_samples_add_sample(struct isl_scan_callback *cb,
31 __isl_take isl_vec *sample)
33 struct scan_samples *ss = (struct scan_samples *)cb;
35 ss->samples = isl_mat_extend(ss->samples, ss->samples->n_row + 1,
36 ss->samples->n_col);
37 if (!ss->samples)
38 goto error;
40 isl_seq_cpy(ss->samples->row[ss->samples->n_row - 1],
41 sample->el, sample->size);
43 isl_vec_free(sample);
44 return 0;
45 error:
46 isl_vec_free(sample);
47 return -1;
50 static struct isl_mat *isl_basic_set_scan_samples(struct isl_basic_set *bset)
52 isl_ctx *ctx;
53 unsigned dim;
54 struct scan_samples ss;
56 ctx = isl_basic_set_get_ctx(bset);
57 dim = isl_basic_set_total_dim(bset);
58 ss.callback.add = scan_samples_add_sample;
59 ss.samples = isl_mat_alloc(ctx, 0, 1 + dim);
60 if (!ss.samples)
61 goto error;
63 if (isl_basic_set_scan(bset, &ss.callback) < 0) {
64 isl_mat_free(ss.samples);
65 return NULL;
68 return ss.samples;
69 error:
70 isl_basic_set_free(bset);
71 return NULL;
74 static struct isl_mat *isl_basic_set_samples(struct isl_basic_set *bset)
76 struct isl_mat *T;
77 struct isl_mat *samples;
79 if (!bset)
80 return NULL;
82 if (bset->n_eq == 0)
83 return isl_basic_set_scan_samples(bset);
85 bset = isl_basic_set_remove_equalities(bset, &T, NULL);
86 samples = isl_basic_set_scan_samples(bset);
87 return isl_mat_product(samples, isl_mat_transpose(T));
90 int main(int argc, char **argv)
92 struct isl_ctx *ctx = isl_ctx_alloc();
93 struct isl_basic_set *bset;
94 struct isl_mat *samples;
96 bset = isl_basic_set_read_from_file(ctx, stdin, 0);
97 samples = isl_basic_set_samples(bset);
98 isl_mat_print_internal(samples, stdout, 0);
99 isl_mat_free(samples);
100 isl_ctx_free(ctx);
102 return 0;