aGrUM  0.17.2
a C++ library for (probabilistic) graphical models
idCondSet_tpl.h
Go to the documentation of this file.
1 
29 #ifndef DOXYGEN_SHOULD_SKIP_THIS
30 
31 
32 namespace gum {
33 
34  namespace learning {
35 
36 
38  template < template < typename > class ALLOC >
40  GUM_CONSTRUCTOR(IdCondSetIterator);
41  }
42 
43 
45  template < template < typename > class ALLOC >
47  const IdCondSet< ALLOC >& idset) :
48  __seq(&(idset.ids())) {
49  GUM_CONSTRUCTOR(IdCondSetIterator);
50  }
51 
52 
54  template < template < typename > class ALLOC >
55  INLINE IdCondSetIterator< ALLOC >::IdCondSetIterator(
56  const IdCondSetIterator< ALLOC >& from) :
57  __seq(from.__seq),
58  __index(from.__index) {
59  GUM_CONS_CPY(IdCondSetIterator);
60  }
61 
62 
64  template < template < typename > class ALLOC >
65  INLINE IdCondSetIterator< ALLOC >::IdCondSetIterator(
66  IdCondSetIterator< ALLOC >&& from) :
67  __seq(from.__seq),
68  __index(from.__index) {
69  GUM_CONS_MOV(IdCondSetIterator);
70  }
71 
72 
74  template < template < typename > class ALLOC >
75  INLINE IdCondSetIterator< ALLOC >::~IdCondSetIterator() {
76  GUM_DESTRUCTOR(IdCondSetIterator);
77  }
78 
79 
81  template < template < typename > class ALLOC >
82  INLINE void IdCondSetIterator< ALLOC >::__gotoEnd() {
83  if (__seq != nullptr)
84  __index = __seq->size();
85  else
86  __index = std::size_t(0);
87  }
88 
89 
91  template < template < typename > class ALLOC >
92  INLINE IdCondSetIterator< ALLOC >& IdCondSetIterator< ALLOC >::operator=(
93  const IdCondSetIterator< ALLOC >& from) {
94  __seq = from.__seq;
95  __index = from.__index;
96  return *this;
97  }
98 
99 
101  template < template < typename > class ALLOC >
102  INLINE IdCondSetIterator< ALLOC >&
103  IdCondSetIterator< ALLOC >::operator=(IdCondSetIterator< ALLOC >&& from) {
104  __seq = from.__seq;
105  __index = from.__index;
106  return *this;
107  }
108 
109 
111  template < template < typename > class ALLOC >
113  return __seq->operator[](__index);
114  }
115 
116 
118  template < template < typename > class ALLOC >
120  const IdCondSetIterator< ALLOC >& from) const {
121  return (__index != from.__index) || (__seq != from.__seq);
122  }
123 
124 
126  template < template < typename > class ALLOC >
128  const IdCondSetIterator< ALLOC >& from) const {
129  return !operator!=(from);
130  }
131 
132 
134  template < template < typename > class ALLOC >
135  INLINE IdCondSetIterator< ALLOC >& IdCondSetIterator< ALLOC >::operator++() {
136  ++__index;
137  return *this;
138  }
139 
140 
142  template < template < typename > class ALLOC >
143  INLINE IdCondSetIterator< ALLOC >&
144  IdCondSetIterator< ALLOC >::operator+=(const std::size_t i) {
145  __index += i;
146  return *this;
147  }
148 
149 
151  template < template < typename > class ALLOC >
152  IdCondSetIterator< ALLOC >
153  IdCondSetIterator< ALLOC >::operator+(const std::size_t i) {
154  IdCondSetIterator< ALLOC > res(*this);
155  res += i;
156  return res;
157  }
158 
159 
161  template < template < typename > class ALLOC >
162  std::size_t IdCondSetIterator< ALLOC >::pos() const {
163  if (__seq == nullptr)
164  GUM_ERROR(UndefinedIteratorValue,
165  "The IdCondSet is empty, so its iterators have no position");
166  if (__index >= __seq->size())
167  GUM_ERROR(UndefinedIteratorValue,
168  "the IdCondSet iterator has no position because it reached "
169  "the set's end.");
170  return __index;
171  }
172 
173 
176 
177 
179  template < template < typename > class ALLOC >
180  INLINE typename IdCondSet< ALLOC >::allocator_type
181  IdCondSet< ALLOC >::getAllocator() const {
182  return *this;
183  }
184 
185 
187  template < template < typename > class ALLOC >
188  INLINE IdCondSet< ALLOC >::IdCondSet(
189  const typename IdCondSet< ALLOC >::allocator_type& alloc) :
190  ALLOC< NodeId >(alloc),
191  __end_safe(*this) {
192  GUM_CONSTRUCTOR(IdCondSet);
193  }
194 
195 
197  template < template < typename > class ALLOC >
198  INLINE IdCondSet< ALLOC >::IdCondSet(
199  const std::vector< NodeId, ALLOC< NodeId > >& ids,
200  const bool rhs_ids,
201  const bool ordered_ids,
202  const typename IdCondSet< ALLOC >::allocator_type& alloc) :
203  ALLOC< NodeId >(alloc),
204  __end_safe(*this) {
205  __ids.resize(ids.size());
206 
207  // if the rhs_ids should be considered as unordered, we sort them by
208  // increasing order so that we can compare easily two different rhs_ids
209  if (!ordered_ids) {
210  std::vector< NodeId, ALLOC< NodeId > > vect(ids);
211  std::sort(vect.begin(), vect.end());
212  for (const auto id: vect)
213  __ids << id;
214  } else {
215  for (const auto id: ids)
216  __ids << id;
217  }
218 
219  if (!rhs_ids) __nb_lhs_ids = __ids.size();
220 
221  // update the end iterator
222  __end_safe.__gotoEnd();
223 
224  GUM_CONSTRUCTOR(IdCondSet);
225  }
226 
227 
229  template < template < typename > class ALLOC >
230  INLINE IdCondSet< ALLOC >::IdCondSet(
231  NodeId var1,
232  const std::vector< NodeId, ALLOC< NodeId > >& rhs_ids,
233  const bool ordered_rhs_ids,
234  const typename IdCondSet< ALLOC >::allocator_type& alloc) :
235  ALLOC< NodeId >(alloc),
236  __nb_lhs_ids(std::size_t(1)), __end_safe(*this) {
237  __ids.resize(rhs_ids.size() + std::size_t(1));
238  __ids << var1;
239 
240  // if the rhs_ids should be considered as unordered, we sort them by
241  // increasing order so that we can compare easily two different rhs_ids
242  if (!ordered_rhs_ids) {
243  std::vector< NodeId, ALLOC< NodeId > > vect(rhs_ids);
244  std::sort(vect.begin(), vect.end());
245  for (const auto id: vect)
246  __ids << id;
247  } else {
248  for (const auto id: rhs_ids)
249  __ids << id;
250  }
251 
252  // update the end iterator
253  __end_safe.__gotoEnd();
254 
255  GUM_CONSTRUCTOR(IdCondSet);
256  }
257 
258 
260  template < template < typename > class ALLOC >
261  INLINE IdCondSet< ALLOC >::IdCondSet(
262  NodeId var1,
263  NodeId var2,
264  const std::vector< NodeId, ALLOC< NodeId > >& rhs_ids,
265  const bool ordered_lhs_vars,
266  const bool ordered_rhs_ids,
267  const typename IdCondSet< ALLOC >::allocator_type& alloc) :
268  ALLOC< NodeId >(alloc),
269  __nb_lhs_ids(std::size_t(2)), __end_safe(*this) {
270  __ids.resize(rhs_ids.size() + std::size_t(2));
271 
272  // if the variables on the left side are unordered, sort them by
273  // increasing order
274  if (!ordered_lhs_vars && (var1 > var2)) std::swap(var1, var2);
275  __ids << var1;
276  __ids << var2;
277 
278  // if the rhs_ids should be considered as unordered, we sort them by
279  // increasing order so that we can compare easily two different rhs_ids
280  if (!ordered_rhs_ids) {
281  std::vector< NodeId, ALLOC< NodeId > > vect(rhs_ids);
282  std::sort(vect.begin(), vect.end());
283  for (const auto id: vect)
284  __ids << id;
285  } else {
286  for (const auto id: rhs_ids)
287  __ids << id;
288  }
289 
290  // update the end iterator
291  __end_safe.__gotoEnd();
292 
293  GUM_CONSTRUCTOR(IdCondSet);
294  }
295 
296 
298  template < template < typename > class ALLOC >
299  INLINE IdCondSet< ALLOC >::IdCondSet(
300  NodeId var1,
301  NodeId var2,
302  NodeId var3,
303  const std::vector< NodeId, ALLOC< NodeId > >& rhs_ids,
304  const bool ordered_lhs_vars,
305  const bool ordered_rhs_ids,
306  const typename IdCondSet< ALLOC >::allocator_type& alloc) :
307  ALLOC< NodeId >(alloc),
308  __nb_lhs_ids(std::size_t(3)), __end_safe(*this) {
309  __ids.resize(rhs_ids.size() + std::size_t(3));
310 
311  // if the variables on the left side are unordered, sort them by
312  // increasing order
313  if (!ordered_lhs_vars) {
314  if (var1 > var2) std::swap(var1, var2);
315  if (var1 > var3) std::swap(var1, var3);
316  if (var2 > var3) std::swap(var2, var3);
317  }
318  __ids << var1;
319  __ids << var2;
320  __ids << var3;
321 
322  // if the rhs_ids should be considered as unordered, we sort them by
323  // increasing order so that we can compare easily two different rhs_ids
324  if (!ordered_rhs_ids) {
325  std::vector< NodeId, ALLOC< NodeId > > vect(rhs_ids);
326  std::sort(vect.begin(), vect.end());
327  for (const auto id: vect)
328  __ids << id;
329  } else {
330  for (const auto id: rhs_ids)
331  __ids << id;
332  }
333 
334  // update the end iterator
335  __end_safe.__gotoEnd();
336 
337  GUM_CONSTRUCTOR(IdCondSet);
338  }
339 
340 
342  template < template < typename > class ALLOC >
343  INLINE IdCondSet< ALLOC >::IdCondSet(
344  const IdCondSet< ALLOC >& from,
345  const typename IdCondSet< ALLOC >::allocator_type& alloc) :
346  ALLOC< NodeId >(alloc),
347  __ids(from.__ids), __nb_lhs_ids(from.__nb_lhs_ids), __end_safe(*this) {
348  __end_safe.__gotoEnd();
349  GUM_CONS_CPY(IdCondSet);
350  }
351 
352 
354  template < template < typename > class ALLOC >
355  INLINE IdCondSet< ALLOC >::IdCondSet(const IdCondSet< ALLOC >& from) :
356  IdCondSet< ALLOC >(from, from.getAllocator()) {}
357 
358 
360  template < template < typename > class ALLOC >
361  INLINE IdCondSet< ALLOC >::IdCondSet(
362  IdCondSet< ALLOC >&& from,
363  const typename IdCondSet< ALLOC >::allocator_type& alloc) :
364  ALLOC< NodeId >(alloc),
365  __ids(std::move(from.__ids)), __nb_lhs_ids(from.__nb_lhs_ids),
366  __end_safe(*this) {
367  __end_safe.__gotoEnd();
368  GUM_CONS_MOV(IdCondSet);
369  }
370 
371 
373  template < template < typename > class ALLOC >
374  INLINE IdCondSet< ALLOC >::IdCondSet(IdCondSet< ALLOC >&& from) :
375  IdCondSet< ALLOC >(std::move(from), from.getAllocator()) {}
376 
377 
379  template < template < typename > class ALLOC >
380  IdCondSet< ALLOC >* IdCondSet< ALLOC >::clone(
381  const typename IdCondSet< ALLOC >::allocator_type& alloc) const {
382  ALLOC< IdCondSet< ALLOC > > allocator(alloc);
383  IdCondSet< ALLOC >* new_set = allocator.allocate(1);
384  try {
385  allocator.construct(new_set, *this, alloc);
386  } catch (...) {
387  allocator.deallocate(new_set, 1);
388  throw;
389  }
390 
391  return new_set;
392  }
393 
394 
396  template < template < typename > class ALLOC >
397  IdCondSet< ALLOC >* IdCondSet< ALLOC >::clone() const {
398  return clone(this->getAllocator());
399  }
400 
401 
403  template < template < typename > class ALLOC >
404  INLINE IdCondSet< ALLOC >::~IdCondSet() {
405  GUM_DESTRUCTOR(IdCondSet);
406  }
407 
408 
410  template < template < typename > class ALLOC >
411  INLINE IdCondSet< ALLOC >&
412  IdCondSet< ALLOC >::operator=(const IdCondSet< ALLOC >& from) {
413  if (this != &from) {
414  __ids = from.__ids;
415  __nb_lhs_ids = from.__nb_lhs_ids;
416  __end_safe.__gotoEnd();
417  }
418  return *this;
419  }
420 
421 
423  template < template < typename > class ALLOC >
424  INLINE IdCondSet< ALLOC >&
425  IdCondSet< ALLOC >::operator=(IdCondSet< ALLOC >&& from) {
426  if (this != &from) {
427  __ids = std::move(from.__ids);
428  __nb_lhs_ids = from.__nb_lhs_ids;
429  __end_safe.__gotoEnd();
430  }
431  return *this;
432  }
433 
434 
436  template < template < typename > class ALLOC >
437  INLINE NodeId IdCondSet< ALLOC >::operator[](const std::size_t index) const {
438  return __ids.atPos(index);
439  }
440 
441 
443  template < template < typename > class ALLOC >
444  INLINE bool
445  IdCondSet< ALLOC >::operator==(const IdCondSet< ALLOC >& from) const {
446  if (__nb_lhs_ids != from.__nb_lhs_ids) return false;
447 
448  const std::size_t size = __ids.size();
449 
450  if (size != from.__ids.size()) return false;
451 
452  for (std::size_t i = std::size_t(0); i < size; ++i) {
453  if (__ids[i] != from.__ids[i]) return false;
454  }
455 
456  return true;
457  }
458 
459 
461  template < template < typename > class ALLOC >
462  INLINE bool
463  IdCondSet< ALLOC >::operator!=(const IdCondSet< ALLOC >& from) const {
464  return !operator==(from);
465  }
466 
467 
469  template < template < typename > class ALLOC >
470  INLINE typename IdCondSet< ALLOC >::iterator_safe
471  IdCondSet< ALLOC >::beginSafe() const {
472  return IdCondSetIterator< ALLOC >(*this);
473  }
474 
475 
477  template < template < typename > class ALLOC >
478  INLINE const typename IdCondSet< ALLOC >::iterator_safe&
479  IdCondSet< ALLOC >::endSafe() const {
480  return __end_safe;
481  }
482 
483 
485  template < template < typename > class ALLOC >
486  INLINE typename IdCondSet< ALLOC >::iterator
487  IdCondSet< ALLOC >::begin() const {
488  return IdCondSetIterator< ALLOC >(*this);
489  }
490 
491 
493  template < template < typename > class ALLOC >
494  INLINE const typename IdCondSet< ALLOC >::iterator&
495  IdCondSet< ALLOC >::end() const {
496  return __end_safe;
497  }
498 
499 
501  template < template < typename > class ALLOC >
502  INLINE const Sequence< NodeId, ALLOC< NodeId > >&
503  IdCondSet< ALLOC >::ids() const {
504  return __ids;
505  }
506 
507 
509  template < template < typename > class ALLOC >
510  IdCondSet< ALLOC > IdCondSet< ALLOC >::conditionalIdCondSet() const {
511  IdCondSet< ALLOC > set(this->getAllocator());
512  const std::size_t size = __ids.size();
513  for (std::size_t i = __nb_lhs_ids; i < size; ++i)
514  set.__ids << __ids[i];
515  set.__end_safe.__gotoEnd();
516  return set;
517  }
518 
519 
521  template < template < typename > class ALLOC >
522  void IdCondSet< ALLOC >::erase(const NodeId id) {
523  // search for id in Sequence __ids
524  const std::size_t size = __ids.size();
525  std::size_t pos = std::size_t(0);
526  for (; pos < size; ++pos) {
527  if (__ids[pos] == id) break;
528  }
529 
530  // if we found the id, remove it
531  if (pos < size) {
532  __ids.erase(SequenceIteratorSafe< NodeId >(__ids, pos));
533  if (pos < __nb_lhs_ids) --__nb_lhs_ids;
534  __end_safe.__gotoEnd();
535  }
536  }
537 
538 
540  template < template < typename > class ALLOC >
541  std::string IdCondSet< ALLOC >::toString() const {
542  std::stringstream str;
543 
544  str << '{';
545  bool deja = false;
546 
547  for (std::size_t i = std::size_t(0); i < __nb_lhs_ids; ++i) {
548  if (deja)
549  str << " , ";
550  else
551  deja = true;
552  str << __ids[i];
553  }
554 
555  deja = false;
556  for (auto iter = __ids.begin() + __nb_lhs_ids; iter != __ids.end(); ++iter) {
557  if (deja)
558  str << " , ";
559  else {
560  deja = true;
561  str << " | ";
562  }
563  str << *iter;
564  }
565 
566  str << '}';
567 
568  return str.str();
569  }
570 
571 
573  template < template < typename > class ALLOC >
574  INLINE std::size_t IdCondSet< ALLOC >::nbLHSIds() const {
575  return __nb_lhs_ids;
576  }
577 
578 
580  template < template < typename > class ALLOC >
581  INLINE std::size_t IdCondSet< ALLOC >::nbRHSIds() const {
582  return __ids.size() - __nb_lhs_ids;
583  }
584 
585 
587  template < template < typename > class ALLOC >
588  bool IdCondSet< ALLOC >::contains(const IdCondSet< ALLOC >& set) const {
589  if (set.__ids.size() > __ids.size()) return false;
590  for (const auto node: set.__ids) {
591  if (!__ids.exists(node)) return false;
592  }
593  return true;
594  }
595 
596 
598  template < template < typename > class ALLOC >
599  INLINE void IdCondSet< ALLOC >::clear() {
600  __ids.clear();
601  __nb_lhs_ids = std::size_t(0);
602  __end_safe.__gotoEnd();
603  }
604 
605 
607  template < template < typename > class ALLOC >
608  INLINE std::size_t IdCondSet< ALLOC >::size() const {
609  return __ids.size();
610  }
611 
612 
614  template < template < typename > class ALLOC >
615  INLINE std::size_t IdCondSet< ALLOC >::pos(const NodeId id) const {
616  return __ids.pos(id);
617  }
618 
619 
621  template < template < typename > class ALLOC >
622  INLINE bool IdCondSet< ALLOC >::exists(const NodeId id) const {
623  return __ids.exists(id);
624  }
625 
626 
628  template < template < typename > class ALLOC >
629  INLINE bool IdCondSet< ALLOC >::hasConditioningSet() const {
630  return __nb_lhs_ids != __ids.size();
631  }
632 
633 
635  template < template < typename > class ALLOC >
636  INLINE bool IdCondSet< ALLOC >::empty() const {
637  return __ids.empty();
638  }
639 
640 
641  // the display operator
642  template < template < typename > class ALLOC >
643  std::ostream& operator<<(std::ostream& stream,
644  const IdCondSet< ALLOC >& idset) {
645  return stream << idset.toString();
646  }
647 
648  } /* namespace learning */
649 
650 
651  // Returns the value of a key as a Size.
652  template < template < typename > class ALLOC >
653  Size HashFunc< learning::IdCondSet< ALLOC > >::castToSize(
654  const learning::IdCondSet< ALLOC >& key) {
655  Size h = Size(key.nbLHSIds());
656  const Sequence< NodeId, ALLOC< NodeId > >& vect = key.ids();
657  const std::size_t size = vect.size();
658 
659  std::size_t i = std::size_t(0);
660  while (i < size) {
661  const Size id = Size(vect[i]);
662  ++i;
663  h += Size(i) * id;
664  }
665 
666  return h;
667  }
668 
669 
670  // the hash function for idSets
671  template < template < typename > class ALLOC >
672  INLINE Size HashFunc< learning::IdCondSet< ALLOC > >::operator()(
673  const learning::IdCondSet< ALLOC >& key) const {
674  return (castToSize(key) * HashFuncConst::gold) & this->_hash_mask;
675  }
676 
677 } /* namespace gum */
678 
679 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
static constexpr Size gold
Definition: hashFunc.h:76
STL namespace.
void swap(HashTable< LpCol, double > *&a, HashTable< LpCol, double > *&b)
Swap the addresses of two pointers to hashTables.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Definition: agrum.h:25
IdCondSetIterator()
default constructor
std::ostream & operator<<(std::ostream &output, const BayesNet< GUM_SCALAR > &bn)
Prints map&#39;s DAG in output using the Graphviz-dot format.
Definition: BayesNet_tpl.h:626
LpExpr operator+(LpExpr &&lhs, const T2 &rhs)
Overload of operator + between anything ( a scalar, a variable or an expression ) and anything except...
bool operator==(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.h:243
LpExpr operator*(const SCALAR &lhs, const LpCol &rhs)
Overload of operator * between a scalar and a variable.
bool operator!=(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.h:251
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
Size NodeId
Type for node ids.
Definition: graphElements.h:98
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55