ArrayMap< K, V, SORTED, COMPARE, ARRAY > Class Template Reference

#include <arraymap.h>

Inheritance diagram for ArrayMap< K, V, SORTED, COMPARE, ARRAY >:

Detailed Description

template<typename K, typename V, Bool SORTED = true, typename COMPARE = DefaultCompare, typename ARRAY = BaseArraySelector<>>
class maxon::ArrayMap< K, V, SORTED, COMPARE, ARRAY >

An ArrayMap maps keys to values using an underlying array whose elements are key-value-pairs. It may be either sorted or unsorted. For very small maps, unsorted array maps are generally faster. For large maps (exceeding about 100 entries), you should consider using another implementation such as HashMap or BurstTrieMap because especially insertion and erasure become slow with array maps: For a sorted ArrayMap, those operations have a time cost of O(N), while searching has a time cost of O(log N).

Of all maps, the ArrayMap is the most efficient with respect to memory usage as it really only stores the key-value-pairs.

See HashMap for more examples on how to use maps in general.

Template Parameters
KType of keys.
VType of values.
SORTEDUse true for a sorted array.
COMPAREClass to be used to compare the keys.
ARRAYAn array selector template to choose the array implementation to use.
See also
$ref maps

Classes

class  EntryIteratorBase
 
class  IteratorTemplate
 
class  KeyIteratorBase
 
class  NonConstIteratorBase
 
class  ValueIteratorBase
 

Public Types

using IsArrayMap = std::true_type
 
using ArrayType = typename ArrayMapHelper< K, V, SORTED, COMPARE, ARRAY >::Array
 
using Super = MapBase< ArrayMap< K, V, SORTED, COMPARE, ARRAY >, K, V, ArrayType, COMPARE >
 
using Entry = Pair< K, V >
 
using IteratorBase = AutoIterator< const ArrayType >
 
using Iterator = IteratorTemplate< false, EntryIteratorBase >
 
using ConstIterator = IteratorTemplate< true, EntryIteratorBase >
 
using KeyIterator = IteratorTemplate< false, KeyIteratorBase >
 
using ConstKeyIterator = IteratorTemplate< true, KeyIteratorBase >
 
using ValueIterator = IteratorTemplate< false, ValueIteratorBase >
 
using ConstValueIterator = IteratorTemplate< true, ValueIteratorBase >
 
- Public Types inherited from MapBase0< ArrayMap< K, V, true, DefaultCompare, BaseArraySelector<> >, K, V, ArrayMapHelper< K, V, true, DefaultCompare, BaseArraySelector<> >::Array, DefaultCompare >
using MapType = ArrayMap< K, V, true, DefaultCompare, BaseArraySelector<> >
 
using Super = BaseCollection< ArrayMap< K, V, true, DefaultCompare, BaseArraySelector<> >, ArrayMapHelper< K, V, true, DefaultCompare, BaseArraySelector<> >::Array >
 
using KeyType = K
 
using ValueType = V
 
- Public Types inherited from BaseCollection< ArrayMap< K, V, true, DefaultCompare, BaseArraySelector<> >, ArrayMapHelper< K, V, true, DefaultCompare, BaseArraySelector<> >::Array >
using IsCollection = std::true_type
 

Public Member Functions

Iterator Begin ()
 
Iterator End ()
 
ConstIterator Begin () const
 
ConstIterator End () const
 
KeyIterator GetKeys ()
 
ConstKeyIterator GetKeys () const
 
ValueIterator GetValues ()
 
ConstValueIterator GetValues () const
 
 ArrayMap ()
 
 ArrayMap (ArrayMap &&src)
 
 MAXON_OPERATOR_MOVE_ASSIGNMENT (ArrayMap)
 
template<typename MAP , typename CMP >
SFINAEHelper< Bool, typename MAP::IsArrayMap, typename MAP::IsSorted, typename std::enable_if< STD_IS_REPLACEMENT(same, typename std::decay< CMP >::type, DefaultCompare)>::type >::type IsEqualImpl (const MAP &b, CMP &&cmp, OverloadRank1) const
 
Int GetOperationCountForSearch () const
 
ResultRef< V > InsertKey (const K &key, Bool &created=BoolLValue())
 
ResultRef< V > InsertKey (K &&key, Bool &created=BoolLValue())
 
ResultMemT< IteratorInsertEntry (const K &key, Bool &created=BoolLValue())
 
ResultMemT< IteratorInsertEntry (K &&key, Bool &created=BoolLValue())
 
ResultMemT< IteratorInsert (const K &key, const V &value, Bool &created=BoolLValue())
 
ResultMemT< IteratorInsert (K &&key, const V &value, Bool &created=BoolLValue())
 
ResultMemT< IteratorInsert (const K &key, V &&value, Bool &created=BoolLValue())
 
ResultMemT< IteratorInsert (K &&key, V &&value, Bool &created=BoolLValue())
 
template<typename KEYCOMPARE = COMPARE, typename KEY >
const V * FindValue (const KEY &key) const
 
template<typename KEYCOMPARE = COMPARE, typename KEY >
V * FindValue (const KEY &key)
 
Iterator Find (const K &key)
 
ConstIterator Find (const K &key) const
 
Iterator FindFloor (const K &key)
 
ConstIterator FindFloor (const K &key) const
 
Iterator FindCeiling (const K &key)
 
ConstIterator FindCeiling (const K &key) const
 
ResultOk< BoolErase (const K &key)
 
template<template< Bool > class SUPER>
IteratorTemplate< false, SUPER > Erase (const IteratorTemplate< false, SUPER > &position, Int eraseCnt=1)
 
const ArrayTypeGetUnderlyingArray () const
 
ArrayTypeGetUnderlyingArray ()
 
- Public Member Functions inherited from MapBase< ArrayMap< K, V, true, DefaultCompare, BaseArraySelector<> >, K, V, ArrayMapHelper< K, V, true, DefaultCompare, BaseArraySelector<> >::Array, DefaultCompare >
MAXON_ATTRIBUTE_FORCE_INLINE MapBase (ARGS &&... args)
 
MapImpl< ArrayMap< K, V, true, DefaultCompare, BaseArraySelector<> > & > ToMap ()
 
MapImpl< const ArrayMap< K, V, true, DefaultCompare, BaseArraySelector<> > & > ToMap () const
 
MAXON_ATTRIBUTE_FORCE_INLINE operator MapImpl< ArrayMap< K, V, true, DefaultCompare, BaseArraySelector<> > & > ()
 
MAXON_ATTRIBUTE_FORCE_INLINE operator MapImpl< const ArrayMap< K, V, true, DefaultCompare, BaseArraySelector<> > & > () const
 
- Public Member Functions inherited from MapBase0< ArrayMap< K, V, true, DefaultCompare, BaseArraySelector<> >, K, V, ArrayMapHelper< K, V, true, DefaultCompare, BaseArraySelector<> >::Array, DefaultCompare >
MAXON_ATTRIBUTE_FORCE_INLINE MapBase0 (ARGS &&... args)
 
MAXON_ATTRIBUTE_FORCE_INLINE Bool Contains (typename ByValueParam< K >::type key) const
 
MAXON_ATTRIBUTE_FORCE_INLINE SFINAEHelper< Bool, typename PAIR::KeyType >::type Contains (const PAIR &pair) const
 
ResultRef< V > Append (const K &key)
 
SFINAEHelper< ResultRef< V >, typename PAIR::KeyType >::type Append (const PAIR &pair)
 
Result< void > Add (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags=COLLECTION_RESIZE_FLAGS::ON_GROW_RESERVE_CAPACITY)
 
Result< void > AppendAll (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags=COLLECTION_RESIZE_FLAGS::ON_GROW_RESERVE_CAPACITY)
 
Result< void > AppendAllInverse (COLLECTION2 &&other)
 
Bool ContainsAllImpl (COLLECTION2 &&other, OverloadRank0) const
 
Result< void > SubtractImpl (COLLECTION2 &&other, OverloadRank0)
 
Bool IsEqualImpl (const COLLECTION2 &other, COMPARE &&cmp, OverloadRank0) const
 
UInt GetHashCode () const
 
- Public Member Functions inherited from BaseCollection< ArrayMap< K, V, true, DefaultCompare, BaseArraySelector<> >, ArrayMapHelper< K, V, true, DefaultCompare, BaseArraySelector<> >::Array >
MAXON_ATTRIBUTE_FORCE_INLINE BaseCollection (ARGS &&... args)
 
MAXON_ATTRIBUTE_FORCE_INLINE std::enable_if< maxon::IsCollection< COLLECTION2 >::value, Bool >::type operator== (const COLLECTION2 &other) const
 
MAXON_ATTRIBUTE_FORCE_INLINE std::enable_if< maxon::IsCollection< COLLECTION2 >::value, Bool >::type operator!= (const COLLECTION2 &other) const
 
MAXON_ATTRIBUTE_FORCE_INLINE std::enable_if< maxon::IsCollection< COLLECTION2 >::value &&!STD_IS_REPLACEMENT(same, typename std::decay< COMPARE >::type, EQUALITY), Bool >::type IsEqual (const COLLECTION2 &other, COMPARE &&cmp=COMPARE()) const
 
MAXON_ATTRIBUTE_FORCE_INLINE Result< void > AppendAll (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags=COLLECTION_RESIZE_FLAGS::ON_GROW_RESERVE_CAPACITY)
 
MAXON_ATTRIBUTE_FORCE_INLINE Result< void > CopyFrom (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags=COLLECTION_RESIZE_FLAGS::FIT_TO_SIZE)
 
MAXON_ATTRIBUTE_FORCE_INLINE Result< void > Subtract (COLLECTION2 &&other)
 
MAXON_ATTRIBUTE_FORCE_INLINE Result< void > Intersect (const COLLECTION2 &other)
 
Bool Intersects (const COLLECTION2 &other) const
 
MAXON_ATTRIBUTE_FORCE_INLINE Result< void > CopyFromImpl (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags, OverloadRank0)
 
Result< void > AppendAllImpl (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags, Bool overwrite, OverloadRank0)
 
Result< void > IntersectImpl (COLLECTION2 &&other, OverloadRank0)
 
MAXON_ATTRIBUTE_FORCE_INLINE Bool IsEmpty () const
 
MAXON_ATTRIBUTE_FORCE_INLINE Bool IsPopulated () const
 
String ToString (const FormatStatement *formatStatement) const
 
MAXON_ATTRIBUTE_FORCE_INLINE Bool ContainsAll (COLLECTION2 &&other) const
 
Bool ContainsAllImpl (COLLECTION2 &&other, OverloadRank0) const
 

Static Public Member Functions

static const EntryGetEntry (const V *value)
 
static EntryGetEntry (typename std::remove_const< V >::type *value)
 
- Static Public Member Functions inherited from MapBase0< ArrayMap< K, V, true, DefaultCompare, BaseArraySelector<> >, K, V, ArrayMapHelper< K, V, true, DefaultCompare, BaseArraySelector<> >::Array, DefaultCompare >
static const K & GetMapKey (const K &key)
 
static const K & GetMapKey (const PAIR &pair)
 

Additional Inherited Members

- Static Public Attributes inherited from MapBase0< ArrayMap< K, V, true, DefaultCompare, BaseArraySelector<> >, K, V, ArrayMapHelper< K, V, true, DefaultCompare, BaseArraySelector<> >::Array, DefaultCompare >
static const COLLECTION_KIND KIND
 

Member Typedef Documentation

◆ IsArrayMap

using IsArrayMap = std::true_type

◆ ArrayType

using ArrayType = typename ArrayMapHelper<K, V, SORTED, COMPARE, ARRAY>::Array

◆ Super

using Super = MapBase<ArrayMap<K, V, SORTED, COMPARE, ARRAY>, K, V, ArrayType, COMPARE>

◆ Entry

using Entry = Pair<K, V>

◆ IteratorBase

◆ Iterator

◆ ConstIterator

◆ KeyIterator

◆ ConstKeyIterator

◆ ValueIterator

◆ ConstValueIterator

Constructor & Destructor Documentation

◆ ArrayMap() [1/2]

ArrayMap ( )

◆ ArrayMap() [2/2]

ArrayMap ( ArrayMap< K, V, SORTED, COMPARE, ARRAY > &&  src)

Member Function Documentation

◆ GetEntry() [1/2]

static const Entry* GetEntry ( const V *  value)
static

Returns the pointer to the entry to which #value belongs. You must not use this function if you cannot guarantee that value is a part of an entry.

Parameters
[in]valueA pointer to a value which is known to belong to an entry.
Returns
The entry of which #value is a part.

◆ GetEntry() [2/2]

static Entry* GetEntry ( typename std::remove_const< V >::type *  value)
static

Returns the pointer to the entry to which #value belongs. You must not use this function if you cannot guarantee that value is a part of an entry.

Parameters
[in]valueA pointer to a value which is known to belong to an entry.
Returns
The entry of which #value is a part.

◆ Begin() [1/2]

Iterator Begin ( )

Returns an iterator pointing to the first entry of this map. For a sorted map, the iteration order corresponds to the order of the keys.

Returns
Iterator for this map pointing to the first element.

◆ End() [1/2]

Iterator End ( )

Returns an iterator pointing just behind the last entry of this map. For a sorted map, the iteration order corresponds to the order of the keys.

Returns
Iterator for this map pointing behind the last element.

◆ Begin() [2/2]

ConstIterator Begin ( ) const

Returns an iterator pointing to the first entry of this map. For a sorted map, the iteration order corresponds to the order of the keys.

Returns
Iterator for this map pointing to the first element.

◆ End() [2/2]

ConstIterator End ( ) const

Returns an iterator pointing just behind the last entry of this map. For a sorted map, the iteration order corresponds to the order of the keys.

Returns
Iterator for this map pointing behind the last element.

◆ GetKeys() [1/2]

KeyIterator GetKeys ( )

Returns a foreach iterator to iterate over all keys of this map. For a sorted map, this will yield all keys in ascending order.

Returns
Foreach iterator over all keys.

◆ GetKeys() [2/2]

ConstKeyIterator GetKeys ( ) const

Returns a foreach iterator to iterate over all keys of this map. For a sorted map, this will yield all keys in ascending order.

Returns
Foreach iterator over all keys.

◆ GetValues() [1/2]

ValueIterator GetValues ( )

Returns a foreach iterator to iterate over all values of this map. For a sorted map, this will yield all values in ascending order of the corresponding keys.

Returns
Foreach iterator over all values.

◆ GetValues() [2/2]

ConstValueIterator GetValues ( ) const

Returns a foreach iterator to iterate over all values of this map. For a sorted map, this will yield all values in ascending order of the corresponding keys.

Returns
Foreach iterator over all values.

◆ MAXON_OPERATOR_MOVE_ASSIGNMENT()

MAXON_OPERATOR_MOVE_ASSIGNMENT ( ArrayMap< K, V, SORTED, COMPARE, ARRAY >  )

◆ IsEqualImpl()

SFINAEHelper<Bool, typename MAP::IsArrayMap, typename MAP::IsSorted, typename std::enable_if<STD_IS_REPLACEMENT(same, typename std::decay<CMP>::type, DefaultCompare)>::type>::type IsEqualImpl ( const MAP &  b,
CMP &&  cmp,
OverloadRank1   
) const

◆ GetOperationCountForSearch()

Int GetOperationCountForSearch ( ) const

Returns an estimate of the number of operations needed to locate a given key in this map. This is used when two collections are compared: The iteration goes over the collection which would require more operations for search, and each entry is searched in the other collection.

Returns
Estimate for the number of operations.

◆ InsertKey() [1/2]

ResultRef<V> InsertKey ( const K &  key,
Bool created = BoolLValue() 
)

Finds the value associated with the given key, or creates a corresponding entry if it doesn't exist yet. The value of a new entry has to be initialized afterwards (but its default constructor has already been invoked).

Parameters
[in]keyKey of the value to find or create.
[out]createdThis will be set to true if a new entry has been created successfully, otherwise it will be set to false.
Returns
Pointer to value for the given key, or nullptr if an entry didn't exist and allocation of a new entry failed.

◆ InsertKey() [2/2]

ResultRef<V> InsertKey ( K &&  key,
Bool created = BoolLValue() 
)

Finds the value associated with the given key, or creates a corresponding entry if it doesn't exist yet. The value of a new entry has to be initialized afterwards (but its default constructor has already been invoked).

Parameters
[in]keyKey of the value to find or create.
[out]createdThis will be set to true if a new entry has been created successfully, otherwise it will be set to false.
Returns
Pointer to value for the given key, or nullptr if an entry didn't exist and allocation of a new entry failed.

◆ InsertEntry() [1/2]

ResultMemT<Iterator> InsertEntry ( const K &  key,
Bool created = BoolLValue() 
)

Finds the entry with the given key, or creates such an entry if it doesn't exist yet, and returns an iterator pointing to the entry. The value of a new entry has to be initialized afterwards (but its default constructor has already been invoked).

Parameters
[in]keyKey of the entry to find or create.
[out]createdThis will be set to true if a new entry has been created successfully, otherwise it will be set to false.
Returns
Iterator to the entry for the given key or OutOfMemoryError if the allocation failed.

◆ InsertEntry() [2/2]

ResultMemT<Iterator> InsertEntry ( K &&  key,
Bool created = BoolLValue() 
)

Finds the entry with the given key, or creates such an entry if it doesn't exist yet, and returns an iterator pointing to the entry. The value of a new entry has to be initialized afterwards (but its default constructor has already been invoked).

Parameters
[in]keyKey of the entry to find or create. If a new entry is created, its key will be constructed by move-semantics if possible.
[out]createdThis will be set to true if a new entry has been created successfully, otherwise it will be set to false.
Returns
Iterator to the entry for the given key or OutOfMemoryError if the allocation failed.

◆ Insert() [1/4]

ResultMemT<Iterator> Insert ( const K &  key,
const V &  value,
Bool created = BoolLValue() 
)

Finds the entry with the given key, or creates such an entry if it doesn't exist yet, and returns an iterator pointing to the entry. The value of the entry will be set to value in any case.

Parameters
[in]keyKey of the entry to find or create.
[in]valueThis will be copied to the value of the entry.
[out]createdThis will be set to true if a new entry has been created successfully, otherwise it will be set to false.
Returns
Iterator to the entry for the given key or OutOfMemoryError if the allocation failed.

◆ Insert() [2/4]

ResultMemT<Iterator> Insert ( K &&  key,
const V &  value,
Bool created = BoolLValue() 
)

Finds the entry with the given key, or creates such an entry if it doesn't exist yet, and returns an iterator pointing to the entry. The value of the entry will be set to value in any case.

Parameters
[in]keyKey of the entry to find or create. If a new entry is created, its key will be constructed by move-semantics if possible.
[in]valueThis will be copied to the value of the entry.
[out]createdThis will be set to true if a new entry has been created successfully, otherwise it will be set to false.
Returns
Iterator to the entry for the given key or OutOfMemoryError if the allocation failed.

◆ Insert() [3/4]

ResultMemT<Iterator> Insert ( const K &  key,
V &&  value,
Bool created = BoolLValue() 
)

Finds the entry with the given key, or creates such an entry if it doesn't exist yet, and returns an iterator pointing to the entry. The value of the entry will be set to value in any case.

Parameters
[in]keyKey of the entry to find or create.
[in]valueThis will be moved to the value of the entry.
[out]createdThis will be set to true if a new entry has been created successfully, otherwise it will be set to false.
Returns
Iterator to the entry for the given key or OutOfMemoryError if the allocation failed.

◆ Insert() [4/4]

ResultMemT<Iterator> Insert ( K &&  key,
V &&  value,
Bool created = BoolLValue() 
)

Finds the entry with the given key, or creates such an entry if it doesn't exist yet, and returns an iterator pointing to the entry. The value of the entry will be set to value in any case.

Parameters
[in]keyKey of the entry to find or create. If a new entry is created, its key will be constructed by move-semantics if possible.
[in]valueThis will be moved to the value of the entry.
[out]createdThis will be set to true if a new entry has been created successfully, otherwise it will be set to false.
Returns
Iterator to the entry for the given key or OutOfMemoryError if the allocation failed.

◆ FindValue() [1/2]

const V* FindValue ( const KEY &  key) const

Finds the value associated with the given key in this map.

Parameters
[in]keyKey to search for.
Returns
Pointer to value for the given key, or nullptr if no entry exists for the key.

◆ FindValue() [2/2]

V* FindValue ( const KEY &  key)

Finds the value associated with the given key in this map.

Parameters
[in]keyKey to search for.
Returns
Pointer to value for the given key, or nullptr if no entry exists for the key.

◆ Find() [1/2]

Iterator Find ( const K &  key)

Finds the entry for the given key in this map.

Parameters
[in]keyKey to search for.
Returns
Iterator pointing to the entry with the given key, or an invalid iterator if this doesn't exist.

◆ Find() [2/2]

ConstIterator Find ( const K &  key) const

Finds the entry for the given key in this map.

Parameters
[in]keyKey to search for.
Returns
Iterator pointing to the entry with the given key, or an invalid iterator if this doesn't exist.

◆ FindFloor() [1/2]

Iterator FindFloor ( const K &  key)

Finds the entry with the greatest key less than or equal to the given key. If no such entry exists, the returned iterator will be invalid (its operator Bool will return false). This function is only supported by sorted array maps.

Parameters
[in]keyKey to search for.
Returns
Iterator pointing to the entry with greatest key less than or equal to the given key, or an invalid iterator if this doesn't exist.

◆ FindFloor() [2/2]

ConstIterator FindFloor ( const K &  key) const

Finds the entry with the greatest key less than or equal to the given key. If no such entry exists, the returned iterator will be invalid (its operator Bool will return false). This function is only supported by sorted array maps.

Parameters
[in]keyKey to search for.
Returns
Iterator pointing to the entry with greatest key less than or equal to the given key, or an invalid iterator if this doesn't exist.

◆ FindCeiling() [1/2]

Iterator FindCeiling ( const K &  key)

Finds the entry with the smallest key greater than or equal to the given key. If no such entry exists, the returned iterator will be invalid (its operator Bool will return false). This function is only supported by sorted array maps.

Parameters
[in]keyKey to search for.
Returns
Iterator pointing to the entry with smallest key greater than or equal to the given key, or an invalid iterator if this doesn't exist.

◆ FindCeiling() [2/2]

ConstIterator FindCeiling ( const K &  key) const

Finds the entry with the smallest key greater than or equal to the given key. If no such entry exists, the returned iterator will be invalid (its operator Bool will return false). This function is only supported by sorted array maps.

Parameters
[in]keyKey to search for.
Returns
Iterator pointing to the entry with smallest key greater than or equal to the given key, or an invalid iterator if this doesn't exist.

◆ Erase() [1/2]

ResultOk<Bool> Erase ( const K &  key)

Removes an entry with the given key from this map (if possible).

Parameters
[in]keyKey of the map entry to be be removed.
Returns
True if an entry was found and removed for #key, otherwise false.

◆ Erase() [2/2]

IteratorTemplate<false, SUPER> Erase ( const IteratorTemplate< false, SUPER > &  position,
Int  eraseCnt = 1 
)

Removes eraseCnt elements from this map starting at the position given by position. The returned iterator will point to the element behind the last removed element.

Parameters
[in]positionIterator pointing to the first element to be removed.
[in]eraseCntNumber of elements to remove.
Returns
Iterator pointing to the element behind the last removed element.

◆ GetUnderlyingArray() [1/2]

const ArrayType& GetUnderlyingArray ( ) const

◆ GetUnderlyingArray() [2/2]

ArrayType& GetUnderlyingArray ( )