aGrUM  0.21.0
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 index) const
 Get the index-th label. More...
 
virtual double numerical (Idx index) const
 get a numerical representation of the index-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 _maxBound_
The upper bound.
long _minBound_
The lower bound.
long minVal() const
Returns the lower 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().

49  :
50  DiscreteVariable(aName, aDesc), _minBound_(0), _maxBound_(1) {
51  GUM_CONSTRUCTOR(RangeVariable);
52  }
long _maxBound_
The upper bound.
long _minBound_
The lower 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 59 of file rangeVariable.cpp.

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

59  :
60  DiscreteVariable(aDRV), _minBound_(aDRV._minBound_), _maxBound_(aDRV._maxBound_) {
61  GUM_CONS_CPY(RangeVariable);
62  }
long _maxBound_
The upper bound.
long _minBound_
The lower 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 67 of file rangeVariable.cpp.

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

67  {
68  GUM_DESTRUCTOR(RangeVariable);
69  ;
70  }
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 88 of file rangeVariable_inl.h.

88  {
89  return ((_minBound_ <= val) && (val <= _maxBound_));
90  }
long _maxBound_
The upper bound.
long _minBound_
The lower 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 { return new RangeVariable(*this); }
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 40 of file rangeVariable_inl.h.

40  {
41  return (_maxBound_ < _minBound_) ? Size(0) : Size(_maxBound_ + 1 - _minBound_);
42  }
long _maxBound_
The upper bound.
long _minBound_
The lower 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 64 of file rangeVariable_inl.h.

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

◆ label()

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

Get the index-th label.

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

Implements gum::DiscreteVariable.

Definition at line 47 of file rangeVariable_inl.h.

47  {
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  }
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:51

◆ 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 82 of file rangeVariable_inl.h.

82 { return _maxBound_; }
long _maxBound_
The upper bound.

◆ minVal()

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

Returns the lower bound.

Definition at line 76 of file rangeVariable_inl.h.

76 { 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  index) const
virtual

get a numerical representation of the index-the value.

Implements gum::DiscreteVariable.

Definition at line 59 of file rangeVariable_inl.h.

59  {
60  return double(_minBound_ + static_cast< long >(indice));
61  }
long _minBound_
The lower bound.

◆ operator!=() [1/2]

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

inequality operator

Reimplemented in gum::IntegerVariable.

◆ 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 95 of file rangeVariable_inl.h.

95  {
96  _minBound_ = aRV._minBound_;
97  _maxBound_ = aRV._maxBound_;
98  return *this;
99  }
long _maxBound_
The upper bound.
long _minBound_
The lower bound.

◆ operator==() [1/2]

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

equality operator

Reimplemented in gum::IntegerVariable.

◆ 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 85 of file rangeVariable_inl.h.

85 { _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 79 of file rangeVariable_inl.h.

79 { _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 101 of file rangeVariable_inl.h.

Member Data Documentation

◆ _maxBound_

long gum::RangeVariable::_maxBound_
private

The upper bound.

Definition at line 160 of file rangeVariable.h.

◆ _minBound_

long gum::RangeVariable::_minBound_
private

The lower bound.

Definition at line 157 of file rangeVariable.h.


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