aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
gum::RangeVariable Class Reference

Defines a discrete random variable over an integer interval. More...

#include <rangeVariable.h>

+ Inheritance diagram for gum::RangeVariable:
+ Collaboration diagram for gum::RangeVariable:

Public Member Functions

virtual const std::string domain () const
 string represent the domain of the variable More...
 
Idx operator[] (const std::string &label) const
 from the label to its index in var. More...
 
std::string toString () const
 string version of *this More...
 
std::string toStringWithDescription () const
 string version of *this using description attribute instead of name. More...
 
Constructors / Destructors
 RangeVariable (const std::string &aName, const std::string &aDesc, long minVal, long maxVal)
 constructors More...
 
 RangeVariable (const std::string &aName, const std::string &aDesc)
 by de default min=0, max=1 More...
 
 RangeVariable (const RangeVariable &aDRV)
 Copy Constructor. More...
 
virtual ~RangeVariable ()
 destructor More...
 
virtual RangeVariableclone () const
 Copy Factory. More...
 
Accessors / Modifiers
virtual Size domainSize () const
 returns the size of the random discrete variable domain More...
 
virtual VarType varType () const
 returns the type of variable More...
 
virtual std::string label (Idx indice) const
 Get the indice-th label. More...
 
virtual double numerical (Idx indice) const
 get a numerical representation of he indice-the value. More...
 
long minVal () const
 Returns the lower bound. More...
 
void setMinVal (long minVal)
 Set a new value for the lower bound. More...
 
long maxVal () const
 Returns the upper bound. More...
 
void setMaxVal (long maxVal)
 Set a new value of the upper bound. More...
 
bool belongs (long val) const
 Returns true if the param belongs to the variable's interval. More...
 
virtual Idx index (const std::string &) const
 
Operators
RangeVariableoperator= (const RangeVariable &aRV)
 Copy operator. More...
 
Accessors / Modifiers
bool empty () const
 
std::vector< std::string > labels () const
 vector of labels More...
 
Operators
virtual bool operator== (const DiscreteVariable &aRV) const
 equality operator More...
 
virtual bool operator!= (const DiscreteVariable &aRV) const
 inequality operator More...
 
Operators
virtual bool operator== (const Variable &aRV) const
 equality operator More...
 
virtual bool operator!= (const Variable &aRV) const
 inequality operator More...
 
Accessors / Modifiers
void setName (const std::string &theValue)
 sets the name of the variable More...
 
const std::string & name () const
 returns the name of the variable More...
 
void setDescription (const std::string &theValue) const
 sets the description of the variable More...
 
const std::string & description () const
 returns the description of the variable More...
 

Protected Member Functions

void copy_ (const Variable &aRV)
 protected copy More...
 

Detailed Description

Defines a discrete random variable over an integer interval.

It is technically possible to create RangeVariable with minVal > maxVal (or modify in that way an already created RangeVariable). The result is an empty variable (i.e. empty() returns true). If maxVal - minVal < 0, then domainsize() = 0.

Definition at line 53 of file rangeVariable.h.

Constructor & Destructor Documentation

◆ RangeVariable() [1/3]

gum::RangeVariable::RangeVariable ( const std::string &  aName,
const std::string &  aDesc,
long  minVal,
long  maxVal 
)

constructors

Definition at line 40 of file rangeVariable.cpp.

References gum::Set< Key, Alloc >::emplace().

43  :
44  DiscreteVariable(aName, aDesc),
46  GUM_CONSTRUCTOR(RangeVariable);
47  }
long minBound__
The lower bound.
long minVal() const
Returns the lower bound.
long maxBound__
The upper bound.
long maxVal() const
Returns the upper bound.
RangeVariable(const std::string &aName, const std::string &aDesc, long minVal, long maxVal)
constructors
DiscreteVariable()
(protected) Default constructor
+ Here is the call graph for this function:

◆ RangeVariable() [2/3]

gum::RangeVariable::RangeVariable ( const std::string &  aName,
const std::string &  aDesc 
)

by de default min=0, max=1

Definition at line 49 of file rangeVariable.cpp.

References gum::Set< Key, Alloc >::emplace().

50  :
51  DiscreteVariable(aName, aDesc),
52  minBound__(0), maxBound__(1) {
53  GUM_CONSTRUCTOR(RangeVariable);
54  }
long minBound__
The lower bound.
long maxBound__
The upper bound.
RangeVariable(const std::string &aName, const std::string &aDesc, long minVal, long maxVal)
constructors
DiscreteVariable()
(protected) Default constructor
+ Here is the call graph for this function:

◆ RangeVariable() [3/3]

gum::RangeVariable::RangeVariable ( const RangeVariable aDRV)

Copy Constructor.

If aDRV haves any listener, it will not be copied.

Parameters
aDRVthe variable we copy

Definition at line 61 of file rangeVariable.cpp.

References gum::Set< Key, Alloc >::emplace().

61  :
62  DiscreteVariable(aDRV), minBound__(aDRV.minBound__),
63  maxBound__(aDRV.maxBound__) {
64  GUM_CONS_CPY(RangeVariable);
65  }
long minBound__
The lower bound.
long maxBound__
The upper bound.
RangeVariable(const std::string &aName, const std::string &aDesc, long minVal, long maxVal)
constructors
DiscreteVariable()
(protected) Default constructor
+ Here is the call graph for this function:

◆ ~RangeVariable()

gum::RangeVariable::~RangeVariable ( )
virtual

destructor

Definition at line 70 of file rangeVariable.cpp.

References gum::Set< Key, Alloc >::emplace().

70 { GUM_DESTRUCTOR(RangeVariable); }
RangeVariable(const std::string &aName, const std::string &aDesc, long minVal, long maxVal)
constructors
+ Here is the call graph for this function:

Member Function Documentation

◆ belongs()

INLINE bool gum::RangeVariable::belongs ( long  val) const

Returns true if the param belongs to the variable's interval.

Definition at line 94 of file rangeVariable_inl.h.

94  {
95  return ((minBound__ <= val) && (val <= maxBound__));
96  }
long minBound__
The lower bound.
long maxBound__
The upper bound.

◆ clone()

INLINE RangeVariable * gum::RangeVariable::clone ( ) const
virtual

Copy Factory.

Returns
Returns a pointer on a new copy of this.

Implements gum::DiscreteVariable.

Definition at line 37 of file rangeVariable_inl.h.

37  {
38  return new RangeVariable(*this);
39  }
RangeVariable(const std::string &aName, const std::string &aDesc, long minVal, long maxVal)
constructors

◆ copy_()

void gum::Variable::copy_ ( const Variable aRV)
protectedinherited

protected copy

Parameters
aRVto be copied

◆ description()

const std::string& gum::Variable::description ( ) const
inherited

returns the description of the variable

◆ domain()

const std::string gum::RangeVariable::domain ( ) const
virtual

string represent the domain of the variable

Implements gum::DiscreteVariable.

Definition at line 72 of file rangeVariable.cpp.

References gum::Set< Key, Alloc >::emplace().

72  {
73  std::stringstream s;
74  s << "[" << minVal() << "," << maxVal() << "]";
75  return s.str();
76  }
long minVal() const
Returns the lower bound.
long maxVal() const
Returns the upper bound.
+ Here is the call graph for this function:

◆ domainSize()

INLINE Size gum::RangeVariable::domainSize ( ) const
virtual

returns the size of the random discrete variable domain

Warning
: It is technically possible to create RangeVariable with minVal

maxVal

(or modify in that way an already created RangeVariable). The result is an empty variable (i.e. empty() returns true). If maxVal - minVal < 0, then domainsize() = 0.

Implements gum::DiscreteVariable.

Definition at line 42 of file rangeVariable_inl.h.

42  {
43  return (maxBound__ < minBound__) ? Size(0) : Size(maxBound__ + 1 - minBound__);
44  }
long minBound__
The lower bound.
long maxBound__
The upper bound.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

◆ empty()

bool gum::DiscreteVariable::empty ( ) const
inherited
Returns
true if the domainSize() < 2;

◆ index()

INLINE Idx gum::RangeVariable::index ( const std::string &  label) const
virtual
Returns
the modality index from the label
Exceptions
NotFound

Implements gum::DiscreteVariable.

Definition at line 66 of file rangeVariable_inl.h.

66  {
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  }
bool belongs(long val) const
Returns true if the param belongs to the variable&#39;s interval.
virtual std::string label(Idx indice) const
Get the indice-th label.
long minBound__
The lower bound.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

◆ label()

INLINE std::string gum::RangeVariable::label ( Idx  indice) const
virtual

Get the indice-th label.

Parameters
indicethe index of the label we wish to return
Exceptions
OutOfBound

Implements gum::DiscreteVariable.

Definition at line 49 of file rangeVariable_inl.h.

49  {
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  }
bool belongs(long val) const
Returns true if the param belongs to the variable&#39;s interval.
long minBound__
The lower bound.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

◆ labels()

std::vector< std::string > gum::DiscreteVariable::labels ( ) const
inherited

vector of labels

◆ maxVal()

INLINE long gum::RangeVariable::maxVal ( ) const

Returns the upper bound.

Definition at line 88 of file rangeVariable_inl.h.

88 { return maxBound__; }
long maxBound__
The upper bound.

◆ minVal()

INLINE long gum::RangeVariable::minVal ( ) const

Returns the lower bound.

Definition at line 82 of file rangeVariable_inl.h.

82 { return minBound__; }
long minBound__
The lower bound.

◆ name()

const std::string& gum::Variable::name ( ) const
inherited

returns the name of the variable

◆ numerical()

INLINE double gum::RangeVariable::numerical ( Idx  indice) const
virtual

get a numerical representation of he indice-the value.

Implements gum::DiscreteVariable.

Definition at line 61 of file rangeVariable_inl.h.

61  {
62  return double(minBound__ + static_cast< long >(indice));
63  }
long minBound__
The lower bound.

◆ operator!=() [1/2]

virtual bool gum::Variable::operator!= ( const Variable aRV) const
virtualinherited

inequality operator

◆ operator!=() [2/2]

virtual bool gum::DiscreteVariable::operator!= ( const DiscreteVariable aRV) const
virtualinherited

inequality operator

◆ operator=()

INLINE RangeVariable & gum::RangeVariable::operator= ( const RangeVariable aRV)

Copy operator.

Parameters
aRVto be copied
Returns
a ref to *this

Definition at line 101 of file rangeVariable_inl.h.

101  {
102  minBound__ = aRV.minBound__;
103  maxBound__ = aRV.maxBound__;
104  return *this;
105  }
long minBound__
The lower bound.
long maxBound__
The upper bound.

◆ operator==() [1/2]

virtual bool gum::Variable::operator== ( const Variable aRV) const
virtualinherited

equality operator

◆ operator==() [2/2]

virtual bool gum::DiscreteVariable::operator== ( const DiscreteVariable aRV) const
virtualinherited

equality operator

◆ operator[]()

Idx gum::DiscreteVariable::operator[] ( const std::string &  label) const
inlineinherited

from the label to its index in var.

Warning
This operation may have different complexity in different subclasses.
Exceptions
NotFound

Definition at line 139 of file discreteVariable.h.

139 { return index(label); };
virtual std::string label(Idx i) const =0
get the indice-th label. This method is pure virtual.
virtual Idx index(const std::string &label) const =0

◆ setDescription()

void gum::Variable::setDescription ( const std::string &  theValue) const
inherited

sets the description of the variable

Warning
since description__ is mutable, setDescription() is const
Parameters
theValue

◆ setMaxVal()

INLINE void gum::RangeVariable::setMaxVal ( long  maxVal)

Set a new value of the upper bound.

Definition at line 91 of file rangeVariable_inl.h.

91 { maxBound__ = maxVal; }
long maxBound__
The upper bound.
long maxVal() const
Returns the upper bound.

◆ setMinVal()

INLINE void gum::RangeVariable::setMinVal ( long  minVal)

Set a new value for the lower bound.

Definition at line 85 of file rangeVariable_inl.h.

85 { minBound__ = minVal; }
long minBound__
The lower bound.
long minVal() const
Returns the lower bound.

◆ setName()

void gum::Variable::setName ( const std::string &  theValue)
inherited

sets the name of the variable

Parameters
theValue

◆ toString()

std::string gum::DiscreteVariable::toString ( ) const
inherited

string version of *this

◆ toStringWithDescription()

std::string gum::DiscreteVariable::toStringWithDescription ( ) const
inherited

string version of *this using description attribute instead of name.

◆ varType()

INLINE VarType gum::RangeVariable::varType ( ) const
virtual

returns the type of variable

Implements gum::DiscreteVariable.

Definition at line 107 of file rangeVariable_inl.h.

Member Data Documentation

◆ maxBound__

long gum::RangeVariable::maxBound__
private

The upper bound.

Definition at line 163 of file rangeVariable.h.

◆ minBound__

long gum::RangeVariable::minBound__
private

The lower bound.

Definition at line 160 of file rangeVariable.h.


The documentation for this class was generated from the following files: