1 /* Partial_Function 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_Partial_Function_inlines_hh
25 #define PPL_Partial_Function_inlines_hh 1
26 
27 #include "assertions.hh"
28 #include <stdexcept>
29 
30 namespace Parma_Polyhedra_Library {
31 
32 inline
Partial_Function()33 Partial_Function::Partial_Function()
34   : max(0) {
35 }
36 
37 inline bool
has_empty_codomain() const38 Partial_Function::has_empty_codomain() const {
39   PPL_ASSERT(vec.empty() == codomain.empty());
40   return vec.empty();
41 }
42 
43 inline dimension_type
max_in_codomain() const44 Partial_Function::max_in_codomain() const {
45   if (has_empty_codomain()) {
46     throw std::runtime_error("Partial_Function::max_in_codomain() called"
47                              " when has_empty_codomain()");
48   }
49   PPL_ASSERT(codomain.begin() != codomain.end()
50              && max == *codomain.rbegin());
51   return max;
52 }
53 
54 inline void
insert(dimension_type i,dimension_type j)55 Partial_Function::insert(dimension_type i, dimension_type j) {
56 #ifndef NDEBUG
57   // The partial function has to be an injective map.
58   std::pair<std::set<dimension_type>::iterator, bool> s = codomain.insert(j);
59   PPL_ASSERT(s.second);
60 #endif // #ifndef NDEBUG
61 
62   // Expand `vec' if needed.
63   const dimension_type sz = vec.size();
64   if (i >= sz) {
65     vec.insert(vec.end(), i - sz + 1, not_a_dimension());
66   }
67 
68   // We cannot remap the same index to another one.
69   PPL_ASSERT(i < vec.size() && vec[i] == not_a_dimension());
70   vec[i] = j;
71 
72   // Maybe update `max'.
73   if (j > max) {
74     max = j;
75   }
76   PPL_ASSERT(codomain.begin() != codomain.end()
77              && max == *codomain.rbegin());
78 }
79 
80 inline bool
maps(dimension_type i,dimension_type & j) const81 Partial_Function::maps(dimension_type i, dimension_type& j) const {
82   if (i >= vec.size()) {
83     return false;
84   }
85   const dimension_type vec_i = vec[i];
86   if (vec_i == not_a_dimension()) {
87     return false;
88   }
89   j = vec_i;
90   return true;
91 }
92 
93 } // namespace Parma_Polyhedra_Library
94 
95 #endif // !defined(PPL_Partial_Function_inlines_hh)
96