8 #ifndef INCL_evo_maplist_h 9 #define INCL_evo_maplist_h 228 template<
class TKey,
class TValue,
class TCompare=Compare<TKey>,
class TSize=SizeT>
232 #if defined(_MSC_VER) || defined(EVO_OLDCC) // avoid errors with older compilers and MSVC 238 typedef typename MapBaseType::IterKey IterKey;
239 typedef typename MapBaseType::IterItem IterItem;
275 MapList(
const ThisType& src) : data_(src.data_)
282 #if defined(EVO_CPP11) 283 using typename MapBaseType::InitPair;
291 for (
const auto&
item : init)
298 MapList(ThisType&& src) : data_(std::move(src.data_)) {
300 src.MapBaseType::size_ = 0;
308 data_ = std::move(src.data_);
310 src.MapBaseType::size_ = 0;
324 {
set(src);
return *
this; }
333 data_.items = src.data_.items;
344 ThisType&
set(
const MapBaseType& src) {
357 ThisType&
set(
const ThisType& src) {
358 ((Compare&)data_) = (
const Compare&)src.data_;
359 data_.items.set(src.data_.items);
365 data_.items.setempty();
379 {
return data_.items.null(); }
382 {
return data_.items.shared(); }
385 {
return data_.items.capacity(); }
389 using MapBaseType::operator==;
390 using MapBaseType::operator!=;
397 {
return (
this == &map || data_.items == map.data_.items); }
404 {
return (
this != &map && data_.items != map.data_.items); }
410 {
return Iter(*
this); }
418 {
return IterM(*
this); }
422 {
return Iter(*
this); }
434 return (search(pos, key) != NULL);
437 const Value*
find(
const Key& key)
const {
439 const Item*
item = search(pos, key);
440 return (item != NULL ? &item->
second : NULL);
445 const Item*
item = search(pos, key);
446 return (item != NULL ? (Value*)&item->
second : NULL);
454 { Size pos;
return (search(pos, key) == NULL ?
END : pos); }
457 Iter
iter(
const Key& key)
const {
459 const Item*
item = search(iterkey.a, key);
460 return (item != NULL ?
Iter(*
this, iterkey, (IterItem*)item) :
Iter(*
this,
iterEND));
466 const Item*
item = search(iterkey.a, key);
467 return (item != NULL ?
IterM(*
this, iterkey, (IterItem*)item) :
IterM(*
this,
iterEND));
470 Item&
getitem(
const Key& key,
bool* created=NULL) {
473 if ( (item=(Item*)search(pos, key)) == NULL) {
474 item = &data_.items.itemM(data_.items.insertnew(pos));
479 }
else if (created != NULL)
485 Value&
get(
const Key& key,
bool* created=NULL)
494 const Item&
item(Size index)
const 495 {
return data_.items.item(index); }
505 {
return data_.items.itemM(index); }
514 { data_.items.unshare();
return *
this; }
517 { data_.items.capacity(size);
return *
this; }
520 { data_.items.capacitymin(min);
return *
this; }
523 { data_.items.compact();
return *
this; }
531 Item&
add(
const Key& key,
const Value& value,
bool update=
true) {
533 Item& upditem =
getitem(key, &created_val);
534 if (created_val || update)
542 ThisType&
add(
const MapBaseType& map,
bool update=
true) {
553 bool remove(
const Key& key) {
556 if (search(index, key) != NULL) {
557 data_.items.remove(index);
565 {
return remove((IterM&)iter, dir); }
568 if (iter &&
this == &iter.getParent()) {
569 IterKey& iterkey = iter.getKey();
570 data_.items.remove(iterkey.a);
571 bool nextitem =
false;
575 { --iterkey.a; nextitem =
true; }
576 }
else if (iterkey.a < data_.items.size())
580 iter.setData( (IterItem*)&data_.items.item(iterkey.a) );
595 assert( index < data_.items.size() );
596 data_.items.remove(index);
608 {
return data_.items; }
616 {
return data_.items; }
622 void iterInitMutable()
623 { data_.items.iterInitMutable(); }
624 const IterItem* iterFirst(IterKey& key)
const 625 {
return (IterItem*)data_.items.iterFirst(key.a); }
626 const IterItem* iterNext(IterKey& key)
const 627 {
return (IterItem*)data_.items.iterNext(key.a); }
628 const IterItem* iterLast(IterKey& key)
const 629 {
return (IterItem*)data_.items.iterLast(key.a); }
630 const IterItem* iterPrev(IterKey& key)
const 631 {
return (IterItem*)data_.items.iterPrev(key.a); }
635 const Item*
getiter(IterKey& iterkey,
const Key& key)
const 636 {
return search(iterkey.a, key); }
642 struct Data :
public Compare {
647 Data(
const Data& data) :
Compare(data), items(data.items) {
650 #if defined(EVO_CPP11) 651 Data(Data&& data) :
Compare(std::move((Compare&&)data)), items(std::move(data.items)) {
654 *((Compare*)
this) = std::move((Compare&&)data);
655 items = std::move(data.items);
663 const Item* search(Size& index,
const Key& key)
const {
665 Size left = 0, right = data_.items.size(), mid = 0;
666 while (left < right) {
667 mid = left + ((right-left) / 2);
668 const Item&
item = data_.items.item(mid);
669 cmp = data_.Compare::operator()(key, item.
first);
672 }
else if (cmp == 0) {
685 #if defined(INCL_evo_string_h) || defined(DOXYGEN) List< Item > & advList()
Advanced: Get internal list.
Definition: maplist.h:615
Iter cend() const
Get iterator at end (const).
Definition: maplist.h:413
ThisType & operator=(const MapBaseType &src)
Assignment operator.
Definition: maplist.h:323
Pair< Key, Value > Item
Item type (key/value pair)
Definition: map.h:137
void removeat(Size index)
Remove item at given position (mutable).
Definition: maplist.h:594
ThisType & compact()
Reduce capacity to fit current size (modifier).
Definition: maplist.h:522
Map< TKey, TValue, TSize > MapBaseType
Map base type
Definition: map.h:133
bool shared() const
Get whether shared.
Definition: maplist.h:381
Size size_
Map size (number of items, automatically updated by concrete set members)
Definition: map.h:180
MapList< String, String > StrMapList
MapList using String keys and values.
Definition: maplist.h:690
Iter cbegin() const
Get iterator at first item (const).
Definition: maplist.h:409
Size capacity() const
Get map capacity.
Definition: maplist.h:384
~MapList()
Destructor.
Definition: maplist.h:279
MapList(const std::initializer_list< InitPair > &init)
< Used with initializer_list constructor (C++11)
Definition: maplist.h:288
bool operator!=(const ThisType &map) const
Inequality operator.
Definition: maplist.h:403
Random access iterator.
Definition: iter.h:904
Iter iter(const Key &key) const
Find (lookup) iterator for given key (const).
Definition: maplist.h:457
ThisType & operator=(const ThisType &src)
Assignment operator.
Definition: maplist.h:332
ThisType & unshare()
Make data unique by allocating new buffer, if needed (modifier).
Definition: maplist.h:513
ThisType & capacity(Size size)
Set map capacity.
Definition: maplist.h:516
const ThisType & asconst() const
Explicitly use a const reference to this.
Definition: maplist.h:316
MapList(ThisType &&src)
Move constructor (C++11).
Definition: maplist.h:298
bool operator==(const ThisType &map) const
Equality operator.
Definition: maplist.h:396
Value & operator[](const Key &key)
Get item value for key (mutable).
Definition: maplist.h:510
Basic integer type.
Definition: type.h:980
Item & itemM(Size index)
Get item at position (mutable).
Definition: maplist.h:504
ThisType & reserve(Size size)
Reserve space for new items.
Definition: maplist.h:526
TKey Key
Key type.
Definition: map.h:135
Iter begin() const
Get iterator at first item (const).
Definition: maplist.h:421
MapList(const ThisType &src)
Copy constructor.
Definition: maplist.h:275
Item & add(const Key &key, const Value &value, bool update=true)
Add or update using given key and value.
Definition: maplist.h:531
ThisType & setempty()
Set as empty but not null.
Definition: maplist.h:364
Item & getitem(const Key &key, bool *created=NULL)
Get map item for key (mutable).
Definition: maplist.h:470
ThisType & clear()
Clear by removing all items.
Definition: maplist.h:370
String container.
Definition: string.h:674
Size findindex(const Key &key) const
Find (lookup) index for given key (const).
Definition: maplist.h:453
Bidirectional iterator.
Definition: iter.h:611
MapList(const MapBaseType &src)
Copy constructor.
Definition: maplist.h:267
TA first
First value (same as a() and key())
Definition: pair.h:42
const Value & value() const
Get value for pair (second value) (const).
Definition: pair.h:138
static const EndT END
Special integer value for indicating end of items or no item.
Definition: type.h:1846
MapList()
Constructor.
Definition: maplist.h:258
const List< Item > & advList() const
Advanced: Get internal list (const).
Definition: maplist.h:607
OptionInfoShPtr Value
Value type.
Definition: map.h:136
IteratorDir
Iterator direction value.
Definition: iter.h:27
IterM begin()
Get iterator at first item (mutable).
Definition: maplist.h:417
IterM end()
Get iterator at end.
Definition: maplist.h:425
Reverse iterator direction.
Definition: iter.h:30
IteratorRa< ThisType >::Const Iter
Iterator (const) - IteratorRa.
Definition: maplist.h:252
const Item & item(Size index) const
Get item at position (const).
Definition: maplist.h:494
Evo C++ Library namespace.
Definition: alg.h:11
No iterator direction.
Definition: iter.h:28
SizeT Size
Size type for size values (must be unsigned integer) – default: SizeT.
Definition: map.h:134
const Key & key() const
Get key for pair (first value) (const).
Definition: pair.h:114
Size size() const
Get map size (number of items).
Definition: map.h:272
IterM iterM(const Key &key)
Find (lookup) iterator for given key (mutable).
Definition: maplist.h:464
MapList< TKey, TValue, TCompare, TSize > ThisType
This type.
Definition: maplist.h:249
Map implemented as an ordered list.
Definition: maplist.h:229
ThisType & operator=(ThisType &&src)
Move assignment operator (C++11).
Definition: maplist.h:307
End iterator position.
Definition: iter.h:23
Value * findM(const Key &key)
Find (lookup) value for given key (mutable).
Definition: maplist.h:443
ThisType & capacitymin(Size min)
Set map capacity to at least given minimum.
Definition: maplist.h:519
const Item * getiter(IterKey &iterkey, const Key &key) const
Used by base class to get data to initialize iterator.
Definition: maplist.h:635
Iter end() const
Get iterator at end (const).
Definition: maplist.h:429
Item & add(const Item &item, bool update=true)
Add or update using given item.
Definition: maplist.h:539
const Value * find(const Key &key) const
Find (lookup) value for given key (const).
Definition: maplist.h:437
ThisType & add(const MapBaseType &map, bool update=true)
Add items from given map.
Definition: maplist.h:542
Stores a key/value pair of independent objects or values.
Definition: pair.h:32
IteratorRa< ThisType > IterM
Iterator (mutable) - IteratorRa.
Definition: maplist.h:253
bool contains(const Key &key) const
Get whether map contains the given key.
Definition: maplist.h:432
Associative container holding key/value pairs for fast lookup.
Definition: map.h:129
T & min(T &a, T &b)
Returns lowest of given values.
Definition: alg.h:26
TB second
Second value (same as b() and value())
Definition: pair.h:43
TCompare Compare
Compare type to use
Definition: maplist.h:250
bool null() const
Get whether map is null.
Definition: maplist.h:378