Nebula
Loading...
Searching...
No Matches
Util::Dictionary< KEYTYPE, VALUETYPE > Class Template Reference

#include <dictionary.h>

Detailed Description

template<class KEYTYPE, class VALUETYPE>
class Util::Dictionary< KEYTYPE, VALUETYPE >

A collection of key/value pairs with quick value retrieval by key at roughly O(log n).

Internally the dictionary is implemented as a sorted array.

On insertion performance: Key/value pairs are inserted with the Add() method, which normally calls the Util::Array::InsertSorted() method internally. If many insertions are performed at once, it may be beneficial to call BeginBulkAdd() before, and EndBulkAdd() after adding the key/value pairs. Between BeginBulkAdd() and EndBulkAdd(), the Add() method will just append the new elements to the internal array, and only call Util::Array::Sort() inside EndBulkAdd().

Any methods which require the internal array to be sorted will throw an assertion between BeginBulkAdd() and EndBulkAdd().

Public Member Functions

 Dictionary ()
 default constructor
 
 Dictionary (const Dictionary< KEYTYPE, VALUETYPE > &rhs)
 copy constructor
 
 Dictionary (Dictionary< KEYTYPE, VALUETYPE > &&rhs) noexcept
 move constructor
 
void operator= (const Dictionary< KEYTYPE, VALUETYPE > &rhs)
 assignment operator
 
void operator= (Dictionary< KEYTYPE, VALUETYPE > &&rhs) noexcept
 move operator
 
VALUETYPE & operator[] (const KEYTYPE &key)
 read/write [] operator
 
const VALUETYPE & operator[] (const KEYTYPE &key) const
 read-only [] operator
 
SizeT Size () const
 return number of key/value pairs in the dictionary
 
void Clear ()
 clear the dictionary
 
bool IsEmpty () const
 return true if empty
 
void Reserve (SizeT numElements)
 reserve space (useful if number of elements is known beforehand)
 
void BeginBulkAdd ()
 begin a bulk insert (array will be sorted at End)
 
IndexT Add (const KeyValuePair< KEYTYPE, VALUETYPE > &kvp)
 add a key/value pair
 
IndexT Add (const KEYTYPE &key, const VALUETYPE &value)
 add a key and associated value
 
VALUETYPE & Emplace (const KEYTYPE &key)
 creates a new entry of VALUETYPE if key does not exist, or returns the existing element
 
void EndBulkAdd ()
 end a bulk insert (this will sort the internal array)
 
void Merge (const Dictionary< KEYTYPE, VALUETYPE > &rhs)
 merge two dictionaries
 
void Erase (const KEYTYPE &key)
 erase a key and its associated value
 
void EraseAtIndex (IndexT index)
 erase a key at index
 
IndexT FindIndex (const KEYTYPE &key) const
 find index of key/value pair (InvalidIndex if doesn't exist)
 
bool Contains (const KEYTYPE &key) const
 return true if key exists in the array
 
bool Contains (const KEYTYPE &key, IndexT &index) const
 return true if key exists in the array, and saves index
 
const KEYTYPE & KeyAtIndex (IndexT index) const
 get a key at given index
 
VALUETYPE & ValueAtIndex (IndexT index)
 access to value at given index
 
const VALUETYPE & ValueAtIndex (IndexT index) const
 get a value at given index
 
KeyValuePair< KEYTYPE, VALUETYPE > & KeyValuePairAtIndex (IndexT index) const
 get key/value pair at index
 
Array< KEYTYPE > KeysAsArray () const
 get all keys as an Util::Array
 
Array< VALUETYPE > ValuesAsArray () const
 get all keys as an Util::Array
 
template<class RETURNTYPE >
RETURNTYPE KeysAs () const
 get all keys as (typically) an array
 
template<class RETURNTYPE >
RETURNTYPE ValuesAs () const
 get all keys as (typically) an array
 
KeyValuePair< KEYTYPE, VALUETYPE > * begin () const
 functions for stl like behaviour
 
KeyValuePair< KEYTYPE, VALUETYPE > * end () const
 
void clear ()
 
void emplace (KEYTYPE &&key, VALUETYPE &&value)
 

Protected Member Functions

void SortIfDirty () const
 make sure the key value pair array is sorted
 

Protected Attributes

Array< KeyValuePair< KEYTYPE, VALUETYPE > > keyValuePairs
 
bool inBulkInsert
 

Constructor & Destructor Documentation

◆ Dictionary() [1/3]

template<class KEYTYPE , class VALUETYPE >
Util::Dictionary< KEYTYPE, VALUETYPE >::Dictionary ( )

default constructor

◆ Dictionary() [2/3]

template<class KEYTYPE , class VALUETYPE >
Util::Dictionary< KEYTYPE, VALUETYPE >::Dictionary ( const Dictionary< KEYTYPE, VALUETYPE > & rhs)

copy constructor

◆ Dictionary() [3/3]

template<class KEYTYPE , class VALUETYPE >
Util::Dictionary< KEYTYPE, VALUETYPE >::Dictionary ( Dictionary< KEYTYPE, VALUETYPE > && rhs)
noexcept

move constructor

Member Function Documentation

◆ Add() [1/2]

template<class KEYTYPE , class VALUETYPE >
IndexT Util::Dictionary< KEYTYPE, VALUETYPE >::Add ( const KEYTYPE & key,
const VALUETYPE & value )
inline

add a key and associated value

◆ Add() [2/2]

template<class KEYTYPE , class VALUETYPE >
IndexT Util::Dictionary< KEYTYPE, VALUETYPE >::Add ( const KeyValuePair< KEYTYPE, VALUETYPE > & kvp)
inline

add a key/value pair

◆ begin()

template<class KEYTYPE , class VALUETYPE >
KeyValuePair< KEYTYPE, VALUETYPE > * Util::Dictionary< KEYTYPE, VALUETYPE >::begin ( ) const
inline

functions for stl like behaviour

◆ BeginBulkAdd()

template<class KEYTYPE , class VALUETYPE >
void Util::Dictionary< KEYTYPE, VALUETYPE >::BeginBulkAdd ( )
inline

begin a bulk insert (array will be sorted at End)

◆ Clear()

template<class KEYTYPE , class VALUETYPE >
void Util::Dictionary< KEYTYPE, VALUETYPE >::Clear ( )
inline

clear the dictionary

◆ clear()

template<class KEYTYPE , class VALUETYPE >
void Util::Dictionary< KEYTYPE, VALUETYPE >::clear ( )
inline

◆ Contains() [1/2]

template<class KEYTYPE , class VALUETYPE >
bool Util::Dictionary< KEYTYPE, VALUETYPE >::Contains ( const KEYTYPE & key) const
inline

return true if key exists in the array

◆ Contains() [2/2]

template<class KEYTYPE , class VALUETYPE >
bool Util::Dictionary< KEYTYPE, VALUETYPE >::Contains ( const KEYTYPE & key,
IndexT & index ) const
inline

return true if key exists in the array, and saves index

◆ Emplace()

template<class KEYTYPE , class VALUETYPE >
VALUETYPE & Util::Dictionary< KEYTYPE, VALUETYPE >::Emplace ( const KEYTYPE & key)
inline

creates a new entry of VALUETYPE if key does not exist, or returns the existing element

◆ emplace()

template<class KEYTYPE , class VALUETYPE >
void Util::Dictionary< KEYTYPE, VALUETYPE >::emplace ( KEYTYPE && key,
VALUETYPE && value )
inline

◆ end()

template<class KEYTYPE , class VALUETYPE >
KeyValuePair< KEYTYPE, VALUETYPE > * Util::Dictionary< KEYTYPE, VALUETYPE >::end ( ) const
inline

◆ EndBulkAdd()

template<class KEYTYPE , class VALUETYPE >
void Util::Dictionary< KEYTYPE, VALUETYPE >::EndBulkAdd ( )
inline

end a bulk insert (this will sort the internal array)

◆ Erase()

template<class KEYTYPE , class VALUETYPE >
void Util::Dictionary< KEYTYPE, VALUETYPE >::Erase ( const KEYTYPE & key)
inline

erase a key and its associated value

◆ EraseAtIndex()

template<class KEYTYPE , class VALUETYPE >
void Util::Dictionary< KEYTYPE, VALUETYPE >::EraseAtIndex ( IndexT index)
inline

erase a key at index

◆ FindIndex()

template<class KEYTYPE , class VALUETYPE >
IndexT Util::Dictionary< KEYTYPE, VALUETYPE >::FindIndex ( const KEYTYPE & key) const
inline

find index of key/value pair (InvalidIndex if doesn't exist)

◆ IsEmpty()

template<class KEYTYPE , class VALUETYPE >
bool Util::Dictionary< KEYTYPE, VALUETYPE >::IsEmpty ( ) const
inline

return true if empty

◆ KeyAtIndex()

template<class KEYTYPE , class VALUETYPE >
const KEYTYPE & Util::Dictionary< KEYTYPE, VALUETYPE >::KeyAtIndex ( IndexT index) const
inline

get a key at given index

◆ KeysAs()

template<class KEYTYPE , class VALUETYPE >
template<class RETURNTYPE >
RETURNTYPE Util::Dictionary< KEYTYPE, VALUETYPE >::KeysAs ( ) const
inline

get all keys as (typically) an array

◆ KeysAsArray()

template<class KEYTYPE , class VALUETYPE >
Array< KEYTYPE > Util::Dictionary< KEYTYPE, VALUETYPE >::KeysAsArray ( ) const
inline

get all keys as an Util::Array

◆ KeyValuePairAtIndex()

template<class KEYTYPE , class VALUETYPE >
KeyValuePair< KEYTYPE, VALUETYPE > & Util::Dictionary< KEYTYPE, VALUETYPE >::KeyValuePairAtIndex ( IndexT index) const
inline

get key/value pair at index

◆ Merge()

template<class KEYTYPE , class VALUETYPE >
void Util::Dictionary< KEYTYPE, VALUETYPE >::Merge ( const Dictionary< KEYTYPE, VALUETYPE > & rhs)
inline

merge two dictionaries

◆ operator=() [1/2]

template<class KEYTYPE , class VALUETYPE >
void Util::Dictionary< KEYTYPE, VALUETYPE >::operator= ( const Dictionary< KEYTYPE, VALUETYPE > & rhs)
inline

assignment operator

◆ operator=() [2/2]

template<class KEYTYPE , class VALUETYPE >
void Util::Dictionary< KEYTYPE, VALUETYPE >::operator= ( Dictionary< KEYTYPE, VALUETYPE > && rhs)
inlinenoexcept

move operator

◆ operator[]() [1/2]

template<class KEYTYPE , class VALUETYPE >
VALUETYPE & Util::Dictionary< KEYTYPE, VALUETYPE >::operator[] ( const KEYTYPE & key)
inline

read/write [] operator

◆ operator[]() [2/2]

template<class KEYTYPE , class VALUETYPE >
const VALUETYPE & Util::Dictionary< KEYTYPE, VALUETYPE >::operator[] ( const KEYTYPE & key) const
inline

read-only [] operator

◆ Reserve()

template<class KEYTYPE , class VALUETYPE >
void Util::Dictionary< KEYTYPE, VALUETYPE >::Reserve ( SizeT numElements)
inline

reserve space (useful if number of elements is known beforehand)

◆ Size()

template<class KEYTYPE , class VALUETYPE >
SizeT Util::Dictionary< KEYTYPE, VALUETYPE >::Size ( ) const
inline

return number of key/value pairs in the dictionary

◆ SortIfDirty()

template<class KEYTYPE , class VALUETYPE >
void Util::Dictionary< KEYTYPE, VALUETYPE >::SortIfDirty ( ) const
protected

make sure the key value pair array is sorted

◆ ValueAtIndex() [1/2]

template<class KEYTYPE , class VALUETYPE >
VALUETYPE & Util::Dictionary< KEYTYPE, VALUETYPE >::ValueAtIndex ( IndexT index)
inline

access to value at given index

◆ ValueAtIndex() [2/2]

template<class KEYTYPE , class VALUETYPE >
const VALUETYPE & Util::Dictionary< KEYTYPE, VALUETYPE >::ValueAtIndex ( IndexT index) const
inline

get a value at given index

◆ ValuesAs()

template<class KEYTYPE , class VALUETYPE >
template<class RETURNTYPE >
RETURNTYPE Util::Dictionary< KEYTYPE, VALUETYPE >::ValuesAs ( ) const

get all keys as (typically) an array

◆ ValuesAsArray()

template<class KEYTYPE , class VALUETYPE >
Array< VALUETYPE > Util::Dictionary< KEYTYPE, VALUETYPE >::ValuesAsArray ( ) const
inline

get all keys as an Util::Array

Member Data Documentation

◆ inBulkInsert

template<class KEYTYPE , class VALUETYPE >
bool Util::Dictionary< KEYTYPE, VALUETYPE >::inBulkInsert
protected

◆ keyValuePairs

template<class KEYTYPE , class VALUETYPE >
Array<KeyValuePair<KEYTYPE, VALUETYPE> > Util::Dictionary< KEYTYPE, VALUETYPE >::keyValuePairs
protected

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