aGrUM  0.14.2
rangeVariable_inl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Christophe GONZALES *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
26 #include <sstream>
27 
28 // to ease IDE parsing
30 
31 namespace gum {
32 
33  // Copy Factory.
34  // @return Returns a pointer on a new copy of this.
36  return new RangeVariable(*this);
37  }
38 
39  // returns the size of the random discrete variable domain
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)) {
69  GUM_ERROR(NotFound, "Bad label : " << label << " for " << *this);
70  }
71 
72  if (!belongs(target)) {
73  GUM_ERROR(NotFound, "Bad label : " << label << " for " << *this);
74  }
75 
76  return static_cast< Idx >(target - __minBound);
77  }
78 
79  // Returns the lower bound.
80  INLINE long RangeVariable::minVal() const { return __minBound; }
81 
82  // Set a new value for the lower bound.
84 
85  // Returns the upper bound.
86  INLINE long RangeVariable::maxVal() const { return __maxBound; }
87 
88  // Set a new value of the upper bound.
90 
91  // Returns true if the param belongs to the variable's interval.
92  INLINE bool RangeVariable::belongs(long val) const {
93  return ((__minBound <= val) && (val <= __maxBound));
94  }
95 
96  // Copy operator
97  // @param aRV to be copied
98  // @return a ref to *this
100  __minBound = aRV.__minBound;
101  __maxBound = aRV.__maxBound;
102  return *this;
103  }
104 
105  INLINE VarType RangeVariable::varType() const { return VarType::Range; }
106 
107 } /* 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.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
virtual VarType varType() const
returns the type of variable
virtual DiscreteVariable * clone() const
Copy Factory.
VarType
Definition: variable.h:38
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:51
RangeVariable(const std::string &aName, const std::string &aDesc, long minVal=0, long maxVal=1)
Default constructor.
Header of gumRangeVariable.
Size Idx
Type for indexes.
Definition: types.h:50
long __minBound
The lower bound.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52