8 #ifndef INCL_evo_setlist_h 9 #define INCL_evo_setlist_h 249 template<
class TKey,
class TCompare=Compare<TKey>,
class TSize=SizeT>
253 #if defined(_MSC_VER) || defined(EVO_OLDCC) // avoid errors with older compilers and MSVC 259 typedef typename SetBaseType::IterKey IterKey;
260 typedef typename SetBaseType::IterItem IterItem;
294 SetList(
const ThisType& src) : data_(src.data_)
301 #if defined(EVO_CPP11) 308 for (
const auto& val : init)
315 SetList(ThisType&& src) : data_(std::move(src.data_)) {
317 src.SetBaseType::size_ = 0;
325 data_ = std::move(src.data_);
327 src.SetBaseType::size_ = 0;
341 {
set(src);
return *
this; }
345 {
set(src);
return *
this; }
354 ThisType&
set(
const SetBaseType& src) {
362 ThisType&
set(
const ThisType& src) {
363 ((Compare&)data_) = (
const Compare&)src.data_;
364 data_.items.set(src.data_.items);
370 data_.items.setempty();
384 {
return data_.items.null(); }
387 {
return data_.items.shared(); }
390 {
return data_.items.capacity(); }
409 using SetBaseType::operator==;
410 using SetBaseType::operator!=;
414 {
return (
this == &
set || data_.items ==
set.data_.items); }
418 {
return (
this != &
set && data_.items !=
set.data_.items); }
424 {
return Iter(*
this); }
432 {
return IterM(*
this); }
436 {
return Iter(*
this); }
447 { Size i = 0;
return (search(i, value) != NULL); }
450 Iter
iter(
const Value& value)
const {
452 const Item*
item = search(iterkey.a, value);
453 return (item != NULL ?
Iter(*
this, iterkey, (IterItem*)item) :
Iter(*
this,
iterEND));
468 const Item*
item = search(iterkey.a, value);
470 return Iter(*
this, iterkey, (IterItem*)item);
471 if (iterkey.a < data_.items.size())
472 return Iter(*
this, iterkey, (IterItem*)&data_.items.item(iterkey.a));
488 const Item*
item = search(iterkey.a, value);
490 return IterM(*
this, iterkey, (IterItem*)item);
491 if (iterkey.a < data_.items.size())
492 return IterM(*
this, iterkey, (IterItem*)&data_.items.item(iterkey.a));
506 if (search(iterkey.a, value) != NULL)
508 if (iterkey.a < data_.items.size())
509 return Iter(*
this, iterkey, (IterItem*)&data_.items.item(iterkey.a));
523 if (search(iterkey.a, value) != NULL)
525 if (iterkey.a < data_.items.size())
526 return IterM(*
this, iterkey, (IterItem*)&data_.items.item(iterkey.a));
533 const Item*
item = search(iterkey.a, value);
534 return (item != NULL ?
IterM(*
this, iterkey, (IterItem*)item) :
IterM(*
this,
iterEND));
537 Value&
get(
const Value& value,
bool* created=NULL) {
540 if ( (item=(Item*)search(pos, value)) == NULL) {
541 item = &(data_.items.itemM(data_.items.insert(pos, &value, 1)));
545 }
else if (created != NULL)
556 const Value&
item(Size index)
const 557 {
return data_.items.item(index); }
562 { data_.items.unshare();
return *
this; }
565 { data_.items.capacity(size);
return *
this; }
568 { data_.items.capacitymin(min);
return *
this; }
572 { data_.items.reserve(size);
return *
this; }
575 { data_.items.compact();
return *
this; }
579 Value&
add(
const Value&
item,
bool update=
false) {
581 Value& upditem =
get(
item, &created_val);
582 if (!created_val && update)
589 bool remove(
const Value& value) {
591 if (search(index, value) != NULL) {
592 data_.items.remove(index);
606 Size
remove(
const Value& value, Size count) {
608 if (count > 0 && search(index, value) != NULL) {
609 count = data_.items.remove(index, count);
617 {
return remove((IterM&)iter, dir); }
621 if (iter &&
this == &iter.getParent()) {
622 IterKey& iterkey = iter.getKey();
623 data_.items.remove(iterkey.a);
624 bool nextitem =
false;
628 { --iterkey.a; nextitem =
true; }
629 }
else if (iterkey.a < data_.items.size())
633 iter.setData( (IterItem*)&data_.items.item(iterkey.a) );
650 assert( index < data_.items.size() );
651 count = data_.items.remove(index, count);
666 if (count > 0 && start &&
this == &start.getParent()) {
667 count = data_.items.remove(start.index().a, count);
686 if (start &&
this == &start.getParent()) {
687 const Size index = start.index().a;
689 if (
this == &end.getParent()) {
690 const Size end_index = end.index().a;
691 if (end_index > index) {
692 const Size count = data_.items.remove(index, end_index - index);
699 const Size count = data_.items.remove(index,
ALL);
712 void iterInitMutable()
713 { data_.items.iterInitMutable(); }
714 const IterItem* iterFirst(IterKey& key)
const 715 {
return (IterItem*)data_.items.iterFirst(key.a); }
716 const IterItem* iterNext(IterKey& key)
const 717 {
return (IterItem*)data_.items.iterNext(key.a); }
718 const IterItem* iterNext(Size count, IterKey& key)
const 719 {
return (IterItem*)data_.items.iterNext(count, key.a); }
720 const IterItem* iterLast(IterKey& key)
const 721 {
return (IterItem*)data_.items.iterLast(key.a); }
722 const IterItem* iterPrev(IterKey& key)
const 723 {
return (IterItem*)data_.items.iterPrev(key.a); }
727 const Value*
getiter(IterKey& iterkey,
const Value& value)
const 728 {
return search(iterkey.a, value); }
732 struct Data :
public TCompare {
733 using TCompare::operator();
739 Data(
const Data& data) : TCompare(data), items(data.items)
741 #if defined(EVO_CPP11) 742 Data(Data&& data) : TCompare(std::move(data)), items(std::move(data.items))
746 ((TCompare&)*
this) = std::move((TCompare&)data);
747 items = std::move(data.items);
755 const Value* search(Size& index,
const Value& value)
const {
757 Size left = 0, right = data_.items.size(), mid = 0;
758 while (left < right) {
759 mid = left + ((right - left) / 2);
760 const Value&
item = data_.items.item(mid);
761 cmp = data_(value, item);
764 }
else if (cmp == 0) {
777 #if defined(INCL_evo_string_h) || defined(DOXYGEN) ThisType & operator=(ThisType &&src)
Move assignment operator (C++11).
Definition: setlist.h:324
Associative container with unique values for fast lookup.
Definition: set.h:119
~SetList()
Destructor.
Definition: setlist.h:298
IterM iter_lowerM(const Value &value)
Find first value greater or equal to given value (lower bound).
Definition: setlist.h:486
ThisType & operator=(const ThisType &src)
Assignment operator.
Definition: setlist.h:344
SetList()
Default constructor.
Definition: setlist.h:277
TKey Item
Item type (same as Value)
Definition: set.h:127
bool shared() const
Get whether shared.
Definition: setlist.h:386
Random access iterator.
Definition: iter.h:904
Set< TKey, TSize > SetBaseType
Set base type
Definition: set.h:123
IterM begin()
Get iterator at first item (mutable).
Definition: setlist.h:431
SetList(const SetBaseType &src)
Copy constructor.
Definition: setlist.h:286
IterM iter_upperM(const Value &value)
Find first value greater than given value (upper bound).
Definition: setlist.h:521
ThisType & unshare()
Make data unique by allocating new buffer, if needed (modifier).
Definition: setlist.h:561
Size size() const
Get set size (number of items).
Definition: set.h:230
TCompare Compare
Compare type to use
Definition: setlist.h:271
Iter iter(const Value &value) const
Find (lookup) iterator for given value (const).
Definition: setlist.h:450
Iter iter_lower(const Value &value) const
Find first value greater or equal to given value (lower bound) (const).
Definition: setlist.h:466
ThisType & capacity(Size size)
Set capacity for set (modifier).
Definition: setlist.h:564
ThisType & compact()
Reduce capacity to fit current size (modifier).
Definition: setlist.h:574
Iter iter_upper(const Value &value) const
Find first value greater than given value (upper bound) (const).
Definition: setlist.h:504
Basic integer type.
Definition: type.h:980
const Compare & get_compare() const
Get comparison object being used for comparisons (const).
Definition: setlist.h:400
IterM iterM(const Value &value)
Find (lookup) iterator for given value (mutable).
Definition: setlist.h:531
Size size_
Set size (number of items, automatically updated by concrete set members)
Definition: set.h:146
Size remove_range(IterM &start, IterM &end)
Remove range of values using iterators (mutable).
Definition: setlist.h:685
Size remove_range(IterM &start, Size count)
Remove range of values using iterator (mutable).
Definition: setlist.h:665
SetList< String > StrSetList
SetList using String values.
Definition: setlist.h:782
SetList(ThisType &&src)
Move constructor (C++11).
Definition: setlist.h:315
IterM end()
Get iterator at end.
Definition: setlist.h:439
String container.
Definition: string.h:674
bool contains(const Value &value) const
Get whether the set contains the given value.
Definition: setlist.h:446
Bidirectional iterator.
Definition: iter.h:611
static const EndT ALL
Special integer value for indicating all items or all remaining items.
Definition: type.h:1839
bool operator==(const ThisType &set) const
Equality operator.
Definition: setlist.h:413
IteratorDir
Iterator direction value.
Definition: iter.h:27
Iter cend() const
Get iterator at end (const).
Definition: setlist.h:427
Iter cbegin() const
Get iterator at first item (const).
Definition: setlist.h:423
Reverse iterator direction.
Definition: iter.h:30
Evo C++ Library namespace.
Definition: alg.h:11
No iterator direction.
Definition: iter.h:28
Iter end() const
Get iterator at end (const).
Definition: setlist.h:443
ThisType & operator=(const SetBaseType &src)
Assignment operator.
Definition: setlist.h:340
bool null() const
Get whether set is null.
Definition: setlist.h:383
Size capacity() const
Get set capacity.
Definition: setlist.h:389
TKey Key
Key type (same as Value)
Definition: set.h:125
SizeT Size
Size type for size values (must be unsigned integer) – default: SizeT.
Definition: set.h:124
IteratorRa< ThisType >::Const Iter
Iterator (const) - IteratorRa.
Definition: setlist.h:273
ThisType & reserve(Size size)
Reserve space for new items.
Definition: setlist.h:571
Size removeat(Size index, Size count=1)
Remove value(s) at given position/index (mutable).
Definition: setlist.h:649
End iterator position.
Definition: iter.h:23
TKey Value
Value type.
Definition: set.h:126
ThisType & clear()
Clear by removing all items.
Definition: setlist.h:375
Set implemented as an ordered sequential array/list.
Definition: setlist.h:250
ThisType & capacitymin(Size min)
Set capacity to at least given minimum for set (modifier).
Definition: setlist.h:567
Value & add(const Value &item, bool update=false)
Add or update using given item.
Definition: setlist.h:579
Iter begin() const
Get iterator at first item (const).
Definition: setlist.h:435
const ThisType & asconst() const
Explicitly use a const reference to this.
Definition: setlist.h:333
SetList(std::initializer_list< Value > init)
Sequence constructor (C++11).
Definition: setlist.h:305
const Value * getiter(IterKey &iterkey, const Value &value) const
Used by base class to get data to initialize iterator.
Definition: setlist.h:727
SetList< TKey, TCompare, TSize > ThisType
This type.
Definition: setlist.h:270
const Value & item(Size index) const
Get item value at position (const).
Definition: setlist.h:556
T & min(T &a, T &b)
Returns lowest of given values.
Definition: alg.h:26
IteratorRa< ThisType > IterM
Iterator (mutable) - IteratorRa.
Definition: setlist.h:274
Compare & get_compare()
Get comparison object being used for comparisons.
Definition: setlist.h:406
SetList(const ThisType &src)
Copy constructor.
Definition: setlist.h:294
bool operator!=(const ThisType &set) const
Inequality operator.
Definition: setlist.h:417
bool ordered() const
Get whether set is ordered.
Definition: setlist.h:392
ThisType & setempty()
Set as empty but not null.
Definition: setlist.h:369