22 #ifndef DOXYGEN_SHOULD_SKIP_THIS 25 # include <agrum/tools/variables/discretizedVariable.h> 28 template <
typename T_TICKS >
29 INLINE
void DiscretizedVariable< T_TICKS >::copy_(
const DiscretizedVariable< T_TICKS >& aDRV) {
31 IDiscretizedVariable::copy_(aDRV);
33 for (Idx i = 0; i < aDRV._ticks_size_; ++i) {
34 addTick((T_TICKS)aDRV._ticks_[i]);
38 template <
typename T_TICKS >
39 Idx DiscretizedVariable< T_TICKS >::dichotomy_(
const T_TICKS& target, Idx min, Idx max)
const {
46 mid = (max + min) / 2;
47 T_TICKS val = _ticks_[mid];
51 else if (target < val)
52 res = dichotomy_(target, min, mid);
53 else if (target > val)
54 res = dichotomy_(target, mid, max);
62 template <
typename T_TICKS >
63 INLINE Idx DiscretizedVariable< T_TICKS >::pos_(
const T_TICKS& target)
const {
64 if (_ticks_size_ < 2) { GUM_ERROR(OutOfBounds,
"not enough ticks") }
66 if (target < _ticks_[0]) { GUM_ERROR(OutOfLowerBound,
"less than first range") }
68 if (target > _ticks_[_ticks_size_ - 1]) { GUM_ERROR(OutOfUpperBound,
"more than last range") }
70 if (target == _ticks_[_ticks_size_ - 1])
72 return _ticks_size_ - 2;
74 return dichotomy_(target, 0, _ticks_size_ - 1);
77 template <
typename T_TICKS >
78 INLINE DiscretizedVariable< T_TICKS >::DiscretizedVariable(
const std::string& aName,
79 const std::string& aDesc) :
80 IDiscretizedVariable(aName, aDesc),
81 _ticks_size_((Size)0) {
82 GUM_CONSTRUCTOR(DiscretizedVariable);
86 template <
typename T_TICKS >
87 INLINE DiscretizedVariable< T_TICKS >::DiscretizedVariable(
const std::string& aName,
88 const std::string& aDesc,
89 const std::vector< T_TICKS >& ticks) :
90 IDiscretizedVariable(aName, aDesc),
91 _ticks_size_((Size)0) {
92 GUM_CONSTRUCTOR(DiscretizedVariable);
93 _ticks_.reserve(ticks.size());
94 for (
const auto t: ticks)
98 template <
typename T_TICKS >
99 DiscretizedVariable< T_TICKS >::DiscretizedVariable(
const DiscretizedVariable< T_TICKS >& aDRV) :
100 IDiscretizedVariable(aDRV) {
101 GUM_CONS_CPY(DiscretizedVariable);
106 template <
typename T_TICKS >
107 DiscretizedVariable< T_TICKS >::~DiscretizedVariable() {
108 GUM_DESTRUCTOR(DiscretizedVariable);
111 template <
typename T_TICKS >
112 DiscretizedVariable< T_TICKS >* DiscretizedVariable< T_TICKS >::clone()
const {
113 return new DiscretizedVariable< T_TICKS >(*
this);
116 template <
typename T_TICKS >
117 INLINE DiscretizedVariable< T_TICKS >&
118 DiscretizedVariable< T_TICKS >::operator=(
const DiscretizedVariable< T_TICKS >& aDRV) {
123 template <
typename T_TICKS >
124 INLINE
bool DiscretizedVariable< T_TICKS >::isTick(
const T_TICKS& aTick)
const {
125 if (_ticks_size_ == 0)
return false;
127 if (_ticks_size_ == 1)
return (_ticks_[0] == aTick);
130 Idx zeIdx = pos_(aTick);
132 if (zeIdx != _ticks_size_ - 2)
133 return (_ticks_[zeIdx] == aTick);
135 return ((_ticks_[zeIdx] == aTick) || (_ticks_[zeIdx + 1] == aTick));
136 }
catch (OutOfBounds&) {
return false; }
139 template <
typename T_TICKS >
140 DiscretizedVariable< T_TICKS >& DiscretizedVariable< T_TICKS >::addTick(
const T_TICKS& aTick) {
142 GUM_ERROR(DefaultInLabel,
"Tick '" << aTick <<
"' already used for variable " << name())
145 if (_ticks_size_ == _ticks_.size()) {
146 _ticks_.resize(_ticks_size_ + 1);
149 if (_ticks_size_ == 0) {
151 }
else if (_ticks_size_ == 1) {
152 if (_ticks_[0] < aTick) {
155 _ticks_[1] = _ticks_[0];
160 Idx zeIdx = pos_(aTick);
162 for (Idx i = _ticks_size_ - 1; i > zeIdx; --i) {
163 _ticks_[i + 1] = _ticks_[i];
166 _ticks_[zeIdx + 1] = aTick;
167 }
catch (OutOfUpperBound&) {
168 _ticks_[_ticks_size_] = aTick;
169 }
catch (OutOfLowerBound&) {
170 for (Idx i = _ticks_size_; i >= 1; --i) {
171 _ticks_[i] = _ticks_[i - 1];
183 template <
typename T_TICKS >
184 INLINE
void DiscretizedVariable< T_TICKS >::eraseTicks() {
185 if (_ticks_size_ != 0) { _ticks_size_ = 0; }
188 template <
typename T_TICKS >
189 INLINE std::string DiscretizedVariable< T_TICKS >::label(Idx i)
const {
190 std::stringstream ss;
192 if (i >= _ticks_size_ - 1) { GUM_ERROR(OutOfBounds,
"inexisting label index") }
194 ss <<
"[" << _ticks_[i] <<
";" << _ticks_[i + 1];
196 ss << ((i == _ticks_size_ - 2) ?
"]" :
"[");
202 template <
typename T_TICKS >
203 INLINE
double DiscretizedVariable< T_TICKS >::numerical(Idx indice)
const {
204 if (indice >= _ticks_size_ - 1) { GUM_ERROR(OutOfBounds,
"inexisting label index") }
206 return double((_ticks_[indice + 1] + _ticks_[indice]) / 2);
209 template <
typename T_TICKS >
210 INLINE Idx DiscretizedVariable< T_TICKS >::index(
const std::string& label)
const {
211 if (empty()) { GUM_ERROR(OutOfBounds,
"empty variable : " + toString()) }
213 std::istringstream i(label);
216 if (!(i >> target)) { GUM_ERROR(NotFound,
"Bad label : " << label <<
" for " << *
this) }
225 template <
typename T_TICKS >
226 INLINE Size DiscretizedVariable< T_TICKS >::domainSize()
const {
227 return (_ticks_size_ < 2) ? Size(0) : Size(_ticks_size_ - 1);
230 template <
typename T_TICKS >
231 INLINE VarType DiscretizedVariable< T_TICKS >::varType()
const {
232 return VarType::Discretized;
235 template <
typename T_TICKS >
236 INLINE
const T_TICKS& DiscretizedVariable< T_TICKS >::tick(Idx i)
const {
237 if (i >= _ticks_size_) { GUM_ERROR(OutOfBounds,
"There is no such tick") }
242 template <
typename T_TICKS >
243 const std::string DiscretizedVariable< T_TICKS >::domain()
const {
247 if (domainSize() > 0) {
250 for (Idx i = 1; i < domainSize(); ++i) {
261 template <
typename T_TICKS >
262 INLINE
const std::vector< T_TICKS >& DiscretizedVariable< T_TICKS >::ticks()
const {
263 return this->_ticks_;
266 template <
typename T_TICKS >
267 INLINE std::vector<
double > DiscretizedVariable< T_TICKS >::ticksAsDoubles()
const {
268 const std::size_t size = _ticks_.size();
269 std::vector<
double > ticks(size);
270 for (std::size_t i = std::size_t(0); i < size; ++i)
271 ticks[i] = (
double)_ticks_[i];