1 /* Octagonal_Shape class implementation (non-inline functions).
2    Copyright (C) 2001-2010 Roberto Bagnara <bagnara@cs.unipr.it>
3    Copyright (C) 2010-2016 BUGSENG srl (http://bugseng.com)
4 
5 This file is part of the Parma Polyhedra Library (PPL).
6 
7 The PPL is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 The PPL is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA.
20 
21 For the most up-to-date information see the Parma Polyhedra Library
22 site: http://bugseng.com/products/ppl/ . */
23 
24 #include "ppl-config.h"
25 #include "Octagonal_Shape_defs.hh"
26 
27 namespace PPL = Parma_Polyhedra_Library;
28 
29 #ifdef PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
30 /*! \relates Parma_Polyhedra_Library::Octagonal_Shape */
31 #endif // defined(PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS)
32 bool
33 PPL::Octagonal_Shape_Helper
extract_octagonal_difference(const Constraint & c,const dimension_type c_space_dim,dimension_type & c_num_vars,dimension_type & c_first_var,dimension_type & c_second_var,Coefficient & c_coeff,Coefficient & c_term)34 ::extract_octagonal_difference(const Constraint& c,
35                                const dimension_type c_space_dim,
36                                dimension_type& c_num_vars,
37                                dimension_type& c_first_var,
38                                dimension_type& c_second_var,
39                                Coefficient& c_coeff,
40                                Coefficient& c_term) {
41   // Check for preconditions.
42   PPL_ASSERT(c.space_dimension() == c_space_dim);
43   PPL_ASSERT(c_num_vars == 0 && c_first_var == 0 && c_second_var == 0);
44 
45   c_first_var = c.expression().first_nonzero(1, c_space_dim + 1);
46 
47   if (c_first_var == c_space_dim + 1) {
48     c_term = c.inhomogeneous_term();
49     return true;
50   }
51 
52   ++c_num_vars;
53   --c_first_var;
54 
55   c_second_var = c.expression().first_nonzero(c_first_var + 2, c_space_dim + 1);
56 
57   if (c_second_var == c_space_dim + 1) {
58     c_term = c.inhomogeneous_term();
59     const Coefficient& c0 = c.coefficient(Variable(c_first_var));
60     c_term *= 2;
61     c_first_var *= 2;
62     if (sgn(c0) < 0) {
63       c_second_var = c_first_var;
64       ++c_first_var;
65     }
66     else {
67       c_second_var = c_first_var + 1;
68     }
69     c_coeff = c0;
70     return true;
71   }
72 
73   ++c_num_vars;
74   --c_second_var;
75 
76   if (!c.expression().all_zeroes(c_second_var + 2, c_space_dim + 1)) {
77     return false;
78   }
79 
80   using std::swap;
81 
82   // FIXME: The calling code expects c_first_var > c_second_var, when
83   // c_num_vars==2, but it shouldn't.
84   swap(c_first_var, c_second_var);
85 
86   // Make sure that `c' is indeed an octagonal difference,
87   // i.e., it is of this form:
88   //   (+/-) a*x (+/-) a*y <=/= b.
89   c_term = c.inhomogeneous_term();
90   const Coefficient& c0 = c.coefficient(Variable(c_first_var));
91   const Coefficient& c1 = c.coefficient(Variable(c_second_var));
92   if (c0 != c1 && c0 != -c1) {
93     // Constraint `c' is not an octagonal difference.
94     return false;
95   }
96   c_first_var *= 2;
97   c_second_var *= 2;
98   if (sgn(c0) < 0) {
99     ++c_first_var;
100   }
101   if (sgn(c1) > 0) {
102     ++c_second_var;
103   }
104   c_coeff = c0;
105 
106   return true;
107 }
108