aGrUM  0.16.0
discretizedVariable_tpl.h
Go to the documentation of this file.
1 
23 #ifndef DOXYGEN_SHOULD_SKIP_THIS
24 # include <sstream>
25 
27 
28 namespace gum {
29  template < typename T_TICKS >
31  const DiscretizedVariable< T_TICKS >& aDRV) {
32  eraseTicks();
34 
35  for (Idx i = 0; i < aDRV.__ticks_size; ++i) {
36  addTick((T_TICKS)aDRV.__ticks[i]);
37  }
38  }
39 
40  template < typename T_TICKS >
41  Idx DiscretizedVariable< T_TICKS >::_dichotomy(const T_TICKS& target,
42  Idx min,
43  Idx max) const {
44  Idx res;
45  Idx mid = 0;
46 
47  if (max - min < 2)
48  res = min;
49  else {
50  mid = (max + min) / 2;
51  T_TICKS val = __ticks[mid];
52 
53  if (target == val)
54  res = mid;
55  else if (target < val)
56  res = _dichotomy(target, min, mid);
57  else if (target > val)
58  res = _dichotomy(target, mid, max);
59  else
60  res = mid;
61  }
62 
63  return res;
64  }
65 
66  template < typename T_TICKS >
67  INLINE Idx DiscretizedVariable< T_TICKS >::_pos(const T_TICKS& target) const {
68  if (__ticks_size < 2) { GUM_ERROR(OutOfBounds, "not enough ticks"); }
69 
70  if (target < __ticks[0]) {
71  GUM_ERROR(OutOfLowerBound, "less than first range");
72  }
73 
74  if (target > __ticks[__ticks_size - 1]) {
75  GUM_ERROR(OutOfUpperBound, "more than last range");
76  }
77 
78  if (target == __ticks[__ticks_size - 1]) // special case for upper limit
79  // (which belongs to class __ticks_size-2
80  return __ticks_size - 2;
81 
82  return _dichotomy(target, 0, __ticks_size - 1);
83  }
84 
85  template < typename T_TICKS >
87  const std::string& aName, const std::string& aDesc) :
88  IDiscretizedVariable(aName, aDesc),
89  __ticks_size((Size)0) {
90  GUM_CONSTRUCTOR(DiscretizedVariable);
91  __ticks.reserve(1);
92  }
93 
94  template < typename T_TICKS >
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)
104  addTick(t);
105  }
106 
107  template < typename T_TICKS >
109  const DiscretizedVariable< T_TICKS >& aDRV) :
110  IDiscretizedVariable(aDRV) {
111  GUM_CONS_CPY(DiscretizedVariable);
112  __ticks.reserve(1);
113  _copy(aDRV);
114  }
115 
116  template < typename T_TICKS >
118  GUM_DESTRUCTOR(DiscretizedVariable);
119  }
120 
121  template < typename T_TICKS >
122  DiscretizedVariable< T_TICKS >* DiscretizedVariable< T_TICKS >::clone() const {
123  return new DiscretizedVariable< T_TICKS >(*this);
124  }
125 
126  template < typename T_TICKS >
127  INLINE DiscretizedVariable< T_TICKS >& DiscretizedVariable< T_TICKS >::
128  operator=(const DiscretizedVariable< T_TICKS >& aDRV) {
129  _copy(aDRV);
130  return *this;
131  }
132 
133  template < typename T_TICKS >
134  INLINE bool DiscretizedVariable< T_TICKS >::isTick(const T_TICKS& aTick) const {
135  if (__ticks_size == 0) return false;
136 
137  if (__ticks_size == 1) return (__ticks[0] == aTick);
138 
139  try {
140  Idx zeIdx = _pos(aTick);
141 
142  if (zeIdx != __ticks_size - 2)
143  return (__ticks[zeIdx] == aTick);
144  else // special case for upper limit
145  return ((__ticks[zeIdx] == aTick) || (__ticks[zeIdx + 1] == aTick));
146  } catch (OutOfBounds&) { return false; }
147  }
148 
149  template < typename T_TICKS >
150  DiscretizedVariable< T_TICKS >&
151  DiscretizedVariable< T_TICKS >::addTick(const T_TICKS& aTick) {
152  if (isTick(aTick)) {
153  GUM_ERROR(DefaultInLabel,
154  "Tick '" << aTick << "' already used for variable " << name());
155  }
156 
157  if (__ticks_size == __ticks.size()) { // streching __ticks if necessary
158  __ticks.resize(__ticks_size + 1);
159  }
160 
161  if (__ticks_size == 0) { // special case for first tick
162  __ticks[0] = aTick;
163  } else if (__ticks_size == 1) { // special case for second tick
164  if (__ticks[0] < aTick) {
165  __ticks[1] = aTick;
166  } else {
167  __ticks[1] = __ticks[0];
168  __ticks[0] = aTick;
169  }
170  } else {
171  try {
172  Idx zeIdx =
173  _pos(aTick); // aTick is in [ __ticks[zeIdx],__ticks[zeIdx+1] [
174 
175  for (Idx i = __ticks_size - 1; i > zeIdx; --i) {
176  __ticks[i + 1] = __ticks[i];
177  }
178 
179  __ticks[zeIdx + 1] = aTick;
180  } catch (OutOfUpperBound&) { // new upper bound
181  __ticks[__ticks_size] = aTick;
182  } catch (OutOfLowerBound&) { // new lower bound
183  for (Idx i = __ticks_size; i >= 1; --i) {
184  __ticks[i] = __ticks[i - 1];
185  }
186 
187  __ticks[0] = aTick;
188  }
189  }
190 
191  __ticks_size++;
192 
193  return *this;
194  }
195 
196  template < typename T_TICKS >
198  if (__ticks_size != 0) { __ticks_size = 0; }
199  }
200 
201  template < typename T_TICKS >
202  INLINE std::string DiscretizedVariable< T_TICKS >::label(Idx i) const {
203  std::stringstream ss;
204 
205  if (i >= __ticks_size - 1) {
206  GUM_ERROR(OutOfBounds, "inexisting label index");
207  }
208 
209  ss << "[" << __ticks[i] << ";" << __ticks[i + 1];
210 
211  ss << ((i == __ticks_size - 2) ? "]" : "[");
212 
213  return ss.str();
214  }
215 
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");
221  }
222 
223  return double((__ticks[indice + 1] + __ticks[indice]) / 2);
224  }
225 
226  template < typename T_TICKS >
227  INLINE Idx
228  DiscretizedVariable< T_TICKS >::index(const std::string& label) const {
229  if (empty()) { GUM_ERROR(OutOfBounds, "empty variable : " + toString()); }
230 
231  std::istringstream i(label);
232  T_TICKS target;
233 
234  if (!(i >> target)) {
235  GUM_ERROR(NotFound, "Bad label : " << label << " for " << *this);
236  }
237 
238  return _pos(target);
239  }
240 
245  template < typename T_TICKS >
247  return (__ticks_size < 2) ? Size(0) : Size(__ticks_size - 1);
248  }
249 
250  template < typename T_TICKS >
252  return VarType::Discretized;
253  }
254 
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"); }
258 
259  return __ticks[i];
260  }
261 
262  template < typename T_TICKS >
263  const std::string DiscretizedVariable< T_TICKS >::domain() const {
264  std::stringstream s;
265  s << "<";
266 
267  if (domainSize() > 0) {
268  s << label(0);
269 
270  for (Idx i = 1; i < domainSize(); ++i) {
271  s << ",";
272  s << label(i);
273  }
274  }
275 
276  s << ">";
277 
278  return s.str();
279  }
280 
281  template < typename T_TICKS >
282  INLINE const std::vector< T_TICKS >&
284  return this->__ticks;
285  }
286 
287  template < typename T_TICKS >
288  INLINE std::vector< double >
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];
294  return ticks;
295  }
296 
297 } /* namespace gum */
298 
299 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
virtual std::vector< double > ticksAsDoubles() const
return the list of ticks as a vector of doubles
std::vector< T_TICKS > __ticks
void _copy(const Variable &aRV)
protected copy
virtual Idx index(const std::string &label) const
from the label to its index in var.
virtual Size domainSize() const
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
virtual ~DiscretizedVariable()
Destructor.
virtual std::string label(Idx i) const
const std::vector< T_TICKS > & ticks() const
Return the list of ticks.
bool isTick(const T_TICKS &aTick) const
DiscretizedVariable & addTick(const T_TICKS &aTick)
add a tick.
VarType
Definition: variable.h:41
virtual double numerical(Idx indice) const
get a numerical representation of he indice-the value.
DiscretizedVariable(const std::string &aName, const std::string &aDesc)
Constructor.
Idx _pos(const T_TICKS &target) const
seach the class of target (internally use _dichotomy)
DiscretizedVariable< T_TICKS > & operator=(const DiscretizedVariable< T_TICKS > &aDRV)
operator =
const T_TICKS & tick(Idx i) const
from the index to the tick.
void eraseTicks()
erase all the Ticks
IDiscretizedVariable(const std::string &aName, const std::string &aDesc)
Default constructor.
virtual DiscretizedVariable< T_TICKS > * clone() const
a virtual clone
const std::string toString() const
string version of *this
virtual const std::string domain() const
string represent the domain of the variable
virtual VarType varType() const
returns the type of variable
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
const std::string & name() const
returns the name of the variable
void _copy(const DiscretizedVariable< T_TICKS > &aDRV)
make a copy TODO since we removed T_OtherData maybe some changes are needed in this method...
Idx _dichotomy(const T_TICKS &target, Idx min, Idx max) const
perform a dichotomy on ticks
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55