1 /* Octagonal_Shape<T>::Status class implementation: 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 #ifndef PPL_Og_Status_inlines_hh
25 #define PPL_Og_Status_inlines_hh 1
26 
27 namespace Parma_Polyhedra_Library {
28 
29 template <typename T>
30 inline
Status(flags_t mask)31 Octagonal_Shape<T>::Status::Status(flags_t mask)
32   : flags(mask) {
33 }
34 
35 template <typename T>
36 inline
Status()37 Octagonal_Shape<T>::Status::Status()
38   : flags(ZERO_DIM_UNIV) {
39 }
40 
41 template <typename T>
42 inline bool
test_all(flags_t mask) const43 Octagonal_Shape<T>::Status::test_all(flags_t mask) const {
44   return (flags & mask) == mask;
45 }
46 
47 template <typename T>
48 inline bool
test_any(flags_t mask) const49 Octagonal_Shape<T>::Status::test_any(flags_t mask) const {
50   return (flags & mask) != 0;
51 }
52 
53 template <typename T>
54 inline void
set(flags_t mask)55 Octagonal_Shape<T>::Status::set(flags_t mask) {
56   flags |= mask;
57 }
58 
59 template <typename T>
60 inline void
reset(flags_t mask)61 Octagonal_Shape<T>::Status::reset(flags_t mask) {
62   flags &= ~mask;
63 }
64 
65 template <typename T>
66 inline bool
test_zero_dim_univ() const67 Octagonal_Shape<T>::Status::test_zero_dim_univ() const {
68   return flags == ZERO_DIM_UNIV;
69 }
70 
71 template <typename T>
72 inline void
reset_zero_dim_univ()73 Octagonal_Shape<T>::Status::reset_zero_dim_univ() {
74   // This is a no-op if the current status is not zero-dim.
75   if (flags == ZERO_DIM_UNIV) {
76     // In the zero-dim space, if it is not the universe it is empty.
77     flags = EMPTY;
78   }
79 }
80 
81 template <typename T>
82 inline void
set_zero_dim_univ()83 Octagonal_Shape<T>::Status::set_zero_dim_univ() {
84   // Zero-dim universe is incompatible with anything else.
85   flags = ZERO_DIM_UNIV;
86 }
87 
88 template <typename T>
89 inline bool
test_empty() const90 Octagonal_Shape<T>::Status::test_empty() const {
91   return test_any(EMPTY);
92 }
93 
94 template <typename T>
95 inline void
reset_empty()96 Octagonal_Shape<T>::Status::reset_empty() {
97   reset(EMPTY);
98 }
99 
100 template <typename T>
101 inline void
set_empty()102 Octagonal_Shape<T>::Status::set_empty() {
103   flags = EMPTY;
104 }
105 
106 template <typename T>
107 inline bool
test_strongly_closed() const108 Octagonal_Shape<T>::Status::test_strongly_closed() const {
109   return test_any(STRONGLY_CLOSED);
110 }
111 
112 template <typename T>
113 inline void
reset_strongly_closed()114 Octagonal_Shape<T>::Status::reset_strongly_closed() {
115   reset(STRONGLY_CLOSED);
116 }
117 
118 template <typename T>
119 inline void
set_strongly_closed()120 Octagonal_Shape<T>::Status::set_strongly_closed() {
121   set(STRONGLY_CLOSED);
122 }
123 
124 template <typename T>
125 inline bool
OK() const126 Octagonal_Shape<T>::Status::OK() const {
127   if (test_zero_dim_univ()) {
128     // Zero-dim universe is OK.
129     return true;
130   }
131 
132   if (test_empty()) {
133     Status copy = *this;
134     copy.reset_empty();
135     if (copy.test_zero_dim_univ()) {
136       return true;
137     }
138     else {
139 #ifndef NDEBUG
140       std::cerr << "The empty flag is incompatible with any other one."
141                 << std::endl;
142 #endif
143       return false;
144     }
145   }
146 
147   // Any other case is OK.
148   return true;
149 }
150 
151 
152 namespace Implementation {
153 
154 namespace Octagonal_Shapes {
155 // These are the keywords that indicate the individual assertions.
156 extern const char* zero_dim_univ;
157 extern const char* empty;
158 extern const char* strong_closed;
159 const char yes = '+';
160 const char no = '-';
161 const char separator = ' ';
162 
163 /*! \relates Parma_Polyhedra_Library::Octagonal_Shape::Status
164   Reads a keyword and its associated on/off flag from \p s.
165   Returns <CODE>true</CODE> if the operation is successful,
166   returns <CODE>false</CODE> otherwise.
167   When successful, \p positive is set to <CODE>true</CODE> if the flag
168   is on; it is set to <CODE>false</CODE> otherwise.
169 */
170 inline bool
get_field(std::istream & s,const char * keyword,bool & positive)171 get_field(std::istream& s, const char* keyword, bool& positive) {
172   std::string str;
173   if (!(s >> str)
174       || (str[0] != yes && str[0] != no)
175       || str.substr(1) != keyword) {
176     return false;
177   }
178   positive = (str[0] == yes);
179   return true;
180 }
181 
182 } // namespace Octagonal_Shapes
183 
184 } // namespace Implementation
185 
186 template <typename T>
187 inline void
ascii_dump(std::ostream & s) const188 Octagonal_Shape<T>::Status::ascii_dump(std::ostream& s) const {
189   using namespace Implementation::Octagonal_Shapes;
190   s << (test_zero_dim_univ() ? yes : no) << zero_dim_univ
191     << separator
192     << (test_empty() ? yes : no) << empty
193     << separator
194     << separator
195     << (test_strongly_closed() ? yes : no) << strong_closed
196     << separator;
197 }
198 
199 template <typename T>
200 inline bool
ascii_load(std::istream & s)201 Octagonal_Shape<T>::Status::ascii_load(std::istream& s) {
202   using namespace Implementation::Octagonal_Shapes;
203   PPL_UNINITIALIZED(bool, positive);
204 
205   if (!get_field(s, zero_dim_univ, positive)) {
206     return false;
207   }
208   if (positive) {
209     set_zero_dim_univ();
210   }
211 
212   if (!get_field(s, empty, positive)) {
213     return false;
214   }
215 
216   if (positive) {
217     set_empty();
218   }
219 
220   if (!get_field(s, strong_closed, positive)) {
221     return false;
222   }
223   if (positive) {
224     set_strongly_closed();
225   }
226   else {
227     reset_strongly_closed();
228   }
229   // Check invariants.
230   PPL_ASSERT(OK());
231   return true;
232 }
233 
234 } // namespace Parma_Polyhedra_Library
235 
236 #endif // !defined(PPL_Og_Status_inlines_hh)
237