isl_tab.c: tab_mat_product: avoid NULL pointer dereference
[isl.git] / polytope_scan.c
blob41983aad3080c9b4ececc51385f0eb37ce742093
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_equalities.h"
12 #include "isl_seq.h"
13 #include "isl_scan.h"
15 /* The input of this program is the same as that of the "polytope_scan"
16 * program from the barvinok distribution.
18 * Constraints of set is PolyLib format.
20 * The input set is assumed to be bounded.
23 struct scan_samples {
24 struct isl_scan_callback callback;
25 struct isl_mat *samples;
28 static int scan_samples_add_sample(struct isl_scan_callback *cb,
29 __isl_take isl_vec *sample)
31 struct scan_samples *ss = (struct scan_samples *)cb;
33 ss->samples = isl_mat_extend(ss->samples, ss->samples->n_row + 1,
34 ss->samples->n_col);
35 if (!ss->samples)
36 goto error;
38 isl_seq_cpy(ss->samples->row[ss->samples->n_row - 1],
39 sample->el, sample->size);
41 isl_vec_free(sample);
42 return 0;
43 error:
44 isl_vec_free(sample);
45 return -1;
48 static struct isl_mat *isl_basic_set_scan_samples(struct isl_basic_set *bset)
50 unsigned dim;
51 struct scan_samples ss;
53 dim = isl_basic_set_total_dim(bset);
54 ss.callback.add = scan_samples_add_sample;
55 ss.samples = isl_mat_alloc(bset->ctx, 0, 1 + dim);
56 if (!ss.samples)
57 goto error;
59 if (isl_basic_set_scan(bset, &ss.callback) < 0) {
60 isl_mat_free(ss.samples);
61 return NULL;
64 return ss.samples;
65 error:
66 isl_basic_set_free(bset);
67 return NULL;
70 static struct isl_mat *isl_basic_set_samples(struct isl_basic_set *bset)
72 struct isl_mat *T;
73 struct isl_mat *samples;
75 if (bset->n_eq == 0)
76 return isl_basic_set_scan_samples(bset);
78 bset = isl_basic_set_remove_equalities(bset, &T, NULL);
79 samples = isl_basic_set_scan_samples(bset);
80 return isl_mat_product(samples, isl_mat_transpose(T));
83 int main(int argc, char **argv)
85 struct isl_ctx *ctx = isl_ctx_alloc();
86 struct isl_basic_set *bset;
87 struct isl_mat *samples;
89 bset = isl_basic_set_read_from_file(ctx, stdin, 0);
90 samples = isl_basic_set_samples(bset);
91 isl_mat_dump(samples, stdout, 0);
92 isl_mat_free(samples);
93 isl_ctx_free(ctx);
95 return 0;