1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * Copyright 1993-2004 George A Howlett. |
---|
4 | * |
---|
5 | * Permission is hereby granted, free of charge, to any person obtaining |
---|
6 | * a copy of this software and associated documentation files (the |
---|
7 | * "Software"), to deal in the Software without restriction, including |
---|
8 | * without limitation the rights to use, copy, modify, merge, publish, |
---|
9 | * distribute, sublicense, and/or sell copies of the Software, and to |
---|
10 | * permit persons to whom the Software is furnished to do so, subject to |
---|
11 | * the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be |
---|
14 | * included in all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
---|
17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
---|
18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
---|
19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
---|
20 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
---|
21 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
---|
22 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
---|
23 | */ |
---|
24 | #ifndef CHAIN_H |
---|
25 | #define CHAIN_H |
---|
26 | |
---|
27 | /** |
---|
28 | * \brief A ChainLink is the container structure for the Chain. |
---|
29 | */ |
---|
30 | class ChainLink |
---|
31 | { |
---|
32 | public: |
---|
33 | ChainLink() |
---|
34 | { |
---|
35 | _clientData = NULL; |
---|
36 | _next = _prev = NULL; |
---|
37 | } |
---|
38 | |
---|
39 | ChainLink(void *clientData) |
---|
40 | { |
---|
41 | _clientData = clientData; |
---|
42 | _next = _prev = NULL; |
---|
43 | } |
---|
44 | |
---|
45 | void *getValue() |
---|
46 | { |
---|
47 | return _clientData; |
---|
48 | } |
---|
49 | |
---|
50 | void setValue(void *clientData) |
---|
51 | { |
---|
52 | _clientData = clientData; |
---|
53 | } |
---|
54 | |
---|
55 | ChainLink *next() |
---|
56 | { |
---|
57 | return _next; |
---|
58 | } |
---|
59 | |
---|
60 | ChainLink *prev() |
---|
61 | { |
---|
62 | return _prev; |
---|
63 | } |
---|
64 | |
---|
65 | friend class Chain; |
---|
66 | |
---|
67 | private: |
---|
68 | ChainLink *_prev; ///< Link to the previous link |
---|
69 | ChainLink *_next; ///< Link to the next link |
---|
70 | void *_clientData; ///< Pointer to the data object |
---|
71 | }; |
---|
72 | |
---|
73 | typedef int (ChainCompareProc) (ChainLink *l1Ptr, ChainLink *l2Ptr); |
---|
74 | |
---|
75 | /** |
---|
76 | * \brief A Chain is a doubly linked list structure. |
---|
77 | */ |
---|
78 | class Chain |
---|
79 | { |
---|
80 | public: |
---|
81 | Chain() |
---|
82 | { |
---|
83 | init(); |
---|
84 | } |
---|
85 | ~Chain() |
---|
86 | { |
---|
87 | reset(); |
---|
88 | } |
---|
89 | |
---|
90 | void init() |
---|
91 | { |
---|
92 | _nLinks = 0; |
---|
93 | _head = _tail = NULL; |
---|
94 | } |
---|
95 | |
---|
96 | ChainLink *append(void *clientData); |
---|
97 | |
---|
98 | ChainLink *prepend(void *clientData); |
---|
99 | |
---|
100 | ChainLink *getNthLink(long position); |
---|
101 | |
---|
102 | void linkBefore(ChainLink *linkPtr, ChainLink *beforePtr); |
---|
103 | |
---|
104 | void linkAfter(ChainLink *linkPtr, ChainLink *afterPtr); |
---|
105 | |
---|
106 | void deleteLink(ChainLink *linkPtr); |
---|
107 | |
---|
108 | void unlink(ChainLink *linkPtr); |
---|
109 | |
---|
110 | void reset(); |
---|
111 | |
---|
112 | void sort(ChainCompareProc *proc); |
---|
113 | |
---|
114 | long getLength() |
---|
115 | { |
---|
116 | return _nLinks; |
---|
117 | } |
---|
118 | |
---|
119 | ChainLink *firstLink() |
---|
120 | { |
---|
121 | return _head; |
---|
122 | } |
---|
123 | |
---|
124 | ChainLink *lastLink() |
---|
125 | { |
---|
126 | return _tail; |
---|
127 | } |
---|
128 | |
---|
129 | void appendLink(ChainLink *linkPtr) |
---|
130 | { |
---|
131 | linkBefore(linkPtr, (ChainLink *)NULL); |
---|
132 | } |
---|
133 | |
---|
134 | void prependLink(ChainLink *linkPtr) |
---|
135 | { |
---|
136 | linkAfter(linkPtr, (ChainLink *)NULL); |
---|
137 | } |
---|
138 | |
---|
139 | private: |
---|
140 | ChainLink *_head; ///< Pointer to first element in chain |
---|
141 | ChainLink *_tail; ///< Pointer to last element in chain |
---|
142 | long _nLinks; ///< Number of elements in chain |
---|
143 | }; |
---|
144 | |
---|
145 | #endif |
---|