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_(
30 const DiscretizedVariable< T_TICKS >& aDRV) {
32 IDiscretizedVariable::copy_(aDRV);
34 for (Idx i = 0; i < aDRV.ticks_size__; ++i) {
35 addTick((T_TICKS)aDRV.ticks__[i]);
39 template <
typename T_TICKS >
40 Idx DiscretizedVariable< T_TICKS >::dichotomy_(
const T_TICKS& target,
49 mid = (max + min) / 2;
50 T_TICKS val = ticks__[mid];
54 else if (target < val)
55 res = dichotomy_(target, min, mid);
56 else if (target > val)
57 res = dichotomy_(target, mid, max);
65 template <
typename T_TICKS >
66 INLINE Idx DiscretizedVariable< T_TICKS >::pos_(
const T_TICKS& target)
const {
67 if (ticks_size__ < 2) { GUM_ERROR(OutOfBounds,
"not enough ticks"); }
69 if (target < ticks__[0]) {
70 GUM_ERROR(OutOfLowerBound,
"less than first range");
73 if (target > ticks__[ticks_size__ - 1]) {
74 GUM_ERROR(OutOfUpperBound,
"more than last range");
77 if (target == ticks__[ticks_size__ - 1])
79 return ticks_size__ - 2;
81 return dichotomy_(target, 0, ticks_size__ - 1);
84 template <
typename T_TICKS >
85 INLINE DiscretizedVariable< T_TICKS >::DiscretizedVariable(
86 const std::string& aName,
87 const std::string& aDesc) :
88 IDiscretizedVariable(aName, aDesc),
89 ticks_size__((Size)0) {
90 GUM_CONSTRUCTOR(DiscretizedVariable);
94 template <
typename T_TICKS >
95 INLINE DiscretizedVariable< T_TICKS >::DiscretizedVariable(
96 const std::string& aName,
97 const std::string& aDesc,
98 const std::vector< T_TICKS >& ticks) :
99 IDiscretizedVariable(aName, aDesc),
100 ticks_size__((Size)0) {
101 GUM_CONSTRUCTOR(DiscretizedVariable);
102 ticks__.reserve(ticks.size());
103 for (
const auto t: ticks)
107 template <
typename T_TICKS >
108 DiscretizedVariable< T_TICKS >::DiscretizedVariable(
109 const DiscretizedVariable< T_TICKS >& aDRV) :
110 IDiscretizedVariable(aDRV) {
111 GUM_CONS_CPY(DiscretizedVariable);
116 template <
typename T_TICKS >
117 DiscretizedVariable< T_TICKS >::~DiscretizedVariable() {
118 GUM_DESTRUCTOR(DiscretizedVariable);
121 template <
typename T_TICKS >
122 DiscretizedVariable< T_TICKS >* DiscretizedVariable< T_TICKS >::clone()
const {
123 return new DiscretizedVariable< T_TICKS >(*
this);
126 template <
typename T_TICKS >
127 INLINE DiscretizedVariable< T_TICKS >& DiscretizedVariable< T_TICKS >::operator=(
128 const DiscretizedVariable< T_TICKS >& aDRV) {
133 template <
typename T_TICKS >
134 INLINE
bool DiscretizedVariable< T_TICKS >::isTick(
const T_TICKS& aTick)
const {
135 if (ticks_size__ == 0)
return false;
137 if (ticks_size__ == 1)
return (ticks__[0] == aTick);
140 Idx zeIdx = pos_(aTick);
142 if (zeIdx != ticks_size__ - 2)
143 return (ticks__[zeIdx] == aTick);
145 return ((ticks__[zeIdx] == aTick) || (ticks__[zeIdx + 1] == aTick));
146 }
catch (OutOfBounds&) {
return false; }
149 template <
typename T_TICKS >
150 DiscretizedVariable< T_TICKS >&
151 DiscretizedVariable< T_TICKS >::addTick(
const T_TICKS& aTick) {
153 GUM_ERROR(DefaultInLabel,
154 "Tick '" << aTick <<
"' already used for variable " << name());
157 if (ticks_size__ == ticks__.size()) {
158 ticks__.resize(ticks_size__ + 1);
161 if (ticks_size__ == 0) {
163 }
else if (ticks_size__ == 1) {
164 if (ticks__[0] < aTick) {
167 ticks__[1] = ticks__[0];
175 for (Idx i = ticks_size__ - 1; i > zeIdx; --i) {
176 ticks__[i + 1] = ticks__[i];
179 ticks__[zeIdx + 1] = aTick;
180 }
catch (OutOfUpperBound&) {
181 ticks__[ticks_size__] = aTick;
182 }
catch (OutOfLowerBound&) {
183 for (Idx i = ticks_size__; i >= 1; --i) {
184 ticks__[i] = ticks__[i - 1];
196 template <
typename T_TICKS >
197 INLINE
void DiscretizedVariable< T_TICKS >::eraseTicks() {
198 if (ticks_size__ != 0) { ticks_size__ = 0; }
201 template <
typename T_TICKS >
202 INLINE std::string DiscretizedVariable< T_TICKS >::label(Idx i)
const {
203 std::stringstream ss;
205 if (i >= ticks_size__ - 1) {
206 GUM_ERROR(OutOfBounds,
"inexisting label index");
209 ss <<
"[" << ticks__[i] <<
";" << ticks__[i + 1];
211 ss << ((i == ticks_size__ - 2) ?
"]" :
"[");
217 template <
typename T_TICKS >
218 INLINE
double DiscretizedVariable< T_TICKS >::numerical(Idx indice)
const {
219 if (indice >= ticks_size__ - 1) {
220 GUM_ERROR(OutOfBounds,
"inexisting label index");
223 return double((ticks__[indice + 1] + ticks__[indice]) / 2);
226 template <
typename T_TICKS >
228 DiscretizedVariable< T_TICKS >::index(
const std::string& label)
const {
229 if (empty()) { GUM_ERROR(OutOfBounds,
"empty variable : " + toString()); }
231 std::istringstream i(label);
234 if (!(i >> target)) {
235 GUM_ERROR(NotFound,
"Bad label : " << label <<
" for " << *
this);
245 template <
typename T_TICKS >
246 INLINE Size DiscretizedVariable< T_TICKS >::domainSize()
const {
247 return (ticks_size__ < 2) ? Size(0) : Size(ticks_size__ - 1);
250 template <
typename T_TICKS >
251 INLINE VarType DiscretizedVariable< T_TICKS >::varType()
const {
252 return VarType::Discretized;
255 template <
typename T_TICKS >
256 INLINE
const T_TICKS& DiscretizedVariable< T_TICKS >::tick(Idx i)
const {
257 if (i >= ticks_size__) { GUM_ERROR(OutOfBounds,
"There is no such tick"); }
262 template <
typename T_TICKS >
263 const std::string DiscretizedVariable< T_TICKS >::domain()
const {
267 if (domainSize() > 0) {
270 for (Idx i = 1; i < domainSize(); ++i) {
281 template <
typename T_TICKS >
282 INLINE
const std::vector< T_TICKS >&
283 DiscretizedVariable< T_TICKS >::ticks()
const {
284 return this->ticks__;
287 template <
typename T_TICKS >
288 INLINE std::vector<
double >
289 DiscretizedVariable< T_TICKS >::ticksAsDoubles()
const {
290 const std::size_t size = ticks__.size();
291 std::vector<
double > ticks(size);
292 for (std::size_t i = std::size_t(0); i < size; ++i)
293 ticks[i] = (
double)ticks__[i];