1 mike 1.2 //%2006////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
6 // IBM Corp.; EMC Corporation, The Open Group.
7 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
9 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
11 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec Corporation; The Open Group.
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
15 // of this software and associated documentation files (the "Software"), to
16 // deal in the Software without restriction, including without limitation the
17 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
18 // sell copies of the Software, and to permit persons to whom the Software is
19 // furnished to do so, subject to the following conditions:
20 //
21 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
22 mike 1.2 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
23 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
24 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 //==============================================================================
31 //
32 //%/////////////////////////////////////////////////////////////////////////////
33
34 #include "List.h"
35 #include "PegasusAssert.h"
36
37 PEGASUS_NAMESPACE_BEGIN
38
|
39 kumpf 1.3 ListRep::ListRep(void (*destructor)(Linkable*))
40 : _front(0), _back(0), _size(0)
|
41 mike 1.2 {
42 if (destructor)
|
43 kumpf 1.3 _destructor = destructor;
|
44 mike 1.2 else
|
45 kumpf 1.3 _destructor = 0;
|
46 mike 1.2 }
47
48 ListRep::~ListRep()
49 {
50 PEGASUS_DEBUG_ASSERT(_magic);
51
52 clear();
53 #ifdef PEGASUS_DEBUG
54 memset(this, 0xDD, sizeof(ListRep));
55 #endif
56 }
57
58 void ListRep::clear()
59 {
60 PEGASUS_DEBUG_ASSERT(_magic);
61
62 if (_destructor)
63 {
|
64 kumpf 1.3 // Reset _front, _back, and _size in case the destructor calls
65 // a method of List.
|
66 mike 1.2
|
67 kumpf 1.3 Linkable* front = _front;
68
69 _front = 0;
70 _back = 0;
71 _size = 0;
72
73 for (Linkable* p = front; p; )
74 {
75 PEGASUS_DEBUG_ASSERT(p->magic);
76 Linkable* next = p->next;
77 p->list = 0;
78 _destructor(p);
79 p = next;
80 }
|
81 mike 1.2 }
82 }
83
84 void ListRep::insert_front(Linkable* elem)
85 {
86 PEGASUS_DEBUG_ASSERT(_magic);
87 PEGASUS_DEBUG_ASSERT(elem != 0);
88 PEGASUS_DEBUG_ASSERT(elem->magic);
89 PEGASUS_DEBUG_ASSERT(elem->list == 0);
90
91 elem->list = this;
92 elem->next = _front;
93 elem->prev = 0;
94
95 if (_front)
|
96 kumpf 1.3 _front->prev = elem;
|
97 mike 1.2 else
|
98 kumpf 1.3 _back = elem;
|
99 mike 1.2
100 _front = elem;
101 _size++;
102 }
103
104 void ListRep::insert_back(Linkable* elem)
105 {
106 PEGASUS_DEBUG_ASSERT(_magic);
107 PEGASUS_DEBUG_ASSERT(elem != 0);
108 PEGASUS_DEBUG_ASSERT(elem->magic);
109 PEGASUS_DEBUG_ASSERT(elem->list == 0);
110
111 elem->list = this;
112 elem->prev = _back;
113 elem->next = 0;
114
115 if (_back)
|
116 kumpf 1.3 _back->next = elem;
|
117 mike 1.2 else
|
118 kumpf 1.3 _front = elem;
|
119 mike 1.2
120 _back = elem;
121 _size++;
122 }
123
124 void ListRep::insert_after(
|
125 kumpf 1.3 Linkable* pos,
|
126 mike 1.2 Linkable* elem)
127 {
128 PEGASUS_DEBUG_ASSERT(_magic);
129 PEGASUS_DEBUG_ASSERT(pos != 0);
130 PEGASUS_DEBUG_ASSERT(elem != 0);
131 PEGASUS_DEBUG_ASSERT(elem->magic);
132 PEGASUS_DEBUG_ASSERT(pos->magic);
133 PEGASUS_DEBUG_ASSERT(elem->list == 0);
134
135 elem->list = this;
136 elem->prev = pos;
137 elem->next = pos->next;
138
139 if (pos->next)
|
140 kumpf 1.3 pos->next->prev = elem;
|
141 mike 1.2
142 pos->next = elem;
143
144 if (pos == _back)
|
145 kumpf 1.3 _back = elem;
|
146 mike 1.2
147 _size++;
148 }
149
150 void ListRep::insert_before(
|
151 kumpf 1.3 Linkable* pos,
|
152 mike 1.2 Linkable* elem)
153 {
154 PEGASUS_DEBUG_ASSERT(_magic);
155 PEGASUS_DEBUG_ASSERT(pos != 0);
156 PEGASUS_DEBUG_ASSERT(elem != 0);
157 PEGASUS_DEBUG_ASSERT(pos->magic);
158 PEGASUS_DEBUG_ASSERT(elem->magic);
159 PEGASUS_DEBUG_ASSERT(elem->list == 0);
160
161 elem->list = this;
162 elem->next = pos;
163 elem->prev = pos->prev;
164
165 if (pos->prev)
|
166 kumpf 1.3 pos->prev->next = elem;
|
167 mike 1.2
168 pos->prev = elem;
169
170 if (pos == _front)
|
171 kumpf 1.3 _front = elem;
|
172 mike 1.2
173 _size++;
174 }
175
176 void ListRep::remove(Linkable* pos)
177 {
178 PEGASUS_DEBUG_ASSERT(_magic);
179 PEGASUS_DEBUG_ASSERT(pos != 0);
180 PEGASUS_DEBUG_ASSERT(pos->magic);
181 PEGASUS_DEBUG_ASSERT(pos->list == this);
182 PEGASUS_DEBUG_ASSERT(_size != 0);
183
184 if (_size == 0)
|
185 kumpf 1.3 return;
|
186 mike 1.2
187 if (pos->prev)
|
188 kumpf 1.3 pos->prev->next = pos->next;
|
189 mike 1.2
190 if (pos->next)
|
191 kumpf 1.3 pos->next->prev = pos->prev;
|
192 mike 1.2
193 if (pos == _front)
|
194 kumpf 1.3 _front = pos->next;
|
195 mike 1.2
196 if (pos == _back)
|
197 kumpf 1.3 _back = pos->prev;
|
198 mike 1.2
199 pos->list = 0;
200
201 _size--;
202 }
203
204 bool ListRep::contains(const Linkable* elem)
205 {
206 PEGASUS_DEBUG_ASSERT(_magic);
207 PEGASUS_DEBUG_ASSERT(elem != 0);
208 PEGASUS_DEBUG_ASSERT(elem->magic);
209
210 for (const Linkable* p = _front; p; p = p->next)
211 {
|
212 kumpf 1.3 if (p == elem)
213 return true;
|
214 mike 1.2 }
215
216 // Not found!
217 return false;
218 }
219
220 Linkable* ListRep::remove_front()
221 {
222 PEGASUS_DEBUG_ASSERT(_magic);
223
224 if (_size == 0)
|
225 kumpf 1.3 return 0;
|
226 mike 1.2
227 Linkable* elem = _front;
228 remove(elem);
229
230 return elem;
231 }
232
233 Linkable* ListRep::remove_back()
234 {
235 PEGASUS_DEBUG_ASSERT(_magic);
|
236 kumpf 1.5.14.1
237 if (_size == 0)
238 return 0;
|
239 mike 1.2
240 Linkable* elem = _back;
241 remove(elem);
242
243 return elem;
244 }
245
246 Linkable* ListRep::find(ListRep::Equal equal, const void* client_data)
247 {
248 PEGASUS_DEBUG_ASSERT(_magic);
249 PEGASUS_DEBUG_ASSERT(equal != 0);
250
251 for (Linkable* p = _front; p; p = p->next)
252 {
|
253 kumpf 1.3 if ((*equal)(p, client_data))
254 {
255 PEGASUS_DEBUG_ASSERT(p->magic);
256 return p;
257 }
|
258 mike 1.2 }
259
260 // Not found!
261 return 0;
262 }
263
264 Linkable* ListRep::remove(ListRep::Equal equal, const void* client_data)
265 {
266 PEGASUS_DEBUG_ASSERT(_magic);
267 PEGASUS_DEBUG_ASSERT(equal != 0);
268
269 Linkable* p = find(equal, client_data);
270
271 if (p)
272 {
|
273 kumpf 1.3 PEGASUS_DEBUG_ASSERT(p->magic);
274 remove(p);
|
275 mike 1.2 }
276
277 return p;
278 }
279
280 void ListRep::apply(ListRep::Apply apply, const void* client_data)
281 {
282 PEGASUS_DEBUG_ASSERT(_magic);
283 PEGASUS_DEBUG_ASSERT(apply != 0);
284
285 for (Linkable* p = _front; p; p = p->next)
|
286 kumpf 1.3 (*apply)(p, client_data);
|
287 mike 1.2 }
288
289 PEGASUS_NAMESPACE_END
|