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
-
TKey | Map key type |
TValue | Map value type |
TCompare | Comparison type to use |
TSize | Size type to use for size values (must be unsigned integer) – default: SizeT |
- Features
- Similar to STL
map
, with a storage implementation similar to STL vector
- Values are stored sequentially in memory as a dynamic array – random access with item() uses constant time
- Values are always unique (no duplicate keys)
- This uses binary search – lookups are
O(log n)
, inserts are O(n + log n)
since values must be shifted to make room
- Items are each stored as a Pair and are kept ordered by key
- This uses List internally while keeping items ordered
- No memory allocated by new empty map
- Preallocates extra memory when buffer grows
- Sharing makes copying efficient
- See List features
C++11:
- Range-based for loop – see STL Compatibility
MapList<int,int> map;
for (
auto&
item : map.asconst()) {
}
- Initialization lists
MapList<int,int> map = {{3, 300}, {1, 100}, {2, 200}};
MapList<String,String> strmap = {{"key1", "foo"}, {"key2", "bar"}};
- Move semantics
- 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
int main() {
MyMap map;
map[1] = 10;
map.addsplit("3=30,4=40");
bool has1 = map.contains(1);
bool has5 = map.contains(5);
bool has1_10 = map.contains(1, 10);
bool has1_20 = map.contains(1, 20);
bool has5_50 = map.contains(5, 50);
int* value = map.findM(1);
if (value != NULL)
*value = 100;
return 0;
}
Output:
1=10,2=20,3=30,4=40
1=100
2=20
3=30
4=40
Example using descending (reverse) order
int main() {
MyMap map;
map[1] = 10;
map.get(3) = 30;
return 0;
}
Output:
|
| 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...
|
|
Item & | add (const Key &key, const Value &value, bool update=true) |
| Add or update using given key and value. More...
|
|
Item & | add (const Item &item, bool update=true) |
| Add or update using given item. More...
|
|
ThisType & | add (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 ThisType & | asconst () 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...
|
|
ThisType & | capacity (Size size) |
| Set map capacity. More...
|
|
ThisType & | capacitymin (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...
|
|
ThisType & | clear () |
| Clear by removing all items. More...
|
|
ThisType & | compact () |
| 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 Value * | find (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...
|
|
Value * | findM (const Key &key) |
| Find (lookup) value for given key (mutable). More...
|
|
Value & | get (const Key &key, bool *created=NULL) |
| Get item value for key (mutable). More...
|
|
Item & | getitem (const Key &key, bool *created=NULL) |
| Get map item for key (mutable). More...
|
|
const Item & | item (Size index) const |
| Get item at position (const). More...
|
|
Item & | itemM (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...
|
|
ThisType & | operator= (ThisType &&src) |
| Move assignment operator (C++11). More...
|
|
ThisType & | operator= (const MapBaseType &src) |
| Assignment operator. More...
|
|
ThisType & | operator= (const ThisType &src) |
| Assignment operator. More...
|
|
bool | operator== (const ThisType &map) const |
| Equality operator. More...
|
|
Value & | operator[] (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...
|
|
ThisType & | reserve (Size size) |
| Reserve space for new items. More...
|
|
ThisType & | set () |
| Set as null and empty. More...
|
|
ThisType & | set (const MapBaseType &src) |
| Set as copy of given map. More...
|
|
ThisType & | set (const ThisType &src) |
| Set as copy of given MapList map. More...
|
|
ThisType & | setempty () |
| Set as empty but not null. More...
|
|
bool | shared () const |
| Get whether shared. More...
|
|
Size | size () const |
| Get map size (number of items). More...
|
|
ThisType & | unshare () |
| Make data unique by allocating new buffer, if needed (modifier). More...
|
|