3 #include <Core/Utils/StdOptional.hpp>
6 #include <initializer_list>
19 template <
typename T1,
typename T2>
24 using value_type = T2;
25 using key_to_value_map = std::map<key_type, value_type>;
26 using value_to_key_map = std::map<value_type, key_type>;
42 bool insert( std::pair<key_type, value_type> p );
63 void replace( std::pair<key_type, value_type> p );
79 bool remove( std::pair<key_type, value_type> p );
122 typename std::map<key_type, value_type>::const_iterator
begin() const noexcept;
125 typename std::map<key_type, value_type>::const_iterator
cbegin() const noexcept;
130 typename std::map<key_type, value_type>::const_iterator
end() const noexcept;
133 typename std::map<key_type, value_type>::const_iterator
cend() const noexcept;
141 key_to_value_map m_keyToValue;
142 value_to_key_map m_valueToKey;
145 template <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 );
154 template <
typename T1,
typename T2>
156 auto [i1, r1] = m_valueToKey.insert( { p.second, p.first } );
158 auto [i2, r2] = m_keyToValue.insert( std::move( p ) );
164 template <
typename T1,
typename T2>
169 template <
typename T1,
typename T2>
172 auto it1 = std::find_if(
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 );
179 auto it2 = std::find_if(
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 );
190 template <
typename T1,
typename T2>
195 template <
typename T1,
typename T2>
198 auto it1 = m_keyToValue.find(
key );
199 auto it2 = m_valueToKey.find(
value );
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 );
209 template <
typename T1,
typename T2>
211 return remove( p.frist, p.second );
214 template <
typename T1,
typename T2>
215 typename BijectiveAssociation<T1, T2>::value_type
217 return m_keyToValue.at( k );
220 template <
typename T1,
typename T2>
221 typename BijectiveAssociation<T1, T2>::key_type
223 return m_valueToKey.at( k );
226 template <
typename T1,
typename T2>
227 std::optional<typename BijectiveAssociation<T1, T2>::key_type>
229 auto itr = m_valueToKey.find( k );
230 if ( itr == m_valueToKey.end() )
return {};
234 template <
typename T1,
typename T2>
235 typename BijectiveAssociation<T1, T2>::value_type
237 return m_keyToValue.at( k );
240 template <
typename T1,
typename T2>
241 std::optional<typename BijectiveAssociation<T1, T2>::value_type>
243 auto itr = m_keyToValue.find( k );
244 if ( itr == m_keyToValue.end() )
return {};
248 template <
typename T1,
typename T2>
250 return m_keyToValue.cbegin();
253 template <
typename T1,
typename T2>
255 return m_keyToValue.cend();
258 template <
typename T1,
typename T2>
260 return m_keyToValue.begin();
263 template <
typename T1,
typename T2>
265 return m_keyToValue.end();
268 template <
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}).
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(std::initializer_list< std::pair< key_type, value_type >> pairs)
Constructor from { <T1, T2> } 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.