File: listnode.h

package info (click to toggle)
estic 1.61-5
  • links: PTS
  • area: main
  • in suites: potato
  • size: 3,968 kB
  • ctags: 6,407
  • sloc: cpp: 41,916; asm: 1,620; makefile: 436; ansic: 402; sh: 40
file content (261 lines) | stat: -rw-r--r-- 5,903 bytes parent folder | download | duplicates (6)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*****************************************************************************/
/*                                                                           */
/*                                 LISTNODE.H                                */
/*                                                                           */
/* (C) 1993,94  Ullrich von Bassewitz                                        */
/*              Zwehrenbuehlstrasse 33                                       */
/*              D-72070 Tuebingen                                            */
/* EMail:       uz@ibb.schwaben.com                                          */
/*                                                                           */
/*****************************************************************************/



// $Id$
//
// $Log$
//
//



#ifndef __LISTNODE_H
#define __LISTNODE_H


#include <stddef.h>

#include "machine.h"
#include "check.h"
#include "object.h"




/*****************************************************************************/
/*                             class _ListNode                               */
/*****************************************************************************/



// This is an implementation class for the template class ListNode. It has
// the complete functionality but uses void pointers instead of typed pointers.
// This hopefully will result in smaller programs.

class _ListNode: public Object {

protected:
    // pointer to the data
    void* ContentsPtr;

    // pointers to the prevoius and next node
    _ListNode* PrevNode;
    _ListNode* NextNode;

public:
    _ListNode (void* DataPtr = NULL);
    ~_ListNode ();

    // get contents of the node
    void* Contents ();

    // get the previous and next nodes
    _ListNode* Next ();
    _ListNode* Prev ();

    // check if the list is empty (one node only)
    int IsEmpty ();

    // linking in the list
    void InsertIn (_ListNode* R);
    void InsertAfter (_ListNode* N);
    void InsertBefore (_ListNode* N);

    // unlink a node
    void Unlink ();

    // traverse through all nodes
    _ListNode* Traverse (int Forward, int (*F) (_ListNode*, void*),
                            void* UserPtr = NULL);

    // count number of nodes
    u16 NodeCount ();

    // convert node to number and vice versa
    _ListNode* NodeWithNumber (u16 X);
    u16 NumberOfNode (_ListNode* N);

};



inline void* _ListNode::Contents ()
{
    return ContentsPtr;
}



inline _ListNode* _ListNode::Next ()
{
    return NextNode;
}



inline _ListNode* _ListNode::Prev ()
{
    return PrevNode;
}



inline int _ListNode::IsEmpty ()
{
    return (NextNode == this);
}



inline void _ListNode::InsertIn (_ListNode* R)
{
    InsertAfter (R);
}



/*****************************************************************************/
/*                              class ListNode                               */
/*****************************************************************************/



template <class T> class ListNode: public _ListNode {

public:
    ListNode (T* DataPtr = NULL);

    // get contents of the node
    T* Contents ();

    // get the previous and next nodes
    ListNode<T>* Next ();
    ListNode<T>* Prev ();

    // linking in the list
    void InsertIn (ListNode<T>* R);
    void InsertAfter (ListNode<T>* N);
    void InsertBefore (ListNode<T>* N);

    // traverse through all nodes
    ListNode* Traverse (int Forward, int (*F) (ListNode<T>*, void*),
                            void* UserPtr = NULL);

    // convert node to number and vice versa
    ListNode<T>* NodeWithNumber (u16 X);
    u16 NumberOfNode (ListNode<T>* N);

};



template <class T>
inline ListNode<T>::ListNode (T* DataPtr):
    _ListNode (DataPtr)
{
}



template <class T>
inline T* ListNode<T>::Contents ()
{
    return (T*) _ListNode::Contents ();
}



template <class T>
inline ListNode<T>* ListNode<T>::Next ()
{
    return (ListNode<T>*) _ListNode::Next ();
}



template <class T>
inline ListNode<T>* ListNode<T>::Prev ()
{
    return (ListNode<T>*) _ListNode::Prev ();
}



template <class T>
inline void ListNode<T>::InsertIn (ListNode<T>* R)
{
    _ListNode::InsertIn ((_ListNode*) R);
}



template <class T>
inline void ListNode<T>::InsertAfter (ListNode<T>* N)
// inserts one node after another
{
    _ListNode::InsertAfter ((_ListNode*) N);
}



template <class T>
inline void ListNode<T>::InsertBefore (ListNode<T>* N)
// inserts one node before another
{
    _ListNode::InsertBefore ((_ListNode*) N);
}



template <class T>
inline ListNode<T>* ListNode<T>::Traverse (int Forward,
                                           int (*F) (ListNode<T>*, 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.
{
    typedef int (*UntypedFunc) (_ListNode*, void*);
    return (ListNode<T>*) _ListNode::Traverse (Forward, (UntypedFunc) F, UserPtr);
}



template <class T>
inline ListNode<T>* ListNode<T>::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).
{
    return (ListNode<T>*) _ListNode::NodeWithNumber (X);
}



template <class T>
inline u16 ListNode<T>::NumberOfNode (ListNode<T>* N)
// Returns the number of node N. Counting begins with "this" node
// (which has number 0) and proceeds in "Next" direction.
{
    return _ListNode::NumberOfNode ((_ListNode*) N);
}



// End of LISTNODE.H

#endif