libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
HashMap< K, V > Class Template Reference

Unordered associative container wrapping std::unordered_map. More...

#include <hashmap.h>

Public Types

using Ptr = SharedPtr< HashMap >
 Shared pointer type for HashMap.
 
using Data = std::unordered_map< K, V >
 Underlying std::unordered_map storage type.
 
using Key = K
 Key type.
 
using Value = V
 Value type.
 
using Iterator = typename Data::iterator
 Mutable forward iterator.
 
using ConstIterator = typename Data::const_iterator
 Const forward iterator.
 

Public Member Functions

 HashMap ()=default
 Default constructor. Creates an empty hash map.
 
 HashMap (const HashMap &other)
 Copy constructor.
 
 HashMap (HashMap &&other) noexcept
 Move constructor.
 
 HashMap (std::initializer_list< std::pair< const K, V > > initList)
 Constructs a hash map from an initializer list of key-value pairs.
 
 ~HashMap ()=default
 Destructor.
 
HashMapoperator= (const HashMap &other)
 Copy assignment operator.
 
HashMapoperator= (HashMap &&other) noexcept
 Move assignment operator.
 
Iterator begin () noexcept
 Returns a mutable iterator to the first entry.
 
ConstIterator begin () const noexcept
 Returns a const iterator to the first entry.
 
ConstIterator cbegin () const noexcept
 Returns a const iterator to the first entry.
 
ConstIterator constBegin () const noexcept
 Returns a const iterator to the first entry.
 
Iterator end () noexcept
 Returns a mutable iterator to one past the last entry.
 
ConstIterator end () const noexcept
 Returns a const iterator to one past the last entry.
 
ConstIterator cend () const noexcept
 Returns a const iterator to one past the last entry.
 
ConstIterator constEnd () const noexcept
 Returns a const iterator to one past the last entry.
 
bool isEmpty () const noexcept
 Returns true if the hash map has no entries.
 
size_t size () const noexcept
 Returns the number of key-value pairs.
 
Voperator[] (const K &key)
 Returns a reference to the value for key, inserting a default-constructed value if it does not exist.
 
const Voperator[] (const K &key) const
 Returns a const reference to the value for key.
 
V value (const K &key, const V &defaultValue=V{}) const
 Returns the value for key, or defaultValue if the key is not present.
 
bool contains (const K &key) const
 Returns true if key exists in the hash map.
 
Iterator find (const K &key)
 Finds the entry for key.
 
ConstIterator find (const K &key) const
 Const overload of find().
 
void insert (const K &key, const V &val)
 Inserts or assigns a key-value pair.
 
void insert (const K &key, V &&val)
 Inserts or assigns a key-value pair (move overload).
 
bool remove (const K &key)
 Removes the entry for key.
 
Iterator remove (Iterator pos)
 Removes the entry at pos.
 
void clear () noexcept
 Removes all entries.
 
void swap (HashMap &other) noexcept
 Swaps contents with another hash map.
 
List< Kkeys () const
 Returns a list of all keys.
 
List< Vvalues () const
 Returns a list of all values.
 
template<typename Func >
void forEach (Func &&func) const
 Calls func for every key-value pair.
 

Friends

bool operator== (const HashMap &lhs, const HashMap &rhs)
 Returns true if both hash maps have identical contents.
 
bool operator!= (const HashMap &lhs, const HashMap &rhs)
 Returns true if the hash maps differ.
 

Detailed Description

template<typename K, typename V>
class HashMap< K, V >

Unordered associative container wrapping std::unordered_map.

Provides a Qt-inspired API over std::unordered_map with consistent naming

  • Example
    cache.insert("width", 1920);
    cache.insert("height", 1080);
    int w = cache.value("width", 0); // 1920
    Dynamic array container wrapping std::vector.
    Definition list.h:40
    Iterator insert(ConstIterator pos, const T &value)
    Inserts a value before the position given by an iterator.
    Definition list.h:341
    conventions matching the rest of libpromeki.
    Template Parameters
    KKey type (must be hashable).
    VValue type.

Constructor & Destructor Documentation

◆ HashMap()

template<typename K , typename V >
HashMap< K, V >::HashMap ( std::initializer_list< std::pair< const K, V > >  initList)
inline

Constructs a hash map from an initializer list of key-value pairs.

Parameters
initListBrace-enclosed list of {key, value} pairs.

Member Function Documentation

◆ constBegin()

template<typename K , typename V >
ConstIterator HashMap< K, V >::constBegin ( ) const
inlinenoexcept

Returns a const iterator to the first entry.

◆ constEnd()

template<typename K , typename V >
ConstIterator HashMap< K, V >::constEnd ( ) const
inlinenoexcept

Returns a const iterator to one past the last entry.

◆ find()

template<typename K , typename V >
Iterator HashMap< K, V >::find ( const K key)
inline

Finds the entry for key.

Parameters
keyThe key to search for.
Returns
Iterator to the entry, or end() if not found.

◆ forEach()

template<typename K , typename V >
template<typename Func >
void HashMap< K, V >::forEach ( Func &&  func) const
inline

Calls func for every key-value pair.

Template Parameters
FuncCallable with signature void(const K &, const V &).
Parameters
funcThe function to invoke.

◆ insert() [1/2]

template<typename K , typename V >
void HashMap< K, V >::insert ( const K key,
const V val 
)
inline

Inserts or assigns a key-value pair.

Parameters
keyThe key.
valThe value.

◆ insert() [2/2]

template<typename K , typename V >
void HashMap< K, V >::insert ( const K key,
V &&  val 
)
inline

Inserts or assigns a key-value pair (move overload).

Parameters
keyThe key.
valThe value (moved).

◆ operator[]() [1/2]

template<typename K , typename V >
V & HashMap< K, V >::operator[] ( const K key)
inline

Returns a reference to the value for key, inserting a default-constructed value if it does not exist.

Parameters
keyThe key to look up.
Returns
Reference to the mapped value.

◆ operator[]() [2/2]

template<typename K , typename V >
const V & HashMap< K, V >::operator[] ( const K key) const
inline

Returns a const reference to the value for key.

The key must already exist; behavior is undefined otherwise. Prefer value() or find() when the key may not exist.

Parameters
keyThe key to look up.
Returns
Const reference to the mapped value.

◆ remove() [1/2]

template<typename K , typename V >
bool HashMap< K, V >::remove ( const K key)
inline

Removes the entry for key.

Parameters
keyThe key to remove.
Returns
True if an entry was removed, false if the key was not found.

◆ remove() [2/2]

template<typename K , typename V >
Iterator HashMap< K, V >::remove ( Iterator  pos)
inline

Removes the entry at pos.

Parameters
posIterator to the entry to remove.
Returns
Iterator to the next entry.

◆ swap()

template<typename K , typename V >
void HashMap< K, V >::swap ( HashMap< K, V > &  other)
inlinenoexcept

Swaps contents with another hash map.

Parameters
otherThe hash map to swap with.

◆ value()

template<typename K , typename V >
V HashMap< K, V >::value ( const K key,
const V defaultValue = V{} 
) const
inline

Returns the value for key, or defaultValue if the key is not present.

Parameters
keyThe key to look up.
defaultValueFallback value.
Returns
The mapped value or the default.

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