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 NV_CHAIN_H |
---|
25 | #define NV_CHAIN_H |
---|
26 | |
---|
27 | namespace nv { |
---|
28 | |
---|
29 | /** |
---|
30 | * \brief A ChainLink is the container structure for the Chain. |
---|
31 | */ |
---|
32 | class ChainLink |
---|
33 | { |
---|
34 | public: |
---|
35 | ChainLink() |
---|
36 | { |
---|
37 | _clientData = NULL; |
---|
38 | _next = _prev = NULL; |
---|
39 | } |
---|
40 | |
---|
41 | ChainLink(void *clientData) |
---|
42 | { |
---|
43 | _clientData = clientData; |
---|
44 | _next = _prev = NULL; |
---|
45 | } |
---|
46 | |
---|
47 | void *getValue() |
---|
48 | { |
---|
49 | return _clientData; |
---|
50 | } |
---|
51 | |
---|
52 | void setValue(void *clientData) |
---|
53 | { |
---|
54 | _clientData = clientData; |
---|
55 | } |
---|
56 | |
---|
57 | ChainLink *next() |
---|
58 | { |
---|
59 | return _next; |
---|
60 | } |
---|
61 | |
---|
62 | ChainLink *prev() |
---|
63 | { |
---|
64 | return _prev; |
---|
65 | } |
---|
66 | |
---|
67 | friend class Chain; |
---|
68 | |
---|
69 | private: |
---|
70 | ChainLink *_prev; ///< Link to the previous link |
---|
71 | ChainLink *_next; ///< Link to the next link |
---|
72 | void *_clientData; ///< Pointer to the data object |
---|
73 | }; |
---|
74 | |
---|
75 | typedef int (ChainCompareProc) (ChainLink *l1Ptr, ChainLink *l2Ptr); |
---|
76 | |
---|
77 | /** |
---|
78 | * \brief A Chain is a doubly linked list structure. |
---|
79 | */ |
---|
80 | class Chain |
---|
81 | { |
---|
82 | public: |
---|
83 | Chain() |
---|
84 | { |
---|
85 | init(); |
---|
86 | } |
---|
87 | ~Chain() |
---|
88 | { |
---|
89 | reset(); |
---|
90 | } |
---|
91 | |
---|
92 | void init() |
---|
93 | { |
---|
94 | _nLinks = 0; |
---|
95 | _head = _tail = NULL; |
---|
96 | } |
---|
97 | |
---|
98 | ChainLink *append(void *clientData); |
---|
99 | |
---|
100 | ChainLink *prepend(void *clientData); |
---|
101 | |
---|
102 | ChainLink *getNthLink(long position); |
---|
103 | |
---|
104 | void linkBefore(ChainLink *linkPtr, ChainLink *beforePtr); |
---|
105 | |
---|
106 | void linkAfter(ChainLink *linkPtr, ChainLink *afterPtr); |
---|
107 | |
---|
108 | void deleteLink(ChainLink *linkPtr); |
---|
109 | |
---|
110 | void unlink(ChainLink *linkPtr); |
---|
111 | |
---|
112 | void reset(); |
---|
113 | |
---|
114 | void sort(ChainCompareProc *proc); |
---|
115 | |
---|
116 | long getLength() |
---|
117 | { |
---|
118 | return _nLinks; |
---|
119 | } |
---|
120 | |
---|
121 | ChainLink *firstLink() |
---|
122 | { |
---|
123 | return _head; |
---|
124 | } |
---|
125 | |
---|
126 | ChainLink *lastLink() |
---|
127 | { |
---|
128 | return _tail; |
---|
129 | } |
---|
130 | |
---|
131 | void appendLink(ChainLink *linkPtr) |
---|
132 | { |
---|
133 | linkBefore(linkPtr, (ChainLink *)NULL); |
---|
134 | } |
---|
135 | |
---|
136 | void prependLink(ChainLink *linkPtr) |
---|
137 | { |
---|
138 | linkAfter(linkPtr, (ChainLink *)NULL); |
---|
139 | } |
---|
140 | |
---|
141 | private: |
---|
142 | ChainLink *_head; ///< Pointer to first element in chain |
---|
143 | ChainLink *_tail; ///< Pointer to last element in chain |
---|
144 | long _nLinks; ///< Number of elements in chain |
---|
145 | }; |
---|
146 | |
---|
147 | } |
---|
148 | |
---|
149 | #endif |
---|