AMCAX Kernel 1.0.0.0
Loading...
Searching...
No Matches
AMCAX::IndexMap< key_type, value_type, hasher, keyeq > Class Template Reference

Template class of indexed map. More...

#include <common/IndexMap.hpp>

Public Types

using  map_type = std::unordered_map<key_type, int, hasher, keyeq>
 Internal map type.
 
using  vector_type = std::deque<typename map_type::value_type*>
 Internal vector of pointers.
 
using  value_vector_type = std::deque<value_type>
 Internal vector of values.
 

Public Member Functions

 IndexMap () noexcept(noexcept(map_type()) &&noexcept(vector_type()) &&noexcept(value_vector_type()))=default
 Default constructor.
 
template<typename iter>
 IndexMap (iter first, iter last)
 Construct from a range of key-value pairs.
 
 IndexMap (const IndexMap &other)
 Copy constructor.
 
 IndexMap (IndexMap &&other) noexcept
 Default move constructor.
 
 ~IndexMap ()=default
 Default destructor.
 
IndexMap &  operator= (const IndexMap &other)
 Copy assignment.
 
IndexMap &  operator= (IndexMap &&other) noexcept
 Default move assignment.
 
bool  empty () const noexcept
 Is the map empty.
 
int  size () const noexcept
 Get the size of the map.
 
void  clear () noexcept
 Clear the indexed map.
 
template<typename K, typename V>
int  insert (K &&key, V &&value)
 Insert a key with a value.
 
template<typename K, typename... V>
int  emplace (K &&key, V &&... value)
 Insert a key with a value in-place construction.
 
template<typename K, typename V>
int  insert_replace (K &&key, V &&value)
 Insert a new key with a value or replace a value of an existed key.
 
bool  erase (int i) noexcept
 Erase the i-th element.
 
bool  erase_key (const key_type &key)
 Erase a key if it is in the map.
 
bool  exchange (int i, int j) noexcept
 Exchange the i-th and j-th elements.
 
bool  erase_last () noexcept
 Erase the last element.
 
const value_type &  operator[] (const key_type &key) const
 Get the value from a key.
 
value_type &  operator[] (const key_type &key)
 Get the value from a key.
 
const value_type &  operator() (int id) const noexcept
 Get the i-th value.
 
value_type &  operator() (int id) noexcept
 Get the i-th value.
 
const key_type &  find_key (int id) const noexcept
 Get the i-th key.
 
bool  contains (const key_type &key) const
 Does the map contain a given key.
 
int  index (const key_type &key) const
 Get the index of a given key.
 
template<typename K, typename... T>
bool  set (int i, K &&key, T &&... value)
 Set the i-th element by a given key and a given value.
 
constexpr auto  begin () noexcept
 Get the iterator of the first element.
 
constexpr auto  begin () const noexcept
 Get the const iterator of the first element.
 
constexpr auto  cbegin () const noexcept
 Get the const iterator of the first element.
 
constexpr auto  end () noexcept
 Get the iterator after the last element.
 
constexpr auto  end () const noexcept
 Get the const iterator after the last element.
 
constexpr auto  cend () const noexcept
 Get the const iterator after the last element.
 
constexpr auto  rbegin () noexcept
 Get the reversed iterator of the last element.
 
constexpr auto  rbegin () const noexcept
 Get the reversed const iterator of the last element.
 
constexpr auto  crbegin () const noexcept
 Get the reversed const iterator of the last element.
 
constexpr auto  rend () noexcept
 Get the reversed iterator before the first element.
 
constexpr auto  rend () const noexcept
 Get the reversed const iterator before the first element.
 
constexpr auto  crend () const noexcept
 Get the reversed const iterator before the first element.
 

Static Public Attributes

static constexpr int  InvalidIndex = -1
 Invalid index.
 

Detailed Description

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
class AMCAX::IndexMap< key_type, value_type, hasher, keyeq >

Template class of indexed map.

Template Parameters
key_typeType of key
value_typeType of value
hasherType of hasher
keyeqType of equality operator

Constructor & Destructor Documentation

◆ IndexMap() [1/2]

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
template<typename iter>
AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::IndexMap ( iter first ,
iter last  )
inline explicit

Construct from a range of key-value pairs.

Template Parameters
iterThe type of iterator
Parameters
firstThe first iterator
lastThe last iterator

◆ IndexMap() [2/2]

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::IndexMap ( const IndexMap< key_type, value_type, hasher, keyeq > & other )
inline

Copy constructor.

Parameters
otherThe other indexed map

Member Function Documentation

◆ begin() [1/2]

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
auto AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::begin ( ) const
inline nodiscard constexpr noexcept

Get the const iterator of the first element.

Returns
The const begin iterator

◆ begin() [2/2]

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
auto AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::begin ( )
inline nodiscard constexpr noexcept

Get the iterator of the first element.

Returns
The begin iterator

◆ cbegin()

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
auto AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::cbegin ( ) const
inline nodiscard constexpr noexcept

Get the const iterator of the first element.

Returns
The const begin iterator

◆ cend()

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
auto AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::cend ( ) const
inline nodiscard constexpr noexcept

Get the const iterator after the last element.

Returns
The end iterator

◆ contains()

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
bool AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::contains ( const key_type & key ) const
inline nodiscard

Does the map contain a given key.

Parameters
keyThe given key
Returns
True if the map contains the given key

◆ crbegin()

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
auto AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::crbegin ( ) const
inline nodiscard constexpr noexcept

Get the reversed const iterator of the last element.

Returns
The reversed const begin iterator

◆ crend()

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
auto AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::crend ( ) const
inline nodiscard constexpr noexcept

Get the reversed const iterator before the first element.

Returns
The reversed const end iterator

◆ emplace()

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
template<typename K, typename... V>
int AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::emplace ( K && key ,
V &&... value  )
inline

Insert a key with a value in-place construction.

If there is a key in the map equal to the given key, do nothing and return the index of the existed key

Template Parameters
KType of new key
...VType of new value
Parameters
keyThe new key
...valueThe new value
Returns
The index of the new key

◆ empty()

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
bool AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::empty ( ) const
inline nodiscard noexcept

Is the map empty.

Returns
True if the map is empty

◆ end() [1/2]

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
auto AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::end ( ) const
inline nodiscard constexpr noexcept

Get the const iterator after the last element.

Returns
The end iterator

◆ end() [2/2]

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
auto AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::end ( )
inline nodiscard constexpr noexcept

Get the iterator after the last element.

Returns
The end iterator

◆ erase()

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
bool AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::erase ( int i )
inline noexcept

Erase the i-th element.

Swap the last element and the i-th element and erase the last element

Parameters
iThe index
Returns
True if the index is valid

◆ erase_key()

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
bool AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::erase_key ( const key_type & key )
inline

Erase a key if it is in the map.

Parameters
keyThe key
Returns
True if the key is erased

◆ erase_last()

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
bool AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::erase_last ( )
inline noexcept

Erase the last element.

Returns
True if the set is not empty

◆ exchange()

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
bool AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::exchange ( int i ,
int j  )
inline noexcept

Exchange the i-th and j-th elements.

Parameters
iThe first index
jThe second index
Returns
True if the indices are valid

◆ find_key()

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
const key_type & AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::find_key ( int id ) const
inline nodiscard noexcept

Get the i-th key.

Parameters
idThe index
Returns
The i-th key

◆ index()

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
int AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::index ( const key_type & key ) const
inline nodiscard

Get the index of a given key.

Parameters
keyThe given key
Returns
The index of the given key if the key is found, or the invalid index otherwise

◆ insert()

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
template<typename K, typename V>
int AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::insert ( K && key ,
V && value  )
inline

Insert a key with a value.

If there is a key in the map equal to the given key, do nothing and return the index of the existed key

Template Parameters
KType of new key
VType of new value
Parameters
keyThe new key
valueThe new value
Returns
The index of the new key

◆ insert_replace()

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
template<typename K, typename V>
int AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::insert_replace ( K && key ,
V && value  )
inline

Insert a new key with a value or replace a value of an existed key.

Template Parameters
KType of new key
VType of new value
Parameters
keyThe new key
valueThe new value
Returns
The index of the new key or the existed key

◆ operator()() [1/2]

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
const value_type & AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::operator() ( int id ) const
inline nodiscard noexcept

Get the i-th value.

Parameters
idThe index
Returns
The i-th value

◆ operator()() [2/2]

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
value_type & AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::operator() ( int id )
inline nodiscard noexcept

Get the i-th value.

Parameters
idThe index
Returns
The i-th value

◆ operator=()

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
IndexMap & AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::operator= ( const IndexMap< key_type, value_type, hasher, keyeq > & other )
inline

Copy assignment.

Copy the map and reconstruct the vector of iterators

Parameters
otherThe other indexed map
Returns
The reference to self

◆ operator[]() [1/2]

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
value_type & AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::operator[] ( const key_type & key )
inline nodiscard

Get the value from a key.

Parameters
keyThe key
Returns
The correspoding value

◆ operator[]() [2/2]

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
const value_type & AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::operator[] ( const key_type & key ) const
inline nodiscard

Get the value from a key.

Parameters
keyThe key
Returns
The corresponding value

◆ rbegin() [1/2]

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
auto AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::rbegin ( ) const
inline nodiscard constexpr noexcept

Get the reversed const iterator of the last element.

Returns
The reversed const begin iterator

◆ rbegin() [2/2]

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
auto AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::rbegin ( )
inline nodiscard constexpr noexcept

Get the reversed iterator of the last element.

Returns
The revserd begin iterator

◆ rend() [1/2]

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
auto AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::rend ( ) const
inline nodiscard constexpr noexcept

Get the reversed const iterator before the first element.

Returns
The reversed const end iterator

◆ rend() [2/2]

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
auto AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::rend ( )
inline nodiscard constexpr noexcept

Get the reversed iterator before the first element.

Returns
The reversed end iterator

◆ set()

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
template<typename K, typename... T>
bool AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::set ( int i ,
K && key ,
T &&... value  )
inline

Set the i-th element by a given key and a given value.

Template Parameters
KType of new key
...TType of new value
Parameters
iThe index
keyThe key
...valueThe value
Returns
True if the index is valid, and the key is not in the set

◆ size()

template<class key_type, class value_type, class hasher = std::hash<key_type>, class keyeq = std::equal_to<key_type>>
int AMCAX::IndexMap< key_type, value_type, hasher, keyeq >::size ( ) const
inline nodiscard noexcept

Get the size of the map.

Returns
The size of the map

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