Rogue Wave Software logo banner

Click on the banner to return to the Class Reference home page.

©Copyright 1996 Rogue Wave Software

RWTPtrDlist<T>

Synopsis

#include <rw/tpdlist.h> 
RWTPtrDlist<T> dlist;

Please Note!


If you have the Standard C++ Library, use the interface described here. Otherwise, use the restricted interface to RWTPtrDlist described in Appendix A.


Description

This class maintains a pointer-based collection of values, implemented as a doubly-linked list. Class T is the type pointed to by the items in the collection.

Persistence

Isomorphic

Example

In this example, a pointer-based doubly-linked list of user type Dog is exercised.

//
// tpdlist.cpp
//
#include <rw/tpdlist.h>
#include <iostream.h>
#include <string.h>

class Dog {
  char* name;
public:
  Dog( const char* c) {
   name = new char[strlen(c)+1];
   strcpy(name, c); }

  ~Dog() { delete name; }

  // Define a copy constructor:
  Dog(const Dog& dog) {
   name = new char[strlen(dog.name)+1];
   strcpy(name, dog.name); }

  // Define an assignment operator:
  void operator=(const Dog& dog) {
   if (this!=&dog) {
     delete name;
     name = new char[strlen(dog.name)+1];
     strcpy(name, dog.name);
  }
  }

  // Define an equality test operator:
  int operator==(const Dog& dog) const {
  return strcmp(name, dog.name)==0; }

  // Order alphabetically by name:
  int operator<(const Dog& dog) const {
  return strcmp(name, dog.name)<0; }

  friend ostream& operator<<(ostream& str, const Dog& dog){
    str << dog.name;
    return str;}
};

main(){
  RWTPtrDlist<Dog> terriers;
  terriers.insert(new Dog("Cairn Terrier"));
  terriers.insert(new Dog("Irish Terrier"));
  terriers.insert(new Dog("Schnauzer"));

  Dog key1("Schnauzer");
  cout << "The list " <<
    (terriers.contains(&key1) ? "does " : "does not ") <<
    "contain a Schnauzer\n";

  Dog key2("Irish Terrier");
  terriers.insertAt(
      terriers.index(&key2),
      new Dog("Fox Terrier")
    );

  Dog* d;
  while (!terriers.isEmpty()) {
    d = terriers.get();
    cout << *d << endl;
    delete d;
  }

  return 0;
}

Program Output:
The list does contain a Schnauzer
Cairn Terrier
Fox Terrier
Irish Terrier
Schnauzer

Related Classes

Classes RWTPtrDeque<T>, RWTPtrSlist<T>, and RWTPtrOrderedVector<T> also provide a Rogue Wave pointer-based interface to C++-standard sequence collections.

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::size_type              size_type;
typedef container_type::difference_type        difference_type;
typedef container_type::iterator               iterator;
typedef container_type::const_iterator         const_iterator;
typedef T*                                     value_type;
typedef
typedef T*                                     reference;
typedef T* const&                              const_reference;

Public Constructors

RWTPtrDlist<T>();
RWTPtrDlist<T>(const RWTPtrDlist<T>& rwlst);
RWTPtrDlist<T>(const list<T*, allocator>& lst);
RWTPtrDlist<T>(size_type n, T* a=0);
RWTPtrDlist<T>(T*const* first, T*const* last);

Public Member Operators

RWTPtrDlist<T>&
operator=(const list<T*, allocator>& lst);
RWTPtrDlist<T>&
operator=(const RWTPtrDlist<T>& lst);
bool
operator<(const RWTPtrDlist<T>& lst);
bool
operator==(const RWTPtrDlist<T>& lst);
reference
operator()(size_type i);
const_reference
operator()(size_type i) const;
reference
operator[](size_type i);
const_reference
operator[](size_type i) const;

Public Member Functions

void
append(T* a);
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;
const
const_reference 
at (size_type i);
reference
at(size_type i);
iterator
begin();
const_iterator
begin() const;
void
clear();
void
clearAndDestroy();
bool
contains(const T* a) const;
bool
contains(bool (*fn)(T*,void*), void* d) const;
bool
contains(bool (*fn)(const T*,void*), void* d) const;
iterator
end();
const_iterator
end() const;
size_type
entries() const;
T*
find(const T* a) const;
T*
find(bool (*fn)(T*,void*), void* d) const;
T*
find(bool (*fn)(const T*,void*), void* d) const;
reference
first();
const_reference
first() const;
T*
get();
size_type
index(const T* a) const;
size_type
index(bool (*fn)(T*,void*), void* d) const;
size_type
index(bool (*fn)(const T*,void*), void* d) const;
bool
insert(T* a);
void
insertAt(size_type i, T* a);
bool
isEmpty() const;
T*&
last();
T*const&
last() const;
reference
maxElement();
const_reference
maxElement() const;
reference
minElement();
const_reference
minElement() const;
size_type
occurrencesOf(const T* a) const;
size_type
occurrencesOf(bool (*fn)( T*,void*), void* d) const;
size_type
occurrencesOf(bool (*fn)(const T*,void*), void* d) const;
void
prepend(T* a);
T*
remove(const T* a);
T*
remove(bool (*fn)( T*,void*), void* d);
T*
remove(bool (*fn)(const T*,void*), void* d);
size_type
removeAll(const T* a);
size_type
removeAll(bool (*fn)( T*,void*), void* d);
size_type
removeAll(bool (*fn)(const T*,void*), void* d);
T*
removeAt(size_type i);
T*
removeFirst();
T*
removeLast();
size_type
replaceAll(const T* oldVal,T* newVal);
size_type
replaceAll(bool (*fn)(T*, void*),void* d,T* newVal);
size_type
replaceAll(bool (*fn)(const T*, void*),void* d,T* newVal);
void
sort();
list<T*, allocator>&
std();
const list<T*, allocator>&
std() const;

Static Public Data Member

const size_type  npos;

Related Global Operators

RWvostream&
operator<<(RWvostream& strm, const RWTPtrDlist<T>& coll);
RWFile&
operator<<(RWFile& strm, const RWTPtrDlist<T>& coll);
RWvistream&
operator>>(RWvistream& strm, RWTPtrDlist<T>& coll);
RWFile&
operator>>(RWFile& strm, RWTPtrDlist<T>& coll);
RWvistream&
operator>>(RWvistream& strm, RWTPtrDlist<T>*& p);
RWFile&
operator>>(RWFile& strm, RWTPtrDlist<T>*& p);