libUTL++
|
Abstraction for a Collection whose objects may be sorted. More...
#include <SortedCollection.h>
Public Member Functions | |
void | clobber (const SortedCollection *rhs) |
Where self and rhs intersect, make self's object equal to the rhs version (i.e. More... | |
virtual int | compare (const Object &rhs) const |
Compare with another SortedCollection by comparing contained objects. More... | |
virtual Object * | addOrFind (const Object *object) |
Add the given object, or find a matching object already contained. More... | |
virtual bool | addOrUpdate (const Object *object) |
Add or update the given object. More... | |
void | insert (const Object &object, const BidIt &it) |
Insert an object before the pointed-to location. More... | |
virtual void | insert (const Object *object, const BidIt &it) |
Insert an object before the pointed-to location. More... | |
void | reverse () |
Reverse the order of the contained objects. More... | |
Accessors | |
void | setOrdering (const Ordering &ordering, uint_t algorithm=sort_quickSort) |
Set the ordering, and optionally sort the collection to reflect the new ordering. More... | |
virtual void | setOrdering (Ordering *ordering, uint_t algorithm=sort_quickSort) |
Set the ordering, and optionally sort the collection to reflect the new ordering. More... | |
Querying and Searching | |
void | findEqualRange (const Object &key, BidIt &begin, BidIt &end) const |
Find the range elements [begin,end) that match a given key. More... | |
virtual void | findEqualRange (const Object &key, BidIt &begin, BidIt &end) |
Find the range of elements [begin,end) that match a given key. More... | |
void | findFirstIt (const Object &key, BidIt &it, bool insert=false) const |
Find the first object matching the given key. More... | |
virtual void | findFirstIt (const Object &key, BidIt &it, bool insert=false) |
Find the first object matching the given key. More... | |
void | findLastIt (const Object &key, BidIt &it) const |
Find the last object matching the given key. More... | |
virtual void | findLastIt (const Object &key, BidIt &it) |
Find the last object matching the given key. More... | |
Object * | findLinear (const Object &key) const |
Linear search for an object matching the given key. More... | |
Object * | findLinearSorted (const Object &key) const |
Linear search for an object matching the given key. More... | |
Removing Objects | |
bool | remove (const Object *key) |
virtual bool | remove (const Object &key) |
Remove the object matching the given key. More... | |
void | remove (const SortedCollection *rhs) |
Remove objects that have a match in rhs. More... | |
virtual void | removeIt (BidIt &it) |
Remove the object the given iterator points to. More... | |
virtual void | removeIt (BidIt &begin, const BidIt &end) |
Remove part of the collection. More... | |
SortedCollection & | operator-= (const SortedCollection &rhs) |
Remove objects that have a match in rhs. More... | |
Set Algorithms | |
Collection * | difference (const SortedCollection *rhs, Collection *out=nullptr) const |
Determine the difference between self and rhs. More... | |
void | intersect (const SortedCollection *rhs) |
Set self to the intersection of self and rhs. More... | |
Collection * | intersection (const SortedCollection *rhs, Collection *out=nullptr, bool multiSet=false) const |
Determine the intersection of self and rhs. More... | |
size_t | intersectCard (const SortedCollection *rhs) const |
Determine the cardinality of the intersection of self and rhs. More... | |
bool | intersects (const SortedCollection *rhs) const |
Determine whether the intersection of self and rhs is non-empty. More... | |
bool | isSubSet (const SortedCollection *rhs) const |
Determine whether self is a subset of rhs. More... | |
bool | isSuperSet (const SortedCollection *rhs) const |
Determine whether self is a superset of rhs. More... | |
Collection * | merge (const SortedCollection *rhs, Collection *out=nullptr) const |
Merge with rhs to form a single sorted sequence. More... | |
Collection * | symmetricDifference (const SortedCollection *rhs, Collection *out=nullptr) const |
Determine the symmetric difference of self and rhs. More... | |
void | symmetricDifference (const SortedCollection *rhs, Collection *lhsOut, Collection *rhsOut) const |
Determine the symmetric difference of self and rhs. More... | |
Collection * | unique (Collection *out=nullptr) |
Remove duplicate objects. More... | |
SortedCollection & | operator &= (const SortedCollection *rhs) |
Intersect with rhs. More... | |
bool | operator<= (const SortedCollection *rhs) |
Determine whether self is a subset of rhs. More... | |
bool | operator>= (const SortedCollection *rhs) |
Determine whether self is a superset of rhs. More... | |
Sorting | |
bool | testSorted () const |
Determine whether the collection is sorted (often useful for testing). More... | |
void | multiKeyQuickSort (bool key=true, bool reverse=false) |
Multi-key quick-sort the collection. More... | |
void | sort () |
Sort the collection using the quicksort algorithm. More... | |
virtual void | sort (uint_t algorithm) |
Sort the collection using the given algorithm. More... | |
Public Member Functions inherited from utl::Collection | |
void | assertOwner () |
Assert ownership of contained objects. More... | |
virtual void | copy (const Object &rhs) |
Copy another instance. More... | |
virtual void | steal (Object &rhs) |
"Steal" the internal representation from another instance. More... | |
virtual void | vclone (const Object &rhs) |
Make an exact copy of another instance. More... | |
virtual void | dump (Stream &os, uint_t level=uint_t_max) const |
Dump a human-readable representation of self to the given output stream. More... | |
virtual void | serialize (Stream &stream, uint_t io, uint_t mode=ser_default) |
Serialize to or from a stream. More... | |
void | serialize (const RunTimeClass *rtc, Stream &stream, uint_t io, uint_t mode=ser_default) |
Serialize to/from the given stream. More... | |
virtual size_t | innerAllocatedSize () const |
Get the "inner" allocated size. More... | |
virtual bool | update (const Object *object) |
Update the given object. More... | |
bool | isOwner () const |
Get the owner flag. More... | |
void | setOwner (bool owner) |
Set the owner flag. More... | |
const Ordering * | ordering () const |
Get the ordering. More... | |
Ordering * | ordering () |
Get the ordering. More... | |
void | setOrdering (const Ordering &ordering, uint_t algorithm=sort_quickSort) |
Set the ordering, and optionally sort the collection to reflect the new ordering. More... | |
bool | empty () const |
Determine whether the collection is empty. More... | |
bool | isMultiSet () const |
Get the multiSet flag. More... | |
void | setMultiSet (bool multiSet) |
Set the multiSet flag. More... | |
bool | isMarked () const |
Get marked flag. More... | |
void | setMarked (bool marked=true) |
Set marked flag. More... | |
size_t | items () const |
Get the number of contained objects. More... | |
size_t | size () const |
Get the number of contained objects. More... | |
bool | add (const Object &object) |
Add an object to the collection. More... | |
virtual bool | add (const Object *object)=0 |
Add an object to the collection. More... | |
void | add (const Collection &collection) |
Add another collection's objects. More... | |
virtual BidIt * | addIt (const Object *object) |
Add an object to the collection. More... | |
Object * | addOrFind (const Object &object) |
Add the given object, or find a matching object already contained. More... | |
bool | addOrUpdate (const Object &object) |
Add or update the given object. More... | |
Collection & | copyItems (const Collection *src, const Predicate *pred=nullptr, bool predVal=true) |
Copy objects from another collection. More... | |
Collection & | copyItems (const ListNode *src, const Predicate *pred=nullptr, bool predVal=true) |
Copy objects from a double-linked list. More... | |
Collection & | copyItems (const SlistNode *src, const Predicate *pred=nullptr, bool predVal=true) |
Copy objects from a single-linked list. More... | |
Collection & | stealItems (Collection *src) |
"Steal" items from another collection. More... | |
Collection & | operator+= (const Object &rhs) |
Add an object to the collection. More... | |
Collection & | operator+= (const Object *rhs) |
Add an object to the collection. More... | |
Collection & | operator+= (const Collection &rhs) |
Add another collection's objects. More... | |
Collection & | operator-= (const Object &rhs) |
Remove an object from the collection. More... | |
void | dump (Stream &os, uint_t level, bool key, bool printClassName, uint_t indent, const char *separator) const |
Dump contained objects to a stream. More... | |
virtual String | toString () const |
Obtain a string representation by invoking Object::toString() on all contained objects. | |
String | toString (bool key) const |
Obtain a string representation by invoking Object::toString() on all contained objects (or their keys). More... | |
String | toString (const char *sep, bool key=false) const |
Obtain a string represenation by invoking Object::toString() on all contained objects (or their keys). More... | |
String | toString (const String &sep, bool key=false) const |
Obtain a string represenation of the collection by invoking Object::toString() on all contained objects (or their keys). More... | |
iterator | begin () const |
Return a const iterator pointing to the first object in the collection. More... | |
iterator | begin () |
Return an iterator pointing to the first object in the collection. More... | |
virtual BidIt * | beginNew () const =0 |
Return a const iterator pointing to the beginning of the collection. More... | |
virtual BidIt * | beginNew ()=0 |
Return an iterator pointing to the beginning of the collection. More... | |
virtual BidIt * | createIt () const |
Create a const iterator. More... | |
virtual BidIt * | createIt () |
Create an iterator. More... | |
iterator | end () const |
Return a const iterator pointing to the end of the collection. More... | |
iterator | end () |
Return an iterator pointing to the end of the collection. More... | |
virtual BidIt * | endNew () const =0 |
Return a const iterator pointing to the end of the collection. More... | |
virtual BidIt * | endNew ()=0 |
Return an iterator pointing to the end of the collection. More... | |
bool | contains (const Object *key) const |
Determine whether the collection contains an object matching the given key. More... | |
bool | contains (const Object &key) const |
Determine whether the collection contains an object matching the given key. More... | |
size_t | count (const Predicate *pred=nullptr, bool predVal=true) const |
Count contained objects. More... | |
Object * | find (const Object *key) const |
Find an object matching a given key. More... | |
virtual Object * | find (const Object &key) const |
Find an object matching a given key. More... | |
void | findIt (const Object &key, BidIt &it) const |
Find an object matching a given key. More... | |
virtual void | findIt (const Object &key, BidIt &it) |
Find an object matching a given key. More... | |
Object * | first () const |
Return the first object (nullptr if empty). More... | |
bool | has (const Object *key) const |
See contains(). More... | |
bool | has (const Object &key) const |
See contains(). More... | |
Object * | last () const |
Get the last object (nullptr if empty). More... | |
virtual void | clear ()=0 |
Clear the collection by removing all objects. More... | |
bool | remove (const Object *key) |
Remove the object matching the given key. More... | |
Object * | take (BidIt &it) |
Remove the object the given iterator points to, but do not delete the object even if the collection owns its objects. More... | |
Object * | operator[] (const Object *object) |
add-or-find access operator (returns Object*). More... | |
Object & | operator() (const Object *object) |
add-or-find access operator (returns Object&). More... | |
Public Member Functions inherited from utl::Object | |
void | clear () |
Revert to initial state. More... | |
void | dumpWithClassName (Stream &os, uint_t indent=4, uint_t level=uint_t_max) const |
Front-end for dump() that prints the object's class name. More... | |
virtual const Object & | getKey () const |
Get the key for this object. More... | |
bool | hasKey () const |
Determine whether or not the object has a key. More... | |
virtual const Object & | getProxiedObject () const |
Get the proxied object (= self if none). More... | |
virtual Object & | getProxiedObject () |
Get the proxied object (= self if none). More... | |
virtual size_t | hash (size_t size) const |
Get the hash code for the object. More... | |
bool | _isA (const RunTimeClass *runTimeClass) const |
Determine whether self's class is a descendent of the given class. More... | |
operator String () const | |
Conversion to String. More... | |
size_t | allocatedSize () const |
Get the total allocated size of this object. More... | |
virtual void | addOwnedIt (const class FwdIt *it) const |
Notify self that it owns the given iterator. More... | |
virtual void | removeOwnedIt (const class FwdIt *it) const |
Notify self that the given owned iterator has been destroyed. More... | |
bool | operator< (const Object &rhs) const |
Less-than operator. More... | |
bool | operator<= (const Object &rhs) const |
Less-than-or-equal-to operator. More... | |
bool | operator> (const Object &rhs) const |
Greater-than operator. More... | |
bool | operator>= (const Object &rhs) const |
Greater-than-or-equal-to operator. More... | |
bool | operator== (const Object &rhs) const |
Equal-to operator. More... | |
bool | operator!= (const Object &rhs) const |
Unequal-to operator. More... | |
void | serializeIn (Stream &is, uint_t mode=ser_default) |
Serialize from an input stream. More... | |
void | serializeOut (Stream &os, uint_t mode=ser_default) const |
Serialize to an output stream. More... | |
void | serializeOutBoxed (Stream &os, uint_t mode=ser_default) const |
Serialize a boxed object to an output stream. More... | |
Additional Inherited Members | |
Static Public Member Functions inherited from utl::Object | |
static Object * | serializeInNullable (Stream &is, uint_t mode=ser_default) |
Serialize a nullptr-able object from an input stream. More... | |
static void | serializeOutNullable (const Object *object, Stream &os, uint_t mode=ser_default) |
Serialize a nullptr-able object to an output stream. More... | |
static void | serializeNullable (Object *&object, Stream &stream, uint_t io, uint_t mode=ser_default) |
Serialize a nullptr-able object to or from a stream. More... | |
static Object * | serializeInBoxed (Stream &is, uint_t mode=ser_default) |
Serialize a boxed object from an input stream. More... | |
static void | serializeBoxed (Object *&object, Stream &stream, uint_t io, uint_t mode=ser_default) |
Serialize a boxed object to or from a stream. More... | |
Protected Member Functions inherited from utl::FlagsMI | |
FlagsMI () | |
Constructor. More... | |
virtual | ~FlagsMI () |
Destructor. More... | |
void | copyFlags (const FlagsMI &rhs) |
Copy the given flags. More... | |
void | copyFlags (const FlagsMI &rhs, uint_t lsb, uint_t msb) |
Copy (some of) the given flags. More... | |
void | copyFlags (uint64_t flags, uint_t lsb, uint_t msb) |
Copy (some of) the given flags. More... | |
bool | getFlag (uint_t flagNum) const |
Get a user-defined flag. More... | |
void | setFlag (uint_t flagNum, bool val) |
Set a user-defined flag. More... | |
uint64_t | getFlagsNumber (uint64_t mask, uint64_t shift=0) |
Get a multi-bit value in the flags data (which is stored as one 64-bit integer). More... | |
void | setFlagsNumber (uint64_t mask, uint64_t shift, uint64_t num) |
Set a multi-bit value in the flags data (which is stored as one 64-bit integer). More... | |
uint64_t | getFlags () const |
Get the flags. More... | |
void | setFlags (uint64_t flags) |
Set the flags. More... | |
Abstraction for a Collection whose objects may be sorted.
Several of SortedCollection's methods take a parameter called out which specifies the output collection to store the result. For all such methods, if no output collection is given (out == nullptr), a new collection of the same class as self will be created to store the result.
Attributes
Definition at line 33 of file SortedCollection.h.
void utl::SortedCollection::clobber | ( | const SortedCollection * | rhs | ) |
Where self and rhs intersect, make self's object equal to the rhs version (i.e.
"clobber" it with the rhs version).
rhs | collection to clobber with |
|
virtual |
Compare with another SortedCollection by comparing contained objects.
Reimplemented from utl::Object.
Add the given object, or find a matching object already contained.
Reimplemented from utl::Collection.
|
virtual |
Add or update the given object.
If the object already exists, it will be updated, otherwise it will be added.
object | object to add or update |
Reimplemented from utl::Collection.
Insert an object before the pointed-to location.
object | object being inserted |
it | reference location |
Definition at line 75 of file SortedCollection.h.
References utl::clone(), and utl::reverse().
Insert an object before the pointed-to location.
object | object being inserted |
it | reference location |
Reimplemented in utl::Array, utl::List, utl::TArray< T >, utl::TArray< REnode >, utl::TArray< utl::String >, utl::TArray< utl::Span< size_t > >, utl::TArray< LogStream >, utl::TArray< Terminal >, utl::TArray< utl::Stream >, utl::BinTree, and utl::SkipList.
void utl::SortedCollection::reverse | ( | ) |
Reverse the order of the contained objects.
|
inline |
Set the ordering, and optionally sort the collection to reflect the new ordering.
ordering | new ordering (make a copy) |
algorithm | (optional : sort_quickSort) sort algorithm |
Definition at line 104 of file SortedCollection.h.
References utl::sort_quickSort.
|
virtual |
Set the ordering, and optionally sort the collection to reflect the new ordering.
ordering | new ordering |
algorithm | (optional : sort_quickSort) sort algorithm |
Reimplemented from utl::Collection.
Reimplemented in utl::List.
|
inline |
Find the range elements [begin,end) that match a given key.
key | search key |
begin | beginning of located range (= end if no object found) |
end | end of located range (= end if no object found) |
Definition at line 126 of file SortedCollection.h.
References const_cast_this, IFDEBUG, and utl::FwdIt::setConst().
|
virtual |
Find the range of elements [begin,end) that match a given key.
key | search key |
begin | beginning of located range (= end if no object found) |
end | end of located range (= end if no object found) |
Reimplemented in utl::List.
|
inline |
Find the first object matching the given key.
key | search key |
it | const result iterator (= end if no object found) |
insert | (optional : false) find insertion point? |
Definition at line 148 of file SortedCollection.h.
References const_cast_this, IFDEBUG, and utl::FwdIt::setConst().
|
virtual |
Find the first object matching the given key.
key | search key |
it | result iterator (= end if no object found) |
insert | (optional : false) find insertion point? |
Reimplemented in utl::Array, utl::List, utl::TSkipList< T >, utl::TRBtree< T >, utl::TRBtree< String >, utl::TRBtree< Production >, utl::TRBtree< Terminal >, utl::TRBtree< CmdLineArg >, utl::TRBtree< Span< T, D > >, utl::BinTree, utl::TArray< T >, utl::TArray< REnode >, utl::TArray< utl::String >, utl::TArray< utl::Span< size_t > >, utl::TArray< LogStream >, utl::TArray< Terminal >, utl::TArray< utl::Stream >, utl::SkipList, and utl::TList< T >.
Find the last object matching the given key.
key | search key |
it | const result iterator (= end if no object found) |
Definition at line 168 of file SortedCollection.h.
References ASSERTD, const_cast_this, IFDEBUG, and utl::FwdIt::setConst().
Find the last object matching the given key.
key | search key |
it | result iterator (= end if no object found) |
Reimplemented in utl::Array, utl::List, utl::TSkipList< T >, utl::TRBtree< T >, utl::TRBtree< String >, utl::TRBtree< Production >, utl::TRBtree< Terminal >, utl::TRBtree< CmdLineArg >, utl::TRBtree< Span< T, D > >, utl::BinTree, utl::TArray< T >, utl::TArray< REnode >, utl::TArray< utl::String >, utl::TArray< utl::Span< size_t > >, utl::TArray< LogStream >, utl::TArray< Terminal >, utl::TArray< utl::Stream >, utl::TList< T >, and utl::SkipList.
Linear search for an object matching the given key.
key | search key |
Linear search for an object matching the given key.
Since the collection is known to be sorted, an unsuccessful search can terminate more quickly.
key | search key |
|
inlinevirtual |
Remove the object matching the given key.
key | search key |
Reimplemented from utl::Collection.
Reimplemented in utl::Array, utl::List, utl::BinTree, utl::SkipList, utl::TSkipList< T >, utl::TRBtree< T >, utl::TRBtree< String >, utl::TRBtree< Production >, utl::TRBtree< Terminal >, utl::TRBtree< CmdLineArg >, utl::TRBtree< Span< T, D > >, utl::SpanCol< T, D >, and utl::SpanAllocator< T, D >.
Definition at line 208 of file SortedCollection.h.
References utl::remove().
|
inline |
Remove objects that have a match in rhs.
rhs | sorted collection to compare with |
Definition at line 219 of file SortedCollection.h.
References utl::difference().
|
inlinevirtual |
Remove the object the given iterator points to.
it | iterator |
Reimplemented from utl::Collection.
Reimplemented in utl::Array, utl::List, utl::TArray< T >, utl::TArray< REnode >, utl::TArray< utl::String >, utl::TArray< utl::Span< size_t > >, utl::TArray< LogStream >, utl::TArray< Terminal >, utl::TArray< utl::Stream >, utl::BinTree, and utl::SkipList.
Definition at line 229 of file SortedCollection.h.
Remove part of the collection.
begin | beginning of subsequence to be removed |
end | end of subsequence to be removed |
|
inline |
Remove objects that have a match in rhs.
rhs | sorted collection to compare with |
Definition at line 246 of file SortedCollection.h.
References utl::difference().
Collection* utl::SortedCollection::difference | ( | const SortedCollection * | rhs, |
Collection * | out = nullptr |
||
) | const |
Determine the difference between self and rhs.
rhs | sorted collection to compare with |
out | (optional) output collection |
|
inline |
Set self to the intersection of self and rhs.
rhs | sorted collection to intersect with |
Definition at line 270 of file SortedCollection.h.
References utl::intersectCard(), utl::intersects(), and utl::isSubSet().
Collection* utl::SortedCollection::intersection | ( | const SortedCollection * | rhs, |
Collection * | out = nullptr , |
||
bool | multiSet = false |
||
) | const |
Determine the intersection of self and rhs.
rhs | sorted collection to intersect with |
out | (optional) output collection |
multiSet | (optional : false) can one of rhs's objects match more than one of self's objects? |
size_t utl::SortedCollection::intersectCard | ( | const SortedCollection * | rhs | ) | const |
Determine the cardinality of the intersection of self and rhs.
rhs | sorted collection to intersect with |
bool utl::SortedCollection::intersects | ( | const SortedCollection * | rhs | ) | const |
Determine whether the intersection of self and rhs is non-empty.
rhs | sorted collection to intersect with |
bool utl::SortedCollection::isSubSet | ( | const SortedCollection * | rhs | ) | const |
Determine whether self is a subset of rhs.
rhs | sorted collection to compare with |
Referenced by isSuperSet().
|
inline |
Determine whether self is a superset of rhs.
rhs | sorted collection to compare with |
Definition at line 318 of file SortedCollection.h.
References isSubSet(), utl::merge(), utl::symmetricDifference(), and utl::unique().
Collection* utl::SortedCollection::merge | ( | const SortedCollection * | rhs, |
Collection * | out = nullptr |
||
) | const |
Merge with rhs to form a single sorted sequence.
rhs | sorted collection to merge with |
out | (optional) output collection |
Collection* utl::SortedCollection::symmetricDifference | ( | const SortedCollection * | rhs, |
Collection * | out = nullptr |
||
) | const |
Determine the symmetric difference of self and rhs.
rhs | sorted collection to compare with |
out | (optional) output collection |
void utl::SortedCollection::symmetricDifference | ( | const SortedCollection * | rhs, |
Collection * | lhsOut, | ||
Collection * | rhsOut | ||
) | const |
Determine the symmetric difference of self and rhs.
rhs | sorted collection to compare with |
lhsOut | output collection for self's objects |
rhsOut | output collection for rhs's objects |
Collection* utl::SortedCollection::unique | ( | Collection * | out = nullptr | ) |
Remove duplicate objects.
out | (optional) output collection |
|
inline |
Intersect with rhs.
rhs | sorted collection to intersect with |
Definition at line 365 of file SortedCollection.h.
References utl::intersect().
|
inline |
Determine whether self is a subset of rhs.
rhs | sorted collection to compare with |
Definition at line 377 of file SortedCollection.h.
References utl::isSubSet().
|
inline |
Determine whether self is a superset of rhs.
rhs | sorted collection to compare with |
Definition at line 388 of file SortedCollection.h.
References utl::isSuperSet(), and utl::multiKeyQuickSort().
bool utl::SortedCollection::testSorted | ( | ) | const |
Determine whether the collection is sorted (often useful for testing).
void utl::SortedCollection::multiKeyQuickSort | ( | bool | key = true , |
bool | reverse = false |
||
) |
Multi-key quick-sort the collection.
Every object in the collection must either be a String (key = false) or have a String as its key (key = true).
key | (optional : true) objects' strings are their keys? |
reverse | (optional : false) reverse sort? |
|
inline |
Sort the collection using the quicksort algorithm.
Definition at line 411 of file SortedCollection.h.
References utl::sort(), and utl::sort_quickSort.
|
virtual |
Sort the collection using the given algorithm.
algorithm | sort algorithm (see utl::sort_t) |
Reimplemented in utl::Array, and utl::List.