62 template <
typename T1,
typename T2,
bool Gen >
65 for (
auto iter = f2s.
cbegin(); iter != f2s.
cend(); ++iter) {
76 val1->second = &(
const_cast< T2&
>(val2->first));
77 val2->second = &(
const_cast< T1&
>(val1->first));
86 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 >
246 template <
typename T1,
typename T2,
bool Gen >
247 template <
typename K >
248 requires(std::same_as< T1, std::string > && std::convertible_to< K, std::string_view >
249 && !std::same_as< std::decay_t< K >, std::string >)
255 template <
typename T1,
typename T2,
bool Gen >
260 template <
typename T1,
typename T2,
bool Gen >
261 template <
typename K >
262 requires(std::same_as< T1, std::string > && std::convertible_to< K, std::string_view >
263 && !std::same_as< std::decay_t< K >, std::string >)
269 template <
typename T1,
typename T2,
bool Gen >
275 template <
typename T1,
typename T2,
bool Gen >
284 template <
typename T1,
typename T2,
bool Gen >
285 optional_ref< const T2 >
292 template <
typename T1,
typename T2,
bool Gen >
293 template <
typename K >
294 requires(std::same_as< T1, std::string > && std::convertible_to< K, std::string_view >
295 && !std::same_as< std::decay_t< K >, std::string >)
303 template <
typename T1,
typename T2,
bool Gen >
309 "the bijection contains an element with the same couple (" <<
first <<
"," <<
second
324 val1->second = &(
const_cast< T2&
>(val2->first));
325 val2->second = &(
const_cast< T1&
>(val1->first));
331 template <
typename T1,
typename T2,
bool Gen >
337 "the bijection contains an element with the same couple (" <<
first <<
"," <<
second
352 val1->second = &(
const_cast< T2&
>(val2->first));
353 val2->second = &(
const_cast< T1&
>(val1->first));
360 template <
typename T1,
typename T2,
bool Gen >
362 const T1& val)
const {
369 template <
typename T1,
typename T2,
bool Gen >
371 const T2& val)
const {
377 template <
typename T1,
typename T2,
bool Gen >
383 template <
typename T1,
typename T2,
bool Gen >
389 template <
typename T1,
typename T2,
bool Gen >
390 template <
typename... Args >
392 std::pair< T1, T2 > new_elt(std::forward< Args >(args)...);
393 _insert_(std::move(new_elt.first), std::move(new_elt.second));
397 template <
typename T1,
typename T2,
bool Gen >
404 template <
typename T1,
typename T2,
bool Gen >
411 template <
typename T1,
typename T2,
bool Gen >
420 template <
typename T1,
typename T2,
bool Gen >
429 template <
typename T1,
typename T2,
bool Gen >
435 template <
typename T1,
typename T2,
bool Gen >
442 template <
typename T1,
typename T2,
bool Gen >
449 template <
typename T1,
typename T2,
bool Gen >
455 template <
typename T1,
typename T2,
bool Gen >
457 std::stringstream stream;
462 if (!
first) stream <<
", ";
465 stream <<
'(' << iter.first() <<
" <-> " << iter.second() <<
')';
477 template <
typename T1,
typename T2 >
489 template <
typename T1,
typename T2 >
491 std::initializer_list< std::pair< T1, T2 > > list) :
492 _firstToSecond_(
Size(list.size()) / 2, true, false),
493 _secondToFirst_(
Size(list.size()) / 2, true, false) {
496 for (
const auto& elt: list) {
497 insert(elt.first, elt.second);
502 template <
typename T1,
typename T2 >
505 for (
auto iter = f2s.cbegin(); iter != f2s.cend(); ++iter) {
511 _firstToSecond_.erase(iter.key());
522 template <
typename T1,
typename T2 >
532 template <
typename T1,
typename T2 >
541 template <
typename T1,
typename T2 >
547 template <
typename T1,
typename T2 >
554 template <
typename T1,
typename T2 >
561 template <
typename T1,
typename T2 >
568 template <
typename T1,
typename T2 >
575 template <
typename T1,
typename T2 >
582 template <
typename T1,
typename T2 >
589 template <
typename T1,
typename T2 >
596 template <
typename T1,
typename T2 >
603 template <
typename T1,
typename T2 >
605 _firstToSecond_.clear();
606 _secondToFirst_.clear();
613 template <
typename T1,
typename T2 >
617 if (
this != &toCopy) {
619 _copy_(toCopy._firstToSecond_);
629 template <
typename T1,
typename T2 >
633 if (
this != &toCopy) {
646 template <
typename T1,
typename T2 >
648 return _secondToFirst_[second];
652 template <
typename T1,
typename T2 >
654 return _firstToSecond_[first];
658 template <
typename T1,
typename T2 >
660 return _firstToSecond_.exists(first);
664 template <
typename T1,
typename T2 >
666 return _secondToFirst_.exists(second);
670 template <
typename T1,
typename T2 >
672 return _secondToFirst_.tryGet(second);
676 template <
typename T1,
typename T2 >
678 return _firstToSecond_.tryGet(first);
682 template <
typename T1,
typename T2 >
685 if (existsFirst(first) || existsSecond(second)) {
687 "the bijection contains an element with the same couple (" << first <<
"," << second
692 _firstToSecond_.insert(first, second);
695 _secondToFirst_.insert(second, first);
697 _firstToSecond_.erase(first);
703 template <
typename T1,
typename T2 >
705 _insert_(first, second);
709 template <
typename T1,
typename T2 >
710 template <
typename... Args >
712 std::pair< T1, T2 > new_elt(std::forward< Args >(args)...);
713 _insert_(new_elt.first, new_elt.second);
718 template <
typename T1,
typename T2 >
720 if (
auto ptr = tryFirst(second))
return *ptr;
721 _insert_(val, second);
727 template <
typename T1,
typename T2 >
729 if (
auto ptr = trySecond(first))
return *ptr;
730 _insert_(first, val);
735 template <
typename T1,
typename T2 >
737 GUM_ASSERT(_firstToSecond_.empty() == _secondToFirst_.empty());
738 return _firstToSecond_.empty();
742 template <
typename T1,
typename T2 >
744 GUM_ASSERT(_firstToSecond_.size() == _secondToFirst_.size());
745 return _firstToSecond_.size();
749 template <
typename T1,
typename T2 >
751 if (
auto ptr = _firstToSecond_.tryGet(first)) {
752 _secondToFirst_.erase(*ptr);
753 _firstToSecond_.erase(first);
758 template <
typename T1,
typename T2 >
760 if (
auto ptr = _secondToFirst_.tryGet(second)) {
761 _firstToSecond_.erase(*ptr);
762 _secondToFirst_.erase(second);
767 template <
typename T1,
typename T2 >
769 return _firstToSecond_.capacity();
773 template <
typename T1,
typename T2 >
775 _firstToSecond_.resize(new_size);
776 _secondToFirst_.resize(new_size);
780 template <
typename T1,
typename T2 >
782 _firstToSecond_.setResizePolicy(new_policy);
783 _secondToFirst_.setResizePolicy(new_policy);
787 template <
typename T1,
typename T2 >
789 return _firstToSecond_.resizePolicy();
793 template <
typename T1,
typename T2 >
795 std::stringstream stream;
799 for (iterator iter = begin(); iter != end(); ++iter) {
800 if (!first) stream <<
", ";
803 stream <<
'(' << iter.first() <<
" <-> " << iter.second() <<
')';
815 template <
typename T1,
typename T2 >
821 template <
typename T1,
typename T2 >
822 template <
bool Gen >
825 _iter_{bijection._firstToSecond_.cbeginSafe()} {
830 template <
typename T1,
typename T2 >
832 _iter_{bijection._firstToSecond_.cbeginSafe()} {
837 template <
typename T1,
typename T2 >
844 template <
typename T1,
typename T2 >
851 template <
typename T1,
typename T2 >
857 template <
typename T1,
typename T2 >
863 template <
typename T1,
typename T2 >
866 _iter_ = std::move(toCopy._iter_);
871 template <
typename T1,
typename T2 >
878 template <
typename T1,
typename T2 >
885 template <
typename T1,
typename T2 >
891 template <
typename T1,
typename T2 >
894 return _iter_ != toCompare._iter_;
898 template <
typename T1,
typename T2 >
901 return _iter_ == toCompare._iter_;
905 template <
typename T1,
typename T2 >
911 template <
typename T1,
typename T2 >
924 template <
typename T1,
typename T2 >
930 template <
typename T1,
typename T2 >
931 template <
bool Gen >
934 _iter_{bijection._firstToSecond_.cbegin()} {
939 template <
typename T1,
typename T2 >
941 _iter_{bijection._firstToSecond_.cbegin()} {
946 template <
typename T1,
typename T2 >
953 template <
typename T1,
typename T2 >
955 _iter_{std::move(from._iter_)} {
960 template <
typename T1,
typename T2 >
966 template <
typename T1,
typename T2 >
971 template <
typename T1,
typename T2 >
974 _iter_ = std::move(toCopy._iter_);
979 template <
typename T1,
typename T2 >
986 template <
typename T1,
typename T2 >
993 template <
typename T1,
typename T2 >
999 template <
typename T1,
typename T2 >
1002 return _iter_ != toCompare._iter_;
1006 template <
typename T1,
typename T2 >
1009 return _iter_ == toCompare._iter_;
1013 template <
typename T1,
typename T2 >
1019 template <
typename T1,
typename T2 >
1029 template <
typename T1,
typename T2 >
1038 template <
typename T1,
typename T2 >
1046 template <
typename T1,
typename T2 >
1054 template <
typename T1,
typename T2 >
1062 template <
typename T1,
typename T2 >
1068 template <
typename T1,
typename T2 >
1075 template <
typename T1,
typename T2 >
1082 template <
typename T1,
typename T2 >
1088 template <
bool gen >
1089 template <
typename T >
1094 template <
typename T >
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.
BijectionIteratorSafe< T1, T2 > iterator_safe
types for STL compliance
void setResizePolicy(const bool new_policy) noexcept
Change the gum::Bijection resizing policy.
optional_ref< const T2 > trySecond(const T1 &first) const
Returns an optional reference to the second value associated with the given first value,...
bool resizePolicy() const noexcept
Returns true if the resize policy is automatic.
BijectionIteratorSafe< T1, T2 > const_iterator_safe
types for STL compliance
const iterator & end() const noexcept
Returns the unsafe iterator at the end of the gum::Bijection.
HashTable12 _firstToSecond_
The gum::HashTable associating T2 objects to T1 objects.
const iterator_safe & endSafe() const noexcept
Returns the safe iterator at the end of the gum::Bijection.
BijectionIterator< T1, T2 > const_iterator
types for STL compliance
const_iterator cbegin() const
Returns the constant unsafe iterator at the beginning of the gum::Bjection.
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
BijectionImplementation< T1, T2, Gen > & operator=(const BijectionImplementation< T1, T2, Gen > &toCopy)
Copy operator.
const_iterator_safe cbeginSafe() const
Returns the constant safe iterator at the begining of the gum::Bijection.
const T2 & second(const T1 &first) const
Returns the second value of a pair given its first value.
BijectionImplementation(const BijectionImplementation< const gum::DiscreteVariable *, const gum::DiscreteVariable *, true > &toCopy)
optional_ref< const T1 > tryFirst(const T2 &second) const
Returns a pointer to the first value associated with the given second value, or nullptr if not found.
bool empty() const noexcept
Returns true if the gum::Bijection doesn't contain any association.
friend class BijectionIterator< T1, T2 >
a friend to speed-up accesses
void insert(const T1 &first, const T2 &second)
Inserts a new association in the gum::Bijection.
void resize(Size new_size)
Manually resize the gum::Bijection.
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.
HashTable12::value_type * _insert_(const T1 &first, const T2 &second)
Inserts a new association into the gum::Bijection.
friend class Bijection< T1, T2 >
Size capacity() const noexcept
Returns the number of hashtables slots used.
bool existsSecond(const T2 &second) const
Returns true if second is the second element in a pair in the gum::Bijection.
const T1 & first(const T2 &second) const
Returns the first value of a pair given its second value.
void _copy_(const HashTable< T1, T2 * > &source)
A function that performs a complete copy of another gum::Bijection.
void clear()
Removes all the associations from the gum::Bijection.
Size size() const noexcept
Returns the number of associations stored within the gum::Bijection.
~BijectionImplementation()
Destructor.
void eraseFirst(const T1 &first)
Erases an association containing the given first element.
iterator_safe beginSafe() const
Returns the safe iterator at the beginning of the gum::Bijection.
void eraseSecond(const T2 &second)
Erases an association containing the given second element.
const const_iterator & cend() const noexcept
Returns the constant iterator at the end of the gum::Bijection.
friend class BijectionIteratorSafe< T1, T2 >
a friend to speed-up accesses
iterator begin() const
Returns the unsafe iterator at the beginning of the gum::Bijection.
friend class BijectionImplementation
a friend to speed-up accesses
bool existsFirst(const T1 &first) const
Returns true if first is the first element in a pair in 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.
const const_iterator_safe & cendSafe() const noexcept
Returns the constant safe iterator at the end of the gum::Bijection.
Safe iterators for bijectionIterator.
~BijectionIteratorSafe() noexcept
Class destructor.
HashIter _iter_
The hashTable iterator that actually does all the job.
BijectionIteratorSafe< T1, T2 > & operator=(const BijectionIteratorSafe< T1, T2 > &toCopy)
Copy operator.
BijectionIteratorSafe< T1, T2 > operator+(Size nb) noexcept
Returns a new iterator.
bool operator==(const BijectionIteratorSafe< T1, T2 > &toCompare) const noexcept
Equality operator.
BijectionIteratorSafe< T1, T2 > & operator+=(Size nb) noexcept
Moves the iterator by nb elements.
const T2 & second() const
Returns the second element of the current association.
bool operator!=(const BijectionIteratorSafe< T1, T2 > &toCompare) const noexcept
Inequality operator.
BijectionIteratorSafe() noexcept
Default constructor.
BijectionIteratorSafe(const BijectionImplementation< T1, T2, Gen > &bijection)
Begin constructor.
const T1 & first() const
Returns the first element of the current association.
BijectionIteratorSafe< T1, T2 > & operator++() noexcept
Go to the next association, if it exists.
friend class BijectionImplementation
Unsafe iterators for bijection.
BijectionIterator< T1, T2 > & operator+=(Size nb) noexcept
Moves the iterator by nb elements.
BijectionIterator(const BijectionImplementation< T1, T2, Gen > &bijection)
Begin constructor.
const T1 & first() const
Returns the first element of the current association.
BijectionIterator< T1, T2 > & operator++() noexcept
Go to the next association, if it exists.
bool operator==(const BijectionIterator< T1, T2 > &toCompare) const noexcept
Equality operator.
BijectionIterator< T1, T2 > & operator=(const BijectionIterator< T1, T2 > &toCopy)
Copy operator.
~BijectionIterator() noexcept
Class destructor.
bool operator!=(const BijectionIterator< T1, T2 > &toCompare) const noexcept
Inequality operator.
BijectionIterator< T1, T2 > operator+(Size nb) noexcept
Return a new iterator.
HashIter _iter_
The hashTable iterator that actually does all the job.
const T2 & second() const
Returns the second element of the current association.
friend class BijectionImplementation
BijectionIterator() noexcept
Default constructor.
Set of pairs of elements with fast search for both elements.
Bijection< T1, T2 > & operator=(const Bijection< T1, T2 > &toCopy)
Copy operator.
Bijection(Size size=HashTableConst::default_size, bool resize_policy=HashTableConst::default_resize_policy)
Default constructor: creates a gum::Bijection without any association.
~Bijection()
Class destructor.
Exception : a similar element already exists.
The class for generic Hash Tables.
const const_iterator & cend() const noexcept
Returns the unsafe const_iterator pointing to the end of the hashtable.
std::pair< const T1, T2 * > value_type
const_iterator cbegin() const
Returns an unsafe const_iterator pointing to the beginning of the hashtable.
A lightweight wrapper around a pointer providing an optional-like API for references (not supported b...
#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 const T & op_second(const T *x)
Returns a refeence over a pointer.