RWTValSortedDlist(3C++) RWTValSortedDlist(3C++)
Name
RWTValSortedDlist<T,C> - Rogue Wave library class
Synopsis
#include <rw/tvsrtdli.h>
RWTValSortedDlist<T,C> srtdlist;
Standard C++ Library Dependent!RWTValSortedDlist requires the Standard C++ Library.
Description
This class maintains an always-sorted collection of values, implemented
as a doubly-linked list.
Persistence
Isomorphic.
Example
In this example, a sorted doubly-linked list of RWDates is exercised.
//
// tvsdldat.cpp
//
#include <rw/tvsrtdli.h>
#include <rw/rwdate.h>
#include <iostream.h>
#include <function.h>
main(){
RWTValSortedDList<RWDate, less<RWDate> > lst;
lst.insert(RWDate(10, "Aug", 1991));
lst.insert(RWDate(9, "Aug", 1991));
lst.insert(RWDate(1, "Sep", 1991));
lst.insert(RWDate(14, "May", 1990));
lst.insert(RWDate(1, "Sep", 1991)); // Add a duplicate
lst.insert(RWDate(2, "June", 1991));
for (int i=0; i<lst.entries(); i++)
cout << lst[i] << endl;
return 0;
}
Program Output:
05/14/90
06/02/91
08/09/91
Page 1
RWTValSortedDlist(3C++) RWTValSortedDlist(3C++)
08/10/91
09/01/91
09/01/91
Related Classes
RWTValSortedVector<T> is an alternative always-sorted collections.
RWTValDlist<T> is an unsorted doubly-linked list of values. Class
list<T,allocator> is the C++-standard collection that serves as the
underlying implementation for this class.
Public Typedefs
typedef list<T,allocator> container_type;
typedef container_type::const_iterator iterator;
typedef container_type::const_iterator const_iterator;
typedef container_type::size_type size_type;
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
Public Constructors
RWTValSortedDlist<T,C>();
Constructs an empty doubly-linked list.
RWTValSortedDlist<T,C>(const list<T,allocator>& lst);
Constructs a doubly-linked list by copying and sorting all elements of
lst.
RWTValSortedDlist<T,C>(const RWTValSortedDlist<T,C>& rwlst);
Copy constructor.
RWTValSortedDlist<T,C>(size_type n, const T& val = T());
Constructs a doubly-linked list with n elements, each initialized to val.
RWTValSortedDlist<T,C>(const T* first, const T* last);
Constructs a doubly-linked list by copying and sorting elements from the
array of Ts pointed to by first, up to, but not including, the element
Page 2
RWTValSortedDlist(3C++) RWTValSortedDlist(3C++)
pointed to by last.
Public Member Operators
RWTValSortedDlist<T,C>&
operator=(const RWTValSortedDlist<T,C>& lst);
RWTValSortedDlist<T,C>&
operator=(const list<T,allocator>& lst);
Destroys all elements of self and replaces them by copying (and sorting,
if necessary) all elements of lst.
bool
operator<(const RWTValSortedDlist<T,C>& lst) const;
bool
operator<(const list<T,allocator>& lst) const;
Returns true if self compares lexicographically less than lst, otherwise
returns false. Assumes that type T has well-defined less-than semantics
(T::operator<(const T&) or equivalent).
bool
operator==(const RWTValSortedDlist<T,C>& lst) const;
bool
operator==(const list<T>& 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.
const_reference
operator()(size_type i) const;
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.
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.
Page 3
RWTValSortedDlist(3C++) RWTValSortedDlist(3C++)
Public Member Functions
void
apply(void (*fn)(const_reference,void*), void* d) const;
Applies the user-defined function pointed to by fn to every item in the
collection. This function must have prototype:
void yourfun(const_reference a, void* d);
Client data may be passed through parameter d.
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;
Returns an iterator positioned at the first element of self.
void
clear();
Clears the collection by removing all items from self. Each item will
have its destructor called.
bool
contains(const_reference 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_reference,void*), void* d) const;
Returns true if there exists an element t in self such that the
Page 4
RWTValSortedDlist(3C++) RWTValSortedDlist(3C++)
expression ((*fn)(t,d)) is true, otherwise returns false. fn points to a
user-defined tester function which must have prototype:
bool yourTester(const_reference 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;
Returns the number of items in self.
bool
find(const_reference a, value_type& k) const;
If there exists an element t in self such that the expression (t == a) is
true, assigns t to k and returns true. Otherwise, returns false and
leaves the value of k unchanged.
bool
find(bool (*fn)(const_reference,void*), void* d,
value_type& k) const;
If there exists an element t in self such that the expression
((*fn)(t,d)) is true, assigns t to k and returns true. Otherwise,
returns false and leaves the value of k unchanged. fn points to a user-
defined tester function which must have prototype:
bool yourTester(const_reference a, void* d);
Client data may be passed through parameter d.
Page 5
RWTValSortedDlist(3C++) RWTValSortedDlist(3C++)
reference
first();
const_reference
first() const;
Returns a reference to the first element of self.
size_type
index(const_reference 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_reference,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:
bool yourTester(const_reference 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 manner. Returns the
number of items inserted into self.
bool
insert(const_reference 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.
Page 6
RWTValSortedDlist(3C++) RWTValSortedDlist(3C++)
bool
isSorted() const;
Returns true if the collection is sorted relative to the supplied
comparator object, false otherwise.
const_reference
last() const;
Returns a reference to the last item in the collection.
size_type
merge(const RWTValSortedDlist&<T,C> dl);
Inserts all elements of dl into self, preserving sorted order.
size_type
occurrencesOf(const_reference) const;
Returns the number of elements t in self such that the expression (t ==
a) is true.
size_type
occurrencesOf(bool (*fn)(const_reference,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_reference a, void* d);
Client data may be passed through parameter d.
bool
remove(const_reference a);
Removes the first element t in self such that the expression (t == a) is
true and returns true. Returns false if there is no such element.
Page 7
RWTValSortedDlist(3C++) RWTValSortedDlist(3C++)
bool
remove(bool (*fn)(const_reference,void*), void* d);
Removes the first element t in self such that the expression ((*fn)(t,d))
is true and returns true. Returns false if there is no such element. fn
points to a user-defined tester function which must have prototype:
bool yourTester(const_reference a, void* d);
Client data may be passed through parameter d.
size_type
removeAll(const_reference 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_reference,void*), void* d);
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_reference 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.
Page 8
RWTValSortedDlist(3C++) RWTValSortedDlist(3C++)
T
removeFirst();
Removes and returns the first item in the collection.
T
removeLast();
Removes and returns the first item in the collection.
list<T,allocator>&
std();
const list<T,allocator>&
std() const;
Returns a reference to the underlying C++-standard collection that serves
as the implementation for self. It is your responsibility not to violate
the ordering of the elements within the collection.
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 RWTValSortedDlist<T,C>& coll);
RWFile&
operator<<(RWFile& strm, const RWTValSortedDlist<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, RWTValSortedDlist<T,C>& coll);
RWFile&
operator>>(RWFile& strm, RWTValSortedDlist<T,C>& coll);
Restores the contents of the collection coll from the input stream strm.
RWvistream&
operator>>(RWvistream& strm, RWTValSortedDlist<T,C>*& p);
RWFile&
operator>>(RWFile& strm, RWTValSortedDlist<T,C>*& p);
Page 9
RWTValSortedDlist(3C++) RWTValSortedDlist(3C++)
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