OpenEnroth 73e68f7
Loading...
Searching...
No Matches
Functions
MapAccess.h File Reference
#include <cassert>
#include <type_traits>
#include <unordered_map>

Functions

template<class Map , class Key , class Value = typename Map::mapped_type>
Value valueOr (const Map &map, const Key &key, const std::type_identity_t< Value > &def=Value())
 
template<class Map , class Key >
auto * valuePtr (Map &&map, const Key &key)
 
template<class Key , class Value >
std::unordered_map< Value, Key > inverted (const std::unordered_map< Key, Value > &map)
 

Function Documentation

◆ inverted()

template<class Key , class Value >
std::unordered_map< Value, Key > inverted ( const std::unordered_map< Key, Value > &  map)

Inverts an unordered map, creating a map from values into keys.

The values must be unique, otherwise the function will assert.

Parameters
mapMap to invert.
Returns
Inverted map.

◆ valueOr()

template<class Map , class Key , class Value = typename Map::mapped_type>
Value valueOr ( const Map &  map,
const Key &  key,
const std::type_identity_t< Value > &  def = Value() 
)

Convenient map accessor that returns either the value stored in a map, or default value if the key doesn't exist.

Parameters
mapMap to get value from.
keyKey to look up value for.
defDefault value to return if the key is not in the map.
Returns
Value for the provided key, or def if the key was not found in the map.

◆ valuePtr()

template<class Map , class Key >
auto * valuePtr ( Map &&  map,
const Key &  key 
)

Convenient map accessor that returns either the pointer to the value stored in a map, or nullptr if the key doesn't exist.

Parameters
mapMap to get value from.
keyKey to look up value for.
Returns
Pointer to the value for the provided key, or nullptr if the key was not found in the map.