aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
rangeVariable_inl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Inline implementation of gumRangeVariable.
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
27  */
28 #include <sstream>
29 
30 // to ease IDE parsing
31 #include <agrum/tools/variables/rangeVariable.h>
32 
33 namespace gum {
34 
35  // Copy Factory.
36  // @return Returns a pointer on a new copy of this.
37  INLINE RangeVariable* RangeVariable::clone() const {
38  return new RangeVariable(*this);
39  }
40 
41  // returns the size of the random discrete variable domain
42  INLINE Size RangeVariable::domainSize() const {
43  return (maxBound__ < minBound__) ? Size(0) : Size(maxBound__ + 1 - minBound__);
44  }
45 
46  // Get the indice-th label. This method is pure virtual.
47  // @param indice the index of the label we wish to return
48  // @throw OutOfBound
49  INLINE std::string RangeVariable::label(Idx indice) const {
50  long target = static_cast< long >(indice) + minBound__;
51  if (belongs(target)) {
52  std::stringstream strBuff;
53  strBuff << target;
54  return strBuff.str();
55  } else {
56  GUM_ERROR(OutOfBounds, "Indice out of bounds.");
57  }
58  }
59 
60  INLINE
61  double RangeVariable::numerical(Idx indice) const {
62  return double(minBound__ + static_cast< long >(indice));
63  }
64 
65 
66  INLINE Idx RangeVariable::index(const std::string& label) const {
67  std::istringstream i(label);
68  long target;
69 
70  if (!(i >> target)) {
71  GUM_ERROR(NotFound, "Bad label : " << label << " for " << *this);
72  }
73 
74  if (!belongs(target)) {
75  GUM_ERROR(NotFound, "Bad label : " << label << " for " << *this);
76  }
77 
78  return static_cast< Idx >(target - minBound__);
79  }
80 
81  // Returns the lower bound.
82  INLINE long RangeVariable::minVal() const { return minBound__; }
83 
84  // Set a new value for the lower bound.
85  INLINE void RangeVariable::setMinVal(long minVal) { minBound__ = minVal; }
86 
87  // Returns the upper bound.
88  INLINE long RangeVariable::maxVal() const { return maxBound__; }
89 
90  // Set a new value of the upper bound.
91  INLINE void RangeVariable::setMaxVal(long maxVal) { maxBound__ = maxVal; }
92 
93  // Returns true if the param belongs to the variable's interval.
94  INLINE bool RangeVariable::belongs(long val) const {
95  return ((minBound__ <= val) && (val <= maxBound__));
96  }
97 
98  // Copy operator
99  // @param aRV to be copied
100  // @return a ref to *this
101  INLINE RangeVariable& RangeVariable::operator=(const RangeVariable& aRV) {
102  minBound__ = aRV.minBound__;
103  maxBound__ = aRV.maxBound__;
104  return *this;
105  }
106 
107  INLINE VarType RangeVariable::varType() const { return VarType::Range; }
108 
109 } /* namespace gum */