|
1 /* |
|
2 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: inline implementation of list |
|
15 * |
|
16 */ |
|
17 |
|
18 /* |
|
19 * %version: 5 % |
|
20 */ |
|
21 |
|
22 #include <wlanosa.h> |
|
23 #include "osa_includeme.h" |
|
24 |
|
25 // ----------------------------------------------------------------------------- |
|
26 // |
|
27 // ----------------------------------------------------------------------------- |
|
28 // |
|
29 template<class T> |
|
30 inline list<T>::~list() |
|
31 { |
|
32 clear(); |
|
33 } |
|
34 |
|
35 // ----------------------------------------------------------------------------- |
|
36 // |
|
37 // ----------------------------------------------------------------------------- |
|
38 // |
|
39 template<class T> |
|
40 inline list<T>::iterator list<T>::begin() |
|
41 { |
|
42 // for empty ranges begin() == end() |
|
43 return ( !(empty()) ? list_iterator<Node, T>( iFirst ) : end() ); |
|
44 } |
|
45 |
|
46 // ----------------------------------------------------------------------------- |
|
47 // |
|
48 // ----------------------------------------------------------------------------- |
|
49 // |
|
50 template<class T> |
|
51 inline list<T>::const_iterator list<T>::begin() const |
|
52 { |
|
53 // for empty ranges begin() == end() |
|
54 return ( !(empty()) ? list_iterator<Node, T>( iFirst ) : end() ); |
|
55 } |
|
56 |
|
57 // ----------------------------------------------------------------------------- |
|
58 // |
|
59 // ----------------------------------------------------------------------------- |
|
60 // |
|
61 template<class T> |
|
62 inline list<T>::iterator list<T>::end() |
|
63 { |
|
64 return list_iterator<Node, T>(); |
|
65 } |
|
66 |
|
67 // ----------------------------------------------------------------------------- |
|
68 // |
|
69 // ----------------------------------------------------------------------------- |
|
70 // |
|
71 template<class T> |
|
72 inline list<T>::const_iterator list<T>::end() const |
|
73 { |
|
74 return list_iterator<Node, T>(); |
|
75 } |
|
76 |
|
77 // ----------------------------------------------------------------------------- |
|
78 // |
|
79 // ----------------------------------------------------------------------------- |
|
80 // |
|
81 template<class T> |
|
82 inline list<T>::size_type list<T>::size() const |
|
83 { |
|
84 return iNumOfElems; |
|
85 } |
|
86 |
|
87 // ----------------------------------------------------------------------------- |
|
88 // |
|
89 // ----------------------------------------------------------------------------- |
|
90 // |
|
91 template<class T> |
|
92 inline TBool list<T>::empty() const |
|
93 { |
|
94 return (!size()); |
|
95 } |
|
96 |
|
97 // ----------------------------------------------------------------------------- |
|
98 // |
|
99 // ----------------------------------------------------------------------------- |
|
100 // |
|
101 template<class T> |
|
102 inline list<T>::reference list<T>::front() |
|
103 { |
|
104 // front() for empty sequence is undefined so assert |
|
105 MWlanOsa::Assert( |
|
106 reinterpret_cast<const TInt8*>(WLAN_FILE), __LINE__, size() ); |
|
107 return iFirst->iElem; |
|
108 } |
|
109 |
|
110 // ----------------------------------------------------------------------------- |
|
111 // |
|
112 // ----------------------------------------------------------------------------- |
|
113 // |
|
114 template<class T> |
|
115 inline list<T>::const_reference list<T>::front() const |
|
116 { |
|
117 // front() for empty sequence is undefined so assert |
|
118 MWlanOsa::Assert( |
|
119 reinterpret_cast<const TInt8*>(WLAN_FILE), __LINE__, size() ); |
|
120 return iFirst->iElem; |
|
121 } |
|
122 |
|
123 // ----------------------------------------------------------------------------- |
|
124 // |
|
125 // ----------------------------------------------------------------------------- |
|
126 // |
|
127 template<class T> |
|
128 inline list<T>::reference list<T>::back() |
|
129 { |
|
130 // back() for empty sequence is undefined so assert |
|
131 MWlanOsa::Assert( |
|
132 reinterpret_cast<const TInt8*>(WLAN_FILE), __LINE__, size() ); |
|
133 return iLast->iElem; |
|
134 } |
|
135 |
|
136 // ----------------------------------------------------------------------------- |
|
137 // |
|
138 // ----------------------------------------------------------------------------- |
|
139 // |
|
140 template<class T> |
|
141 inline list<T>::const_reference list<T>::back() const |
|
142 { |
|
143 // back() for empty sequence is undefined so assert |
|
144 MWlanOsa::Assert( |
|
145 reinterpret_cast<const TInt8*>(WLAN_FILE), __LINE__, size() ); |
|
146 return iLast->iElem; |
|
147 } |
|
148 |
|
149 // ----------------------------------------------------------------------------- |
|
150 // |
|
151 // ----------------------------------------------------------------------------- |
|
152 // |
|
153 template<class T> |
|
154 inline void list<T>::clear() |
|
155 { |
|
156 Node* current = iFirst; |
|
157 Node* next = current; |
|
158 |
|
159 iNumOfElems = 0; |
|
160 iFirst = NULL; |
|
161 iLast = NULL; |
|
162 |
|
163 while ( current ) |
|
164 { |
|
165 next = current->iNext; |
|
166 delete current; |
|
167 current = next; |
|
168 } |
|
169 } |
|
170 |
|
171 // ----------------------------------------------------------------------------- |
|
172 // |
|
173 // ----------------------------------------------------------------------------- |
|
174 // |
|
175 template<class T> |
|
176 inline void list<T>::push_back( const_reference aElem ) |
|
177 { |
|
178 // allocate a new node for the element |
|
179 Node* node = new Node( aElem ); |
|
180 |
|
181 if ( node ) |
|
182 { |
|
183 // allocation success now append the node to rear |
|
184 if ( !empty() ) |
|
185 { |
|
186 // we are not empty just link to rear |
|
187 node->iPrev = iLast; |
|
188 iLast->iNext = node; |
|
189 iLast = node; |
|
190 } |
|
191 else |
|
192 { |
|
193 // we are empty, so last and first nodes are the same |
|
194 iFirst = node; |
|
195 iLast = iFirst; |
|
196 } |
|
197 |
|
198 ++iNumOfElems; // allways increment element count upon success |
|
199 } |
|
200 else |
|
201 { |
|
202 // allocation failure |
|
203 // left intentionally empty |
|
204 TraceDump(ERROR_LEVEL, ("[WLAN] error: allocation")); |
|
205 Trace( ERROR_LEVEL, |
|
206 reinterpret_cast<const TInt8*>(WLAN_FILE), __LINE__ ); |
|
207 } |
|
208 } |
|
209 |
|
210 // ----------------------------------------------------------------------------- |
|
211 // |
|
212 // ----------------------------------------------------------------------------- |
|
213 // |
|
214 template<class T> |
|
215 inline void list<T>::pop_back() |
|
216 { |
|
217 Node* prev = iLast->iPrev; |
|
218 |
|
219 if ( prev ) |
|
220 { |
|
221 // previous node exists, so make it the new last node |
|
222 prev->iNext = NULL; |
|
223 iLast = prev; |
|
224 } |
|
225 else |
|
226 { |
|
227 // no previous node exist, meaning that we are the only one |
|
228 // and as we are gone there exist no one |
|
229 iFirst = NULL; |
|
230 iLast = NULL; |
|
231 } |
|
232 |
|
233 --iNumOfElems; // always decrement upon pop |
|
234 } |
|
235 |
|
236 // ----------------------------------------------------------------------------- |
|
237 // |
|
238 // ----------------------------------------------------------------------------- |
|
239 // |
|
240 template<class T> |
|
241 inline void list<T>::push_front( const_reference aElem ) |
|
242 { |
|
243 // allocate a new node for the element |
|
244 Node* node = new Node( aElem ); |
|
245 |
|
246 if ( node ) |
|
247 { |
|
248 // allocation success now append the node to rear |
|
249 if ( !empty() ) |
|
250 { |
|
251 // we are not empty just link to front |
|
252 node->iNext = iFirst; |
|
253 iFirst->iPrev = node; |
|
254 iFirst = node; |
|
255 } |
|
256 else |
|
257 { |
|
258 // we are empty, so last and first nodes are the same |
|
259 iFirst = node; |
|
260 iLast = iFirst; |
|
261 } |
|
262 |
|
263 ++iNumOfElems; // allways increment element count upon success |
|
264 |
|
265 } |
|
266 else |
|
267 { |
|
268 // allocation failure |
|
269 // left intentionally empty |
|
270 TraceDump(ERROR_LEVEL, ("[WLAN] error: allocation")); |
|
271 Trace( ERROR_LEVEL, |
|
272 reinterpret_cast<const TInt8*>(WLAN_FILE), __LINE__ ); |
|
273 } |
|
274 } |
|
275 |
|
276 // ----------------------------------------------------------------------------- |
|
277 // |
|
278 // ----------------------------------------------------------------------------- |
|
279 // |
|
280 template<class T> |
|
281 inline void list<T>::pop_front() |
|
282 { |
|
283 Node* next = iFirst->iNext; |
|
284 |
|
285 if ( next ) |
|
286 { |
|
287 // next node exists, so make it the new first node |
|
288 next->iPrev = NULL; |
|
289 iFirst = next; |
|
290 } |
|
291 else |
|
292 { |
|
293 // no next node exist, meaning that we are the only one |
|
294 // and as we are gone there exist no one |
|
295 iFirst = NULL; |
|
296 iLast = NULL; |
|
297 } |
|
298 |
|
299 --iNumOfElems; // allways decrement upon pop |
|
300 } |
|
301 |
|
302 // ----------------------------------------------------------------------------- |
|
303 // |
|
304 // ----------------------------------------------------------------------------- |
|
305 // |
|
306 template<class T> |
|
307 inline list<T>::iterator list<T>::insert( iterator aPos, const T& aElem ) |
|
308 { |
|
309 // allocate a new node for the element |
|
310 Node* node = new Node( aElem ); |
|
311 |
|
312 if ( node ) |
|
313 { |
|
314 // allocation success now insert aElem before aPos |
|
315 if ( aPos->iPrev ) |
|
316 { |
|
317 // a previous link exists |
|
318 aPos->iPrev->iNext = node; // link old previous node to new node |
|
319 } |
|
320 |
|
321 node->iPrev = aPos->iPrev; // back link new node |
|
322 node->iNext = aPos(); // next link new node |
|
323 aPos->iPrev = node; // back link pos to new node |
|
324 |
|
325 // allways increment element count upon success |
|
326 ++iNumOfElems; |
|
327 |
|
328 const TInt KHeadInsertionCountMark( 2 ); |
|
329 if ( size() == KHeadInsertionCountMark ) |
|
330 { |
|
331 // set new list head as this insertion was to the front |
|
332 iFirst = node; |
|
333 } |
|
334 else |
|
335 { |
|
336 // left intentionally empty |
|
337 } |
|
338 } |
|
339 else |
|
340 { |
|
341 // allocation failure |
|
342 // left intentionally empty |
|
343 TraceDump(ERROR_LEVEL, ("[WLAN] error: allocation")); |
|
344 Trace( ERROR_LEVEL, |
|
345 reinterpret_cast<const TInt8*>(WLAN_FILE), __LINE__ ); |
|
346 } |
|
347 |
|
348 // return the position of the inserted element (if any as node can be NULL) |
|
349 // in case of NULL end() is returned |
|
350 return ( (node != NULL) ? list_iterator<Node, T>( node ) : end() ); |
|
351 } |
|
352 |
|
353 // ----------------------------------------------------------------------------- |
|
354 // |
|
355 // ----------------------------------------------------------------------------- |
|
356 // |
|
357 template<class T> |
|
358 inline list<T>::iterator list<T>::erase( iterator aPos ) |
|
359 { |
|
360 // extract node to be erased |
|
361 Node* node = aPos(); |
|
362 |
|
363 if ( node->iNext ) |
|
364 { |
|
365 // next node exists |
|
366 node->iNext->iPrev = node->iPrev; // back link next node |
|
367 } |
|
368 else |
|
369 { |
|
370 // next node does not exist so we don't have to relink it |
|
371 // left intentionally empty |
|
372 } |
|
373 |
|
374 if ( node->iPrev ) |
|
375 { |
|
376 // previous node exists |
|
377 node->iPrev->iNext = node->iNext; // next link previous node |
|
378 } |
|
379 else |
|
380 { |
|
381 // previous node does not exist so we don't have to relink it |
|
382 // left intentionally empty |
|
383 } |
|
384 |
|
385 Node* next_node = node->iNext; |
|
386 |
|
387 if ( node == iFirst ) |
|
388 { |
|
389 // node to be deleted is the first one set the new head |
|
390 iFirst = next_node; |
|
391 } |
|
392 else |
|
393 { |
|
394 // left intentionally empty |
|
395 } |
|
396 if ( node == iLast ) |
|
397 { |
|
398 // node to be deleted is the last one set new tail |
|
399 iLast = node->iPrev; |
|
400 } |
|
401 else |
|
402 { |
|
403 // left unintentionally empty |
|
404 } |
|
405 |
|
406 delete node; // delete the current node |
|
407 |
|
408 --iNumOfElems; // allways decrement element count |
|
409 |
|
410 if ( empty() ) |
|
411 { |
|
412 // we are now empty |
|
413 iFirst = NULL; |
|
414 iLast = NULL; |
|
415 } |
|
416 else |
|
417 { |
|
418 // left intentionally empty |
|
419 } |
|
420 |
|
421 // return the position of the next element |
|
422 // (if any as next_node can be NULL) |
|
423 // in case of NULL end() is returned |
|
424 return ( (NULL != next_node) |
|
425 ? list_iterator<Node, T>( next_node ) : end() ); |
|
426 } |
|
427 |
|
428 |