3#include <Core/Utils/StdOptional.hpp>
6#include <initializer_list>
19template <
typename T1,
typename T2>
24 using value_type = T2;
122 typename std::map<key_type, value_type>::const_iterator
begin() const noexcept;
145template <typename T1, typename T2>
147 std::initializer_list<
std::pair<key_type, value_type>> pairs ) {
148 for (
auto& p : pairs ) {
149 m_valueToKey.
insert( { p.second, p.first } );
150 m_keyToValue.insert( p );
154template <
typename T1,
typename T2>
156 auto [i1, r1] = m_valueToKey.
insert( { p.second, p.first } );
164template <
typename T1,
typename T2>
169template <
typename T1,
typename T2>
173 m_keyToValue.
begin(),
175 [&
value](
const typename key_to_value_map::value_type& v ) { return value == v.second; } );
177 if ( it1 != m_keyToValue.
end() ) m_keyToValue.
erase( it1 );
180 m_valueToKey.
begin(),
182 [&
key](
const typename value_to_key_map::value_type& v ) { return key == v.second; } );
184 if ( it2 != m_valueToKey.
end() ) m_valueToKey.
erase( it2 );
190template <
typename T1,
typename T2>
195template <
typename T1,
typename T2>
198 auto it1 = m_keyToValue.
find(
key );
200 if ( it1 == m_keyToValue.
end() || it2 == m_valueToKey.
end() )
return false;
202 if ( it1->second !=
value || it2->second !=
key )
return false;
204 m_keyToValue.
erase( it1 );
205 m_valueToKey.
erase( it2 );
209template <
typename T1,
typename T2>
211 return remove( p.frist, p.second );
214template <
typename T1,
typename T2>
215typename BijectiveAssociation<T1, T2>::value_type
217 return m_keyToValue.
at( k );
220template <
typename T1,
typename T2>
221typename BijectiveAssociation<T1, T2>::key_type
223 return m_valueToKey.
at( k );
226template <
typename T1,
typename T2>
227std::optional<typename BijectiveAssociation<T1, T2>::key_type>
229 auto itr = m_valueToKey.
find( k );
230 if ( itr == m_valueToKey.
end() )
return {};
234template <
typename T1,
typename T2>
235typename BijectiveAssociation<T1, T2>::value_type
237 return m_keyToValue.
at( k );
240template <
typename T1,
typename T2>
241std::optional<typename BijectiveAssociation<T1, T2>::value_type>
243 auto itr = m_keyToValue.
find( k );
244 if ( itr == m_keyToValue.
end() )
return {};
248template <
typename T1,
typename T2>
250 return m_keyToValue.
cbegin();
253template <
typename T1,
typename T2>
255 return m_keyToValue.
cend();
258template <
typename T1,
typename T2>
260 return m_keyToValue.
begin();
263template <
typename T1,
typename T2>
265 return m_keyToValue.
end();
268template <
typename T1,
typename T2>
270 return m_keyToValue.
size();
Bijective association between two sets {keys} and {values} having the same cardinality....
value_type value(const key_type &key) const
Gets the value associated to key.
std::map< key_type, value_type >::const_iterator cend() const noexcept
Gets a const iterator at the end of the key to value map.
optional< value_type > valueIfExists(const key_type &key) const
Gets the value associated to key if it exists.
std::map< key_type, value_type >::const_iterator cbegin() const noexcept
Gets a const iterator at beginning of the key to value map.
value_type operator()(const key_type &key) const
Gets the value associated to the key.
bool remove(key_type key, value_type value)
Convenient alias for remove({key, value}).
BijectiveAssociation(std::initializer_list< std::pair< key_type, value_type > > pairs)
Constructor from { <T1, T2> } pairs.
bool remove(std::pair< key_type, value_type > p)
Remove a pair from the association.
std::map< key_type, value_type >::const_iterator begin() const noexcept
Gets a const iterator at beginning of the key to value map.
std::map< key_type, value_type >::const_iterator end() const noexcept
Gets a const iterator at the end of the key to value map.
void replace(std::pair< key_type, value_type > p)
bool insert(std::pair< key_type, value_type > p)
Insert a pair into the association.
bool insert(key_type key, value_type value)
Convenient alias for insert({key, value}).
size_t size() const
Gets the size of the association, i.e. the numer of <Key, Value> pairs.
BijectiveAssociation()=default
Creates an empty association.
void replace(key_type key, value_type value)
Convenient alias of replace({key, value}).
key_type key(const value_type &value) const
Gets the key associated to a value.
optional< key_type > keyIfExists(const value_type &value) const
Gets the key associated to value if it exists.
hepler function to manage enum as underlying types in VariableSet