SourcePro® API Reference Guide

 
Loading...
Searching...
No Matches
RWSortedVector Class Reference

Represents a group of ordered items. More...

#include <rw/sortvec.h>

Inheritance diagram for RWSortedVector:
RWOrdered RWSequenceable RWCollection RWCollectable

Public Member Functions

 RWSortedVector ()
 
 RWSortedVector (const RWSortedVector &sv)
 
 RWSortedVector (RWSortedVector &&sv)
 
 RWSortedVector (size_t size)
 
virtual const RWCollectableat (size_t) const
 
virtual RWCollectablecopy () const
 
virtual size_t index (const RWCollectable *target) const
 
virtual RWCollectableinsert (RWCollectable *c)
 
virtual RWClassID isA () const
 
virtual bool isEqual (const RWCollectable *a) const
 
virtual RWCollectablenewSpecies () const
 
virtual size_t occurrencesOf (const RWCollectable *target) const
 
const RWCollectableoperator() (size_t i) const
 
RWSortedVectoroperator= (const RWSortedVector &sv)
 
RWSortedVectoroperator= (RWSortedVector &&sv)
 
bool operator== (const RWSortedVector &sv) const
 
const RWCollectableoperator[] (size_t i) const
 
- Public Member Functions inherited from RWOrdered
 RWOrdered ()
 
 RWOrdered (const RWOrdered &o)
 
 RWOrdered (RWOrdered &&o)
 
 RWOrdered (size_t size)
 
virtual void apply (RWapplyCollectable ap, void *x)
 
virtual void clear ()
 
virtual size_t entries () const
 
virtual RWCollectablefind (const RWCollectable *target) const
 
virtual RWCollectablefirst () const
 
virtual bool isEmpty () const
 
virtual RWCollectablelast () const
 
virtual size_t newCapacity (size_t) const
 
virtual RWConstIteratornewConstIterator () const
 
virtual RWIteratornewIterator ()
 
RWCollectable *& operator() (size_t i)
 
RWOrderedoperator= (const RWOrdered &o)
 
RWOrderedoperator= (RWOrdered &&o)
 
bool operator== (const RWOrdered &od) const
 
RWCollectable *& operator[] (size_t i)
 
RWCollectablepop ()
 
void push (RWCollectable *c)
 
virtual RWCollectableremove (const RWCollectable *target)
 
RWCollectableremoveAt (size_t index)
 
void swap (RWOrdered &o)
 
RWCollectabletop () const
 
- Public Member Functions inherited from RWCollection
virtual ~RWCollection ()
 
RWBag asBag () const
 
RWBinaryTree asBinaryTree () const
 
RWOrdered asOrderedCollection () const
 
RWSet asSet () const
 
RWBinaryTree asSortedCollection () const
 
virtual RWspace binaryStoreSize () const
 
virtual void clearAndDestroy ()
 
virtual bool contains (const RWCollectable *target) const
 
void operator+= (const RWCollection &c)
 
void operator-= (const RWCollection &c)
 
virtual void removeAndDestroy (const RWCollectable *target)
 
virtual void restoreGuts (RWFile &)
 
virtual void restoreGuts (RWvistream &)
 
virtual void saveGuts (RWFile &) const
 
virtual void saveGuts (RWvostream &) const
 
RWCollectionselect (RWtestCollectable tst, void *vp) const
 
- Public Member Functions inherited from RWCollectable
virtual ~RWCollectable ()
 
virtual int compareTo (const RWCollectable *) const
 
virtual unsigned hash () const
 
RWspace recursiveStoreSize () const
 
RWStringID stringID () const
 

Private Member Functions

virtual RWCollectableappend (RWCollectable *a)
 
virtual RWCollectable *& at (size_t)
 
virtual RWCollectableinsertAt (size_t, RWCollectable *)
 
virtual RWCollectableprepend (RWCollectable *)
 

Related Symbols

(Note that these are not member symbols.)

typedef RWOrderedConstIterator RWSortedVectorConstIterator
 
typedef RWOrderedIterator RWSortedVectorIterator
 

Additional Inherited Members

- Static Public Member Functions inherited from RWCollectable
static RWClassID classID (const RWStringID &name)
 
static RWClassID classIsA ()
 
static bool isAtom (RWClassID id)
 
static RWspace nilStoreSize ()
 
- Static Public Attributes inherited from RWCollection
static size_t DEFAULT_CAPACITY
 

Detailed Description

Class RWSortedVector represents a group of ordered items, internally sorted by the compareTo() function and accessible by an index number. Duplicates are allowed. An object stored by RWSortedVector must inherit from the abstract base class RWCollectable. An insertion sort is used to maintain the vector in sorted order.

Because class RWSortedVector is implemented as a vector of pointers, traversing the collection is more efficient than using RWBinaryTree. However, insertions are slower in the center of the collection.

Note
The vector is sorted; do not modify elements contained in the vector in such a way as to invalidate the ordering.
Synopsis
#include <rw/sortvec.h>
Represents a group of ordered items.
Definition sortvec.h:114
Persistence
Polymorphic
Example
#include <rw/collstr.h>
#include <rw/sortvec.h>
int main() {
sv.insert(new RWCollectableString("monster"));
sv.insert(new RWCollectableString("rabbit"));
sv.insert(new RWCollectableString("hog"));
sv.insert(new RWCollectableString("dog"));
sv.insert(new RWCollectableString("cat"));
sv.insert(new RWCollectableString("fish"));
while ((item = (RWCollectableString*)next()) != 0) {
std::cout << *item << std::endl;
}
return 0;
}
Provides a polymorphically persistable RWCString.
Definition collstr.h:50
virtual void clearAndDestroy()
Traverses the collection from the first to the last item.
Definition ordcltn.h:340
virtual RWCollectable * insert(RWCollectable *c)

Program output:

cat
dog
fish
hog
monster
rabbit

Constructor & Destructor Documentation

◆ RWSortedVector() [1/4]

RWSortedVector::RWSortedVector ( )

Constructs an empty RWSortedVector that has an initial capacity of RWCollection::DEFAULT_CAPACITY items. The capacity is increased automatically as needed.

◆ RWSortedVector() [2/4]

RWSortedVector::RWSortedVector ( size_t size)

Constructs an empty RWSortedVector that has an initial capacity of size items. The capacity is increased automatically as needed.

◆ RWSortedVector() [3/4]

RWSortedVector::RWSortedVector ( const RWSortedVector & sv)
inline

Copy constructor. Makes a shallow copy of sv.

◆ RWSortedVector() [4/4]

RWSortedVector::RWSortedVector ( RWSortedVector && sv)
inline

Move constructor. The constructed RWSortedVector takes ownership of the data owned by sv.

Condition:
This method is available only on platforms with rvalue reference support.

Member Function Documentation

◆ append()

virtual RWCollectable * RWSortedVector::append ( RWCollectable * a)
privatevirtual

Adds a to the end of the collection and returns it. Returns rwnil if the insertion was unsuccessful.

Reimplemented from RWOrdered.

◆ at() [1/2]

virtual RWCollectable *& RWSortedVector::at ( size_t i)
privatevirtual

Allows access to the i th element of the collection. The return value can be used as an lvalue. The index i must be between zero and the number of items in the collection less one, or the function throws an exception of type RWBoundsErr.

Reimplemented from RWOrdered.

◆ at() [2/2]

virtual const RWCollectable * RWSortedVector::at ( size_t i) const
virtual

Allows access to the i th element of the collection. The return value cannot be used as an lvalue. The index i must be between zero and the number of items in the collection less one, or an exception of type RWBoundsErr will be thrown.

Reimplemented from RWOrdered.

◆ copy()

virtual RWCollectable * RWSortedVector::copy ( ) const
virtual

Returns a new, copy-constructed object of the same type as self. The caller is responsible for deleting the object.

Reimplemented from RWOrdered.

◆ index()

virtual size_t RWSortedVector::index ( const RWCollectable * target) const
virtual

Performs a binary search to return the index of the first item that compares equal to the target item, or RW_NPOS if no such item can be found.

Note
Derived methods RWOrdered::find() and RWOrdered::remove() use this virtual function to perform their search. Hence, these methods called on an instance of RWSortedVector results in a binary search.

Reimplemented from RWOrdered.

◆ insert()

virtual RWCollectable * RWSortedVector::insert ( RWCollectable * c)
virtual

Performs a binary search to insert the item pointed to by c after all items that compare less than or equal to it, but before all items that compare greater to it. Returns c if the insertion was successful, otherwise returns rwnil.

Reimplemented from RWOrdered.

◆ insertAt()

virtual RWCollectable * RWSortedVector::insertAt ( size_t indx,
RWCollectable * e )
privatevirtual

Adds a new item to the collection at position indx. The item previously at position i is moved to i+1, etc. The index indx must be between 0 and the number of items in the collection, or an exception of type RWBoundsErr will be thrown.

Reimplemented from RWOrdered.

◆ isA()

virtual RWClassID RWSortedVector::isA ( ) const
virtual

Returns __RWSORTEDVECTOR, i.e. a class identifier, that identifies this object's class.

Reimplemented from RWOrdered.

◆ isEqual()

virtual bool RWSortedVector::isEqual ( const RWCollectable * a) const
virtual

Returns true or false based on operator==().

Reimplemented from RWOrdered.

◆ newSpecies()

virtual RWCollectable * RWSortedVector::newSpecies ( ) const
virtual

Returns a new, default-constructed object of the same type as self. The caller is responsible for deleting the object.

Reimplemented from RWOrdered.

◆ occurrencesOf()

virtual size_t RWSortedVector::occurrencesOf ( const RWCollectable * target) const
virtual

Returns the number of items that compare isEqual() to the item pointed to by target.

Reimplemented from RWOrdered.

◆ operator()()

const RWCollectable * RWSortedVector::operator() ( size_t i) const
inline

Returns the i th element in the collection. Bounds checking is enabled by defining the preprocessor directive RWBOUNDS_CHECK before including the header file <rw/sortvec.h>. In this case, if i is out of range, an exception of type RWBoundsErr is thrown. The return value cannot be used as an lvalue.

◆ operator=() [1/2]

RWSortedVector & RWSortedVector::operator= ( const RWSortedVector & sv)
inline

Assignment operator. Makes a shallow copy of o.

◆ operator=() [2/2]

RWSortedVector & RWSortedVector::operator= ( RWSortedVector && sv)
inline

Move assignment. Self takes ownership of the data owned by sv.

Condition:
This method is available only on platforms with rvalue reference support.

◆ operator==()

bool RWSortedVector::operator== ( const RWSortedVector & sv) const
inline

Returns true if for every item in self, the corresponding item in sv at the same index isEqual(). The two collections must also have the same number of members.

◆ operator[]()

const RWCollectable * RWSortedVector::operator[] ( size_t i) const
inline

Returns the i th element in the collection. If i is out of range, an exception of type RWBoundsErr is thrown. The return value cannot be used as an lvalue.

◆ prepend()

virtual RWCollectable * RWSortedVector::prepend ( RWCollectable * c)
privatevirtual

Adds c to the beginning of the collection and returns it. Returns rwnil if the insertion was unsuccessful.

Reimplemented from RWOrdered.

Friends And Related Symbol Documentation

◆ RWSortedVectorConstIterator

RWSortedVectorConstIterator is a typedef for RWOrderedConstIterator. For information on how to use it, please see RWOrderedConstIterator.

◆ RWSortedVectorIterator

RWSortedVectorIterator is a typedef for RWOrderedIterator. For information on how to use it, please see RWOrderedIterator.

Copyright © 2024 Rogue Wave Software, Inc., a Perforce company. All Rights Reserved.