ContiguousRangeMap< K, V, MAP > Class Template Reference

#include <contiguousrangemap.h>

Detailed Description

template<typename K, typename V, typename MAP = BurstTrieMapSelector<>>
class maxon::ContiguousRangeMap< K, V, MAP >

Unlike a normal map, a RangeMap maps whole ranges (of integral values) to values. For each contiguous range having the same value, only a single entry is needed, so a RangeMap will perform much better than a normal map if it is likely that consecutive keys are mapped to the same value.

RangeMap<UInt, String> map;
if (!map.Insert(Range<UInt>(10, 40), "Alice"_s))
return false;
if (!map.Insert(Range<UInt>(30, 50), "Carol"_s))
return false;
if (!map.Erase(Range<UInt>(15, 25)))
return false;

The above example results in a map with three entries: The range 10 ... 14 is mapped to "Alice" as well as the range 26 ... 29, while the range 30 ... 50 is mapped to "Carol".

RangeMap ensures that its ranges don't overlap, and that directly adjacent ranges (with no gap between them) which are mapped to the same value are merged to a single range.

RangeMap supports the usual iterators. They iterate through the map per range.

Template Parameters
KType of keys. This must be an integral type.
VType of values.
MAPA map selector template to choose the underlying map implementation to use. This has to be an ordered map. Note that the default is the BurstTrieMap which only allows unsigned integral types.

Classes

class  EntryIteratorBase
 
class  KeyIteratorBase
 
class  ValueIteratorBase
 

Public Types

using MapType = typename MAP::template Type< K, V >
 
using Range = maxon::Range< K >
 
using ValueType = typename MapType::ValueType
 
using MapValue = typename MapType::ValueType
 
using ConstIterator = typename MapType::template IteratorTemplate< true, EntryIteratorBase >
 
using Iterator = ConstIterator
 
using ConstKeyIterator = typename MapType::template IteratorTemplate< true, KeyIteratorBase >
 
using ConstValueIterator = typename MapType::template IteratorTemplate< true, ValueIteratorBase >
 

Public Member Functions

 ContiguousRangeMap ()
 
 ContiguousRangeMap (ContiguousRangeMap &&src)
 
 MAXON_OPERATOR_MOVE_ASSIGNMENT (ContiguousRangeMap)
 
Result< void > Init (const V &value)
 
Result< void > Init (V &&value)
 
Result< void > CopyFrom (const ContiguousRangeMap &src)
 
Int GetMemorySize () const
 
Result< void > Insert (K rMinValue, K rMaxValue, V &&value)
 
Result< void > Insert (K rMinValue, K rMaxValue, const V &value)
 
Result< void > Insert (K key, const V &value)
 
Result< void > Insert (const Range &range, const V &value)
 
const V * FindValue (K key) const
 
const V * FindRange (K key, Range &rangeOut) const
 
SFINAEHelper< String, K >::type ToString (const FormatStatement *fmt=nullptr) const
 
ConstIterator Begin () const
 
ConstIterator End () const
 
ConstKeyIterator GetKeys () const
 
ConstValueIterator GetValues () const
 
const MapTypeGetMap () const
 
HashInt GetHashCode () const
 
Bool operator== (const ContiguousRangeMap &other) const
 
Bool operator!= (const ContiguousRangeMap &other) const
 
const MapTypeGetUnderlyingMap () const
 
MapTypeGetUnderlyingMap ()
 

Private Member Functions

 MAXON_DISALLOW_COPY_AND_ASSIGN (ContiguousRangeMap)
 

Private Attributes

MapType _map
 

Member Typedef Documentation

◆ MapType

using MapType = typename MAP::template Type<K, V>

◆ Range

using Range = maxon::Range<K>

◆ ValueType

using ValueType = typename MapType::ValueType

◆ MapValue

using MapValue = typename MapType::ValueType

◆ ConstIterator

using ConstIterator = typename MapType::template IteratorTemplate<true, EntryIteratorBase>

◆ Iterator

◆ ConstKeyIterator

using ConstKeyIterator = typename MapType::template IteratorTemplate<true, KeyIteratorBase>

◆ ConstValueIterator

using ConstValueIterator = typename MapType::template IteratorTemplate<true, ValueIteratorBase>

Constructor & Destructor Documentation

◆ ContiguousRangeMap() [1/2]

◆ ContiguousRangeMap() [2/2]

ContiguousRangeMap ( ContiguousRangeMap< K, V, MAP > &&  src)

Member Function Documentation

◆ MAXON_OPERATOR_MOVE_ASSIGNMENT()

MAXON_OPERATOR_MOVE_ASSIGNMENT ( ContiguousRangeMap< K, V, MAP >  )

◆ Init() [1/2]

Result<void> Init ( const V &  value)

Initializes the map. This has to be done before any of the other functions of this map are called.

◆ Init() [2/2]

Result<void> Init ( V &&  value)

Initializes the map. This has to be done before any of the other functions of this map are called.

◆ CopyFrom()

Result<void> CopyFrom ( const ContiguousRangeMap< K, V, MAP > &  src)

Makes this map a copy of src.

Parameters
[in]srcSource from which the entries are taken.
Returns
True if copying succeeded.

◆ GetMemorySize()

Int GetMemorySize ( ) const

Calculates the memory usage for this map. Keys and Values must have a public member GetMemorySize that return the element size.

Returns
Memory size in bytes.

◆ Insert() [1/4]

Result<void> Insert ( rMinValue,
rMaxValue,
V &&  value 
)

Maps the range rMinValue ... rMaxValue to value. Existing overlapping mappings are truncated, split or removed as necessary, or they are merged with the new mapping if their values are equal. If the range is empty (rMaxValue is less than rMinValue), nothing happens.

Parameters
[in]rMinValueLower boundary of the range.
[in]rMaxValueUpper boundary of the range.
[in]valueValue to which the range shall be mapped.

◆ Insert() [2/4]

Result<void> Insert ( rMinValue,
rMaxValue,
const V &  value 
)

Maps the range rMinValue ... rMaxValue to value. Existing overlapping mappings are truncated, split or removed as necessary, or they are merged with the new mapping if their values are equal. If the range is empty (rMaxValue is less than rMinValue), nothing happens.

Parameters
[in]rMinValueLower boundary of the range.
[in]rMaxValueUpper boundary of the range.
[in]valueValue to which the range shall be mapped.

◆ Insert() [3/4]

Result<void> Insert ( key,
const V &  value 
)

Maps the single value key (i.e., the range key ... key consisting of a single element) to value. Existing mappings which overlap key are truncated, split or removed as necessary, or they may be extended to include key if their values are equal.

Parameters
[in]keyA single key which shall be mapped.
[in]valueValue to which the range shall be mapped.

◆ Insert() [4/4]

Result<void> Insert ( const Range range,
const V &  value 
)

Maps the given range to value. Existing overlapping mappings are truncated, split or removed as necessary, or they are merged with the new mapping if their values are equal. If the range is empty, nothing happens.

Parameters
[in]rangeRange which shall be mapped.
[in]valueValue to which the range shall be mapped.

◆ FindValue()

const V* FindValue ( key) const

Finds the value associated with the given key in this map. There always exists such a value, so this function never returns nullptr.

Parameters
[in]keyKey to search for.
Returns
Pointer to value for the given key, never nullptr.

◆ FindRange()

const V* FindRange ( key,
Range rangeOut 
) const

Finds the value associated with the given key in this map. There always exists such a value, so this function never returns nullptr. rangeOut is set to the range.

Parameters
[in]keyKey to search for.
[out]rangeOutrangeOut is set to the found range.
Returns
Pointer to value for the given key, never nullptr.

◆ ToString()

SFINAEHelper<String, K>::type ToString ( const FormatStatement fmt = nullptr) const

◆ Begin()

ConstIterator Begin ( ) const

Returns an iterator pointing to the first entry of this map. The iteration order corresponds to the order of the ranges.

Returns
Iterator for this map pointing to the first element.

◆ End()

ConstIterator End ( ) const

Returns an iterator pointing just behind the last entry of this map. The iteration order corresponds to the order of the ranges.

Returns
Iterator for this map pointing behind the last element.

◆ GetKeys()

ConstKeyIterator GetKeys ( ) const

Returns a foreach iterator to iterate over all keys (i.e., ranges) of this map. This will yield all ranges in ascending order.

Returns
Foreach iterator over all keys (i.e., ranges).

◆ GetValues()

ConstValueIterator GetValues ( ) const

Returns a foreach iterator to iterate over all values of this map. This will yield all values in ascending order of the corresponding ranges.

Returns
Foreach iterator over all values.

◆ GetMap()

const MapType& GetMap ( ) const

◆ GetHashCode()

HashInt GetHashCode ( ) const

◆ operator==()

Bool operator== ( const ContiguousRangeMap< K, V, MAP > &  other) const

◆ operator!=()

Bool operator!= ( const ContiguousRangeMap< K, V, MAP > &  other) const

◆ GetUnderlyingMap() [1/2]

const MapType& GetUnderlyingMap ( ) const

◆ GetUnderlyingMap() [2/2]

MapType& GetUnderlyingMap ( )

◆ MAXON_DISALLOW_COPY_AND_ASSIGN()

MAXON_DISALLOW_COPY_AND_ASSIGN ( ContiguousRangeMap< K, V, MAP >  )
private

Member Data Documentation

◆ _map

MapType _map
private

The underlying map which maps each start of a range to the mapped value. The range ends one before the start of the next range.