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

©Copyright 1996 Rogue Wave Software

RWTValDlist<T>

Alternate template: Standard C++ Library not required

Synopsis

#include <rw/tvdlist.h>
RWTValDlist<T> list;

Please Note!


If you do not have the Standard C++ Library, use the interface described here. Otherwise, use the interface to RWTValDlist described in the Class Reference.


Description

This class maintains a collection of values, implemented as a doubly linked list. This is a value based list: objects are copied in and out of the links that make up the list. Unlike intrusive lists (see class RWTIsvDlist<T>), the objects need not inherit from a link class. However, this makes the class slightly less efficient than the intrusive lists because of the need to allocate a new link off the heap with every insertion and to make a copy of the object in the newly allocated link.

Parameter T represents the type of object to be inserted into the list, either a class or fundamental type. The class T must have:

Persistence

Isomorphic

Example

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

#include <rw/tvdlist.h>
#include <rw/rstream.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; 
  }


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

main()  { 
  RWTValDlist<Dog> terriers;
  terriers.insert("Cairn Terrier");   // automatic type conversion 
  terriers.insert("Irish Terrier");
  terriers.insert("Schnauzer");

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

  terriers.insertAt(
      terriers.index("Irish Terrier"),
      "Fox Terrier"
    );

  while (!terriers.isEmpty())
    cout << terriers.get() << endl;

  return 0;
}

Program output:

The list does contain a Schnauzer
Cairn Terrier
Fox Terrier
Irish Terrier
Schnauzer

Public Constructors

RWTValDlist<T>();
RWTValDlist<T>(const RWTValDlist<T>& list);

Public Operators

RWTValDlist&
operator=(const RWTValDlist<T>& list);
T&
operator[](size_t i);
const T&
operator[](size_t i) const;

Public Member Functions

void
append(const T& a);
void
apply(void (*applyFun)(T&, void*), void* d);
T&
at(size_t i);
const T&
at(size_t i) const;
void
clear();
RWBoolean
contains(const T& a) const;
RWBoolean
contains(RWBoolean (*testFun)(const T&, void*),void* d) 
         const;
size_t
entries() const;
RWBoolean
find(const T& target, T& k) const;
RWBoolean
find(RWBoolean (*testFun)(const T&, void*), void* d,T& k) 
     const;
T&
first();
const T&
first() const;
T
get();
size_t
index(const T& a);
size_t
index(RWBoolean (*testFun)(const T&, void*), void* d) const;
void 
insert(const T& a);
void
insertAt(size_t i, const T& a);
RWBoolean
isEmpty() const;
T&
last();
const T&
last() const;
size_t
occurrencesOf(const T& a) const;
size_t
occurrencesOf(RWBoolean (*testFun)(const T&, void*),
              void* d) const;
void
prepend(const T& a);
RWBoolean
remove(const T& a);
RWBoolean
remove(RWBoolean (*testFun)(const T&, void*),void* d);
size_t
removeAll(const T& a);
size_t
removeAll(RWBoolean (*testFun)(const T&, void*),void* d);
T
removeAt(size_t i);
T
removeFirst();
T
removeLast();

Related Global Operators

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