1 /* Linear_Form 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_Linear_Form_inlines_hh
25 #define PPL_Linear_Form_inlines_hh 1
26 
27 #include "Variable_defs.hh"
28 #include <iostream>
29 #include <stdexcept>
30 
31 namespace Parma_Polyhedra_Library {
32 
33 template <typename C>
34 inline dimension_type
max_space_dimension()35 Linear_Form<C>::max_space_dimension() {
36   return vec_type().max_size() - 1;
37 }
38 
39 template <typename C>
40 inline
Linear_Form()41 Linear_Form<C>::Linear_Form()
42   : vec(1, zero) {
43   vec.reserve(compute_capacity(1, vec_type().max_size()));
44 }
45 
46 template <typename C>
47 inline
Linear_Form(dimension_type sz,bool)48 Linear_Form<C>::Linear_Form(dimension_type sz, bool)
49   : vec(sz, zero) {
50   vec.reserve(compute_capacity(sz, vec_type().max_size()));
51 }
52 
53 template <typename C>
54 inline
Linear_Form(const Linear_Form & f)55 Linear_Form<C>::Linear_Form(const Linear_Form& f)
56   : vec(f.vec) {
57 }
58 
59 template <typename C>
60 inline
~Linear_Form()61 Linear_Form<C>::~Linear_Form() {
62 }
63 
64 template <typename C>
65 inline dimension_type
size() const66 Linear_Form<C>::size() const {
67   return vec.size();
68 }
69 
70 template <typename C>
71 inline void
extend(dimension_type sz)72 Linear_Form<C>::extend(dimension_type sz) {
73   assert(sz > size());
74   vec.reserve(compute_capacity(sz, vec_type().max_size()));
75   vec.resize(sz, zero);
76 }
77 
78 template <typename C>
79 inline
Linear_Form(const C & n)80 Linear_Form<C>::Linear_Form(const C& n)
81   : vec(1, n) {
82   vec.reserve(compute_capacity(1, vec_type().max_size()));
83 }
84 
85 template <typename C>
86 inline dimension_type
space_dimension() const87 Linear_Form<C>::space_dimension() const {
88   return size() - 1;
89 }
90 
91 template <typename C>
92 inline const C&
coefficient(Variable v) const93 Linear_Form<C>::coefficient(Variable v) const {
94   if (v.space_dimension() > space_dimension()) {
95     return zero;
96   }
97   return vec[v.id()+1];
98 }
99 
100 template <typename C>
101 inline C&
operator [](dimension_type i)102 Linear_Form<C>::operator[](dimension_type i) {
103   assert(i < size());
104   return vec[i];
105 }
106 
107 template <typename C>
108 inline const C&
operator [](dimension_type i) const109 Linear_Form<C>::operator[](dimension_type i) const {
110   assert(i < size());
111   return vec[i];
112 }
113 
114 template <typename C>
115 inline const C&
inhomogeneous_term() const116 Linear_Form<C>::inhomogeneous_term() const {
117   return vec[0];
118 }
119 
120 template <typename C>
121 inline memory_size_type
total_memory_in_bytes() const122 Linear_Form<C>::total_memory_in_bytes() const {
123   return sizeof(*this) + external_memory_in_bytes();
124 }
125 
126 /*! \relates Linear_Form */
127 template <typename C>
128 inline Linear_Form<C>
operator +(const Linear_Form<C> & f)129 operator+(const Linear_Form<C>& f) {
130   return f;
131 }
132 
133 /*! \relates Linear_Form */
134 template <typename C>
135 inline Linear_Form<C>
operator +(const Linear_Form<C> & f,const C & n)136 operator+(const Linear_Form<C>& f, const C& n) {
137   return n + f;
138 }
139 
140 /*! \relates Linear_Form */
141 template <typename C>
142 inline Linear_Form<C>
operator +(const Linear_Form<C> & f,const Variable v)143 operator+(const Linear_Form<C>& f, const Variable v) {
144   return v + f;
145 }
146 
147 /*! \relates Linear_Form */
148 template <typename C>
149 inline Linear_Form<C>
operator -(const Linear_Form<C> & f,const C & n)150 operator-(const Linear_Form<C>& f, const C& n) {
151   return -n + f;
152 }
153 
154 /*! \relates Linear_Form */
155 template <typename C>
156 inline Linear_Form<C>
operator -(const Variable v,const Variable w)157 operator-(const Variable v, const Variable w) {
158   return Linear_Form<C>(v, w);
159 }
160 
161 /*! \relates Linear_Form */
162 template <typename C>
163 inline Linear_Form<C>
operator *(const Linear_Form<C> & f,const C & n)164 operator*(const Linear_Form<C>& f, const C& n) {
165   return n * f;
166 }
167 
168 /*! \relates Linear_Form */
169 template <typename C>
170 inline Linear_Form<C>&
operator +=(Linear_Form<C> & f,const C & n)171 operator+=(Linear_Form<C>& f, const C& n) {
172   f[0] += n;
173   return f;
174 }
175 
176 /*! \relates Linear_Form */
177 template <typename C>
178 inline Linear_Form<C>&
operator -=(Linear_Form<C> & f,const C & n)179 operator-=(Linear_Form<C>& f, const C& n) {
180   f[0] -= n;
181   return f;
182 }
183 
184 /*! \relates Linear_Form */
185 template <typename C>
186 inline bool
operator !=(const Linear_Form<C> & x,const Linear_Form<C> & y)187 operator!=(const Linear_Form<C>& x, const Linear_Form<C>& y) {
188   return !(x == y);
189 }
190 
191 template <typename C>
192 inline void
m_swap(Linear_Form & y)193 Linear_Form<C>::m_swap(Linear_Form& y) {
194   using std::swap;
195   swap(vec, y.vec);
196 }
197 
198 template <typename C>
199 inline void
ascii_dump(std::ostream & s) const200 Linear_Form<C>::ascii_dump(std::ostream& s) const {
201   using namespace IO_Operators;
202   dimension_type space_dim = space_dimension();
203   s << space_dim << "\n";
204   for (dimension_type i = 0; i <= space_dim; ++i) {
205     const char separator = ' ';
206     s << vec[i] << separator;
207   }
208   s << "\n";
209 }
210 
211 template <typename C>
212 inline bool
ascii_load(std::istream & s)213 Linear_Form<C>::ascii_load(std::istream& s) {
214   using namespace IO_Operators;
215   dimension_type new_dim;
216   if (!(s >> new_dim)) {
217     return false;
218   }
219 
220   vec.resize(new_dim + 1, zero);
221   for (dimension_type i = 0; i <= new_dim; ++i) {
222     if (!(s >> vec[i])) {
223       return false;
224     }
225   }
226 
227   PPL_ASSERT(OK());
228   return true;
229 }
230 
231 // Floating point analysis related methods.
232 template <typename C>
233 inline bool
overflows() const234 Linear_Form<C>::overflows() const {
235   if (!inhomogeneous_term().is_bounded()) {
236     return true;
237   }
238   for (dimension_type i = space_dimension(); i-- > 0; ) {
239     if (!coefficient(Variable(i)).is_bounded()) {
240       return true;
241     }
242   }
243 
244   return false;
245 }
246 
247 /*! \relates Linear_Form */
248 template <typename C>
249 inline void
swap(Linear_Form<C> & x,Linear_Form<C> & y)250 swap(Linear_Form<C>& x, Linear_Form<C>& y) {
251   x.m_swap(y);
252 }
253 
254 } // namespace Parma_Polyhedra_Library
255 
256 #endif // !defined(PPL_Linear_Form_inlines_hh)
257