1 | /****************************************************************************
|
---|
2 | ** $Id: qvaluelist.h 2 2005-11-16 15:49:26Z dmik $
|
---|
3 | **
|
---|
4 | ** Definition of QValueList class
|
---|
5 | **
|
---|
6 | ** Created : 990406
|
---|
7 | **
|
---|
8 | ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
|
---|
9 | **
|
---|
10 | ** This file is part of the tools module of the Qt GUI Toolkit.
|
---|
11 | **
|
---|
12 | ** This file may be distributed under the terms of the Q Public License
|
---|
13 | ** as defined by Trolltech AS of Norway and appearing in the file
|
---|
14 | ** LICENSE.QPL included in the packaging of this file.
|
---|
15 | **
|
---|
16 | ** This file may be distributed and/or modified under the terms of the
|
---|
17 | ** GNU General Public License version 2 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
19 | ** packaging of this file.
|
---|
20 | **
|
---|
21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
---|
22 | ** licenses may use this file in accordance with the Qt Commercial License
|
---|
23 | ** Agreement provided with the Software.
|
---|
24 | **
|
---|
25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
---|
26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
27 | **
|
---|
28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
---|
29 | ** information about Qt Commercial License Agreements.
|
---|
30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
---|
31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
---|
32 | **
|
---|
33 | ** Contact info@trolltech.com if any conditions of this licensing are
|
---|
34 | ** not clear to you.
|
---|
35 | **
|
---|
36 | **********************************************************************/
|
---|
37 |
|
---|
38 | #ifndef QVALUELIST_H
|
---|
39 | #define QVALUELIST_H
|
---|
40 |
|
---|
41 | #ifndef QT_H
|
---|
42 | #include "qtl.h"
|
---|
43 | #include "qshared.h"
|
---|
44 | #include "qdatastream.h"
|
---|
45 | #endif // QT_H
|
---|
46 |
|
---|
47 | #ifndef QT_NO_STL
|
---|
48 | #include <iterator>
|
---|
49 | #include <list>
|
---|
50 | #endif
|
---|
51 |
|
---|
52 | //#define QT_CHECK_VALUELIST_RANGE
|
---|
53 |
|
---|
54 | #if defined(Q_CC_MSVC)
|
---|
55 | #pragma warning(disable:4284) // "return type for operator -> is not a UDT"
|
---|
56 | #endif
|
---|
57 |
|
---|
58 | template <class T>
|
---|
59 | class QValueListNode
|
---|
60 | {
|
---|
61 | public:
|
---|
62 | QValueListNode( const T& t ) : data( t ) { }
|
---|
63 | QValueListNode() { }
|
---|
64 | #if defined(Q_TEMPLATEDLL)
|
---|
65 | // Workaround MS bug in memory de/allocation in DLL vs. EXE
|
---|
66 | virtual ~QValueListNode() { }
|
---|
67 | #endif
|
---|
68 |
|
---|
69 | QValueListNode<T>* next;
|
---|
70 | QValueListNode<T>* prev;
|
---|
71 | T data;
|
---|
72 | };
|
---|
73 |
|
---|
74 | template<class T>
|
---|
75 | class QValueListIterator
|
---|
76 | {
|
---|
77 | public:
|
---|
78 | /**
|
---|
79 | * Typedefs
|
---|
80 | */
|
---|
81 | typedef QValueListNode<T>* NodePtr;
|
---|
82 | #ifndef QT_NO_STL
|
---|
83 | typedef std::bidirectional_iterator_tag iterator_category;
|
---|
84 | #endif
|
---|
85 | typedef T value_type;
|
---|
86 | typedef size_t size_type;
|
---|
87 | #ifndef QT_NO_STL
|
---|
88 | typedef ptrdiff_t difference_type;
|
---|
89 | #else
|
---|
90 | typedef int difference_type;
|
---|
91 | #endif
|
---|
92 | typedef T* pointer;
|
---|
93 | typedef T& reference;
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Variables
|
---|
97 | */
|
---|
98 | NodePtr node;
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Functions
|
---|
102 | */
|
---|
103 | QValueListIterator() : node( 0 ) {}
|
---|
104 | QValueListIterator( NodePtr p ) : node( p ) {}
|
---|
105 | QValueListIterator( const QValueListIterator<T>& it ) : node( it.node ) {}
|
---|
106 |
|
---|
107 | bool operator==( const QValueListIterator<T>& it ) const { return node == it.node; }
|
---|
108 | bool operator!=( const QValueListIterator<T>& it ) const { return node != it.node; }
|
---|
109 | const T& operator*() const { return node->data; }
|
---|
110 | T& operator*() { return node->data; }
|
---|
111 | // UDT for T = x*
|
---|
112 | // T* operator->() const { return &node->data; }
|
---|
113 |
|
---|
114 | QValueListIterator<T>& operator++() {
|
---|
115 | node = node->next;
|
---|
116 | return *this;
|
---|
117 | }
|
---|
118 |
|
---|
119 | QValueListIterator<T> operator++(int) {
|
---|
120 | QValueListIterator<T> tmp = *this;
|
---|
121 | node = node->next;
|
---|
122 | return tmp;
|
---|
123 | }
|
---|
124 |
|
---|
125 | QValueListIterator<T>& operator--() {
|
---|
126 | node = node->prev;
|
---|
127 | return *this;
|
---|
128 | }
|
---|
129 |
|
---|
130 | QValueListIterator<T> operator--(int) {
|
---|
131 | QValueListIterator<T> tmp = *this;
|
---|
132 | node = node->prev;
|
---|
133 | return tmp;
|
---|
134 | }
|
---|
135 |
|
---|
136 | QValueListIterator<T>& operator+=( int j ) {
|
---|
137 | while ( j-- )
|
---|
138 | node = node->next;
|
---|
139 | return *this;
|
---|
140 | }
|
---|
141 |
|
---|
142 | QValueListIterator<T>& operator-=( int j ) {
|
---|
143 | while ( j-- )
|
---|
144 | node = node->prev;
|
---|
145 | return *this;
|
---|
146 | }
|
---|
147 |
|
---|
148 | };
|
---|
149 |
|
---|
150 | template<class T>
|
---|
151 | class QValueListConstIterator
|
---|
152 | {
|
---|
153 | public:
|
---|
154 | /**
|
---|
155 | * Typedefs
|
---|
156 | */
|
---|
157 | typedef QValueListNode<T>* NodePtr;
|
---|
158 | #ifndef QT_NO_STL
|
---|
159 | typedef std::bidirectional_iterator_tag iterator_category;
|
---|
160 | #endif
|
---|
161 | typedef T value_type;
|
---|
162 | typedef size_t size_type;
|
---|
163 | #ifndef QT_NO_STL
|
---|
164 | typedef ptrdiff_t difference_type;
|
---|
165 | #else
|
---|
166 | typedef int difference_type;
|
---|
167 | #endif
|
---|
168 | typedef const T* pointer;
|
---|
169 | typedef const T& reference;
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Variables
|
---|
173 | */
|
---|
174 | NodePtr node;
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Functions
|
---|
178 | */
|
---|
179 | QValueListConstIterator() : node( 0 ) {}
|
---|
180 | QValueListConstIterator( NodePtr p ) : node( p ) {}
|
---|
181 | QValueListConstIterator( const QValueListConstIterator<T>& it ) : node( it.node ) {}
|
---|
182 | QValueListConstIterator( const QValueListIterator<T>& it ) : node( it.node ) {}
|
---|
183 |
|
---|
184 | bool operator==( const QValueListConstIterator<T>& it ) const { return node == it.node; }
|
---|
185 | bool operator!=( const QValueListConstIterator<T>& it ) const { return node != it.node; }
|
---|
186 | const T& operator*() const { return node->data; }
|
---|
187 | // UDT for T = x*
|
---|
188 | // const T* operator->() const { return &node->data; }
|
---|
189 |
|
---|
190 | QValueListConstIterator<T>& operator++() {
|
---|
191 | node = node->next;
|
---|
192 | return *this;
|
---|
193 | }
|
---|
194 |
|
---|
195 | QValueListConstIterator<T> operator++(int) {
|
---|
196 | QValueListConstIterator<T> tmp = *this;
|
---|
197 | node = node->next;
|
---|
198 | return tmp;
|
---|
199 | }
|
---|
200 |
|
---|
201 | QValueListConstIterator<T>& operator--() {
|
---|
202 | node = node->prev;
|
---|
203 | return *this;
|
---|
204 | }
|
---|
205 |
|
---|
206 | QValueListConstIterator<T> operator--(int) {
|
---|
207 | QValueListConstIterator<T> tmp = *this;
|
---|
208 | node = node->prev;
|
---|
209 | return tmp;
|
---|
210 | }
|
---|
211 | };
|
---|
212 |
|
---|
213 | template <class T>
|
---|
214 | class QValueListPrivate : public QShared
|
---|
215 | {
|
---|
216 | public:
|
---|
217 | /**
|
---|
218 | * Typedefs
|
---|
219 | */
|
---|
220 | typedef QValueListIterator<T> Iterator;
|
---|
221 | typedef QValueListConstIterator<T> ConstIterator;
|
---|
222 | typedef QValueListNode<T> Node;
|
---|
223 | typedef QValueListNode<T>* NodePtr;
|
---|
224 | typedef size_t size_type;
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * Functions
|
---|
228 | */
|
---|
229 | QValueListPrivate();
|
---|
230 | QValueListPrivate( const QValueListPrivate<T>& _p );
|
---|
231 |
|
---|
232 | void derefAndDelete() // ### hack to get around hp-cc brain damage
|
---|
233 | {
|
---|
234 | if ( deref() )
|
---|
235 | delete this;
|
---|
236 | }
|
---|
237 |
|
---|
238 | #if defined(Q_TEMPLATEDLL)
|
---|
239 | // Workaround MS bug in memory de/allocation in DLL vs. EXE
|
---|
240 | virtual
|
---|
241 | #endif
|
---|
242 | ~QValueListPrivate();
|
---|
243 |
|
---|
244 | Iterator insert( Iterator it, const T& x );
|
---|
245 | Iterator remove( Iterator it );
|
---|
246 | NodePtr find( NodePtr start, const T& x ) const;
|
---|
247 | int findIndex( NodePtr start, const T& x ) const;
|
---|
248 | uint contains( const T& x ) const;
|
---|
249 | uint remove( const T& x );
|
---|
250 | NodePtr at( size_type i ) const;
|
---|
251 | void clear();
|
---|
252 |
|
---|
253 | NodePtr node;
|
---|
254 | size_type nodes;
|
---|
255 | };
|
---|
256 |
|
---|
257 | template <class T>
|
---|
258 | Q_INLINE_TEMPLATES QValueListPrivate<T>::QValueListPrivate()
|
---|
259 | {
|
---|
260 | node = new Node; node->next = node->prev = node; nodes = 0;
|
---|
261 | }
|
---|
262 |
|
---|
263 | template <class T>
|
---|
264 | Q_INLINE_TEMPLATES QValueListPrivate<T>::QValueListPrivate( const QValueListPrivate<T>& _p )
|
---|
265 | : QShared()
|
---|
266 | {
|
---|
267 | node = new Node; node->next = node->prev = node; nodes = 0;
|
---|
268 | Iterator b( _p.node->next );
|
---|
269 | Iterator e( _p.node );
|
---|
270 | Iterator i( node );
|
---|
271 | while( b != e )
|
---|
272 | insert( i, *b++ );
|
---|
273 | }
|
---|
274 |
|
---|
275 | template <class T>
|
---|
276 | Q_INLINE_TEMPLATES QValueListPrivate<T>::~QValueListPrivate() {
|
---|
277 | NodePtr p = node->next;
|
---|
278 | while( p != node ) {
|
---|
279 | NodePtr x = p->next;
|
---|
280 | delete p;
|
---|
281 | p = x;
|
---|
282 | }
|
---|
283 | delete node;
|
---|
284 | }
|
---|
285 |
|
---|
286 | template <class T>
|
---|
287 | Q_INLINE_TEMPLATES Q_TYPENAME QValueListPrivate<T>::Iterator QValueListPrivate<T>::insert( Q_TYPENAME QValueListPrivate<T>::Iterator it, const T& x )
|
---|
288 | {
|
---|
289 | NodePtr p = new Node( x );
|
---|
290 | p->next = it.node;
|
---|
291 | p->prev = it.node->prev;
|
---|
292 | it.node->prev->next = p;
|
---|
293 | it.node->prev = p;
|
---|
294 | nodes++;
|
---|
295 | return p;
|
---|
296 | }
|
---|
297 |
|
---|
298 | template <class T>
|
---|
299 | Q_INLINE_TEMPLATES Q_TYPENAME QValueListPrivate<T>::Iterator QValueListPrivate<T>::remove( Q_TYPENAME QValueListPrivate<T>::Iterator it )
|
---|
300 | {
|
---|
301 | Q_ASSERT ( it.node != node );
|
---|
302 | NodePtr next = it.node->next;
|
---|
303 | NodePtr prev = it.node->prev;
|
---|
304 | prev->next = next;
|
---|
305 | next->prev = prev;
|
---|
306 | delete it.node;
|
---|
307 | nodes--;
|
---|
308 | return Iterator( next );
|
---|
309 | }
|
---|
310 |
|
---|
311 | template <class T>
|
---|
312 | Q_INLINE_TEMPLATES Q_TYPENAME QValueListPrivate<T>::NodePtr QValueListPrivate<T>::find( Q_TYPENAME QValueListPrivate<T>::NodePtr start, const T& x ) const
|
---|
313 | {
|
---|
314 | ConstIterator first( start );
|
---|
315 | ConstIterator last( node );
|
---|
316 | while( first != last) {
|
---|
317 | if ( *first == x )
|
---|
318 | return first.node;
|
---|
319 | ++first;
|
---|
320 | }
|
---|
321 | return last.node;
|
---|
322 | }
|
---|
323 |
|
---|
324 | template <class T>
|
---|
325 | Q_INLINE_TEMPLATES int QValueListPrivate<T>::findIndex( Q_TYPENAME QValueListPrivate<T>::NodePtr start, const T& x ) const
|
---|
326 | {
|
---|
327 | ConstIterator first( start );
|
---|
328 | ConstIterator last( node );
|
---|
329 | int pos = 0;
|
---|
330 | while( first != last) {
|
---|
331 | if ( *first == x )
|
---|
332 | return pos;
|
---|
333 | ++first;
|
---|
334 | ++pos;
|
---|
335 | }
|
---|
336 | return -1;
|
---|
337 | }
|
---|
338 |
|
---|
339 | template <class T>
|
---|
340 | Q_INLINE_TEMPLATES uint QValueListPrivate<T>::contains( const T& x ) const
|
---|
341 | {
|
---|
342 | uint result = 0;
|
---|
343 | Iterator first = Iterator( node->next );
|
---|
344 | Iterator last = Iterator( node );
|
---|
345 | while( first != last) {
|
---|
346 | if ( *first == x )
|
---|
347 | ++result;
|
---|
348 | ++first;
|
---|
349 | }
|
---|
350 | return result;
|
---|
351 | }
|
---|
352 |
|
---|
353 | template <class T>
|
---|
354 | Q_INLINE_TEMPLATES uint QValueListPrivate<T>::remove( const T& x )
|
---|
355 | {
|
---|
356 | uint result = 0;
|
---|
357 | Iterator first = Iterator( node->next );
|
---|
358 | Iterator last = Iterator( node );
|
---|
359 | while( first != last) {
|
---|
360 | if ( *first == x ) {
|
---|
361 | first = remove( first );
|
---|
362 | ++result;
|
---|
363 | } else
|
---|
364 | ++first;
|
---|
365 | }
|
---|
366 | return result;
|
---|
367 | }
|
---|
368 |
|
---|
369 | template <class T>
|
---|
370 | Q_INLINE_TEMPLATES Q_TYPENAME QValueListPrivate<T>::NodePtr QValueListPrivate<T>::at( size_type i ) const
|
---|
371 | {
|
---|
372 | Q_ASSERT( i <= nodes );
|
---|
373 | NodePtr p = node->next;
|
---|
374 | for( size_type x = 0; x < i; ++x )
|
---|
375 | p = p->next;
|
---|
376 | return p;
|
---|
377 | }
|
---|
378 |
|
---|
379 | template <class T>
|
---|
380 | Q_INLINE_TEMPLATES void QValueListPrivate<T>::clear()
|
---|
381 | {
|
---|
382 | nodes = 0;
|
---|
383 | NodePtr p = node->next;
|
---|
384 | while( p != node ) {
|
---|
385 | NodePtr next = p->next;
|
---|
386 | delete p;
|
---|
387 | p = next;
|
---|
388 | }
|
---|
389 | node->next = node->prev = node;
|
---|
390 | }
|
---|
391 |
|
---|
392 | #ifdef QT_CHECK_RANGE
|
---|
393 | # if !defined( QT_NO_DEBUG ) && defined( QT_CHECK_VALUELIST_RANGE )
|
---|
394 | # define QT_CHECK_INVALID_LIST_ELEMENT if ( empty() ) qWarning( "QValueList: Warning invalid element" )
|
---|
395 | # define QT_CHECK_INVALID_LIST_ELEMENT_FATAL Q_ASSERT( !empty() );
|
---|
396 | # else
|
---|
397 | # define QT_CHECK_INVALID_LIST_ELEMENT
|
---|
398 | # define QT_CHECK_INVALID_LIST_ELEMENT_FATAL
|
---|
399 | # endif
|
---|
400 | #else
|
---|
401 | # define QT_CHECK_INVALID_LIST_ELEMENT
|
---|
402 | # define QT_CHECK_INVALID_LIST_ELEMENT_FATAL
|
---|
403 | #endif
|
---|
404 |
|
---|
405 | template <class T> class QDeepCopy;
|
---|
406 |
|
---|
407 | template <class T>
|
---|
408 | class QValueList
|
---|
409 | {
|
---|
410 | public:
|
---|
411 | /**
|
---|
412 | * Typedefs
|
---|
413 | */
|
---|
414 | typedef QValueListIterator<T> iterator;
|
---|
415 | typedef QValueListConstIterator<T> const_iterator;
|
---|
416 | typedef T value_type;
|
---|
417 | typedef value_type* pointer;
|
---|
418 | typedef const value_type* const_pointer;
|
---|
419 | typedef value_type& reference;
|
---|
420 | typedef const value_type& const_reference;
|
---|
421 | typedef size_t size_type;
|
---|
422 | #ifndef QT_NO_STL
|
---|
423 | typedef ptrdiff_t difference_type;
|
---|
424 | #else
|
---|
425 | typedef int difference_type;
|
---|
426 | #endif
|
---|
427 |
|
---|
428 | /**
|
---|
429 | * API
|
---|
430 | */
|
---|
431 | QValueList() { sh = new QValueListPrivate<T>; }
|
---|
432 | QValueList( const QValueList<T>& l ) { sh = l.sh; sh->ref(); }
|
---|
433 | #ifndef QT_NO_STL
|
---|
434 | QValueList( const std::list<T>& l )
|
---|
435 | {
|
---|
436 | sh = new QValueListPrivate<T>;
|
---|
437 | qCopy( l.begin(), l.end(), std::back_inserter( *this ) );
|
---|
438 | }
|
---|
439 | #endif
|
---|
440 | ~QValueList() { sh->derefAndDelete(); }
|
---|
441 |
|
---|
442 | QValueList<T>& operator= ( const QValueList<T>& l )
|
---|
443 | {
|
---|
444 | l.sh->ref();
|
---|
445 | sh->derefAndDelete();
|
---|
446 | sh = l.sh;
|
---|
447 | return *this;
|
---|
448 | }
|
---|
449 | #ifndef QT_NO_STL
|
---|
450 | QValueList<T>& operator= ( const std::list<T>& l )
|
---|
451 | {
|
---|
452 | detach();
|
---|
453 | qCopy( l.begin(), l.end(), std::back_inserter( *this ) );
|
---|
454 | return *this;
|
---|
455 | }
|
---|
456 | bool operator== ( const std::list<T>& l ) const
|
---|
457 | {
|
---|
458 | if ( size() != l.size() )
|
---|
459 | return FALSE;
|
---|
460 | const_iterator it2 = begin();
|
---|
461 | #if !defined(Q_CC_MIPS)
|
---|
462 | typename
|
---|
463 | #endif
|
---|
464 | std::list<T>::const_iterator it = l.begin();
|
---|
465 | for ( ; it2 != end(); ++it2, ++it )
|
---|
466 | if ( !((*it2) == (*it)) )
|
---|
467 | return FALSE;
|
---|
468 | return TRUE;
|
---|
469 | }
|
---|
470 | #endif
|
---|
471 | bool operator== ( const QValueList<T>& l ) const;
|
---|
472 | bool operator!= ( const QValueList<T>& l ) const { return !( *this == l ); }
|
---|
473 | iterator begin() { detach(); return iterator( sh->node->next ); }
|
---|
474 | const_iterator begin() const { return const_iterator( sh->node->next ); }
|
---|
475 | const_iterator constBegin() const { return const_iterator( sh->node->next ); }
|
---|
476 | iterator end() { detach(); return iterator( sh->node ); }
|
---|
477 | const_iterator end() const { return const_iterator( sh->node ); }
|
---|
478 | const_iterator constEnd() const { return const_iterator( sh->node ); }
|
---|
479 | iterator insert( iterator it, const T& x ) { detach(); return sh->insert( it, x ); }
|
---|
480 | uint remove( const T& x ) { detach(); return sh->remove( x ); }
|
---|
481 | void clear();
|
---|
482 |
|
---|
483 | // ### 4.0: move out of class
|
---|
484 | QValueList<T>& operator<< ( const T& x )
|
---|
485 | {
|
---|
486 | append( x );
|
---|
487 | return *this;
|
---|
488 | }
|
---|
489 |
|
---|
490 | size_type size() const { return sh->nodes; }
|
---|
491 | bool empty() const { return sh->nodes == 0; }
|
---|
492 | void push_front( const T& x ) { detach(); sh->insert( begin(), x ); }
|
---|
493 | void push_back( const T& x ) { detach(); sh->insert( end(), x ); }
|
---|
494 | iterator erase( iterator pos ) { detach(); return sh->remove( pos ); }
|
---|
495 | iterator erase( iterator first, iterator last );
|
---|
496 | reference front() { QT_CHECK_INVALID_LIST_ELEMENT_FATAL; return *begin(); }
|
---|
497 | const_reference front() const { QT_CHECK_INVALID_LIST_ELEMENT_FATAL; return *begin(); }
|
---|
498 | reference back() { QT_CHECK_INVALID_LIST_ELEMENT_FATAL; return *(--end()); }
|
---|
499 | const_reference back() const { QT_CHECK_INVALID_LIST_ELEMENT_FATAL; return *(--end()); }
|
---|
500 | void pop_front() { QT_CHECK_INVALID_LIST_ELEMENT; erase( begin() ); }
|
---|
501 | void pop_back() {
|
---|
502 | QT_CHECK_INVALID_LIST_ELEMENT;
|
---|
503 | iterator tmp = end();
|
---|
504 | erase( --tmp );
|
---|
505 | }
|
---|
506 | void insert( iterator pos, size_type n, const T& x );
|
---|
507 | // Some compilers (incl. vc++) would instantiate this function even if
|
---|
508 | // it is not used; this would constrain QValueList to classes that provide
|
---|
509 | // an operator<
|
---|
510 | /*
|
---|
511 | void sort()
|
---|
512 | {
|
---|
513 | qHeapSort( *this );
|
---|
514 | }
|
---|
515 | */
|
---|
516 |
|
---|
517 | QValueList<T> operator+ ( const QValueList<T>& l ) const;
|
---|
518 | QValueList<T>& operator+= ( const QValueList<T>& l );
|
---|
519 |
|
---|
520 | iterator fromLast() { detach(); return iterator( sh->node->prev ); }
|
---|
521 | const_iterator fromLast() const { return const_iterator( sh->node->prev ); }
|
---|
522 |
|
---|
523 | bool isEmpty() const { return ( sh->nodes == 0 ); }
|
---|
524 |
|
---|
525 | iterator append( const T& x ) { detach(); return sh->insert( end(), x ); }
|
---|
526 | iterator prepend( const T& x ) { detach(); return sh->insert( begin(), x ); }
|
---|
527 |
|
---|
528 | iterator remove( iterator it ) { detach(); return sh->remove( it ); }
|
---|
529 |
|
---|
530 | T& first() { QT_CHECK_INVALID_LIST_ELEMENT; detach(); return sh->node->next->data; }
|
---|
531 | const T& first() const { QT_CHECK_INVALID_LIST_ELEMENT; return sh->node->next->data; }
|
---|
532 | T& last() { QT_CHECK_INVALID_LIST_ELEMENT; detach(); return sh->node->prev->data; }
|
---|
533 | const T& last() const { QT_CHECK_INVALID_LIST_ELEMENT; return sh->node->prev->data; }
|
---|
534 |
|
---|
535 | T& operator[] ( size_type i ) { QT_CHECK_INVALID_LIST_ELEMENT; detach(); return sh->at(i)->data; }
|
---|
536 | const T& operator[] ( size_type i ) const { QT_CHECK_INVALID_LIST_ELEMENT; return sh->at(i)->data; }
|
---|
537 | iterator at( size_type i ) { QT_CHECK_INVALID_LIST_ELEMENT; detach(); return iterator( sh->at(i) ); }
|
---|
538 | const_iterator at( size_type i ) const { QT_CHECK_INVALID_LIST_ELEMENT; return const_iterator( sh->at(i) ); }
|
---|
539 | iterator find ( const T& x ) { detach(); return iterator( sh->find( sh->node->next, x) ); }
|
---|
540 | const_iterator find ( const T& x ) const { return const_iterator( sh->find( sh->node->next, x) ); }
|
---|
541 | iterator find ( iterator it, const T& x ) { detach(); return iterator( sh->find( it.node, x ) ); }
|
---|
542 | const_iterator find ( const_iterator it, const T& x ) const { return const_iterator( sh->find( it.node, x ) ); }
|
---|
543 | int findIndex( const T& x ) const { return sh->findIndex( sh->node->next, x) ; }
|
---|
544 | size_type contains( const T& x ) const { return sh->contains( x ); }
|
---|
545 |
|
---|
546 | size_type count() const { return sh->nodes; }
|
---|
547 |
|
---|
548 | QValueList<T>& operator+= ( const T& x )
|
---|
549 | {
|
---|
550 | append( x );
|
---|
551 | return *this;
|
---|
552 | }
|
---|
553 | typedef QValueListIterator<T> Iterator;
|
---|
554 | typedef QValueListConstIterator<T> ConstIterator;
|
---|
555 | typedef T ValueType;
|
---|
556 |
|
---|
557 | protected:
|
---|
558 | /**
|
---|
559 | * Helpers
|
---|
560 | */
|
---|
561 | void detach() { if ( sh->count > 1 ) detachInternal(); }
|
---|
562 |
|
---|
563 | /**
|
---|
564 | * Variables
|
---|
565 | */
|
---|
566 | QValueListPrivate<T>* sh;
|
---|
567 |
|
---|
568 | private:
|
---|
569 | void detachInternal();
|
---|
570 |
|
---|
571 | friend class QDeepCopy< QValueList<T> >;
|
---|
572 | };
|
---|
573 |
|
---|
574 | template <class T>
|
---|
575 | Q_INLINE_TEMPLATES bool QValueList<T>::operator== ( const QValueList<T>& l ) const
|
---|
576 | {
|
---|
577 | if ( size() != l.size() )
|
---|
578 | return FALSE;
|
---|
579 | const_iterator it2 = begin();
|
---|
580 | const_iterator it = l.begin();
|
---|
581 | for( ; it != l.end(); ++it, ++it2 )
|
---|
582 | if ( !( *it == *it2 ) )
|
---|
583 | return FALSE;
|
---|
584 | return TRUE;
|
---|
585 | }
|
---|
586 |
|
---|
587 | template <class T>
|
---|
588 | Q_INLINE_TEMPLATES void QValueList<T>::clear()
|
---|
589 | {
|
---|
590 | if ( sh->count == 1 ) sh->clear(); else { sh->deref(); sh = new QValueListPrivate<T>; }
|
---|
591 | }
|
---|
592 |
|
---|
593 | template <class T>
|
---|
594 | Q_INLINE_TEMPLATES Q_TYPENAME QValueList<T>::iterator QValueList<T>::erase( Q_TYPENAME QValueList<T>::iterator first, Q_TYPENAME QValueList<T>::iterator last )
|
---|
595 | {
|
---|
596 | while ( first != last )
|
---|
597 | erase( first++ );
|
---|
598 | return last;
|
---|
599 | }
|
---|
600 |
|
---|
601 |
|
---|
602 | template <class T>
|
---|
603 | Q_INLINE_TEMPLATES void QValueList<T>::insert( Q_TYPENAME QValueList<T>::iterator pos, size_type n, const T& x )
|
---|
604 | {
|
---|
605 | for ( ; n > 0; --n )
|
---|
606 | insert( pos, x );
|
---|
607 | }
|
---|
608 |
|
---|
609 | template <class T>
|
---|
610 | Q_INLINE_TEMPLATES QValueList<T> QValueList<T>::operator+ ( const QValueList<T>& l ) const
|
---|
611 | {
|
---|
612 | QValueList<T> l2( *this );
|
---|
613 | for( const_iterator it = l.begin(); it != l.end(); ++it )
|
---|
614 | l2.append( *it );
|
---|
615 | return l2;
|
---|
616 | }
|
---|
617 |
|
---|
618 | template <class T>
|
---|
619 | Q_INLINE_TEMPLATES QValueList<T>& QValueList<T>::operator+= ( const QValueList<T>& l )
|
---|
620 | {
|
---|
621 | for( const_iterator it = l.begin(); it != l.end(); ++it )
|
---|
622 | append( *it );
|
---|
623 | return *this;
|
---|
624 | }
|
---|
625 |
|
---|
626 | template <class T>
|
---|
627 | Q_INLINE_TEMPLATES void QValueList<T>::detachInternal()
|
---|
628 | {
|
---|
629 | sh->deref(); sh = new QValueListPrivate<T>( *sh );
|
---|
630 | }
|
---|
631 |
|
---|
632 | #ifndef QT_NO_DATASTREAM
|
---|
633 | template <class T>
|
---|
634 | Q_INLINE_TEMPLATES QDataStream& operator>>( QDataStream& s, QValueList<T>& l )
|
---|
635 | {
|
---|
636 | l.clear();
|
---|
637 | Q_UINT32 c;
|
---|
638 | s >> c;
|
---|
639 | for( Q_UINT32 i = 0; i < c; ++i )
|
---|
640 | {
|
---|
641 | T t;
|
---|
642 | s >> t;
|
---|
643 | l.append( t );
|
---|
644 | if ( s.atEnd() )
|
---|
645 | break;
|
---|
646 | }
|
---|
647 | return s;
|
---|
648 | }
|
---|
649 |
|
---|
650 | template <class T>
|
---|
651 | Q_INLINE_TEMPLATES QDataStream& operator<<( QDataStream& s, const QValueList<T>& l )
|
---|
652 | {
|
---|
653 | s << (Q_UINT32)l.size();
|
---|
654 | QValueListConstIterator<T> it = l.begin();
|
---|
655 | for( ; it != l.end(); ++it )
|
---|
656 | s << *it;
|
---|
657 | return s;
|
---|
658 | }
|
---|
659 | #endif // QT_NO_DATASTREAM
|
---|
660 |
|
---|
661 | #define Q_DEFINED_QVALUELIST
|
---|
662 | #define Q_DEFINED_QVALUELIST
|
---|
663 | #include "qwinexport.h"
|
---|
664 | #endif // QVALUELIST_H
|
---|