Evo C++ Library v0.5.1
Classes | Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
MapList< TKey, TValue, TCompare, TSize > Class Template Reference

#include <evo/maplist.h>

Inheritance diagram for MapList< TKey, TValue, TCompare, TSize >:
Inheritance graph
[legend]

Detailed Description

template<class TKey, class TValue, class TCompare = Compare<TKey>, class TSize = SizeT>
class evo::MapList< TKey, TValue, TCompare, TSize >

Map implemented as an ordered list.

This uses List internally.

Template Parameters
TKeyMap key type
TValueMap value type
TCompareComparison type to use
TSizeSize type to use for size values (must be unsigned integer) – default: SizeT
Features

C++11:

Comparison

You can leave the default comparison type (Compare) or specify an alternative.

See: Primitives & Containers

Iterators

Caution: Modifying or resizing a map will shift or invalidate existing iterators using it.

Constructors
Read Access
Modifiers
Helpers
Example

Example using a map of numbers

#include <evo/string.h>
#include <evo/maplist.h>
#include <evo/io.h>
using namespace evo;
static Console& c = con();
int main() {
typedef MapList<int,int> MyMap;
// Create map with numeric keys and values
MyMap map;
map.add(2, 20);
map[1] = 10;
// Add more values from delimited string
map.addsplit("3=30,4=40");
// Join map as delimited string and print it
String str;
str.joinmap(map);
c.out << str << NL;
// Check if map contains key
bool has1 = map.contains(1); // true
bool has5 = map.contains(5); // false
// Check if map contains key and value
bool has1_10 = map.contains(1, 10); // true
bool has1_20 = map.contains(1, 20); // false
bool has5_50 = map.contains(5, 50); // false
// Find and modify value
int* value = map.findM(1);
if (value != NULL)
*value = 100;
// Iterate and print keys and values
for (MyMap::Iter iter(map); iter; ++iter)
c.out << iter->key() << "=" << iter->value() << NL;
return 0;
}

Output:

1=10,2=20,3=30,4=40
1=100
2=20
3=30
4=40

Example using descending (reverse) order

#include <evo/maplist.h>
#include <evo/io.h>
using namespace evo;
static Console& c = con();
int main() {
// Create map with numeric keys and values
MyMap map;
map.add(2, 20);
map[1] = 10;
map.get(3) = 30;
// Iterate and print keys and values
for (MyMap::Iter iter(map); iter; ++iter)
c.out << iter->key() << "=" << iter->value() << NL;
return 0;
}

Output:

3=30
2=20
1=10

Public Types

typedef TCompare Compare
 Compare type to use More...
 
typedef void EvoContainerType
 
typedef Pair< Key, ValueItem
 Item type (key/value pair) More...
 
typedef IteratorRa< ThisType >::Const Iter
 Iterator (const) - IteratorRa. More...
 
typedef IteratorRa< ThisTypeIterM
 Iterator (mutable) - IteratorRa. More...
 
typedef TKey Key
 Key type. More...
 
using KeyPass = typename DataCopy< TKey >::PassType
 Key type for passing through InitPair (C++11) More...
 
typedef Map< TKey, TValue, TSize > MapBaseType
 Map base type More...
 
typedef TSize Size
 Size type for size values (must be unsigned integer) – default: SizeT. More...
 
typedef MapList< TKey, TValue, TCompare, TSize > ThisType
 This type. More...
 
typedef TValue Value
 Value type. More...
 
using ValuePass = typename DataCopy< TValue >::PassType
 Value type for passing through InitPair (C++11) More...
 

Public Member Functions

 MapList ()
 Constructor. More...
 
 MapList (const MapBaseType &src)
 Copy constructor. More...
 
 MapList (const ThisType &src)
 Copy constructor. More...
 
 MapList (const std::initializer_list< InitPair > &init)
 < Used with initializer_list constructor (C++11) More...
 
 MapList (ThisType &&src)
 Move constructor (C++11). More...
 
 ~MapList ()
 Destructor. More...
 
Itemadd (const Key &key, const Value &value, bool update=true)
 Add or update using given key and value. More...
 
Itemadd (const Item &item, bool update=true)
 Add or update using given item. More...
 
ThisTypeadd (const MapBaseType &map, bool update=true)
 Add items from given map. More...
 
template<class T >
Size addsplit (const T &str, char delim=',', char kvdelim='=')
 Split delimited string into map key/value items. More...
 
const List< Item > & advList () const
 Advanced: Get internal list (const). More...
 
List< Item > & advList ()
 Advanced: Get internal list. More...
 
const ThisTypeasconst () const
 Explicitly use a const reference to this. More...
 
IterM begin ()
 Get iterator at first item (mutable). More...
 
Iter begin () const
 Get iterator at first item (const). More...
 
Size capacity () const
 Get map capacity. More...
 
ThisTypecapacity (Size size)
 Set map capacity. More...
 
ThisTypecapacitymin (Size min)
 Set map capacity to at least given minimum. More...
 
Iter cbegin () const
 Get iterator at first item (const). More...
 
Iter cend () const
 Get iterator at end (const). More...
 
ThisTypeclear ()
 Clear by removing all items. More...
 
ThisTypecompact ()
 Reduce capacity to fit current size (modifier). More...
 
bool contains (const Key &key) const
 Get whether map contains the given key. More...
 
bool empty () const
 Get whether map is empty (size is 0). More...
 
IterM end ()
 Get iterator at end. More...
 
Iter end () const
 Get iterator at end (const). More...
 
const Valuefind (const Key &key) const
 Find (lookup) value for given key (const). More...
 
Size findindex (const Key &key) const
 Find (lookup) index for given key (const). More...
 
ValuefindM (const Key &key)
 Find (lookup) value for given key (mutable). More...
 
Valueget (const Key &key, bool *created=NULL)
 Get item value for key (mutable). More...
 
Itemgetitem (const Key &key, bool *created=NULL)
 Get map item for key (mutable). More...
 
const Itemitem (Size index) const
 Get item at position (const). More...
 
ItemitemM (Size index)
 Get item at position (mutable). More...
 
Iter iter (const Key &key) const
 Find (lookup) iterator for given key (const). More...
 
IterM iterM (const Key &key)
 Find (lookup) iterator for given key (mutable). More...
 
bool null () const
 Get whether map is null. More...
 
bool operator!= (const ThisType &map) const
 Inequality operator. More...
 
ThisTypeoperator= (ThisType &&src)
 Move assignment operator (C++11). More...
 
ThisTypeoperator= (const MapBaseType &src)
 Assignment operator. More...
 
ThisTypeoperator= (const ThisType &src)
 Assignment operator. More...
 
bool operator== (const ThisType &map) const
 Equality operator. More...
 
Valueoperator[] (const Key &key)
 Get item value for key (mutable). More...
 
bool ordered () const
 Get whether map is ordered. More...
 
bool remove (const Key &key)
 Find and remove item with given key. More...
 
bool remove (typename MapBaseType::IterM &iter, IteratorDir dir=iterNONE)
 Remove item using given iterator. More...
 
bool remove (IterM &iter, IteratorDir dir=iterNONE)
 Remove item using given iterator. More...
 
void removeat (Size index)
 Remove item at given position (mutable). More...
 
ThisTypereserve (Size size)
 Reserve space for new items. More...
 
ThisTypeset ()
 Set as null and empty. More...
 
ThisTypeset (const MapBaseType &src)
 Set as copy of given map. More...
 
ThisTypeset (const ThisType &src)
 Set as copy of given MapList map. More...
 
ThisTypesetempty ()
 Set as empty but not null. More...
 
bool shared () const
 Get whether shared. More...
 
Size size () const
 Get map size (number of items). More...
 
ThisTypeunshare ()
 Make data unique by allocating new buffer, if needed (modifier). More...
 

Protected Member Functions

const Itemgetiter (IterKey &iterkey, const Key &key) const
 Used by base class to get data to initialize iterator. More...
 

Protected Attributes

bool ordered_
 Whether map is ordered (items are kept in order by key) More...
 
Size size_
 Map size (number of items, automatically updated by concrete set members) More...
 

Member Typedef Documentation

◆ Compare

typedef TCompare Compare

Compare type to use

◆ EvoContainerType

typedef void EvoContainerType

◆ Item

typedef Pair<Key,Value> Item
inherited

Item type (key/value pair)

◆ Iter

typedef IteratorRa<ThisType>::Const Iter

Iterator (const) - IteratorRa.

◆ IterM

Iterator (mutable) - IteratorRa.

◆ Key

typedef TKey Key
inherited

Key type.

◆ KeyPass

using KeyPass = typename DataCopy<TKey>::PassType
inherited

Key type for passing through InitPair (C++11)

◆ MapBaseType

typedef Map<TKey,TValue,TSize> MapBaseType
inherited

Map base type

◆ Size

typedef TSize Size
inherited

Size type for size values (must be unsigned integer) – default: SizeT.

◆ ThisType

typedef MapList<TKey,TValue,TCompare,TSize> ThisType

This type.

◆ Value

typedef TValue Value
inherited

Value type.

◆ ValuePass

using ValuePass = typename DataCopy<TValue>::PassType
inherited

Value type for passing through InitPair (C++11)

Constructor & Destructor Documentation

◆ MapList() [1/5]

MapList ( )
inline

Constructor.

◆ MapList() [2/5]

MapList ( const MapBaseType src)
inline

Copy constructor.

  • Uses default comparison object
  • Results are undefined if TCompare is a function pointer since this will not initialize it
Parameters
srcSource to copy

◆ MapList() [3/5]

MapList ( const ThisType src)
inline

Copy constructor.

  • This copies the comparison object as well
Parameters
srcSource to copy

◆ ~MapList()

~MapList ( )
inline

Destructor.

◆ MapList() [4/5]

MapList ( const std::initializer_list< InitPair > &  init)
inline

< Used with initializer_list constructor (C++11)

Sequence constructor (C++11).

Parameters
initInitializer list, passed as comma-separated values in braces { }

◆ MapList() [5/5]

MapList ( ThisType &&  src)
inline

Move constructor (C++11).

Parameters
srcSource to move

Member Function Documentation

◆ add() [1/3]

Item& add ( const Key key,
const Value value,
bool  update = true 
)
inlinevirtual

Add or update using given key and value.

Parameters
keyKey to find/add (copied)
valueValue to add/update (copied)
updateWhether to update value for existing item
Returns
Item added/updated

Implements Map< TKey, TValue, TSize >.

◆ add() [2/3]

Item& add ( const Item item,
bool  update = true 
)
inlinevirtual

Add or update using given item.

Parameters
itemItem to use for add/update (copied)
updateWhether to update value for existing item
Returns
Item added/updated

Implements Map< TKey, TValue, TSize >.

◆ add() [3/3]

ThisType& add ( const MapBaseType map,
bool  update = true 
)
inlinevirtual

Add items from given map.

Parameters
mapMap to add items from
updateWhether to update value for existing items
Returns
This

Implements Map< TKey, TValue, TSize >.

◆ addsplit()

Size addsplit ( const T &  str,
char  delim = ',',
char  kvdelim = '=' 
)
inlineinherited

Split delimited string into map key/value items.

  • This parses/tokenizes str and adds each item to map, using convert() for conversion to map key and value types
  • Map key and value types must be convertible from String via convert()
  • See joinmap() to join map back into string
Template Parameters
TString type to parse – inferred from str parameter
Parameters
strString to parse
delimItem delimiter to use
kvdelimKey/Value delimiter to use
Returns
Number of items added to map

◆ advList() [1/2]

const List<Item>& advList ( ) const
inline

Advanced: Get internal list (const).

  • Caution: The list must remain sorted, otherwise results are undefined
Returns
Internal list

◆ advList() [2/2]

List<Item>& advList ( )
inline

Advanced: Get internal list.

  • Caution: The list must remain sorted, otherwise results are undefined
Returns
Internal list

◆ asconst()

const ThisType& asconst ( ) const
inline

Explicitly use a const reference to this.

  • This is useful to force using this as const without casting
Returns
This

◆ begin() [1/2]

IterM begin ( )
inline

Get iterator at first item (mutable).

  • This allows compatibility with range-based for loops and other libraries, otherwise use container Iter directly
  • cbegin() is more efficient, since this effectively calls unshare() to make items mutable
Returns
Iterator at first item, or at end position if empty
See also
IterM, end(), cbegin(), cend()

◆ begin() [2/2]

Iter begin ( ) const
inline

Get iterator at first item (const).

  • This allows compatibility with range-based for loops and other libraries, otherwise use container Iter directly
Returns
Iterator at first item, or at end position if empty
See also
end() const, cbegin()

◆ capacity() [1/2]

Size capacity ( ) const
inlinevirtual

Get map capacity.

Returns
Current capacity

Implements Map< TKey, TValue, TSize >.

◆ capacity() [2/2]

ThisType& capacity ( Size  size)
inlinevirtual

Set map capacity.

  • This is just a suggestion – some implementations may ignore it
Parameters
sizeNew capacity
Returns
This

Implements Map< TKey, TValue, TSize >.

◆ capacitymin()

ThisType& capacitymin ( Size  min)
inlinevirtual

Set map capacity to at least given minimum.

  • This is just a suggestion – some implementations may ignore it
Parameters
minMinimum capacity
Returns
This

Implements Map< TKey, TValue, TSize >.

◆ cbegin()

Iter cbegin ( ) const
inline

Get iterator at first item (const).

  • This allows compatibility with range-based for loops and other libraries, otherwise use container Iter directly
Returns
Iterator at first item, or at end position if empty
See also
Iter, cend(), begin(), end()

◆ cend()

Iter cend ( ) const
inline

Get iterator at end (const).

  • This allows compatibility with range-based for loops and other libraries, otherwise use container Iter directly
  • This really just creates an empty iterator
Returns
Iterator at end position
See also
Iter, cbegin(), begin(), end()

◆ clear()

ThisType& clear ( )
inlinevirtual

Clear by removing all items.

  • Does not set as null – null status is unchanged
Returns
This

Implements Map< TKey, TValue, TSize >.

◆ compact()

ThisType& compact ( )
inlinevirtual

Reduce capacity to fit current size (modifier).

  • Call to save memory when done adding items
  • This is just a suggestion – some implementations may ignore it
Returns
This

Reimplemented from Map< TKey, TValue, TSize >.

◆ contains()

bool contains ( const Key key) const
inlinevirtual

Get whether map contains the given key.

Parameters
keyKey to look for
Returns
Whether key was found

Implements Map< TKey, TValue, TSize >.

◆ empty()

bool empty ( ) const
inlineinherited

Get whether map is empty (size is 0).

Returns
Whether empty

◆ end() [1/2]

IterM end ( )
inline

Get iterator at end.

  • This allows compatibility with range-based for loops and other libraries, otherwise use container Iter directly
  • This really just creates an empty iterator
Returns
Iterator at end position
See also
IterM, begin(), cbegin(), cend()

◆ end() [2/2]

Iter end ( ) const
inline

Get iterator at end (const).

  • This allows compatibility with range-based for loops and other libraries, otherwise use container Iter directly
  • This really just creates an empty iterator
Returns
Iterator at end position
See also
begin() const, cend()

◆ find()

const Value* find ( const Key key) const
inlinevirtual

Find (lookup) value for given key (const).

Parameters
keyKey to find
Returns
Found value const pointer, NULL if not found

Implements Map< TKey, TValue, TSize >.

◆ findindex()

Size findindex ( const Key key) const
inline

Find (lookup) index for given key (const).

Parameters
keyKey to find
Returns
Found index for item key, END if not found

◆ findM()

Value* findM ( const Key key)
inlinevirtual

Find (lookup) value for given key (mutable).

Parameters
keyKey to find
Returns
Found value pointer, NULL if not found

Implements Map< TKey, TValue, TSize >.

◆ get()

Value& get ( const Key key,
bool *  created = NULL 
)
inline

Get item value for key (mutable).

  • Item is created with default value if not found
Parameters
keyKey to use
createdStores whether new item was created, if not NULL
Returns
Value reference for key

◆ getitem()

Item& getitem ( const Key key,
bool *  created = NULL 
)
inlinevirtual

Get map item for key (mutable).

  • Item is created with default value if not found.
Parameters
keyKey to use
createdStores whether new item was created, if not NULL
Returns
Value reference for key

Implements Map< TKey, TValue, TSize >.

◆ getiter()

const Item* getiter ( IterKey &  iterkey,
const Key key 
) const
inlineprotectedvirtual

Used by base class to get data to initialize iterator.

Parameters
iterkeySet to iterator data, if item found
keyKey to find
Returns
Item pointer, NULL if not found

Implements Map< TKey, TValue, TSize >.

◆ item()

const Item& item ( Size  index) const
inline

Get item at position (const).

  • Results are undefined if index is out of bounds
Parameters
indexItem index
Returns
Item as read-only (const)

◆ itemM()

Item& itemM ( Size  index)
inline

Get item at position (mutable).

  • Results are undefined if index is out of bounds
  • Calls unshare()
Parameters
indexItem index
Returns
Item (mutable)

◆ iter()

Iter iter ( const Key key) const
inline

Find (lookup) iterator for given key (const).

Parameters
keyKey to find
Returns
Iterator, at end position if not key not found

◆ iterM()

IterM iterM ( const Key key)
inline

Find (lookup) iterator for given key (mutable).

Parameters
keyKey to find
Returns
Iterator, at end position if not key not found

◆ null()

bool null ( ) const
inlinevirtual

Get whether map is null.

Returns
Whether null

Implements Map< TKey, TValue, TSize >.

◆ operator!=()

bool operator!= ( const ThisType map) const
inline

Inequality operator.

Parameters
mapMap to compre
Returns
Whether maps are not equal (don't have same keys and values)

◆ operator=() [1/3]

ThisType& operator= ( ThisType &&  src)
inline

Move assignment operator (C++11).

Parameters
srcSource to move
Returns
This

◆ operator=() [2/3]

ThisType& operator= ( const MapBaseType src)
inline

Assignment operator.

Parameters
srcSource to copy
Returns
This

◆ operator=() [3/3]

ThisType& operator= ( const ThisType src)
inline

Assignment operator.

  • This copies the comparison object as well
Parameters
srcSource to copy
Returns
This

◆ operator==()

bool operator== ( const ThisType map) const
inline

Equality operator.

Parameters
mapMap to compre
Returns
Whether maps are equal (have same keys and values)

◆ operator[]()

Value& operator[] ( const Key key)
inline

Get item value for key (mutable).

  • Item is created with default value if not found
  • Same as get(const Key&)
Parameters
keyKey to use
Returns
Value reference for key

◆ ordered()

bool ordered ( ) const
inlineinherited

Get whether map is ordered.

  • Ordered maps keep items in order by key so iteration is predictable

◆ remove() [1/3]

bool remove ( const Key key)
inlinevirtual

Find and remove item with given key.

Parameters
keyKey to use
Returns
Whether key was removed, false if not found

Implements Map< TKey, TValue, TSize >.

◆ remove() [2/3]

bool remove ( typename MapBaseType::IterM iter,
IteratorDir  dir = iterNONE 
)
inlinevirtual

Remove item using given iterator.

  • This will move the iterator to the next item according to dir, or end position if no more
  • Caution: Removing an item may shift other iterators on same map, in an unpredictable way
Parameters
iterIterator position
dirDirection to move iterator to next item, iterNONE for end position
Returns
Whether item was removed, false if iterator at end position

Implements Map< TKey, TValue, TSize >.

◆ remove() [3/3]

bool remove ( IterM iter,
IteratorDir  dir = iterNONE 
)
inlinevirtual

Remove item using given iterator.

  • This will move the iterator to the next item according to dir, or end position if no more
  • Caution: Removing an item may shift other iterators on same map, in an unpredictable way
Parameters
iterIterator position
dirDirection to move iterator to next item, iterNONE for end position
Returns
Whether item was removed, false if iterator at end position

Implements Map< TKey, TValue, TSize >.

◆ removeat()

void removeat ( Size  index)
inline

Remove item at given position (mutable).

  • Results are undefined if index is out of bounds
  • Calls unshare()
Parameters
indexItem index

◆ reserve()

ThisType& reserve ( Size  size)
inline

Reserve space for new items.

  • For best performance, call this before adding multiple items
  • This calls capacitymin() with current size + size to reserve space
  • This is just a suggestion – some implementations may ignore it
Parameters
sizeSize to reserve
Returns
This

◆ set() [1/3]

ThisType& set ( )
inlinevirtual

Set as null and empty.

Returns
This

Implements Map< TKey, TValue, TSize >.

◆ set() [2/3]

ThisType& set ( const MapBaseType src)
inlinevirtual

Set as copy of given map.

  • This clears all items in current map.
Parameters
srcSource map
Returns
This

Implements Map< TKey, TValue, TSize >.

◆ set() [3/3]

ThisType& set ( const ThisType src)
inlinevirtual

Set as copy of given MapList map.

  • This copies the comparison object as well
Parameters
srcSource map
Returns
This

Implements Map< TKey, TValue, TSize >.

◆ setempty()

ThisType& setempty ( )
inlinevirtual

Set as empty but not null.

Returns
This

Implements Map< TKey, TValue, TSize >.

◆ shared()

bool shared ( ) const
inlinevirtual

Get whether shared.

  • Data is shared when referencing external data or buffer is allocated and shared (reference count > 1)
Returns
Whether shared

Implements Map< TKey, TValue, TSize >.

◆ size()

Size size ( ) const
inlineinherited

Get map size (number of items).

Returns
Map size

◆ unshare()

ThisType& unshare ( )
inlinevirtual

Make data unique by allocating new buffer, if needed (modifier).

  • Use reserve() instead to reserve additional space while unsharing
  • Use to make buffer unique (not shared) and writable (when not empty)
  • This is called automatically by mutable/modifier methods
  • This does nothing if empty or not shared
Returns
This

Implements Map< TKey, TValue, TSize >.

Member Data Documentation

◆ ordered_

bool ordered_
protectedinherited

Whether map is ordered (items are kept in order by key)

◆ size_

Size size_
protectedinherited

Map size (number of items, automatically updated by concrete set members)


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