#include <arraymap.h>
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.
K | Type of keys. |
V | Type of values. |
SORTED | Use true for a sorted array. |
COMPARE | Class to be used to compare the keys. |
ARRAY | An array selector template to choose the array implementation to use. |
Classes | |
class | EntryIteratorBase |
class | IteratorTemplate |
class | KeyIteratorBase |
class | NonConstIteratorBase |
class | ValueIteratorBase |
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< Iterator > | InsertEntry (const K &key, Bool &created=BoolLValue()) |
ResultMemT< Iterator > | InsertEntry (K &&key, Bool &created=BoolLValue()) |
ResultMemT< Iterator > | Insert (const K &key, const V &value, Bool &created=BoolLValue()) |
ResultMemT< Iterator > | Insert (K &&key, const V &value, Bool &created=BoolLValue()) |
ResultMemT< Iterator > | Insert (const K &key, V &&value, Bool &created=BoolLValue()) |
ResultMemT< Iterator > | Insert (K &&key, V &&value, Bool &created=BoolLValue()) |
template<typename KEYCOMPARE = COMPARE, typename KEY = K> | |
Int | FindIndex (const KEY &key) const |
template<typename KEYCOMPARE = COMPARE, typename KEY = K> | |
Opt< const V & > | FindValue (const KEY &key) const |
template<typename KEYCOMPARE = COMPARE, typename KEY = K> | |
Opt< V & > | FindValue (const KEY &key) |
template<typename KEYCOMPARE = COMPARE, typename KEY = K> | |
Iterator | Find (const KEY &key) |
template<typename KEYCOMPARE = COMPARE, typename KEY = K> | |
ConstIterator | Find (const KEY &key) const |
template<typename KEYCOMPARE = COMPARE, typename KEY = K> | |
Iterator | FindFloor (const KEY &key) |
template<typename KEYCOMPARE = COMPARE, typename KEY = K> | |
ConstIterator | FindFloor (const KEY &key) const |
template<typename KEYCOMPARE = COMPARE, typename KEY = K> | |
Iterator | FindCeiling (const KEY &key) |
template<typename KEYCOMPARE = COMPARE, typename KEY = K> | |
ConstIterator | FindCeiling (const KEY &key) const |
ResultOk< Bool > | Erase (const K &key) |
template<template< Bool > class SUPER> | |
IteratorTemplate< false, SUPER > | Erase (const IteratorTemplate< false, SUPER > &position, Int eraseCnt=1) |
template<typename KEY , typename VALUE > | |
Result< typename Helper::Entry & > | AppendUnsorted (KEY &&key, VALUE &&value) |
void | Sort () |
const ArrayType & | GetUnderlyingArray () const |
ArrayType & | GetUnderlyingArray () |
template<typename COLLECTION , typename = typename std::enable_if<STD_IS_REPLACEMENT(same, typename std::decay<COLLECTION>::type, ArrayMap)>::type> | |
Result< void > | CopyFromImpl (COLLECTION &&src, COLLECTION_RESIZE_FLAGS resizeFlags, OverloadRank4) |
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< COLLECTION, KEYTYPE, VALUETYPE, SUPER, HASH > | |
template<typename... ARGS> | |
MAXON_ATTRIBUTE_FORCE_INLINE | MapBase0 (ARGS &&... args) |
MAXON_ATTRIBUTE_FORCE_INLINE Bool | Contains (typename ByValueParam< KEYTYPE >::type key) const |
template<typename PAIR > | |
MAXON_ATTRIBUTE_FORCE_INLINE SFINAEHelper< Bool, typename PAIR::KeyType >::type | Contains (const PAIR &pair) const |
ResultRef< VALUETYPE > | Append (const KEYTYPE &key) |
template<typename PAIR > | |
SFINAEHelper< ResultRef< VALUETYPE >, typename PAIR::KeyType >::type | Append (const PAIR &pair) |
template<typename COLLECTION2 > | |
Result< void > | Add (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags=COLLECTION_RESIZE_FLAGS::ON_GROW_RESERVE_CAPACITY) |
template<typename COLLECTION2 > | |
Result< void > | AppendAll (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags=COLLECTION_RESIZE_FLAGS::ON_GROW_RESERVE_CAPACITY) |
template<typename COLLECTION2 > | |
Result< void > | AppendAllInverse (COLLECTION2 &&other) |
template<typename COLLECTION2 > | |
Bool | ContainsAllImpl (COLLECTION2 &&other, OverloadRank0) const |
template<typename COLLECTION2 > | |
Result< void > | SubtractImpl (COLLECTION2 &&other, OverloadRank0) |
template<typename COLLECTION2 , typename COMPARE > | |
Bool | IsEqualImpl (const COLLECTION2 &other, COMPARE &&cmp, OverloadRank0) const |
HashInt | GetHashCode () const |
UniqueHash | GetUniqueHashCode () const |
Public Member Functions inherited from BaseCollection< COLLECTION, SUPER > | |
template<typename... ARGS> | |
MAXON_ATTRIBUTE_FORCE_INLINE | BaseCollection (ARGS &&... args) |
template<typename COLLECTION2 > | |
MAXON_ATTRIBUTE_FORCE_INLINE std::enable_if< maxon::IsCollection< COLLECTION2 >::value, Bool >::type | operator== (const COLLECTION2 &other) const |
template<typename COLLECTION2 > | |
MAXON_ATTRIBUTE_FORCE_INLINE std::enable_if< maxon::IsCollection< COLLECTION2 >::value, Bool >::type | operator!= (const COLLECTION2 &other) const |
template<typename COMPARE = EqualityCompare, typename COLLECTION2 > | |
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 |
template<typename COLLECTION2 > | |
MAXON_ATTRIBUTE_FORCE_INLINE Result< void > | AppendAll (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags=COLLECTION_RESIZE_FLAGS::ON_GROW_RESERVE_CAPACITY) |
template<typename COLLECTION2 > | |
MAXON_ATTRIBUTE_FORCE_INLINE Result< void > | CopyFrom (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags=COLLECTION_RESIZE_FLAGS::FIT_TO_SIZE) |
template<typename COLLECTION2 > | |
MAXON_ATTRIBUTE_FORCE_INLINE Result< void > | Subtract (COLLECTION2 &&other) |
template<typename COLLECTION2 > | |
MAXON_ATTRIBUTE_FORCE_INLINE Result< void > | Intersect (const COLLECTION2 &other) |
template<typename COLLECTION2 > | |
Bool | Intersects (const COLLECTION2 &other) const |
template<typename COLLECTION2 > | |
MAXON_ATTRIBUTE_FORCE_INLINE Result< void > | CopyFromImpl (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags, OverloadRank0) |
template<typename COLLECTION2 > | |
Result< void > | AppendAllImpl (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags, Bool overwrite, OverloadRank0) |
template<typename COLLECTION2 > | |
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=nullptr) const |
template<typename COLLECTION2 > | |
MAXON_ATTRIBUTE_FORCE_INLINE Bool | ContainsAll (COLLECTION2 &&other) const |
template<typename COLLECTION2 > | |
Bool | ContainsAllImpl (COLLECTION2 &&other, OverloadRank0) const |
Static Public Member Functions | |
static const Entry * | GetEntry (const V *value) |
static Entry * | GetEntry (typename std::remove_const< V >::type *value) |
Static Public Member Functions inherited from MapBase0< COLLECTION, KEYTYPE, VALUETYPE, SUPER, HASH > | |
static const KEYTYPE & | GetMapKey (const KEYTYPE &key) |
template<typename PAIR > | |
static const KEYTYPE & | GetMapKey (const PAIR &pair) |
Additional Inherited Members | |
Static Public Attributes inherited from MapBase0< COLLECTION, KEYTYPE, VALUETYPE, SUPER, HASH > | |
static const COLLECTION_KIND | KIND |
using IsArrayMap = std::true_type |
using Helper = ArrayMapHelper<K, V, SORTED, COMPARE, ARRAY> |
using ArrayType = typename Helper::Array |
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> |
ArrayMap | ( | ) |
|
static |
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.
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.
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.
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.
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.
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.
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.
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.
MAXON_OPERATOR_MOVE_ASSIGNMENT | ( | ArrayMap< K, V, SORTED, COMPARE, ARRAY > | ) |
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 |
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.
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).
[in] | key | Key of the value to find or create. |
[out] | created | This will be set to true if a new entry has been created successfully, otherwise it will be set to false. |
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).
[in] | key | Key of the value to find or create. |
[out] | created | This will be set to true if a new entry has been created successfully, otherwise it will be set to false. |
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).
[in] | key | Key of the entry to find or create. |
[out] | created | This will be set to true if a new entry has been created successfully, otherwise it will be set to false. |
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).
[in] | key | Key of the entry to find or create. If a new entry is created, its key will be constructed by move-semantics if possible. |
[out] | created | This will be set to true if a new entry has been created successfully, otherwise it will be set to false. |
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.
[in] | key | Key of the entry to find or create. |
[in] | value | This will be copied to the value of the entry. |
[out] | created | This will be set to true if a new entry has been created successfully, otherwise it will be set to false. |
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.
[in] | key | Key of the entry to find or create. If a new entry is created, its key will be constructed by move-semantics if possible. |
[in] | value | This will be copied to the value of the entry. |
[out] | created | This will be set to true if a new entry has been created successfully, otherwise it will be set to false. |
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.
[in] | key | Key of the entry to find or create. |
[in] | value | This will be moved to the value of the entry. |
[out] | created | This will be set to true if a new entry has been created successfully, otherwise it will be set to false. |
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.
[in] | key | Key of the entry to find or create. If a new entry is created, its key will be constructed by move-semantics if possible. |
[in] | value | This will be moved to the value of the entry. |
[out] | created | This will be set to true if a new entry has been created successfully, otherwise it will be set to false. |
Int FindIndex | ( | const KEY & | key | ) | const |
Finds the index of the entry with the given key.
[in] | key | The key that the array is searched for. |
Opt<const V&> FindValue | ( | const KEY & | key | ) | const |
Finds the value associated with the given key in this map.
[in] | key | Key to search for. |
Opt<V&> FindValue | ( | const KEY & | key | ) |
Finds the value associated with the given key in this map.
[in] | key | Key to search for. |
Iterator Find | ( | const KEY & | key | ) |
Finds the entry for the given key in this map.
[in] | key | Key to search for. |
key
, or an invalid iterator if this doesn't exist. ConstIterator Find | ( | const KEY & | key | ) | const |
Finds the entry for the given key in this map.
[in] | key | Key to search for. |
key
, or an invalid iterator if this doesn't exist. Iterator FindFloor | ( | const KEY & | 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.
[in] | key | Key to search for. |
key
, or an invalid iterator if this doesn't exist. ConstIterator FindFloor | ( | const KEY & | 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.
[in] | key | Key to search for. |
key
, or an invalid iterator if this doesn't exist. Iterator FindCeiling | ( | const KEY & | 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.
[in] | key | Key to search for. |
key
, or an invalid iterator if this doesn't exist. ConstIterator FindCeiling | ( | const KEY & | 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.
[in] | key | Key to search for. |
key
, or an invalid iterator if this doesn't exist. Removes an entry with the given key from this map (if possible).
[in] | key | Key of the map entry to be be removed. |
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.
[in] | position | Iterator pointing to the first element to be removed. |
[in] | eraseCnt | Number of elements to remove. |
Result<typename Helper::Entry&> AppendUnsorted | ( | KEY && | key, |
VALUE && | value | ||
) |
Appends a map entry with the given key and value at the end, without checking if it already exists and without respecting the sort order. For a sorted ArrayMap you have to call Sort() afterwards. This function can be used if you want to append several entries at once, then it can be faster to do a single sorting step at the end.
[in] | key | Key of the entry to add. |
[in] | value | Value of the entry to add. |
void Sort | ( | ) |
Sorts a map when it's in unsorted state. You have to call this function after a sequence of calls to AppendUnsorted to ensure sorted state of the ArrayMap.
const ArrayType& GetUnderlyingArray | ( | ) | const |
ArrayType& GetUnderlyingArray | ( | ) |
Result<void> CopyFromImpl | ( | COLLECTION && | src, |
COLLECTION_RESIZE_FLAGS | resizeFlags, | ||
OverloadRank4 | |||
) |