1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
/*****************************************************************************/
/* */
/* LISTNODE.CC */
/* */
/* (C) 1993,94 Ullrich von Bassewitz */
/* Zwehrenbuehlstrasse 33 */
/* D-72070 Tuebingen */
/* EMail: uz@ibb.schwaben.com */
/* */
/*****************************************************************************/
// $Id$
//
// $Log$
//
//
#include "listnode.h"
/*****************************************************************************/
/* class _ListNode */
/*****************************************************************************/
_ListNode::_ListNode (void* DataPtr)
{
NextNode = PrevNode = this;
ContentsPtr = DataPtr;
}
_ListNode::~_ListNode ()
{
Unlink ();
}
void _ListNode::InsertAfter (_ListNode* N)
// inserts one node after another
{
// the node cannot be inserted after itself
PRECONDITION (N != this);
// switch pointers
NextNode = N->NextNode;
PrevNode = N;
N->NextNode = this;
NextNode->PrevNode = this;
}
void _ListNode::InsertBefore (_ListNode* N)
// inserts one node before another
{
// the node cannot be inserted before itself
PRECONDITION (N != this);
// switch pointers
PrevNode = N->PrevNode;
NextNode = N;
N->PrevNode = this;
PrevNode->NextNode = this;
}
void _ListNode::Unlink ()
// Unlinks a node from a list.
{
// If the list constists only of this element, no unlink is necessary
if (NextNode == this) {
return;
}
// unlink node
PrevNode->NextNode = NextNode;
NextNode->PrevNode = PrevNode;
// the unlinked node is a list with one node
NextNode = PrevNode = this;
}
_ListNode* _ListNode::Traverse (int Forward, int (*F) (_ListNode*, void*),
void *UserPtr)
// Traverse through a list, starting with the current node and calling the
// given function F with every node as argument. Ends if finally the current
// node is reached again (all nodes have been visited in this case) or if the
// called function returns a value != 0. In the former case, Traverse returns
// a NULL pointer, in the latter, a pointer to the node is returned.
{
_ListNode* N = this;
do {
if (F (N, UserPtr)) {
// function returned true
return N;
}
if (Forward) {
N = N->NextNode;
} else {
N = N->PrevNode;
}
} while (N != this);
// Walked through the whole list, return NULL
return NULL;
}
u16 _ListNode::NodeCount ()
// Returns the node count of the list
{
register u16 Count = 0;
_ListNode* N = this;
do {
Count++;
N = N->NextNode;
} while (N != this);
return Count;
}
_ListNode* _ListNode::NodeWithNumber (u16 X)
// Returns the node with number X. Counting begins with "this", (which has
// number 0) and proceeds in "Next" direction.
// Warning: If X is greater than the number of nodes in the list, the
// result is undefined (wraping around).
{
_ListNode* N = this;
while (X--) {
N = N->NextNode;
}
return N;
}
u16 _ListNode::NumberOfNode (_ListNode* N)
// Returns the number of node N. Counting begins with "this" node
// (which has number 0) and proceeds in "Next" direction.
{
register u16 Count = 0;
_ListNode* R = this;
while (R != N) {
Count++;
R = R->NextNode;
CHECK (R != this); // Means N is not in list
}
return Count;
}
|