aGrUM  0.21.0
a C++ library for (probabilistic) graphical models
gum::IntegerVariable Class Reference

class IntegerVariable More...

#include <integerVariable.h>

+ Inheritance diagram for gum::IntegerVariable:
+ Collaboration diagram for gum::IntegerVariable:

Public Member Functions

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
 IntegerVariable (const std::string &aName, const std::string &aDesc="")
 constructor More...
 
 IntegerVariable (const std::string &aName, const std::string &aDesc, const std::vector< int > &domain)
 constructor assigning a domain to the variable More...
 
 IntegerVariable (const IntegerVariable &from)
 Copy constructor. More...
 
 IntegerVariable (IntegerVariable &&from)
 move constructor More...
 
virtual IntegerVariableclone () const
 virtual copy constructor More...
 
virtual ~IntegerVariable ()
 destructor More...
 
Operators
IntegerVariableoperator= (const IntegerVariable &from)
 copy operator More...
 
IntegerVariableoperator= (IntegerVariable &&from)
 move operator More...
 
virtual bool operator== (const Variable &var) const
 equality operator More...
 
virtual bool operator!= (const Variable &var) const
 inequality operator More...
 
Accessors / Modifiers
virtual Size domainSize () const
 returns the domain size of the discrete random variable More...
 
virtual VarType varType () const
 returns the type of variable More...
 
virtual Idx index (const std::string &label) const
 returns the index of a given label More...
 
virtual std::string label (Idx index) const
 returns a string corresponding to the ith value of the domain More...
 
virtual double numerical (Idx index) const
 get a integer representation of the value at a given index More...
 
virtual const std::string domain () const
 Returns the domain as a string. More...
 
const Sequence< int > & integerDomain () const
 returns the domain as a sequence of values More...
 
IntegerVariableaddValue (int value)
 add a new value to the domain size More...
 
void changeValue (int old_value, int new_value)
 substitute a value by another one More...
 
void eraseValue (int value)
 erase a value from the domain of the variable More...
 
void eraseValues ()
 clear the domain of the variable 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...
 
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

class IntegerVariable

The class representing discrete integer random variables

Definition at line 47 of file integerVariable.h.

Constructor & Destructor Documentation

◆ IntegerVariable() [1/4]

gum::IntegerVariable::IntegerVariable ( const std::string &  aName,
const std::string &  aDesc = "" 
)

constructor

Parameters
aNamethe name of the variable
aDescthe Description of the variable, if any

◆ IntegerVariable() [2/4]

gum::IntegerVariable::IntegerVariable ( const std::string &  aName,
const std::string &  aDesc,
const std::vector< int > &  domain 
)

constructor assigning a domain to the variable

Parameters
aNamethe name of the variable
aDescthe Description of the variable, if any
domainthe domain (set of values) of the variable

Definition at line 31 of file integerVariable.cpp.

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

33  :
34  DiscreteVariable(aName, aDesc) {
35  // get the values in increasing order
36  std::vector< int > dom = domain;
37  std::sort(dom.begin(), dom.end());
38 
39  // store the sorted values into a sequence
40  _domain_.resize(dom.size());
41  for (const int val : dom) {
42  _domain_ << val;
43  }
44 
45  // for debugging purposes
46  GUM_CONSTRUCTOR(IntegerVariable);
47  }
void resize(Size new_size)
Modifies the size of the internal structures of the sequence.
Definition: sequence_tpl.h:659
virtual const std::string domain() const
Returns the domain as a string.
Sequence< int > _domain_
the domain of the variable
DiscreteVariable()
(protected) Default constructor
IntegerVariable(const std::string &aName, const std::string &aDesc="")
constructor
+ Here is the call graph for this function:

◆ IntegerVariable() [3/4]

gum::IntegerVariable::IntegerVariable ( const IntegerVariable from)

Copy constructor.

Parameters
fromthe variable we copy

◆ IntegerVariable() [4/4]

gum::IntegerVariable::IntegerVariable ( IntegerVariable &&  from)

move constructor

◆ ~IntegerVariable()

virtual gum::IntegerVariable::~IntegerVariable ( )
virtual

destructor

Member Function Documentation

◆ addValue()

IntegerVariable & gum::IntegerVariable::addValue ( int  value)

add a new value to the domain size

Exceptions
DuplicateElementis raised if the variable already contains the value
Returns
*this which allows : v.addValue(1).addValue(2)...;

Definition at line 82 of file integerVariable.cpp.

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

82  {
83  const Size size = _domain_.size();
84  if (size == Size(0) || (_domain_[size - 1] < value)) {
85  _domain_.insert(value);
86  } else {
87  // here, the value must not be inserted at the end of the sequence.
88  // it is faster to reconstruct the sequence from scratch
89  std::vector< int > values;
90  values.reserve(_domain_.size() + 1);
91  for (const auto val: _domain_)
92  values.push_back(val);
93  values.push_back(value);
94  std::sort(values.begin(), values.end());
95 
96  Sequence< int > new_domain(_domain_.size() + 1);
97  for (const auto val: values)
98  new_domain.insert(val);
99  _domain_ = std::move(new_domain);
100  }
101 
102  return *this;
103  }
Size size() const noexcept
Returns the size of the sequence.
Definition: sequence_tpl.h:37
Sequence< int > _domain_
the domain of the variable
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
void insert(const Key &k)
Insert an element at the end of the sequence.
Definition: sequence_tpl.h:393
+ Here is the call graph for this function:

◆ changeValue()

void gum::IntegerVariable::changeValue ( int  old_value,
int  new_value 
)

substitute a value by another one

◆ clone()

virtual IntegerVariable* gum::IntegerVariable::clone ( ) const
virtual

virtual copy constructor

Implements gum::DiscreteVariable.

◆ 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::IntegerVariable::domain ( ) const
virtual

Returns the domain as a string.

Implements gum::DiscreteVariable.

Definition at line 62 of file integerVariable.cpp.

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

62  {
63  std::stringstream s;
64  s << "<";
65 
66  const Size size = domainSize();
67  if (size > 0) {
68  s << _domain_[0];
69 
70  for (Idx i = 1; i < size; ++i) {
71  s << ',' << _domain_[i];
72  }
73  }
74 
75  s << ">";
76 
77  return s.str();
78  }
Sequence< int > _domain_
the domain of the variable
virtual Size domainSize() const
returns the domain size of the discrete random variable
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
+ Here is the call graph for this function:

◆ domainSize()

virtual Size gum::IntegerVariable::domainSize ( ) const
virtual

returns the domain size of the discrete random variable

Implements gum::DiscreteVariable.

◆ empty()

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

◆ eraseValue()

void gum::IntegerVariable::eraseValue ( int  value)

erase a value from the domain of the variable

◆ eraseValues()

void gum::IntegerVariable::eraseValues ( )

clear the domain of the variable

◆ index()

virtual Idx gum::IntegerVariable::index ( const std::string &  label) const
virtual

returns the index of a given label

Parameters
labelsearched label
Returns
the index of this label
Exceptions
NotFound

Implements gum::DiscreteVariable.

◆ integerDomain()

const Sequence< int >& gum::IntegerVariable::integerDomain ( ) const

returns the domain as a sequence of values

◆ label()

virtual std::string gum::IntegerVariable::label ( Idx  index) const
virtual

returns a string corresponding to the ith value of the domain

Implements gum::DiscreteVariable.

◆ labels()

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

vector of labels

◆ name()

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

returns the name of the variable

◆ numerical()

virtual double gum::IntegerVariable::numerical ( Idx  index) const
virtual

get a integer representation of the value at a given index

Implements gum::DiscreteVariable.

◆ operator!=() [1/2]

virtual bool gum::IntegerVariable::operator!= ( const Variable var) const
virtual

inequality operator

Reimplemented from gum::Variable.

◆ operator!=() [2/2]

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

inequality operator

◆ operator=() [1/2]

IntegerVariable& gum::IntegerVariable::operator= ( const IntegerVariable from)

copy operator

Parameters
fromthe integer discrete random variable we copy

◆ operator=() [2/2]

IntegerVariable& gum::IntegerVariable::operator= ( IntegerVariable &&  from)

move operator

Parameters
fromthe integer discrete random variable we copy

◆ operator==() [1/2]

bool gum::IntegerVariable::operator== ( const Variable var) const
virtual

equality operator

Reimplemented from gum::Variable.

Definition at line 51 of file integerVariable.cpp.

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

51  {
52  try {
53  const IntegerVariable& xvar = dynamic_cast<const IntegerVariable&>(var);
54  return Variable::operator==(var) && (xvar._domain_ == _domain_);
55  } catch (std::bad_cast&) {
56  return false;
57  }
58  }
Sequence< int > _domain_
the domain of the variable
virtual bool operator==(const Variable &aRV) const
equality operator
IntegerVariable(const std::string &aName, const std::string &aDesc="")
constructor
+ Here is the call graph for this function:

◆ 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

◆ 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()

virtual VarType gum::IntegerVariable::varType ( ) const
virtual

returns the type of variable

Implements gum::DiscreteVariable.

Member Data Documentation

◆ _domain_

Sequence< int > gum::IntegerVariable::_domain_
private

the domain of the variable

Definition at line 158 of file integerVariable.h.


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