1 #include "isl_constraint.h"
3 #include "isl_set_ppl.h"
5 static void ppl_dim_to_isl_dim(isl_basic_map
*bmap
, int pos
,
6 enum isl_dim_type
*type
, int *i_pos
)
8 if (pos
< isl_basic_map_dim(bmap
, isl_dim_out
)) {
13 pos
-= isl_basic_map_dim(bmap
, isl_dim_out
);
15 if (pos
< isl_basic_map_dim(bmap
, isl_dim_in
)) {
20 pos
-= isl_basic_map_dim(bmap
, isl_dim_in
);
22 if (pos
< isl_basic_map_dim(bmap
, isl_dim_div
)) {
27 pos
-= isl_basic_map_dim(bmap
, isl_dim_div
);
29 *type
= isl_dim_param
;
33 ppl_Polyhedron_t
isl_basic_map_to_ppl(__isl_keep isl_basic_map
*bmap
)
39 ppl_Coefficient_t coeff
;
40 int allocated_coeff
= 0;
46 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
47 if (ppl_new_C_Polyhedron_from_space_dimension(&pol
, dim
, 0) < 0)
52 if (ppl_new_Coefficient(&coeff
) < 0)
56 bmap
= isl_basic_map_copy(bmap
);
57 for (c
= isl_basic_map_first_constraint(bmap
); c
;
58 c
= isl_constraint_next(c
)) {
61 ppl_Linear_Expression_t le
;
63 int eq
= isl_constraint_is_equality(c
);
65 if (ppl_new_Linear_Expression_with_dimension(&le
, dim
) < 0)
68 for (i
= 0; i
< dim
; ++i
) {
70 enum isl_dim_type type
;
72 ppl_dim_to_isl_dim(bmap
, i
, &type
, &j
);
73 isl_constraint_get_coefficient(c
, type
, j
, &v
);
74 ppl_assign_Coefficient_from_mpz_t(coeff
, v
);
76 if (ppl_Linear_Expression_add_to_coefficient(le
, i
, coeff
) < 0)
82 isl_constraint_get_constant(c
, &v
);
83 ppl_assign_Coefficient_from_mpz_t(coeff
, v
);
84 if (ppl_Linear_Expression_add_to_inhomogeneous(le
, coeff
) < 0)
87 if (ppl_new_Constraint(&con
, le
,
88 eq
? PPL_CONSTRAINT_TYPE_EQUAL
:
89 PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
) < 0)
92 ppl_delete_Linear_Expression(le
);
94 if (!err
&& ppl_Polyhedron_add_constraint(pol
, con
) < 0)
97 ppl_delete_Constraint(con
);
102 ppl_delete_Coefficient(coeff
);
109 ppl_delete_Coefficient(coeff
);
111 ppl_delete_Polyhedron(pol
);
115 static add_basic_map(isl_basic_map
*bmap
, void *user
)
117 ppl_Polyhedron_t pol
;
118 ppl_Pointset_Powerset_C_Polyhedron_t ps
;
120 ps
= (ppl_Pointset_Powerset_C_Polyhedron_t
) user
;
122 pol
= isl_basic_map_to_ppl(bmap
);
123 isl_basic_map_free(bmap
);
128 if (ppl_Pointset_Powerset_C_Polyhedron_add_disjunct(ps
, pol
))
131 ppl_delete_Polyhedron(pol
);
134 ppl_delete_Polyhedron(pol
);
138 static get_total_dim(isl_basic_map
*bmap
, void *user
)
140 unsigned *dim
= (unsigned *)user
;
142 *dim
= isl_basic_map_total_dim(bmap
);
143 isl_basic_map_free(bmap
);
148 ppl_Pointset_Powerset_C_Polyhedron_t
isl_map_to_ppl(__isl_keep isl_map
*map
)
151 ppl_Pointset_Powerset_C_Polyhedron_t ps
;
153 map
= isl_map_align_divs(map
);
157 if (isl_map_foreach_basic_map(map
, &get_total_dim
, &dim
) < 0)
160 if (ppl_new_Pointset_Powerset_C_Polyhedron_from_space_dimension(&ps
, dim
, 1) < 0)
163 if (isl_map_foreach_basic_map(map
, &add_basic_map
, ps
) < 0)
168 ppl_delete_Pointset_Powerset_C_Polyhedron(ps
);
172 __isl_give isl_basic_map
*isl_basic_map_new_from_ppl(
173 ppl_const_Polyhedron_t pol
, __isl_take isl_dim
*dim
)
177 ppl_Coefficient_t coeff
;
178 ppl_const_Constraint_System_t cs
;
179 ppl_Constraint_System_const_iterator_t cit
, cit_end
;
180 isl_basic_map
*bmap
= NULL
;
181 ppl_dimension_type ppl_dim
;
186 if (ppl_Polyhedron_get_minimized_constraints(pol
, &cs
) < 0)
188 if (ppl_Constraint_System_space_dimension(cs
, &ppl_dim
) < 0)
191 if (ppl_new_Constraint_System_const_iterator(&cit
) < 0)
193 if (ppl_new_Constraint_System_const_iterator(&cit_end
) < 0)
195 if (ppl_Constraint_System_begin(cs
, cit
) < 0)
197 if (ppl_Constraint_System_end(cs
, cit_end
) < 0)
200 if (ppl_new_Coefficient(&coeff
) < 0)
203 extra
= ppl_dim
- isl_dim_total(dim
);
204 dim
= isl_dim_add(dim
, isl_dim_in
, extra
);
205 bmap
= isl_basic_map_universe(isl_dim_copy(dim
));
207 d
= isl_dim_total(dim
);
209 while (!ppl_Constraint_System_const_iterator_equal_test(cit
, cit_end
)) {
210 struct isl_constraint
*c
;
211 ppl_const_Constraint_t con
;
215 if (ppl_Constraint_System_const_iterator_dereference(cit
, &con
) < 0)
218 eq
= ppl_Constraint_type(con
) == PPL_CONSTRAINT_TYPE_EQUAL
;
220 c
= isl_equality_alloc(isl_dim_copy(dim
));
222 c
= isl_inequality_alloc(isl_dim_copy(dim
));
224 for (i
= 0; i
< d
; ++i
) {
226 enum isl_dim_type type
;
228 ppl_dim_to_isl_dim(bmap
, i
, &type
, &j
);
229 if (ppl_Constraint_coefficient(con
, i
, coeff
) < 0)
231 if (ppl_Coefficient_to_mpz_t(coeff
, v
) < 0)
233 isl_constraint_set_coefficient(c
, type
, j
, v
);
235 if (ppl_Constraint_inhomogeneous_term(con
, coeff
) < 0)
237 if (ppl_Coefficient_to_mpz_t(coeff
, v
) < 0)
239 isl_constraint_set_constant(c
, v
);
241 bmap
= isl_basic_map_add_constraint(bmap
, c
);
243 if (ppl_Constraint_System_const_iterator_increment(cit
) < 0)
247 ppl_delete_Coefficient(coeff
);
248 ppl_delete_Constraint_System_const_iterator(cit_end
);
249 ppl_delete_Constraint_System_const_iterator(cit
);
255 bmap
= isl_basic_map_project_out(bmap
, isl_dim_in
,
256 isl_basic_map_dim(bmap
, isl_dim_in
) - extra
, extra
);
260 ppl_delete_Coefficient(coeff
);
262 ppl_delete_Constraint_System_const_iterator(cit_end
);
264 ppl_delete_Constraint_System_const_iterator(cit
);
268 isl_basic_map_free(bmap
);
272 __isl_give isl_map
*isl_map_new_from_ppl(
273 ppl_const_Pointset_Powerset_C_Polyhedron_t ps
, __isl_take isl_dim
*dim
)
276 ppl_Pointset_Powerset_C_Polyhedron_const_iterator_t pit
, pit_end
;
278 map
= isl_map_empty(isl_dim_copy(dim
));
282 if (ppl_new_Pointset_Powerset_C_Polyhedron_const_iterator(&pit
) < 0)
284 if (ppl_Pointset_Powerset_C_Polyhedron_const_iterator_begin(ps
, pit
) < 0)
286 if (ppl_new_Pointset_Powerset_C_Polyhedron_const_iterator(&pit_end
) < 0)
288 if (ppl_Pointset_Powerset_C_Polyhedron_const_iterator_end(ps
, pit_end
) < 0)
291 while (!ppl_Pointset_Powerset_C_Polyhedron_const_iterator_equal_test(pit
, pit_end
)) {
292 ppl_const_Polyhedron_t pol
;
295 if (ppl_Pointset_Powerset_C_Polyhedron_const_iterator_dereference(pit
, &pol
) < 0)
298 bmap
= isl_basic_map_new_from_ppl(pol
, isl_dim_copy(dim
));
300 map
= isl_map_union(map
, isl_map_from_basic_map(bmap
));
302 if (ppl_Pointset_Powerset_C_Polyhedron_const_iterator_increment(pit
) < 0)
306 ppl_delete_Pointset_Powerset_C_Polyhedron_const_iterator(pit
);
307 ppl_delete_Pointset_Powerset_C_Polyhedron_const_iterator(pit_end
);
313 ppl_delete_Pointset_Powerset_C_Polyhedron_const_iterator(pit_end
);
315 ppl_delete_Pointset_Powerset_C_Polyhedron_const_iterator(pit
);
322 ppl_Pointset_Powerset_C_Polyhedron_t
isl_set_to_ppl(__isl_keep isl_set
*set
)
324 return isl_map_to_ppl((isl_map
*)set
);
327 __isl_give isl_set
*isl_set_new_from_ppl(
328 ppl_const_Pointset_Powerset_C_Polyhedron_t ps
, __isl_take isl_dim
*dim
)
330 return (isl_set
*)isl_map_new_from_ppl(ps
, dim
);