CoreComponents 3.0.0
A Modern C++ Toolkit
Loading...
Searching...
No Matches
Map< K, T, O > Class Template Reference

Map data container More...

#include <cc/Map>

Public Types

using Key = K
 Key type.
 
using Value = T
 Value type.
 
using Item = KeyValue<K, T>
 Item type.
 
using Order = O
 Search order.
 

Public Member Functions

const auto & tree () const
 

Standard Iterators

using value_type = Item
 Item value type.
 
using size_type = long
 Type of the container capacity.
 
using iterator = Iterator<BucketVector<Item>>
 Value iterator.
 
using const_iterator = Iterator<const BucketVector<Item>>
 Readonly value iterator.
 
using reverse_iterator = ReverseIterator<BucketVector<Item>>
 Reverse value iterator.
 
using const_reverse_iterator = ReverseIterator<const BucketVector<Item>>
 Readonly reverse value iterator.
 
iterator begin ()
 Return iterator pointing to the first item (if any)
 
iterator end ()
 Return iterator pointing behind the last item
 
const_iterator begin () const
 Return readonly iterator pointing to the first item (if any)
 
const_iterator cbegin () const
 Return readonly iterator pointing to the first item (if any)
 
const_iterator end () const
 Return readonly iterator pointing behind the last item
 
const_iterator cend () const
 Return readonly iterator pointing behind the last item
 
reverse_iterator rbegin ()
 Return reverse iterator pointing to the last item (if any)
 
reverse_iterator rend ()
 Return reverse iterator pointing before the first item
 
const_reverse_iterator rbegin () const
 Return readonly reverse iterator pointing to the last item (if any)
 
const_reverse_iterator crbegin () const
 Return readonly reverse iterator pointing to the last item (if any)
 
const_reverse_iterator rend () const
 Return reverse iterator pointing before the first item
 
const_reverse_iterator crend () const
 Return reverse iterator pointing before the first item
 

Construction and Assignment

 Map ()=default
 Construct an empty map.
 
 Map (const Map &other)=default
 Construct a copy of other.
 
 Map (std::initializer_list< Item > items)
 Construct with initial items.
 
 Map (Map &&other)
 Take over the right-side map other.
 
Mapoperator= (const Map &other)=default
 Assign map other.
 
Mapoperator= (Map &&other)
 Take over the right-side map other.
 
List< ItemtoList () const
 Get corresponding list representation.
 

Item Access

Locator head () const
 Get a locator pointing to the first item.
 
Locator tail () const
 Get a locator pointing to the last item.
 
Locator from (long index) const
 Get a locator pointing to the item at index.
 
long count () const
 Get the number of items stored in the map.
 
long size () const
 Get the number of items stored in the map.
 
bool has (long i) const
 Check if i is a valid index.
 
 operator bool () const
 Check if this map is non-empty.
 
long operator+ () const
 Get the number of items stored in the map.
 
const Itemat (Locator pos) const
 Get constant reference to the item at position pos.
 
const Itemat (long index) const
 Get constant reference to the item at index.
 
Itemat (Locator pos)
 Get reference to the item at position pos.
 
Itemat (long index)
 Get reference to the item at index.
 
const Itemoperator[] (long index) const
 Get constant reference to the item at index.
 
const Itemfirst () const
 Get constant reference to first item.
 
const Itemlast () const
 Get constant reference to last item.
 
bool firstIs (const Item &item) const
 Tell if item is the first item.
 
bool lastIs (const Item &item) const
 Tell if item is the last item.
 
const Itemmin () const
 Get constant reference to the first item.
 
const Itemmax () const
 Get constant reference to the last item.
 

Map Operations

template<class Pattern >
bool find (const Pattern &pattern, Out< Locator > pos=None{}) const
 Search for a key in the map.
 
template<class Pattern >
bool lookup (const Pattern &pattern, Out< Value > value) const
 Search for pattern and return value.
 
template<class Pattern >
bool contains (const Pattern &pattern) const
 Convenience function to check if the map contains pattern.
 
template<class Pattern >
Locator lowerBound (const Pattern &pattern) const
 Convenience function to find the start of a search range.
 
template<class Pattern >
Locator upperBound (const Pattern &pattern) const
 Convenience function to find the end of a search range.
 
bool insert (const Key &key, const Value &value, Out< Locator > pos=None{})
 Insert a new key-value pair if the map doesn't contain the key already.
 
void establish (const Key &key, const Value &value)
 Insert a new or overwrite an existing key-value mapping.
 
bool remove (const Key &key)
 Remove the given key from the map.
 
Value value (const Key &key, const Value &fallback) const
 Map key to value (or return fallback value)
 
Value value (const Key &key) const
 Map key to value
 
Value operator() (const Key &key) const
 Map key to value
 
Valueoperator() (const Key &key)
 Make sure key exists and return a reference to its value
 

Positional Operations

void removeAt (Locator &pos)
 Remove item at position pos (pos is automatically advanced to the next item)
 
void removeAt (long index)
 Remove item at index.
 

Global Operations

template<class F >
void forEach (F f) const
 Call function f for each item.
 
template<class F , class Pattern >
void forEachInRange (const Pattern &lower, const Pattern &upper, F f) const
 Call function f for each item in range [lower, upper].
 
void deplete ()
 Remove all items.
 

Comparism Operators

template<class Other >
bool operator== (const Other &other) const
 Equality operator.
 

Detailed Description

template<class K, class T = K, class O = DefaultOrder>
class cc::Map< K, T, O >

Map data container

Template Parameters
KKey type
TValue type
OSearch order

Member Typedef Documentation

◆ Key

template<class K , class T = K, class O = DefaultOrder>
using Key = K

Key type.

◆ Value

template<class K , class T = K, class O = DefaultOrder>
using Value = T

Value type.

◆ Item

template<class K , class T = K, class O = DefaultOrder>
using Item = KeyValue<K, T>

Item type.

◆ Order

template<class K , class T = K, class O = DefaultOrder>
using Order = O

Search order.

◆ value_type

template<class K , class T = K, class O = DefaultOrder>
using value_type = Item

Item value type.

◆ size_type

template<class K , class T = K, class O = DefaultOrder>
using size_type = long

Type of the container capacity.

◆ iterator

template<class K , class T = K, class O = DefaultOrder>
using iterator = Iterator<BucketVector<Item>>

Value iterator.

◆ const_iterator

template<class K , class T = K, class O = DefaultOrder>
using const_iterator = Iterator<const BucketVector<Item>>

Readonly value iterator.

◆ reverse_iterator

template<class K , class T = K, class O = DefaultOrder>
using reverse_iterator = ReverseIterator<BucketVector<Item>>

Reverse value iterator.

◆ const_reverse_iterator

template<class K , class T = K, class O = DefaultOrder>
using const_reverse_iterator = ReverseIterator<const BucketVector<Item>>

Readonly reverse value iterator.

Constructor & Destructor Documentation

◆ Map() [1/4]

template<class K , class T = K, class O = DefaultOrder>
Map ( )
default

Construct an empty map.

◆ Map() [2/4]

template<class K , class T = K, class O = DefaultOrder>
Map ( const Map< K, T, O > & other)
default

Construct a copy of other.

◆ Map() [3/4]

template<class K , class T = K, class O = DefaultOrder>
Map ( std::initializer_list< Item > items)

Construct with initial items.

◆ Map() [4/4]

template<class K , class T = K, class O = DefaultOrder>
Map ( Map< K, T, O > && other)

Take over the right-side map other.

Member Function Documentation

◆ operator=() [1/2]

template<class K , class T = K, class O = DefaultOrder>
Map & operator= ( const Map< K, T, O > & other)
default

Assign map other.

◆ operator=() [2/2]

template<class K , class T = K, class O = DefaultOrder>
Map & operator= ( Map< K, T, O > && other)

Take over the right-side map other.

◆ toList()

template<class K , class T = K, class O = DefaultOrder>
List< Item > toList ( ) const

Get corresponding list representation.

◆ head()

template<class K , class T = K, class O = DefaultOrder>
Locator head ( ) const

Get a locator pointing to the first item.

◆ tail()

template<class K , class T = K, class O = DefaultOrder>
Locator tail ( ) const

Get a locator pointing to the last item.

◆ from()

template<class K , class T = K, class O = DefaultOrder>
Locator from ( long index) const

Get a locator pointing to the item at index.

◆ count()

template<class K , class T = K, class O = DefaultOrder>
long count ( ) const

Get the number of items stored in the map.

◆ size()

template<class K , class T = K, class O = DefaultOrder>
long size ( ) const

Get the number of items stored in the map.

◆ has()

template<class K , class T = K, class O = DefaultOrder>
bool has ( long i) const

Check if i is a valid index.

◆ operator bool()

template<class K , class T = K, class O = DefaultOrder>
operator bool ( ) const
explicit

Check if this map is non-empty.

◆ operator+()

template<class K , class T = K, class O = DefaultOrder>
long operator+ ( ) const

Get the number of items stored in the map.

◆ at() [1/4]

template<class K , class T = K, class O = DefaultOrder>
const Item & at ( Locator pos) const

Get constant reference to the item at position pos.

◆ at() [2/4]

template<class K , class T = K, class O = DefaultOrder>
const Item & at ( long index) const

Get constant reference to the item at index.

◆ at() [3/4]

template<class K , class T = K, class O = DefaultOrder>
Item & at ( Locator pos)

Get reference to the item at position pos.

◆ at() [4/4]

template<class K , class T = K, class O = DefaultOrder>
Item & at ( long index)

Get reference to the item at index.

◆ operator[]()

template<class K , class T = K, class O = DefaultOrder>
const Item & operator[] ( long index) const

Get constant reference to the item at index.

◆ first()

template<class K , class T = K, class O = DefaultOrder>
const Item & first ( ) const

Get constant reference to first item.

◆ last()

template<class K , class T = K, class O = DefaultOrder>
const Item & last ( ) const

Get constant reference to last item.

◆ firstIs()

template<class K , class T = K, class O = DefaultOrder>
bool firstIs ( const Item & item) const

Tell if item is the first item.

Todo
needs unit test

◆ lastIs()

template<class K , class T = K, class O = DefaultOrder>
bool lastIs ( const Item & item) const

Tell if item is the last item.

◆ min()

template<class K , class T = K, class O = DefaultOrder>
const Item & min ( ) const

Get constant reference to the first item.

◆ max()

template<class K , class T = K, class O = DefaultOrder>
const Item & max ( ) const

Get constant reference to the last item.

◆ find()

template<class K , class T = K, class O = DefaultOrder>
template<class Pattern >
bool find ( const Pattern & pattern,
Out< Locator > pos = None{} ) const

Search for a key in the map.

Template Parameters
PatternPattern type (must be comparable with Key)
Parameters
patternPattern to search for
posReturns the position of the item that was found
Returns
True if pattern was found
Note
The returned Locator pos will always point to the first element greater or equal pattern.

◆ lookup()

template<class K , class T = K, class O = DefaultOrder>
template<class Pattern >
bool lookup ( const Pattern & pattern,
Out< Value > value ) const

Search for pattern and return value.

Template Parameters
PatternPattern type (must be comparable with Key)
Returns
True if pattern was found

◆ contains()

template<class K , class T = K, class O = DefaultOrder>
template<class Pattern >
bool contains ( const Pattern & pattern) const

Convenience function to check if the map contains pattern.

Template Parameters
PatternPattern type (must be comparable with Key)
Parameters
patternPattern to search for
Returns
True if pattern was found

◆ lowerBound()

template<class K , class T = K, class O = DefaultOrder>
template<class Pattern >
Locator lowerBound ( const Pattern & pattern) const

Convenience function to find the start of a search range.

Template Parameters
PatternPattern type (must be comparable with Key)
Parameters
patternPattern to search for
Returns
Locator position
Note
pattern is included in the search range
See also
forEachInRange()

◆ upperBound()

template<class K , class T = K, class O = DefaultOrder>
template<class Pattern >
Locator upperBound ( const Pattern & pattern) const

Convenience function to find the end of a search range.

Template Parameters
PatternPattern type (must be comparable with Key)
Parameters
patternPattern to search for
Returns
Locator position
Note
pattern is included in the search range
See also
forEachInRange()

◆ insert()

template<class K , class T = K, class O = DefaultOrder>
bool insert ( const Key & key,
const Value & value,
Out< Locator > pos = None{} )

Insert a new key-value pair if the map doesn't contain the key already.

Parameters
keySearch key
valueNew value
posReturns a locator pointing to the existing or newly inserted key-value pair
Returns
True if the new key-value pair was inserted successfully

◆ establish()

template<class K , class T = K, class O = DefaultOrder>
void establish ( const Key & key,
const Value & value )

Insert a new or overwrite an existing key-value mapping.

Parameters
keySearch key
valueNew value

◆ remove()

template<class K , class T = K, class O = DefaultOrder>
bool remove ( const Key & key)

Remove the given key from the map.

Returns
True if a matching key-value pair was found and removed

◆ value() [1/2]

template<class K , class T = K, class O = DefaultOrder>
Value value ( const Key & key,
const Value & fallback ) const

Map key to value (or return fallback value)

◆ value() [2/2]

template<class K , class T = K, class O = DefaultOrder>
Value value ( const Key & key) const

Map key to value

◆ operator()() [1/2]

template<class K , class T = K, class O = DefaultOrder>
Value operator() ( const Key & key) const

Map key to value

◆ operator()() [2/2]

template<class K , class T = K, class O = DefaultOrder>
Value & operator() ( const Key & key)

Make sure key exists and return a reference to its value

◆ removeAt() [1/2]

template<class K , class T = K, class O = DefaultOrder>
void removeAt ( Locator & pos)

Remove item at position pos (pos is automatically advanced to the next item)

◆ removeAt() [2/2]

template<class K , class T = K, class O = DefaultOrder>
void removeAt ( long index)

Remove item at index.

◆ forEach()

template<class K , class T = K, class O = DefaultOrder>
template<class F >
void forEach ( F f) const

Call function f for each item.

Template Parameters
FFunction type (lambda or functor)
Parameters
fUnary function which gets called for each item

◆ forEachInRange()

template<class K , class T = K, class O = DefaultOrder>
template<class F , class Pattern >
void forEachInRange ( const Pattern & lower,
const Pattern & upper,
F f ) const

Call function f for each item in range [lower, upper].

Template Parameters
FFunction type (lambda or functor)
Parameters
lowerLower boundary
upperUpper boundary
fUnary function which gets called for each item
Note
lower and upper are included in the iteration range.

◆ deplete()

template<class K , class T = K, class O = DefaultOrder>
void deplete ( )

Remove all items.

◆ begin() [1/2]

template<class K , class T = K, class O = DefaultOrder>
iterator begin ( )

Return iterator pointing to the first item (if any)

◆ end() [1/2]

template<class K , class T = K, class O = DefaultOrder>
iterator end ( )

Return iterator pointing behind the last item

◆ begin() [2/2]

template<class K , class T = K, class O = DefaultOrder>
const_iterator begin ( ) const

Return readonly iterator pointing to the first item (if any)

◆ cbegin()

template<class K , class T = K, class O = DefaultOrder>
const_iterator cbegin ( ) const

Return readonly iterator pointing to the first item (if any)

◆ end() [2/2]

template<class K , class T = K, class O = DefaultOrder>
const_iterator end ( ) const

Return readonly iterator pointing behind the last item

◆ cend()

template<class K , class T = K, class O = DefaultOrder>
const_iterator cend ( ) const

Return readonly iterator pointing behind the last item

◆ rbegin() [1/2]

template<class K , class T = K, class O = DefaultOrder>
reverse_iterator rbegin ( )

Return reverse iterator pointing to the last item (if any)

◆ rend() [1/2]

template<class K , class T = K, class O = DefaultOrder>
reverse_iterator rend ( )

Return reverse iterator pointing before the first item

◆ rbegin() [2/2]

template<class K , class T = K, class O = DefaultOrder>
const_reverse_iterator rbegin ( ) const

Return readonly reverse iterator pointing to the last item (if any)

◆ crbegin()

template<class K , class T = K, class O = DefaultOrder>
const_reverse_iterator crbegin ( ) const

Return readonly reverse iterator pointing to the last item (if any)

◆ rend() [2/2]

template<class K , class T = K, class O = DefaultOrder>
const_reverse_iterator rend ( ) const

Return reverse iterator pointing before the first item

◆ crend()

template<class K , class T = K, class O = DefaultOrder>
const_reverse_iterator crend ( ) const

Return reverse iterator pointing before the first item

◆ operator==()

template<class K , class T = K, class O = DefaultOrder>
template<class Other >
bool operator== ( const Other & other) const

Equality operator.