1 | |
---|
2 | /* |
---|
3 | * Chain.h -- |
---|
4 | * |
---|
5 | * Copyright 1993-2004 George A Howlett. |
---|
6 | * |
---|
7 | * Permission is hereby granted, free of charge, to any person obtaining |
---|
8 | * a copy of this software and associated documentation files (the |
---|
9 | * "Software"), to deal in the Software without restriction, including |
---|
10 | * without limitation the rights to use, copy, modify, merge, publish, |
---|
11 | * distribute, sublicense, and/or sell copies of the Software, and to |
---|
12 | * permit persons to whom the Software is furnished to do so, subject to |
---|
13 | * the following conditions: |
---|
14 | * |
---|
15 | * The above copyright notice and this permission notice shall be |
---|
16 | * included in all copies or substantial portions of the Software. |
---|
17 | * |
---|
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
---|
19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
---|
20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
---|
21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
---|
22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
---|
23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
---|
24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
---|
25 | */ |
---|
26 | #ifndef _CHAIN_H |
---|
27 | #define _CHAIN_H |
---|
28 | |
---|
29 | /* |
---|
30 | * A ChainLink is the container structure for the Chain. |
---|
31 | */ |
---|
32 | class ChainLink { |
---|
33 | friend class Chain; |
---|
34 | private: |
---|
35 | ChainLink *_prev; /* Link to the previous link */ |
---|
36 | ChainLink *_next; /* Link to the next link */ |
---|
37 | void *_clientData; /* Pointer to the data object */ |
---|
38 | public: |
---|
39 | ChainLink(void) { |
---|
40 | _clientData = NULL; |
---|
41 | _next = _prev = NULL; |
---|
42 | } |
---|
43 | ChainLink(void *clientData) { |
---|
44 | _clientData = clientData; |
---|
45 | _next = _prev = NULL; |
---|
46 | } |
---|
47 | void *GetValue(void) { |
---|
48 | return _clientData; |
---|
49 | } |
---|
50 | void SetValue(void *clientData) { |
---|
51 | _clientData = clientData; |
---|
52 | } |
---|
53 | ChainLink *Next(void) { |
---|
54 | return _next; |
---|
55 | } |
---|
56 | ChainLink *Prev(void) { |
---|
57 | return _prev; |
---|
58 | } |
---|
59 | }; |
---|
60 | |
---|
61 | typedef int (ChainCompareProc) (ChainLink *l1Ptr, ChainLink *l2Ptr); |
---|
62 | |
---|
63 | /* |
---|
64 | * A Chain is a doubly linked list structure. |
---|
65 | */ |
---|
66 | class Chain { |
---|
67 | private: |
---|
68 | ChainLink *_head; /* Pointer to first element in chain */ |
---|
69 | ChainLink *_tail; /* Pointer to last element in chain */ |
---|
70 | long _nLinks; /* Number of elements in chain */ |
---|
71 | public: |
---|
72 | Chain(void) { |
---|
73 | Init(); |
---|
74 | } |
---|
75 | ~Chain(void) { |
---|
76 | Reset(); |
---|
77 | } |
---|
78 | void Init(void) { |
---|
79 | _nLinks = 0; |
---|
80 | _head = _tail = NULL; |
---|
81 | } |
---|
82 | ChainLink *Append(void *clientData); |
---|
83 | ChainLink *Prepend(void *clientData); |
---|
84 | ChainLink *GetNthLink(long position); |
---|
85 | void LinkBefore(ChainLink *linkPtr, ChainLink *beforePtr); |
---|
86 | void LinkAfter(ChainLink *linkPtr, ChainLink *afterPtr); |
---|
87 | void DeleteLink(ChainLink *linkPtr); |
---|
88 | void Unlink(ChainLink *linkPtr); |
---|
89 | void Reset(void); |
---|
90 | void Sort(ChainCompareProc *proc); |
---|
91 | long GetLength(void) { |
---|
92 | return _nLinks; |
---|
93 | } |
---|
94 | ChainLink *FirstLink(void) { |
---|
95 | return _head; |
---|
96 | } |
---|
97 | ChainLink *LastLink(void) { |
---|
98 | return _tail; |
---|
99 | } |
---|
100 | void AppendLink(ChainLink *linkPtr) { |
---|
101 | LinkBefore(linkPtr, (ChainLink *)NULL); |
---|
102 | } |
---|
103 | void PrependLink(ChainLink *linkPtr) { |
---|
104 | LinkAfter(linkPtr, (ChainLink *)NULL); |
---|
105 | } |
---|
106 | }; |
---|
107 | |
---|
108 | #endif /* _CHAIN_H */ |
---|