DoubleLink.h.The class DoubleLink implements a base class for nodes in the list, class DoubleLinkList implements the list itself, and class DoubleLinkIter forms an iterator. WARNING: We consider this class to have serious design flaws, so it may be reworked quite a bit in subsequent Ptolemy releases.
There are two constructors:
DoubleLink(Pointer p, DoubleLink* next, DoubleLink* prev):The first form initializes the
prevpointers of the node as well as the contents. The second form sets these pointers to null and only initializes the contents pointer.
Pointer content();Return the content pointer of the node.
virtual ~DoubleLink();The destructor is virtual.
void unlinkMe();Delete the node from the list it is contained in. I.e. connect the elements pointed to by the
nextpointers. The object pointed to by the node is not deleted.
The following data members are protected:
DoubleLink *next; // next node in the list
DoubleLink *prev; // previous node in the list
Pointer e; // contents of this node
DoubleLinkList();The first constructor creates an empty list. The second creates a one-node list containing the object pointed to by
e.That object must live at least as long as the link lives.
virtual ~DoubleLinkList();The destructor is virtual. It deletes all DoubleLinks in the list, but does not delete the objects pointed to by each link.
DoubleLink* createLink(Pointer e);Return a newly allocated DoubleLink that contains a pointer to
e. It is up to the caller to delete the DoubleLink, or to use either
void insertLink(DoubleLink *x);These methods insert an item at the beginning of the list. The first inserts a DoubleLink; the second creates a DoubleLink with
void insert(Pointer e);
createLinkand inserts that. If the second form is used, the link should only be removed using
unlinkwill not delete the DoubleLink.
void appendLink(DoubleLink *x);These methods append at the end of the list. The first appends a DoubleLink; the second creates a DoubleLink with
void append(Pointer e);
createLinkand appends that. If the second form is used, the link should only be removed using
unlinkwill not delete the DoubleLink.
void insertAhead(DoubleLink *y, DoubleLink *x);The first method inserts
void insertBehind(DoubleLink *y, DoubleLink *x);
yimmediately ahead of the DoubleLink pointed to by
x; the second inserts
yimmediately after the DoubleLink pointed to by
x. Both of these functions assume that
xis in the list; disaster may result otherwise.
DoubleLink* unlink(DoubleLink *x);Remove the link
xfrom the list and return a pointer to it. Make sure that
xis in the list before calling this method, or disaster may result.
void removeLink(DoubleLink *x);This is the same as
xis deleted as well. The same cautions apply.
void remove(Pointer e);Search for a DoubleLink whose contents match
e. If a match is found, the node is removed from the list and the DoubleLink is deleted. The object pointed to by
eis not deleted. The search starts at the head of the list.
int find(Pointer e);Search for a DoubleLink whose contents match
e. If a match is found, 1 (true) is returned; otherwise 0 (false) is returned. The search starts at the head of the list.
virtual void initialize();Delete all DoubleLinks in the list and make the list empty.
void reset();Make the list empty, but do not delete the DoubleLinks in each of the nodes.
int size();Return the number of elements in the list. This method should be const but isn't.
DoubleLink *head();Return a pointer to the head or to the tail of the list. If the list is empty both methods will return a null pointer.
DoubleLink *getHeadLink();The first method gets and removes the head link, returning a pointer to it. The second method returns the object pointed to by the head link, and deletes the DoubleLink. If the list is empty, both functions return a null pointer.
DoubleLink *getTailLink();These methods are identical to the previous pair except that they remove the last node rather than the first.
The following two data members are protected:
nextoperator returns the Pointer values contained within the nodes; it is also possible to use the non-standard
nextLinkfunction to return successive DoubleLink pointers.