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

Ordered associative container wrapping std::map. More...

#include <map.h>

Public Types

using Ptr = SharedPtr< Map >
 Shared pointer type for Map.
 
using Data = std::map< K, V >
 Underlying std::map storage type.
 
using Iterator = typename Data::iterator
 Mutable forward iterator.
 
using ConstIterator = typename Data::const_iterator
 Const forward iterator.
 
using RevIterator = typename Data::reverse_iterator
 Mutable reverse iterator.
 
using ConstRevIterator = typename Data::const_reverse_iterator
 Const reverse iterator.
 

Public Member Functions

 Map ()=default
 Default constructor. Creates an empty map.
 
 Map (const Map &other)
 Copy constructor.
 
 Map (Map &&other) noexcept
 Move constructor.
 
 Map (std::initializer_list< std::pair< const K, V > > initList)
 Constructs a map from an initializer list of key-value pairs.
 
 ~Map ()=default
 Destructor.
 
Mapoperator= (const Map &other)
 Copy assignment operator.
 
Mapoperator= (Map &&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.
 
RevIterator rbegin () noexcept
 Returns a mutable reverse iterator to the last entry.
 
RevIterator revBegin () noexcept
 Returns a mutable reverse iterator to the last entry.
 
ConstRevIterator crbegin () const noexcept
 Returns a const reverse iterator to the last entry.
 
ConstRevIterator constRevBegin () const noexcept
 Returns a const reverse iterator to the last entry.
 
RevIterator rend () noexcept
 Returns a mutable reverse iterator to one before the first entry.
 
RevIterator revEnd () noexcept
 Returns a mutable reverse iterator to one before the first entry.
 
ConstRevIterator crend () const noexcept
 Returns a const reverse iterator to one before the first entry.
 
ConstRevIterator constRevEnd () const noexcept
 Returns a const reverse iterator to one before the first entry.
 
bool isEmpty () const noexcept
 Returns true if the 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 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 (Map &other) noexcept
 Swaps contents with another 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 Map &lhs, const Map &rhs)
 Returns true if both maps have identical contents.
 
bool operator!= (const Map &lhs, const Map &rhs)
 Returns true if the maps differ.
 

Detailed Description

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

Ordered associative container wrapping std::map.

Provides a Qt-inspired API over std::map with consistent naming conventions matching the rest of libpromeki.

Example
Map<String, int> ages = {{"Alice", 30}, {"Bob", 25}};
ages.insert("Carol", 28);
int a = ages.value("Alice", -1); // 30
bool found = ages.contains("Dave"); // false
ages.forEach([](const String &k, int v) { ... });
Dynamic array container wrapping std::vector.
Definition list.h:40
bool contains(const T &val) const
Returns true if the list contains the given value.
Definition list.h:593
void forEach(Func &&func) const
Calls func for every element.
Definition list.h:583
Iterator insert(ConstIterator pos, const T &value)
Inserts a value before the position given by an iterator.
Definition list.h:341
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35
Template Parameters
KKey type.
VValue type.

Constructor & Destructor Documentation

◆ Map()

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

Constructs a 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 Map< K, V >::constBegin ( ) const
inlinenoexcept

Returns a const iterator to the first entry.

◆ constEnd()

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

Returns a const iterator to one past the last entry.

◆ constRevBegin()

template<typename K , typename V >
ConstRevIterator Map< K, V >::constRevBegin ( ) const
inlinenoexcept

Returns a const reverse iterator to the last entry.

◆ constRevEnd()

template<typename K , typename V >
ConstRevIterator Map< K, V >::constRevEnd ( ) const
inlinenoexcept

Returns a const reverse iterator to one before the first entry.

◆ find()

template<typename K , typename V >
Iterator Map< 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 Map< 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 Map< 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 Map< 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 & Map< 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 & Map< 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 Map< 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 Map< K, V >::remove ( Iterator  pos)
inline

Removes the entry at pos.

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

◆ revBegin()

template<typename K , typename V >
RevIterator Map< K, V >::revBegin ( )
inlinenoexcept

Returns a mutable reverse iterator to the last entry.

◆ revEnd()

template<typename K , typename V >
RevIterator Map< K, V >::revEnd ( )
inlinenoexcept

Returns a mutable reverse iterator to one before the first entry.

◆ swap()

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

Swaps contents with another map.

Parameters
otherThe map to swap with.

◆ value()

template<typename K , typename V >
V Map< 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: