aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
rangeVariable_inl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by 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 { return new RangeVariable(*this); }
38 
39  // returns the size of the random discrete variable domain
40  INLINE Size RangeVariable::domainSize() const {
41  return (_maxBound_ < _minBound_) ? Size(0) : Size(_maxBound_ + 1 - _minBound_);
42  }
43 
44  // Get the indice-th label. This method is pure virtual.
45  // @param indice the index of the label we wish to return
46  // @throw OutOfBound
47  INLINE std::string RangeVariable::label(Idx indice) const {
48  long target = static_cast< long >(indice) + _minBound_;
49  if (belongs(target)) {
50  std::stringstream strBuff;
51  strBuff << target;
52  return strBuff.str();
53  } else {
54  GUM_ERROR(OutOfBounds, "Indice out of bounds.")
55  }
56  }
57 
58  INLINE
59  double RangeVariable::numerical(Idx indice) const {
60  return double(_minBound_ + static_cast< long >(indice));
61  }
62 
63 
64  INLINE Idx RangeVariable::index(const std::string& label) const {
65  std::istringstream i(label);
66  long target;
67 
68  if (!(i >> target)) { GUM_ERROR(NotFound, "Bad label : " << label << " for " << *this) }
69 
70  if (!belongs(target)) { GUM_ERROR(NotFound, "Bad label : " << label << " for " << *this) }
71 
72  return static_cast< Idx >(target - _minBound_);
73  }
74 
75  // Returns the lower bound.
76  INLINE long RangeVariable::minVal() const { return _minBound_; }
77 
78  // Set a new value for the lower bound.
79  INLINE void RangeVariable::setMinVal(long minVal) { _minBound_ = minVal; }
80 
81  // Returns the upper bound.
82  INLINE long RangeVariable::maxVal() const { return _maxBound_; }
83 
84  // Set a new value of the upper bound.
85  INLINE void RangeVariable::setMaxVal(long maxVal) { _maxBound_ = maxVal; }
86 
87  // Returns true if the param belongs to the variable's interval.
88  INLINE bool RangeVariable::belongs(long val) const {
89  return ((_minBound_ <= val) && (val <= _maxBound_));
90  }
91 
92  // Copy operator
93  // @param aRV to be copied
94  // @return a ref to *this
95  INLINE RangeVariable& RangeVariable::operator=(const RangeVariable& aRV) {
96  _minBound_ = aRV._minBound_;
97  _maxBound_ = aRV._maxBound_;
98  return *this;
99  }
100 
101  INLINE VarType RangeVariable::varType() const { return VarType::Range; }
102 
103 } /* namespace gum */