SatNOGS-COMMS  4.1.0
A COMMS subsystem for CubeSats
Loading...
Searching...
No Matches
satnogs::comms::utils::hash_map< N, K, V > Class Template Reference

Compile-time hash-map (associative key-value container) that performs all operations in constexpr context. This means that keys and values have to be constexpr and noexcept constructible and provide constexpr noexcept operator=. More...

#include <hash_map.hpp>

Public Types

using key_type = K
using value_type = V
using size_type = decltype(N)
using data_type = std::array<std::pair<K, V>, N>
 underlying structure used for the actual implementation
using const_iterator = typename data_type::const_iterator

Public Member Functions

template<typename... E>
constexpr hash_map (E &&...elements) noexcept
 The only construction that might be used, all keys and values must be provided in the constructor.
constexpr const_iterator find (const K &key) const noexcept
 Searches map for a given key and returns iterator.
constexpr std::pair< bool, const V & > at (const K &key) const noexcept
 Searches for a given key, aimed to return associated value with it.
constexpr const V & operator[] (const K &key) const noexcept
 Retrieves reference to constant to a value. Doesn't perform any bounds checking, behaviour is undefined if the key doesn't exist.
constexpr bool contains (const K &key) const noexcept
 Checks if element with given key exists.
constexpr size_type size () const noexcept
 Retrieves size of a hash-map, might also be called indirectly using the std::size(...).
constexpr const_iterator begin () const noexcept
 Gives constant iterator to a beginning, needed for the C++11 for-each cycle or the std::for_each.
constexpr const_iterator cbegin () const noexcept
 Gives constant iterator to a beginning, might be also called using std::cbegin(...).
constexpr const_iterator end () const noexcept
 Gives constant iterator to an end, needed for the C++11 for-each cycle or the std::for_each.
constexpr const_iterator cend () const noexcept
 Gives constant iterator to a beginning, might be also called using std::cend(...).
constexpr bool empty () const noexcept
 Hash-map cannot be empty; this might also be called using std::empty(...).
constexpr std::array< K, N > keys () const noexcept
 Returns all keys in the hash-map.

Protected Member Functions

template<const auto... KeyIdx>
constexpr std::array< K, N > keys_impl (std::index_sequence< KeyIdx... >) const

Detailed Description

template<std::size_t N, typename K, typename V>
class satnogs::comms::utils::hash_map< N, K, V >

Compile-time hash-map (associative key-value container) that performs all operations in constexpr context. This means that keys and values have to be constexpr and noexcept constructible and provide constexpr noexcept operator=.

Behaviour is undefined, if there are multiple same keys.

There's actually no hash function needed, see details section.

Implemented as an std::array containing pairs, so no hashing is involved.

Template Parameters
Ntotal number of elements
Kdata type for keys
Vdata type for values

Definition at line 65 of file hash_map.hpp.

Member Typedef Documentation

◆ const_iterator

template<std::size_t N, typename K, typename V>
using satnogs::comms::utils::hash_map< N, K, V >::const_iterator = typename data_type::const_iterator
See also
std::array<...>::const_iterator

Definition at line 77 of file hash_map.hpp.

◆ data_type

template<std::size_t N, typename K, typename V>
using satnogs::comms::utils::hash_map< N, K, V >::data_type = std::array<std::pair<K, V>, N>

underlying structure used for the actual implementation

Definition at line 75 of file hash_map.hpp.

◆ key_type

template<std::size_t N, typename K, typename V>
using satnogs::comms::utils::hash_map< N, K, V >::key_type = K
See also
std::unordered_map<...>::key_type

Definition at line 69 of file hash_map.hpp.

◆ size_type

template<std::size_t N, typename K, typename V>
using satnogs::comms::utils::hash_map< N, K, V >::size_type = decltype(N)
See also
std::unordered_map<...>::size_type

Definition at line 73 of file hash_map.hpp.

◆ value_type

template<std::size_t N, typename K, typename V>
using satnogs::comms::utils::hash_map< N, K, V >::value_type = V
See also
std::unordered_map<...>::value_type

Definition at line 71 of file hash_map.hpp.

Constructor & Destructor Documentation

◆ hash_map()

template<std::size_t N, typename K, typename V>
template<typename... E>
satnogs::comms::utils::hash_map< N, K, V >::hash_map ( E &&... elements)
inlineexplicitconstexprnoexcept

The only construction that might be used, all keys and values must be provided in the constructor.

Template Parameters
Rvariadic arguments automatically deduced by the compiler
Parameters
elementsseries of std::pair<K, V>, cannot be empty

Definition at line 84 of file hash_map.hpp.

Member Function Documentation

◆ at()

template<std::size_t N, typename K, typename V>
std::pair< bool, const V & > satnogs::comms::utils::hash_map< N, K, V >::at ( const K & key) const
inlinenodiscardconstexprnoexcept

Searches for a given key, aimed to return associated value with it.

Parameters
keykey to be searched for
Returns
pair, where first denotes whether element was found, second given value

Deliberately not throwing an exception, and returning pair instead, as this generates much shorter assembly on clang and msvc

Definition at line 109 of file hash_map.hpp.

◆ begin()

template<std::size_t N, typename K, typename V>
const_iterator satnogs::comms::utils::hash_map< N, K, V >::begin ( ) const
inlinenodiscardconstexprnoexcept

Gives constant iterator to a beginning, needed for the C++11 for-each cycle or the std::for_each.

Returns
constant iterator to a beginning
See also
cbegin()

Definition at line 155 of file hash_map.hpp.

◆ cbegin()

template<std::size_t N, typename K, typename V>
const_iterator satnogs::comms::utils::hash_map< N, K, V >::cbegin ( ) const
inlinenodiscardconstexprnoexcept

Gives constant iterator to a beginning, might be also called using std::cbegin(...).

Returns
constant iterator to a beginning
See also
std::unordered_map<...>::cbegin()

Definition at line 165 of file hash_map.hpp.

◆ cend()

template<std::size_t N, typename K, typename V>
const_iterator satnogs::comms::utils::hash_map< N, K, V >::cend ( ) const
inlinenodiscardconstexprnoexcept

Gives constant iterator to a beginning, might be also called using std::cend(...).

Returns
constant iterator to an end (past the last element)
See also
std::unordered_map<...>::cend()

Definition at line 185 of file hash_map.hpp.

◆ contains()

template<std::size_t N, typename K, typename V>
bool satnogs::comms::utils::hash_map< N, K, V >::contains ( const K & key) const
inlinenodiscardconstexprnoexcept

Checks if element with given key exists.

Parameters
keykey to be searched for
Returns
boolean that denotes key's existence

Definition at line 136 of file hash_map.hpp.

◆ empty()

template<std::size_t N, typename K, typename V>
bool satnogs::comms::utils::hash_map< N, K, V >::empty ( ) const
inlinenodiscardconstexprnoexcept

Hash-map cannot be empty; this might also be called using std::empty(...).

Returns
false – cannot be empty
See also
std::unordered_map<...>::empty()

Definition at line 195 of file hash_map.hpp.

◆ end()

template<std::size_t N, typename K, typename V>
const_iterator satnogs::comms::utils::hash_map< N, K, V >::end ( ) const
inlinenodiscardconstexprnoexcept

Gives constant iterator to an end, needed for the C++11 for-each cycle or the std::for_each.

Returns
constant iterator to an end (past the last element)
See also
cend()

Definition at line 175 of file hash_map.hpp.

◆ find()

template<std::size_t N, typename K, typename V>
const_iterator satnogs::comms::utils::hash_map< N, K, V >::find ( const K & key) const
inlinenodiscardconstexprnoexcept

Searches map for a given key and returns iterator.

Parameters
keykey to be searched for
Returns
constant iterator to an element (cend, if not found)

Definition at line 96 of file hash_map.hpp.

◆ keys()

template<std::size_t N, typename K, typename V>
std::array< K, N > satnogs::comms::utils::hash_map< N, K, V >::keys ( ) const
inlinenodiscardconstexprnoexcept

Returns all keys in the hash-map.

Returns
an std::array containing all keys

Definition at line 203 of file hash_map.hpp.

◆ keys_impl()

template<std::size_t N, typename K, typename V>
template<const auto... KeyIdx>
std::array< K, N > satnogs::comms::utils::hash_map< N, K, V >::keys_impl ( std::index_sequence< KeyIdx... > ) const
inlinenodiscardconstexprprotected

Definition at line 248 of file hash_map.hpp.

◆ operator[]()

template<std::size_t N, typename K, typename V>
const V & satnogs::comms::utils::hash_map< N, K, V >::operator[] ( const K & key) const
inlinenodiscardconstexprnoexcept

Retrieves reference to constant to a value. Doesn't perform any bounds checking, behaviour is undefined if the key doesn't exist.

Parameters
keykey to be searched for
Returns
reference to constant to a value associated with the key

Definition at line 127 of file hash_map.hpp.

◆ size()

template<std::size_t N, typename K, typename V>
size_type satnogs::comms::utils::hash_map< N, K, V >::size ( ) const
inlinenodiscardconstexprnoexcept

Retrieves size of a hash-map, might also be called indirectly using the std::size(...).

Returns
total size of a container

Definition at line 145 of file hash_map.hpp.


The documentation for this class was generated from the following file: