1 /* Variable class declaration.
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_Variable_defs_hh
25 #define PPL_Variable_defs_hh 1
26 
27 #include "Variable_types.hh"
28 #include "Init_types.hh"
29 #include "globals_types.hh"
30 #include <iosfwd>
31 #include <set>
32 
33 namespace Parma_Polyhedra_Library {
34 
35 namespace IO_Operators {
36 
37 //! Output operator.
38 /*! \relates Parma_Polyhedra_Library::Variable */
39 std::ostream&
40 operator<<(std::ostream& s, const Variable v);
41 
42 } // namespace IO_Operators
43 
44 //! Defines a total ordering on variables.
45 /*! \relates Variable */
46 bool less(Variable v, Variable w);
47 
48 /*! \relates Variable */
49 void
50 swap(Variable& x, Variable& y);
51 
52 } // namespace Parma_Polyhedra_Library
53 
54 //! A dimension of the vector space.
55 /*! \ingroup PPL_CXX_interface
56   An object of the class Variable represents a dimension of the space,
57   that is one of the Cartesian axes.
58   Variables are used as basic blocks in order to build
59   more complex linear expressions.
60   Each variable is identified by a non-negative integer,
61   representing the index of the corresponding Cartesian axis
62   (the first axis has index 0).
63   The space dimension of a variable is the dimension of the vector space
64   made by all the Cartesian axes having an index less than or equal to
65   that of the considered variable; thus, if a variable has index \f$i\f$,
66   its space dimension is \f$i+1\f$.
67 
68   Note that the ``meaning'' of an object of the class Variable
69   is completely specified by the integer index provided to its
70   constructor:
71   be careful not to be mislead by C++ language variable names.
72   For instance, in the following example the linear expressions
73   <CODE>e1</CODE> and <CODE>e2</CODE> are equivalent,
74   since the two variables <CODE>x</CODE> and <CODE>z</CODE> denote
75   the same Cartesian axis.
76   \code
77   Variable x(0);
78   Variable y(1);
79   Variable z(0);
80   Linear_Expression e1 = x + y;
81   Linear_Expression e2 = y + z;
82   \endcode
83 
84 */
85 class Parma_Polyhedra_Library::Variable {
86 
87 public:
88   //! Builds the variable corresponding to the Cartesian axis of index \p i.
89   /*!
90     \exception std::length_error
91     Thrown if <CODE>i+1</CODE> exceeds
92     <CODE>Variable::max_space_dimension()</CODE>.
93   */
94   explicit Variable(dimension_type i);
95 
96   //! Returns the index of the Cartesian axis associated to the variable.
97   dimension_type id() const;
98 
99   //! Returns the maximum space dimension a Variable can handle.
100   static dimension_type max_space_dimension();
101 
102   //! Returns the dimension of the vector space enclosing \p *this.
103   /*!
104     The returned value is <CODE>id()+1</CODE>.
105   */
106   dimension_type space_dimension() const;
107 
108   //! Returns the total size in bytes of the memory occupied by \p *this.
109   memory_size_type total_memory_in_bytes() const;
110 
111   //! Returns the size in bytes of the memory managed by \p *this.
112   memory_size_type external_memory_in_bytes() const;
113 
114   //! Checks if all the invariants are satisfied.
115   bool OK() const;
116 
117   //! Type of output functions.
118   typedef void output_function_type(std::ostream& s, const Variable v);
119 
120   //! The default output function.
121   static void default_output_function(std::ostream& s, const Variable v);
122 
123   //! Sets the output function to be used for printing Variable objects.
124   static void set_output_function(output_function_type* p);
125 
126   //! Returns the pointer to the current output function.
127   static output_function_type* get_output_function();
128 
129   //! Binary predicate defining the total ordering on variables.
130   /*! \ingroup PPL_CXX_interface */
131   struct Compare {
132     //! Returns <CODE>true</CODE> if and only if \p x comes before \p y.
133     bool operator()(Variable x, Variable y) const;
134   };
135 
136   //! Swaps *this and v.
137   void m_swap(Variable& v);
138 
139 private:
140   //! The index of the Cartesian axis.
141   dimension_type varid;
142 
143   // The initialization class needs to set the default output function.
144   friend class Init;
145 
146   friend std::ostream&
147   Parma_Polyhedra_Library::IO_Operators::operator<<(std::ostream& s,
148                                                     const Variable v);
149 
150   //! Pointer to the current output function.
151   static output_function_type* current_output_function;
152 
153 };
154 
155 #include "Variable_inlines.hh"
156 
157 #endif // !defined(PPL_Variable_defs_hh)
158