aGrUM  0.16.0
rangeVariable_inl.h
Go to the documentation of this file.
1 
29 #include <sstream>
30 
31 // to ease IDE parsing
33 
34 namespace gum {
35 
36  // Copy Factory.
37  // @return Returns a pointer on a new copy of this.
39  return new RangeVariable(*this);
40  }
41 
42  // returns the size of the random discrete variable domain
44  return (__maxBound < __minBound) ? Size(0) : Size(__maxBound + 1 - __minBound);
45  }
46 
47  // Get the indice-th label. This method is pure virtual.
48  // @param indice the index of the label we wish to return
49  // @throw OutOfBound
50  INLINE std::string RangeVariable::label(Idx indice) const {
51  long target = static_cast< long >(indice) + __minBound;
52  if (belongs(target)) {
53  std::stringstream strBuff;
54  strBuff << target;
55  return strBuff.str();
56  } else {
57  GUM_ERROR(OutOfBounds, "Indice out of bounds.");
58  }
59  }
60 
61  INLINE
62  double RangeVariable::numerical(Idx indice) const {
63  return double(__minBound + static_cast< long >(indice));
64  }
65 
66 
67  INLINE Idx RangeVariable::index(const std::string& label) const {
68  std::istringstream i(label);
69  long target;
70 
71  if (!(i >> target)) {
72  GUM_ERROR(NotFound, "Bad label : " << label << " for " << *this);
73  }
74 
75  if (!belongs(target)) {
76  GUM_ERROR(NotFound, "Bad label : " << label << " for " << *this);
77  }
78 
79  return static_cast< Idx >(target - __minBound);
80  }
81 
82  // Returns the lower bound.
83  INLINE long RangeVariable::minVal() const { return __minBound; }
84 
85  // Set a new value for the lower bound.
87 
88  // Returns the upper bound.
89  INLINE long RangeVariable::maxVal() const { return __maxBound; }
90 
91  // Set a new value of the upper bound.
93 
94  // Returns true if the param belongs to the variable's interval.
95  INLINE bool RangeVariable::belongs(long val) const {
96  return ((__minBound <= val) && (val <= __maxBound));
97  }
98 
99  // Copy operator
100  // @param aRV to be copied
101  // @return a ref to *this
103  __minBound = aRV.__minBound;
104  __maxBound = aRV.__maxBound;
105  return *this;
106  }
107 
108  INLINE VarType RangeVariable::varType() const { return VarType::Range; }
109 
110 } /* namespace gum */
bool belongs(long val) const
Returns true if the param belongs to the variable&#39;s interval.
long __maxBound
The upper bound.
virtual Idx index(const std::string &) const
virtual std::string label(Idx indice) const
Get the indice-th label.
RangeVariable & operator=(const RangeVariable &aRV)
Copy operator.
virtual Size domainSize() const
returns the size of the random discrete variable domain
virtual double numerical(Idx indice) const
get a numerical representation of he indice-the value.
long minVal() const
Returns the lower bound.
void setMinVal(long minVal)
Set a new value for the lower bound.
Base class for discrete random variable.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
virtual VarType varType() const
returns the type of variable
virtual DiscreteVariable * clone() const
Copy Factory.
VarType
Definition: variable.h:41
void setMaxVal(long maxVal)
Set a new value of the upper bound.
long maxVal() const
Returns the upper bound.
Defines a discrete random variable over an integer interval.
Definition: rangeVariable.h:54
RangeVariable(const std::string &aName, const std::string &aDesc, long minVal, long maxVal)
constructors
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Size Idx
Type for indexes.
Definition: types.h:53
long __minBound
The lower bound.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55