RWTPtrSortedDlist(3C++) RWTPtrSortedDlist(3C++)
Name
RWTPtrSortedDlist<T,C> - Rogue Wave library class
Synopsis
#include <rw/tpsrtdli.h>
RWTPtrSortedDlist<T,C> srtdlist;
Standard C++ Library Dependent!RWTPtrSortedDlist requires the Standard C++ Library.
Description
This class maintains an always-sorted pointer-based collection of values,
implemented as a doubly-linked list. Items are ordered according to a
comparison object of type C. Class T is the type pointed to by the items
in the collection. C must induce a total ordering on elements of type T
via a public member
bool operator()(const T& x, const T& y)
which returns true if x should precede y within the collection. The
structure less<T> from the C++-standard header file <functional> is an
example. Note that items in the collection will be dereferenced before
being compared.
Persistence
Isomorphic.
Example
In this example, a sorted doubly-linked list of RWDates is exercised.
//
// tpsrtdli.cpp
//
#include <rw/tpsrtdli.h>
#include <rw/rwdate.h>
#include <iostream.h>
Page 1
RWTPtrSortedDlist(3C++) RWTPtrSortedDlist(3C++)
main(){
RWTPtrSortedDList<RWDate,greater<RWDate> > lst;
lst.insert(new RWDate(10, "Aug", 1991));
lst.insert(new RWDate(9, "Aug", 1991));
lst.insert(new RWDate(1, "Sep", 1991));
lst.insert(new RWDate(14, "May", 1990));
lst.insert(new RWDate(1, "Sep", 1991)); // Add a duplicate
lst.insert(new RWDate(2, "June", 1991));
for (int i=0; i<lst.entries(); i++)
cout << *lst[i] << endl;
lst.clearAndDestroy();
return 0;
}
Program Output:
09/01/91
09/01/91
08/10/91
08/09/91
06/02/91
05/14/90
Related Classes
Class RWTPtrSortedVector<T> is an alternative always-sorted pointer-based
collection. RWTPtrDlist<T> is an unsorted pointer-based doubly-linked
list. Class list<T*,allocator> is the C++-standard collection that
serves as the underlying implementation for this class.
Public Typedefs
typedef rw_deref_compare<C,T> container_comp;
typedef list<T*,allocator> container_type;
typedef container_type::size_type size_type;
typedef container_type::difference_type difference_type;
typedef container_type::const_iterator const_iterator;
typedef container_type::iterator iterator;
typedef T* value_type;
typedef T*& reference;
typedef T* const& const_reference;
Page 2
RWTPtrSortedDlist(3C++) RWTPtrSortedDlist(3C++)
Public Constructors
RWTPtrSortedDlist<T,C>();
Constructs an empty doubly-linked list.
RWTPtrSortedDlist<T,C>(const RWTPtrSortedDlist<T,C>& lst);
Copy constructor.
RWTPtrSortedDlist<T,C>(const list<T*,allocator>& lst);
Constructs a doubly-linked list by iterating over all elements in lst and
performing an order preserving insertion on self for each.
RWTPtrSortedDlist<T,C>(size_type n, T* p);
Constructs a doubly-linked list with n elements, each initialized to p.
RWTPtrSortedDlist<T,C>(T** first,T** last);
Constructs a doubly-linked list by copying and sorting elements from the
array of T*s pointed to by first, up to, but not including, the element
pointed to by last.
Public Member Operators
bool
operator<(const RWTPtrSortedDlist<T,C>& lst) const;
Returns true if self compares lexicographically less than lst, otherwise
returns false. Items in each collection are dereferenced before being
compared.
bool
operator==(const RWTPtrSortedDlist<T,C>& lst) const;
Returns true if self compares equal to lst, otherwise returns false. Two
collections are equal if both have the same number of entries, and
iterating through both collections produces, in turn, individual elements
that compare equal to each other. Elements are dereferenced before being
compared.
reference
operator()(size_type i);
const_reference
operator()(size_type I) const;
Page 3
RWTPtrSortedDlist(3C++) RWTPtrSortedDlist(3C++)
Returns a reference to the ith element of self. Index i should be
between 0 and one less then the number of entries, otherwise the results
are undefined--no bounds checking is performed.
reference
operator[](size_type I);
const_reference
operator[](size_type I) const;
Returns a reference to the ith element of self. Index i must be between
0 and one less then the number of entries in self, otherwise the
function throws an exception of type RWBoundsErr.
Public Member Functions
void
apply(void (*fn)(T*&,void*), void* d);
void
apply(void (*fn)(T*,void*), void* d);
void
apply(void (*fn)(const T*,void*), void* d) const;
Applies the user-defined function pointed to by fn to every item in the
collection. This function must have one of the prototypes:
void yourfun(const T* a, void* d);
void yourfun(T* a, void* d);
void yourfun(T* &a,void* d)
Client data may be passed through parameter d.
reference
at(size_type i);
const_reference
at(size_type i) const;
Returns a reference to the ith element of self. Index i must be between
0 and one less then the number of entries in self, otherwise the
function throws an exception of type RWBoundsErr.
iterator
begin();
const_iterator
begin() const;
Page 4
RWTPtrSortedDlist(3C++) RWTPtrSortedDlist(3C++)
Returns an iterator positioned at the first element of self.
void
clear();
Clears the collection by removing all items from self.
void
clearAndDestroy();
Removes all items from the collection and uses operator delete to destroy
the objects pointed to by those items. Do not use this method if
multiple pointers to the same object are stored.
bool
contains(const T* a) const;
Returns true if there exists an element t in self such that the
expression(*t == *a) is true, otherwise returns false.
bool
contains(bool (*fn)(const T*,void*), void* d) const;
Returns true if there exists an element t in self such that the
expression ((*fn)(t,d)) is true, otherwise returns false. fn points to a
user-defined tester function which must have prototype:
bool yourTester(const T* a, void* d);
Client data may be passed through parameter d.
iterator
end();
const_iterator
end() const;
Returns an iterator positioned "just past" the last element in self.
size_type
entries() const;
Page 5
RWTPtrSortedDlist(3C++) RWTPtrSortedDlist(3C++)
Returns the number of items in self.
const T*
find(const T* a) const;
If there exists an element t in self such that the expression (*t == *a)
is true, returns t. Otherwise, returns rwnil.
const T*
find(bool (*fn)(const T*,void*), void* d) const;
If there exists an element t in self such that the expression
((*fn)(t,d)) is true, returns t. Otherwise, returns rwnil. fn points to
a user-defined tester function which must have prototype:
bool yourTester(const T* a, void* d);
Client data may be passed through parameter d.
reference
first();
const_reference
first() const;
Returns a reference to the first element of self.
size_type
index(const T* a) const;
Returns the position of the first item t in self such that (*t == *a), or
returns the static member npos if no such item exists.
size_type
index(bool (*fn)(const T*,void*), void* d) const;
Returns the position of the first item t in self such that((*fn)(t,d)) is
true, or returns the static member npos if no such item exists. fn
points to a user-defined tester function which must have prototype:
Page 6
RWTPtrSortedDlist(3C++) RWTPtrSortedDlist(3C++)
bool yourTester(const T* a, void* d);
Client data may be passed through parameter d.
size_type
insert(const list<T*,allocator>& a);
Adds the items from a to self in an order preserving way. Returns the
number of items inserted.
bool
insert(T* a);
Adds the item a to self. The collection remains sorted. Returns true.
bool
isEmpty() const;
Returns true if there are no items in the collection, false otherwise.
bool
isSorted() const;
Returns true if the collection is sorted relative to the supplied
comparator object, false otherwise.
T*&
last();
T* const&
last() const;
Returns a reference to the last item in the collection.
size_type
merge(const RWTPtrSortedDlist<T,C>& dl);
Inserts all elements of dl into self, preserving sorted order. Returns
the number of items inserted.
size_type
occurrencesOf(const T* a) const;
Returns the number of elements t in self such that the expression (*t ==
*a) is true.
Page 7
RWTPtrSortedDlist(3C++) RWTPtrSortedDlist(3C++)
size_type
occurrencesOf(bool (*fn)(const T*,void*), void* d) const;
Returns the number of elements t in self such that the
expression((*fn)(t,d)) is true. fn points to a user-defined tester
function which must have prototype:
bool yourTester(const T* a, void* d);
Client data may be passed through parameter d.
T*
remove(const T* a);
Removes and returns the first element t in self such that the expression
(*t == *a) is true. Returns rwnil if there is no such element.
T*
remove(bool (*fn)(const T*,void*), void* d);
Removes and returns the first element t in self such that the expression
((*fn)(t,d)) is true. Returns rwnil if there is no such element. fn
points to a user-defined tester function which must have prototype:
bool yourTester(const T* a, void* d);
Client data may be passed through parameter d.
size_type
removeAll(const T* a);
Removes all elements t in self such that the expression (*t == *a) is
true. Returns the number of items removed.
size_type
removeAll(bool (*fn)(const T*,void*), void* d);
Page 8
RWTPtrSortedDlist(3C++) RWTPtrSortedDlist(3C++)
Removes all elements t in self such that the expression ((*fn)(t,d))is
true. Returns the number of items removed. fn points to a user-defined
tester function which must have prototype:
bool yourTester(const T* a, void* d);
Client data may be passed through parameter d.
T*
removeAt(size_type i);
Removes and returns the item at position i in self. This position must
be between zero and one less then the number of entries in the
collection, otherwise the function throws an exception of type
RWBoundsErr.
T*
removeFirst();
Removes and returns the first item in the collection.
T*
removeLast();
Removes and returns the first item in the collection.
const list<T*,allocator>&
std() const;
Returns a reference to the underlying C++-standard collection that serves
as the implementation for self.
Static Public Data Member
const size_type npos;
This is the value returned by member functions such as index to indicate
a non-position. The value is equal to ~(size_type)0.
Related Global Operators
RWvostream&
operator<<(RWvostream& strm,
const RWTPtrSortedDlist<T,C>& coll);
Page 9
RWTPtrSortedDlist(3C++) RWTPtrSortedDlist(3C++)
RWFile&
operator<<(RWFile& strm, const RWTPtrSortedDlist<T,C>& coll);
Saves the collection coll onto the output stream strm, or a reference to
it if it has already been saved.
RWvistream&
operator>>(RWvistream& strm, RWTPtrSortedDlist<T,C>& coll);
RWFile&
operator>>(RWFile& strm, RWTPtrSortedDlist<T,C>& coll);
Restores the contents of the collection coll from the input stream strm.
RWvistream&
operator>>(RWvistream& strm, RWTPtrSortedDlist<T,C>*& p);
RWFile&
operator>>(RWFile& strm, RWTPtrSortedDlist<T,C>*& p);
Looks at the next object on the input stream strm and either creates a
new collection off the heap and sets p to point to it, or sets p to point
to a previously read instance. If a collection is created off the heap,
then you are responsible for deleting it.
Page 10