Plasma Engine  2.0
Loading...
Searching...
No Matches
plArrayMapBase< KEY, VALUE > Class Template Reference

An associative container, similar to plMap, but all data is stored in a sorted contiguous array, which makes frequent lookups more efficient. More...

#include <ArrayMap.h>

Inheritance diagram for plArrayMapBase< KEY, VALUE >:

Classes

struct  Pair
 

Public Types

using const_iterator = typename plDynamicArray<Pair>::const_iterator
 
using const_reverse_iterator = typename plDynamicArray<Pair>::const_reverse_iterator
 
using iterator = typename plDynamicArray<Pair>::iterator
 
using reverse_iterator = typename plDynamicArray<Pair>::reverse_iterator
 

Public Member Functions

 plArrayMapBase (plAllocator *pAllocator)
 Constructor.
 
 plArrayMapBase (const plArrayMapBase &rhs, plAllocator *pAllocator)
 Copy-Constructor.
 
void operator= (const plArrayMapBase &rhs)
 Copy assignment operator.
 
plUInt32 GetCount () const
 Returns the number of elements stored in the map.
 
bool IsEmpty () const
 True if the map contains no elements.
 
void Clear ()
 Purges all elements from the map.
 
template<typename CompatibleKeyType , typename CompatibleValueType >
plUInt32 Insert (CompatibleKeyType &&key, CompatibleValueType &&value)
 Always inserts a new value under the given key. Duplicates are allowed. Returns the index of the newly added element.
 
void Sort () const
 Ensures the internal data structure is sorted. This is done automatically every time a lookup needs to be made.
 
template<typename CompatibleKeyType >
plUInt32 Find (const CompatibleKeyType &key) const
 Returns an index to one element with the given key. If the key is inserted multiple times, there is no guarantee which one is returned. Returns plInvalidIndex when no such element exists.
 
template<typename CompatibleKeyType >
plUInt32 LowerBound (const CompatibleKeyType &key) const
 Returns the index to the first element with a key equal or larger than the given key. Returns plInvalidIndex when no such element exists. If there are multiple keys with the same value, the one at the smallest index is returned.
 
template<typename CompatibleKeyType >
plUInt32 UpperBound (const CompatibleKeyType &key) const
 Returns the index to the first element with a key that is LARGER than the given key. Returns plInvalidIndex when no such element exists. If there are multiple keys with the same value, the one at the smallest index is returned.
 
const KEY & GetKey (plUInt32 uiIndex) const
 Returns the key that is stored at the given index.
 
const VALUE & GetValue (plUInt32 uiIndex) const
 Returns the value that is stored at the given index.
 
VALUE & GetValue (plUInt32 uiIndex)
 Returns the value that is stored at the given index.
 
plDynamicArray< Pair > & GetData ()
 Returns a reference to the map data array.
 
const plDynamicArray< Pair > & GetData () const
 Returns a constant reference to the map data array.
 
template<typename CompatibleKeyType >
VALUE & FindOrAdd (const CompatibleKeyType &key, bool *out_pExisted=nullptr)
 Returns the value stored at the given key. If none exists, one is created. bExisted indicates whether an element needed to be created.
 
template<typename CompatibleKeyType >
VALUE & operator[] (const CompatibleKeyType &key)
 Same as FindOrAdd.
 
const PairGetPair (plUInt32 uiIndex) const
 Returns the key/value pair at the given index.
 
void RemoveAtAndCopy (plUInt32 uiIndex, bool bKeepSorted=false)
 Removes the element at the given index.
 
template<typename CompatibleKeyType >
bool RemoveAndCopy (const CompatibleKeyType &key, bool bKeepSorted=false)
 Removes one element with the given key. Returns true, if one was found and removed. If the same key exists multiple times, you need to call this function multiple times to remove them all.
 
template<typename CompatibleKeyType >
bool Contains (const CompatibleKeyType &key) const
 Returns whether an element with the given key exists.
 
template<typename CompatibleKeyType >
bool Contains (const CompatibleKeyType &key, const VALUE &value) const
 Returns whether an element with the given key and value already exists.
 
void Reserve (plUInt32 uiSize)
 Reserves enough memory to store size elements.
 
void Compact ()
 Compacts the internal memory to not waste any space.
 
bool operator== (const plArrayMapBase< KEY, VALUE > &rhs) const
 Compares the two containers for equality.
 
 PL_ADD_DEFAULT_OPERATOR_NOTEQUAL (const plArrayMapBase< KEY, VALUE > &)
 
plUInt64 GetHeapMemoryUsage () const
 Returns the amount of bytes that are currently allocated on the heap.
 
template<typename CompatibleKeyType >
PL_ALWAYS_INLINE VALUE & operator[] (const CompatibleKeyType &key)
 
template<typename CompatibleKeyType >
PL_ALWAYS_INLINE bool Contains (const CompatibleKeyType &key) const
 

Detailed Description

template<typename KEY, typename VALUE>
class plArrayMapBase< KEY, VALUE >

An associative container, similar to plMap, but all data is stored in a sorted contiguous array, which makes frequent lookups more efficient.

Prefer this container over plMap when you modify the container less often than you look things up (which is in most cases), and when you do not need to store iterators to elements and require them to stay valid when the container is modified.

plArrayMapBase also allows to store multiple values under the same key (like a multi-map).

Member Function Documentation

◆ RemoveAndCopy()

template<typename KEY , typename VALUE >
template<typename CompatibleKeyType >
bool plArrayMapBase< KEY, VALUE >::RemoveAndCopy ( const CompatibleKeyType & key,
bool bKeepSorted = false )

Removes one element with the given key. Returns true, if one was found and removed. If the same key exists multiple times, you need to call this function multiple times to remove them all.

If the map is sorted and bKeepSorted is true, the element will be removed such that the map stays sorted. This is only useful, if only a single (or very few) elements are removed before the next lookup. If multiple values are removed, or new values are going to be inserted, as well, bKeepSorted should be left to false.

◆ RemoveAtAndCopy()

template<typename KEY , typename VALUE >
void plArrayMapBase< KEY, VALUE >::RemoveAtAndCopy ( plUInt32 uiIndex,
bool bKeepSorted = false )

Removes the element at the given index.

If the map is sorted and bKeepSorted is true, the element will be removed such that the map stays sorted. This is only useful, if only a single (or very few) elements are removed before the next lookup. If multiple values are removed, or new values are going to be inserted, as well, bKeepSorted should be left to false.


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