61 template <
typename T1,
typename T2,
bool Gen >
64 for (
auto iter = f2s.
cbegin(); iter != f2s.
cend(); ++iter) {
75 val1->second = &(
const_cast< T2&
>(val2->first));
76 val2->second = &(
const_cast< T1&
>(val1->first));
85 template <
typename T1,
typename T2,
bool Gen >
98 template <
typename T1,
typename T2,
bool Gen >
100 std::initializer_list< std::pair< T1, T2 > > list) :
105 for (
const auto& elt: list) {
106 insert(elt.first, elt.second);
111 template <
typename T1,
typename T2,
bool Gen >
121 template <
typename T1,
typename T2,
bool Gen >
130 template <
typename T1,
typename T2,
bool Gen >
136 template <
typename T1,
typename T2,
bool Gen >
146 template <
typename T1,
typename T2,
bool Gen >
150 if (
this != &toCopy) {
162 template <
typename T1,
typename T2,
bool Gen >
179 template <
typename T1,
typename T2,
bool Gen >
186 template <
typename T1,
typename T2,
bool Gen >
193 template <
typename T1,
typename T2,
bool Gen >
200 template <
typename T1,
typename T2,
bool Gen >
207 template <
typename T1,
typename T2,
bool Gen >
214 template <
typename T1,
typename T2,
bool Gen >
221 template <
typename T1,
typename T2,
bool Gen >
228 template <
typename T1,
typename T2,
bool Gen >
235 template <
typename T1,
typename T2,
bool Gen >
241 template <
typename T1,
typename T2,
bool Gen >
247 template <
typename T1,
typename T2,
bool Gen >
253 template <
typename T1,
typename T2,
bool Gen >
259 template <
typename T1,
typename T2,
bool Gen >
265 "the bijection contains an element with the same couple (" <<
first <<
"," <<
second
280 val1->second = &(
const_cast< T2&
>(val2->first));
281 val2->second = &(
const_cast< T1&
>(val1->first));
287 template <
typename T1,
typename T2,
bool Gen >
293 "the bijection contains an element with the same couple (" <<
first <<
"," <<
second
304 _firstToSecond_.erase(val1->first);
308 val1->second = &(
const_cast< T2&
>(val2->first));
309 val2->second = &(
const_cast< T1&
>(val1->first));
316 template <
typename T1,
typename T2,
bool Gen >
318 const T1& val)
const {
326 template <
typename T1,
typename T2,
bool Gen >
328 const T2& val)
const {
335 template <
typename T1,
typename T2,
bool Gen >
341 template <
typename T1,
typename T2,
bool Gen >
347 template <
typename T1,
typename T2,
bool Gen >
348 template <
typename... Args >
350 std::pair< T1, T2 > new_elt(std::forward< Args >(args)...);
351 _insert_(std::move(new_elt.first), std::move(new_elt.second));
355 template <
typename T1,
typename T2,
bool Gen >
362 template <
typename T1,
typename T2,
bool Gen >
369 template <
typename T1,
typename T2,
bool Gen >
378 template <
typename T1,
typename T2,
bool Gen >
387 template <
typename T1,
typename T2,
bool Gen >
393 template <
typename T1,
typename T2,
bool Gen >
400 template <
typename T1,
typename T2,
bool Gen >
408 template <
typename T1,
typename T2,
bool Gen >
414 template <
typename T1,
typename T2,
bool Gen >
416 std::stringstream stream;
421 if (!
first) stream <<
", ";
424 stream <<
'(' << iter.first() <<
" <-> " << iter.second() <<
')';
436 template <
typename T1,
typename T2 >
438 bool resize_policy) :
449 template <
typename T1,
typename T2 >
451 std::initializer_list< std::pair< T1, T2 > > list) :
452 _firstToSecond_(
Size(list.size()) / 2, true, false),
453 _secondToFirst_(
Size(list.size()) / 2, true, false) {
456 for (
const auto& elt: list) {
457 insert(elt.first, elt.second);
462 template <
typename T1,
typename T2 >
465 for (
auto iter = f2s.cbegin(); iter != f2s.cend(); ++iter) {
466 _firstToSecond_.insert(iter.key(), iter.val());
469 _secondToFirst_.insert(iter.val(), iter.key());
471 _firstToSecond_.erase(iter.key());
482 template <
typename T1,
typename T2 >
492 template <
typename T1,
typename T2 >
501 template <
typename T1,
typename T2 >
507 template <
typename T1,
typename T2 >
514 template <
typename T1,
typename T2 >
521 template <
typename T1,
typename T2 >
524 return *(
reinterpret_cast< const BijectionIterator< T1, T2 >*
>(_Bijection_end_));
528 template <
typename T1,
typename T2 >
535 template <
typename T1,
typename T2 >
542 template <
typename T1,
typename T2 >
545 return BijectionIteratorSafe< T1, T2 >{*
this};
549 template <
typename T1,
typename T2 >
556 template <
typename T1,
typename T2 >
563 template <
typename T1,
typename T2 >
565 _firstToSecond_.clear();
566 _secondToFirst_.clear();
573 template <
typename T1,
typename T2 >
574 INLINE BijectionImplementation< T1, T2, true >&
576 const BijectionImplementation< T1, T2, true >& toCopy) {
578 if (
this != &toCopy) {
580 _copy_(toCopy._firstToSecond_);
590 template <
typename T1,
typename T2 >
595 if (
this != &toCopy) {
608 template <
typename T1,
typename T2 >
610 return _secondToFirst_[second];
614 template <
typename T1,
typename T2 >
616 return _firstToSecond_[first];
620 template <
typename T1,
typename T2 >
622 return _firstToSecond_.exists(first);
626 template <
typename T1,
typename T2 >
628 return _secondToFirst_.exists(second);
632 template <
typename T1,
typename T2 >
635 if (existsFirst(first) || existsSecond(second)) {
637 "the bijection contains an element with the same couple (" << first <<
"," << second
642 _firstToSecond_.insert(first, second);
645 _secondToFirst_.insert(second, first);
647 _firstToSecond_.erase(first);
653 template <
typename T1,
typename T2 >
655 _insert_(first, second);
659 template <
typename T1,
typename T2 >
660 template <
typename... Args >
662 std::pair< T1, T2 > new_elt(std::forward< Args >(args)...);
663 _insert_(new_elt.first, new_elt.second);
668 template <
typename T1,
typename T2 >
672 return first(second);
673 }
catch (NotFound
const&) {
674 _insert_(val, second);
681 template <
typename T1,
typename T2 >
685 return second(first);
686 }
catch (NotFound
const&) {
687 _insert_(first, val);
693 template <
typename T1,
typename T2 >
695 GUM_ASSERT(_firstToSecond_.empty() == _secondToFirst_.empty());
696 return _firstToSecond_.empty();
700 template <
typename T1,
typename T2 >
702 GUM_ASSERT(_firstToSecond_.size() == _secondToFirst_.size());
703 return _firstToSecond_.size();
707 template <
typename T1,
typename T2 >
710 _secondToFirst_.erase(_firstToSecond_[first]);
711 _firstToSecond_.erase(first);
712 }
catch (NotFound
const&) {}
716 template <
typename T1,
typename T2 >
719 _firstToSecond_.erase(_secondToFirst_[second]);
720 _secondToFirst_.erase(second);
721 }
catch (NotFound
const&) {}
725 template <
typename T1,
typename T2 >
727 return _firstToSecond_.capacity();
731 template <
typename T1,
typename T2 >
733 _firstToSecond_.resize(new_size);
734 _secondToFirst_.resize(new_size);
738 template <
typename T1,
typename T2 >
741 _firstToSecond_.setResizePolicy(new_policy);
742 _secondToFirst_.setResizePolicy(new_policy);
746 template <
typename T1,
typename T2 >
748 return _firstToSecond_.resizePolicy();
752 template <
typename T1,
typename T2 >
754 std::stringstream stream;
758 for (iterator iter = begin(); iter != end(); ++iter) {
759 if (!first) stream <<
", ";
762 stream <<
'(' << iter.first() <<
" <-> " << iter.second() <<
')';
774 template <
typename T1,
typename T2 >
780 template <
typename T1,
typename T2 >
781 template <
bool Gen >
784 _iter_{bijection._firstToSecond_.cbeginSafe()} {
789 template <
typename T1,
typename T2 >
792 _iter_{bijection._firstToSecond_.cbeginSafe()} {
797 template <
typename T1,
typename T2 >
804 template <
typename T1,
typename T2 >
811 template <
typename T1,
typename T2 >
817 template <
typename T1,
typename T2 >
825 template <
typename T1,
typename T2 >
828 _iter_ = std::move(toCopy._iter_);
833 template <
typename T1,
typename T2 >
840 template <
typename T1,
typename T2 >
848 template <
typename T1,
typename T2 >
855 template <
typename T1,
typename T2 >
858 return _iter_ != toCompare._iter_;
862 template <
typename T1,
typename T2 >
865 return _iter_ == toCompare._iter_;
869 template <
typename T1,
typename T2 >
875 template <
typename T1,
typename T2 >
888 template <
typename T1,
typename T2 >
894 template <
typename T1,
typename T2 >
895 template <
bool Gen >
898 _iter_{bijection._firstToSecond_.cbegin()} {
903 template <
typename T1,
typename T2 >
905 _iter_{bijection._firstToSecond_.cbegin()} {
910 template <
typename T1,
typename T2 >
917 template <
typename T1,
typename T2 >
919 :
_iter_{std::move(from._iter_)} {
924 template <
typename T1,
typename T2 >
930 template <
typename T1,
typename T2 >
938 template <
typename T1,
typename T2 >
941 _iter_ = std::move(toCopy._iter_);
946 template <
typename T1,
typename T2 >
953 template <
typename T1,
typename T2 >
960 template <
typename T1,
typename T2 >
966 template <
typename T1,
typename T2 >
969 return _iter_ != toCompare._iter_;
973 template <
typename T1,
typename T2 >
976 return _iter_ == toCompare._iter_;
980 template <
typename T1,
typename T2 >
986 template <
typename T1,
typename T2 >
996 template <
typename T1,
typename T2 >
1005 template <
typename T1,
typename T2 >
1013 template <
typename T1,
typename T2 >
1021 template <
typename T1,
typename T2 >
1029 template <
typename T1,
typename T2 >
1035 template <
typename T1,
typename T2 >
1042 template <
typename T1,
typename T2 >
1049 template <
typename T1,
typename T2 >
Set of pairs of elements with fast search for both elements.
A non scalar implementation of a Bijection.
void emplace(Args &&... args)
Emplace a new element in the gum::Bijection.
const iterator & end() const noexcept
Returns the unsafe iterator at the end of the gum::Bijection.
const T1 & firstWithDefault(const T2 &second, const T1 &default_val) const
Returns the first value of a pair given its second value or default_val if second is unfound.
BijectionIteratorSafe< T1, T2 > iterator_safe
types for STL compliance
void eraseSecond(const T2 &second)
Erases an association containing the given second element.
const iterator_safe & endSafe() const noexcept
Returns the safe iterator at the end of the gum::Bijection.
BijectionIteratorSafe< T1, T2 > const_iterator_safe
types for STL compliance
HashTable12 _firstToSecond_
The gum::HashTable associating T2 objects to T1 objects.
HashTable12::value_type * _insert_(const T1 &first, const T2 &second)
Inserts a new association into the gum::Bijection.
~BijectionImplementation()
Destructor.
void _copy_(const HashTable< T1, T2 * > &source)
A function that performs a complete copy of another gum::Bijection.
void eraseFirst(const T1 &first)
Erases an association containing the given first element.
BijectionIterator< T1, T2 > const_iterator
types for STL compliance
const const_iterator_safe & cendSafe() const noexcept
Returns the constant safe iterator at the end of the gum::Bijection.
iterator begin() const
Returns the unsafe iterator at the beginning of the gum::Bijection.
bool existsFirst(const T1 &first) const
Returns true if first is the first element in a pair in the gum::Bijection.
Size capacity() const noexcept
Returns the number of hashtables slots used.
HashTable21 _secondToFirst_
The gum::HashTable associating T1 objects to T2 objects.
std::string toString() const
Returns a friendly representatin of the gum::Bijection.
BijectionIterator< T1, T2 > iterator
types for STL compliance
const_iterator_safe cbeginSafe() const
Returns the constant safe iterator at the begining of the gum::Bijection.
void insert(const T1 &first, const T2 &second)
Inserts a new association in the gum::Bijection.
bool empty() const noexcept
Returns true if the gum::Bijection doesn't contain any association.
const const_iterator & cend() const noexcept
Returns the constant iterator at the end of the gum::Bijection.
void setResizePolicy(const bool new_policy) noexcept
Change the gum::Bijection resizing policy.
friend class BijectionIterator< T1, T2 >
a friend to speed-up accesses
void clear()
Removes all the associations from the gum::Bijection.
const_iterator cbegin() const
Returns the constant unsafe iterator at the beginning of the gum::Bjection.
Size size() const noexcept
Returns the number of associations stored within the gum::Bijection.
friend class Bijection< T1, T2 >
const T2 & secondWithDefault(const T1 &second, const T2 &default_val) const
Returns the second value of a pair given its first value or default_val if first is unfound.
INLINE BijectionImplementation(const BijectionImplementation< const gum::DiscreteVariable *, const gum::DiscreteVariable *, true > &toCopy)
const T1 & first(const T2 &second) const
Returns the first value of a pair given its second value.
void resize(Size new_size)
Manually resize the gum::Bijection.
BijectionImplementation< T1, T2, Gen > & operator=(const BijectionImplementation< T1, T2, Gen > &toCopy)
Copy operator.
const T2 & second(const T1 &first) const
Returns the second value of a pair given its first value.
bool existsSecond(const T2 &second) const
Returns true if second is the second element in a pair in the gum::Bijection.
friend class BijectionIteratorSafe< T1, T2 >
a friend to speed-up accesses
iterator_safe beginSafe() const
Returns the safe iterator at the beginning of the gum::Bijection.
friend class BijectionImplementation
a friend to speed-up accesses
bool resizePolicy() const noexcept
Returns true if the resize policy is automatic.
Safe iterators for bijectionIterator.
BijectionIteratorSafe< T1, T2 > & operator=(const BijectionIteratorSafe< T1, T2 > &toCopy)
Copy operator.
BijectionIteratorSafe< T1, T2 > & operator++() noexcept
Go to the next association, if it exists.
const T2 & second() const
Returns the second element of the current association.
BijectionIteratorSafe< T1, T2 > & operator+=(Size nb) noexcept
Moves the iterator by nb elements.
BijectionIteratorSafe(const BijectionImplementation< T1, T2, Gen > &bijection)
Begin constructor.
INLINE BijectionIteratorSafe(const BijectionImplementation< NodeId, double, Gen > &bijection)
BijectionIteratorSafe< T1, T2 > operator+(Size nb) noexcept
Returns a new iterator.
const T1 & first() const
Returns the first element of the current association.
bool operator==(const BijectionIteratorSafe< T1, T2 > &toCompare) const noexcept
Equality operator.
BijectionIteratorSafe() noexcept
Default constructor.
~BijectionIteratorSafe() noexcept
Class destructor.
friend class BijectionImplementation
bool operator!=(const BijectionIteratorSafe< T1, T2 > &toCompare) const noexcept
Inequality operator.
Unsafe iterators for bijection.
BijectionIterator< T1, T2 > & operator+=(Size nb) noexcept
Moves the iterator by nb elements.
const T1 & first() const
Returns the first element of the current association.
bool operator==(const BijectionIterator< T1, T2 > &toCompare) const noexcept
Equality operator.
BijectionIterator() noexcept
Default constructor.
BijectionIterator(const BijectionImplementation< T1, T2, Gen > &bijection)
Begin constructor.
BijectionIterator< T1, T2 > operator+(Size nb) noexcept
Return a new iterator.
const T2 & second() const
Returns the second element of the current association.
bool operator!=(const BijectionIterator< T1, T2 > &toCompare) const noexcept
Inequality operator.
BijectionIterator< T1, T2 > & operator=(const BijectionIterator< T1, T2 > &toCopy)
Copy operator.
HashIter _iter_
The hashTable iterator that actually does all the job.
~BijectionIterator() noexcept
Class destructor.
BijectionIterator< T1, T2 > & operator++() noexcept
Go to the next association, if it exists.
friend class BijectionImplementation
Set of pairs of elements with fast search for both elements.
~Bijection()
Class destructor.
Bijection(Size size=HashTableConst::default_size, bool resize_policy=HashTableConst::default_resize_policy)
Default constructor: creates a gum::Bijection without any association.
Bijection< T1, T2 > & operator=(const Bijection< T1, T2 > &toCopy)
Copy operator.
Exception : a similar element already exists.
The class for generic Hash Tables.
std::pair< const T1, T2 * > value_type
const_iterator cbegin() const
Returns an unsafe const_iterator pointing to the beginning of the hashtable.
const const_iterator & cend() const noexcept
Returns the unsafe const_iterator pointing to the end of the hashtable.
Exception : the element we looked for cannot be found.
#define GUM_ERROR(type, msg)
std::size_t Size
In aGrUM, hashed values are unsigned long int.
gum is the global namespace for all aGrUM entities
std::ostream & operator<<(std::ostream &stream, const AVLTree< Val, Cmp > &tree)
display the content of a tree
static INLINE const T & op_second(const T *x)
Returns a refeence over a pointer.