isl_basic_map_gauss: normalize resulting constraints
[platform/upstream/isl.git] / polytope_scan.c
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  *
4  * Use of this software is governed by the GNU LGPLv2.1 license
5  *
6  * Written by Sven Verdoolaege, K.U.Leuven, Departement
7  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8  */
9
10 #include <assert.h>
11 #include "isl_equalities.h"
12 #include "isl_seq.h"
13 #include "isl_scan.h"
14 #include <isl_mat_private.h>
15
16 /* The input of this program is the same as that of the "polytope_scan"
17  * program from the barvinok distribution.
18  *
19  * Constraints of set is PolyLib format.
20  *
21  * The input set is assumed to be bounded.
22  */
23
24 struct scan_samples {
25         struct isl_scan_callback callback;
26         struct isl_mat *samples;
27 };
28
29 static int scan_samples_add_sample(struct isl_scan_callback *cb,
30         __isl_take isl_vec *sample)
31 {
32         struct scan_samples *ss = (struct scan_samples *)cb;
33
34         ss->samples = isl_mat_extend(ss->samples, ss->samples->n_row + 1,
35                                                   ss->samples->n_col);
36         if (!ss->samples)
37                 goto error;
38
39         isl_seq_cpy(ss->samples->row[ss->samples->n_row - 1],
40                     sample->el, sample->size);
41
42         isl_vec_free(sample);
43         return 0;
44 error:
45         isl_vec_free(sample);
46         return -1;
47 }
48
49 static struct isl_mat *isl_basic_set_scan_samples(struct isl_basic_set *bset)
50 {
51         unsigned dim;
52         struct scan_samples ss;
53
54         dim = isl_basic_set_total_dim(bset);
55         ss.callback.add = scan_samples_add_sample;
56         ss.samples = isl_mat_alloc(bset->ctx, 0, 1 + dim);
57         if (!ss.samples)
58                 goto error;
59
60         if (isl_basic_set_scan(bset, &ss.callback) < 0) {
61                 isl_mat_free(ss.samples);
62                 return NULL;
63         }
64
65         return ss.samples;
66 error:
67         isl_basic_set_free(bset);
68         return NULL;
69 }
70
71 static struct isl_mat *isl_basic_set_samples(struct isl_basic_set *bset)
72 {
73         struct isl_mat *T;
74         struct isl_mat *samples;
75
76         if (!bset)
77                 return NULL;
78
79         if (bset->n_eq == 0)
80                 return isl_basic_set_scan_samples(bset);
81
82         bset = isl_basic_set_remove_equalities(bset, &T, NULL);
83         samples = isl_basic_set_scan_samples(bset);
84         return isl_mat_product(samples, isl_mat_transpose(T));
85 }
86
87 int main(int argc, char **argv)
88 {
89         struct isl_ctx *ctx = isl_ctx_alloc();
90         struct isl_basic_set *bset;
91         struct isl_mat *samples;
92
93         bset = isl_basic_set_read_from_file(ctx, stdin, 0);
94         samples = isl_basic_set_samples(bset);
95         isl_mat_dump(samples, stdout, 0);
96         isl_mat_free(samples);
97         isl_ctx_free(ctx);
98
99         return 0;
100 }