SPH
Classes | Public Member Functions | List of all members
FlatMap< TKey, TValue, TLess > Class Template Reference

Container of key-value pairs. More...

#include <FlatMap.h>

Inheritance diagram for FlatMap< TKey, TValue, TLess >:
Noncopyable

Classes

struct  Element
 Element of the container. More...
 

Public Member Functions

 FlatMap ()=default
 
 FlatMap (std::initializer_list< Element > list)
 Constructs the map fromm initializer list of elements. More...
 
INLINE TValue & operator[] (const TKey &key)
 Returns a reference to the element, given its key. More...
 
INLINE const TValue & operator[] (const TKey &key) const
 Returns a reference to the element, given its key. More...
 
INLINE TValue & insert (const TKey &key, const TValue &value)
 Adds a new element into the map or sets new value of element with the same key. More...
 
INLINE TValue & insert (const TKey &key, TValue &&value)
 Adds a new element into the map or sets new value of element with the same key. More...
 
INLINE void remove (const TKey &key)
 Removes element with given key from the map. More...
 
INLINE bool tryRemove (const TKey &key)
 Removes element with given key if present, otherwise it does nothing. More...
 
INLINE void clear ()
 Removes all elements from the map. More...
 
INLINE Optional< TValue & > tryGet (const TKey &key)
 Returns a reference to the value matching the given key, or NOTHING if no such value exists. More...
 
INLINE Optional< const TValue & > tryGet (const TKey &key) const
 Returns a reference to the value matching the given key, or NOTHING if no such value exists. More...
 
INLINE bool contains (const TKey &key) const
 Returns true if the map contains element of given key. More...
 
INLINE Size size () const
 Returns the number of elements in the map. More...
 
INLINE Size empty () const
 Returns true if the map contains no elements, false otherwise. More...
 
INLINE Iterator< Elementbegin ()
 Returns the iterator pointing to the first element. More...
 
INLINE Iterator< const Elementbegin () const
 Returns the iterator pointing to the first element. More...
 
INLINE Iterator< Elementend ()
 Returns the iterator pointing to the one-past-last element. More...
 
INLINE Iterator< const Elementend () const
 Returns the iterator pointing to the one-past-last element. More...
 
INLINE operator ArrayView< Element > ()
 
INLINE operator ArrayView< const Element > () const
 
FlatMap clone () const
 

Detailed Description

template<typename TKey, typename TValue, typename TLess = std::less<TKey>>
class FlatMap< TKey, TValue, TLess >

Container of key-value pairs.

Elements are stored in an array sorted according to key. The value look-up is O(log(N)), while inserting or deletion of elements is currently O(N).

Definition at line 19 of file FlatMap.h.

Constructor & Destructor Documentation

◆ FlatMap() [1/2]

template<typename TKey , typename TValue , typename TLess = std::less<TKey>>
FlatMap< TKey, TValue, TLess >::FlatMap ( )
default

◆ FlatMap() [2/2]

template<typename TKey , typename TValue , typename TLess = std::less<TKey>>
FlatMap< TKey, TValue, TLess >::FlatMap ( std::initializer_list< Element list)
inline

Constructs the map fromm initializer list of elements.

Elements do not have to be sorted in the initializer list, the keys of the elements have to be unique, i.e. each key has to be present at most once. This is checked by assert.

Definition at line 39 of file FlatMap.h.

Member Function Documentation

◆ begin() [1/2]

template<typename TKey , typename TValue , typename TLess = std::less<TKey>>
INLINE Iterator<Element> FlatMap< TKey, TValue, TLess >::begin ( )
inline

Returns the iterator pointing to the first element.

Definition at line 155 of file FlatMap.h.

◆ begin() [2/2]

template<typename TKey , typename TValue , typename TLess = std::less<TKey>>
INLINE Iterator<const Element> FlatMap< TKey, TValue, TLess >::begin ( ) const
inline

Returns the iterator pointing to the first element.

Definition at line 160 of file FlatMap.h.

◆ clear()

template<typename TKey , typename TValue , typename TLess = std::less<TKey>>
INLINE void FlatMap< TKey, TValue, TLess >::clear ( )
inline

Removes all elements from the map.

Definition at line 111 of file FlatMap.h.

◆ clone()

template<typename TKey , typename TValue , typename TLess = std::less<TKey>>
FlatMap FlatMap< TKey, TValue, TLess >::clone ( ) const
inline

Definition at line 182 of file FlatMap.h.

◆ contains()

template<typename TKey , typename TValue , typename TLess = std::less<TKey>>
INLINE bool FlatMap< TKey, TValue, TLess >::contains ( const TKey &  key) const
inline

Returns true if the map contains element of given key.

Equivalent to bool(tryGet(key)).

Definition at line 140 of file FlatMap.h.

◆ empty()

template<typename TKey , typename TValue , typename TLess = std::less<TKey>>
INLINE Size FlatMap< TKey, TValue, TLess >::empty ( ) const
inline

Returns true if the map contains no elements, false otherwise.

Definition at line 150 of file FlatMap.h.

◆ end() [1/2]

template<typename TKey , typename TValue , typename TLess = std::less<TKey>>
INLINE Iterator<Element> FlatMap< TKey, TValue, TLess >::end ( )
inline

Returns the iterator pointing to the one-past-last element.

Definition at line 165 of file FlatMap.h.

◆ end() [2/2]

template<typename TKey , typename TValue , typename TLess = std::less<TKey>>
INLINE Iterator<const Element> FlatMap< TKey, TValue, TLess >::end ( ) const
inline

Returns the iterator pointing to the one-past-last element.

Definition at line 170 of file FlatMap.h.

◆ insert() [1/2]

template<typename TKey , typename TValue , typename TLess = std::less<TKey>>
INLINE TValue& FlatMap< TKey, TValue, TLess >::insert ( const TKey &  key,
const TValue &  value 
)
inline

Adds a new element into the map or sets new value of element with the same key.

Definition at line 65 of file FlatMap.h.

◆ insert() [2/2]

template<typename TKey , typename TValue , typename TLess = std::less<TKey>>
INLINE TValue& FlatMap< TKey, TValue, TLess >::insert ( const TKey &  key,
TValue &&  value 
)
inline

Adds a new element into the map or sets new value of element with the same key.

Definition at line 76 of file FlatMap.h.

◆ operator ArrayView< const Element >()

template<typename TKey , typename TValue , typename TLess = std::less<TKey>>
INLINE FlatMap< TKey, TValue, TLess >::operator ArrayView< const Element > ( ) const
inline

Definition at line 178 of file FlatMap.h.

◆ operator ArrayView< Element >()

template<typename TKey , typename TValue , typename TLess = std::less<TKey>>
INLINE FlatMap< TKey, TValue, TLess >::operator ArrayView< Element > ( )
inline

Definition at line 174 of file FlatMap.h.

◆ operator[]() [1/2]

template<typename TKey , typename TValue , typename TLess = std::less<TKey>>
INLINE TValue& FlatMap< TKey, TValue, TLess >::operator[] ( const TKey &  key)
inline

Returns a reference to the element, given its key.

The element must exists in the map, checked by assert.

Definition at line 49 of file FlatMap.h.

◆ operator[]() [2/2]

template<typename TKey , typename TValue , typename TLess = std::less<TKey>>
INLINE const TValue& FlatMap< TKey, TValue, TLess >::operator[] ( const TKey &  key) const
inline

Returns a reference to the element, given its key.

The element must exists in the map, checked by assert.

Definition at line 58 of file FlatMap.h.

◆ remove()

template<typename TKey , typename TValue , typename TLess = std::less<TKey>>
INLINE void FlatMap< TKey, TValue, TLess >::remove ( const TKey &  key)
inline

Removes element with given key from the map.

The element must exists in the map, checked by assert.

Definition at line 89 of file FlatMap.h.

◆ size()

template<typename TKey , typename TValue , typename TLess = std::less<TKey>>
INLINE Size FlatMap< TKey, TValue, TLess >::size ( ) const
inline

Returns the number of elements in the map.

Definition at line 145 of file FlatMap.h.

◆ tryGet() [1/2]

template<typename TKey , typename TValue , typename TLess = std::less<TKey>>
INLINE Optional<TValue&> FlatMap< TKey, TValue, TLess >::tryGet ( const TKey &  key)
inline

Returns a reference to the value matching the given key, or NOTHING if no such value exists.

Safe alternative to operator[].

Definition at line 118 of file FlatMap.h.

◆ tryGet() [2/2]

template<typename TKey , typename TValue , typename TLess = std::less<TKey>>
INLINE Optional<const TValue&> FlatMap< TKey, TValue, TLess >::tryGet ( const TKey &  key) const
inline

Returns a reference to the value matching the given key, or NOTHING if no such value exists.

Definition at line 128 of file FlatMap.h.

◆ tryRemove()

template<typename TKey , typename TValue , typename TLess = std::less<TKey>>
INLINE bool FlatMap< TKey, TValue, TLess >::tryRemove ( const TKey &  key)
inline

Removes element with given key if present, otherwise it does nothing.

Returns
True if the element was removed, false otherwise.

Definition at line 99 of file FlatMap.h.


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