This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/packages/CLPBN/horus/Var.cpp
Tiago Gomes 902624f557 f(void) vs f()
"In fact, the f(void) style has been called an "abomination" by Bjarne Stroustrup, the creator of C++, Dennis Ritchie, the co-creator of C, and Doug McIlroy, head of the research department where Unix was born."
2013-02-28 19:45:37 +00:00

115 lines
1.6 KiB
C++

#include <sstream>
#include "Var.h"
namespace Horus {
std::unordered_map<VarId, VarInfo> Var::varsInfo_;
Var::Var (const Var* v)
{
varId_ = v->varId();
range_ = v->range();
evidence_ = v->getEvidence();
index_ = Util::maxUnsigned();
}
Var::Var (VarId varId, unsigned range, int evidence)
{
assert (range != 0);
assert (evidence < (int) range);
varId_ = varId;
range_ = range;
evidence_ = evidence;
index_ = Util::maxUnsigned();
}
bool
Var::isValidState (int stateIndex)
{
return stateIndex >= 0 && stateIndex < (int) range_;
}
void
Var::setEvidence (int evidence)
{
assert (evidence < (int) range_);
evidence_ = evidence;
}
std::string
Var::label() const
{
if (Var::varsHaveInfo()) {
return Var::getVarInfo (varId_).label;
}
std::stringstream ss;
ss << "x" << varId_;
return ss.str();
}
States
Var::states() const
{
if (Var::varsHaveInfo()) {
return Var::getVarInfo (varId_).states;
}
States states;
for (unsigned i = 0; i < range_; i++) {
std::stringstream ss;
ss << i ;
states.push_back (ss.str());
}
return states;
}
void
Var::addVarInfo (
VarId vid, std::string label, const States& states)
{
assert (Util::contains (varsInfo_, vid) == false);
varsInfo_.insert (std::make_pair (vid, VarInfo (label, states)));
}
VarInfo
Var::getVarInfo (VarId vid)
{
assert (Util::contains (varsInfo_, vid));
return varsInfo_.find (vid)->second;
}
bool
Var::varsHaveInfo()
{
return varsInfo_.empty() == false;
}
void
Var::clearVarsInfo()
{
varsInfo_.clear();
}
} // namespace Horus