author | eckhart.koppen@nokia.com |
Wed, 31 Mar 2010 11:06:36 +0300 | |
changeset 7 | f7bc934e204c |
parent 0 | 1918ee327afb |
child 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
7
f7bc934e204c
5cabc75a39ca2f064f70b40f72ed93c74c4dc19b
eckhart.koppen@nokia.com
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtCore module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qlinkedlist.h" |
|
43 |
||
44 |
QT_BEGIN_NAMESPACE |
|
45 |
||
46 |
QLinkedListData QLinkedListData::shared_null = { |
|
47 |
&QLinkedListData::shared_null, &QLinkedListData::shared_null, |
|
48 |
Q_BASIC_ATOMIC_INITIALIZER(1), 0, true |
|
49 |
}; |
|
50 |
||
51 |
/*! \class QLinkedList |
|
52 |
\brief The QLinkedList class is a template class that provides linked lists. |
|
53 |
||
54 |
\ingroup tools |
|
55 |
\ingroup shared |
|
56 |
||
57 |
\reentrant |
|
58 |
||
59 |
QLinkedList\<T\> is one of Qt's generic \l{container classes}. It |
|
60 |
stores a list of values and provides iterator-based access as |
|
61 |
well as \l{constant time} insertions and removals. |
|
62 |
||
63 |
QList\<T\>, QLinkedList\<T\>, and QVector\<T\> provide similar |
|
64 |
functionality. Here's an overview: |
|
65 |
||
66 |
\list |
|
67 |
\i For most purposes, QList is the right class to use. Its |
|
68 |
index-based API is more convenient than QLinkedList's |
|
69 |
iterator-based API, and it is usually faster than |
|
70 |
QVector because of the way it stores its items in |
|
71 |
memory (see \l{Algorithmic Complexity} for details). |
|
72 |
It also expands to less code in your executable. |
|
73 |
\i If you need a real linked list, with guarantees of \l{constant |
|
74 |
time} insertions in the middle of the list and iterators to |
|
75 |
items rather than indexes, use QLinkedList. |
|
76 |
\i If you want the items to occupy adjacent memory positions, |
|
77 |
use QVector. |
|
78 |
\endlist |
|
79 |
||
80 |
Here's an example of a QLinkedList that stores integers and a |
|
81 |
QLinkedList that stores QTime values: |
|
82 |
||
83 |
\snippet doc/src/snippets/code/src_corelib_tools_qlinkedlist.cpp 0 |
|
84 |
||
85 |
QLinkedList stores a list of items. The default constructor |
|
86 |
creates an empty list. To insert items into the list, you can use |
|
87 |
operator<<(): |
|
88 |
||
89 |
\snippet doc/src/snippets/code/src_corelib_tools_qlinkedlist.cpp 1 |
|
90 |
||
91 |
If you want to get the first or last item in a linked list, use |
|
92 |
first() or last(). If you want to remove an item from either end |
|
93 |
of the list, use removeFirst() or removeLast(). If you want to |
|
94 |
remove all occurrences of a given value in the list, use |
|
95 |
removeAll(). |
|
96 |
||
97 |
A common requirement is to remove the first or last item in the |
|
98 |
list and do something with it. For this, QLinkedList provides |
|
99 |
takeFirst() and takeLast(). Here's a loop that removes the items |
|
100 |
from a list one at a time and calls \c delete on them: |
|
101 |
\snippet doc/src/snippets/code/src_corelib_tools_qlinkedlist.cpp 2 |
|
102 |
||
103 |
QLinkedList's value type must be an \l {assignable data type}. This |
|
104 |
covers most data types that are commonly used, but the compiler |
|
105 |
won't let you, for example, store a QWidget as a value; instead, |
|
106 |
store a QWidget *. A few functions have additional requirements; |
|
107 |
for example, contains() and removeAll() expect the value type to |
|
108 |
support \c operator==(). These requirements are documented on a |
|
109 |
per-function basis. |
|
110 |
||
111 |
If you want to insert, modify, or remove items in the middle of |
|
112 |
the list, you must use an iterator. QLinkedList provides both |
|
113 |
\l{Java-style iterators} (QLinkedListIterator and |
|
114 |
QMutableLinkedListIterator) and \l{STL-style iterators} |
|
115 |
(QLinkedList::const_iterator and QLinkedList::iterator). See the |
|
116 |
documentation for these classes for details. |
|
117 |
||
118 |
\sa QLinkedListIterator, QMutableLinkedListIterator, QList, QVector |
|
119 |
*/ |
|
120 |
||
121 |
/*! \fn QLinkedList::QLinkedList() |
|
122 |
||
123 |
Constructs an empty list. |
|
124 |
*/ |
|
125 |
||
126 |
/*! \fn QLinkedList::QLinkedList(const QLinkedList<T> &other) |
|
127 |
||
128 |
Constructs a copy of \a other. |
|
129 |
||
130 |
This operation occurs in \l{constant time}, because QLinkedList |
|
131 |
is \l{implicitly shared}. This makes returning a QLinkedList from |
|
132 |
a function very fast. If a shared instance is modified, it will |
|
133 |
be copied (copy-on-write), and this takes \l{linear time}. |
|
134 |
||
135 |
\sa operator=() |
|
136 |
*/ |
|
137 |
||
138 |
/*! \fn QLinkedList::~QLinkedList() |
|
139 |
||
140 |
Destroys the list. References to the values in the list, and all |
|
141 |
iterators over this list, become invalid. |
|
142 |
*/ |
|
143 |
||
144 |
/*! \fn QLinkedList<T> &QLinkedList::operator=(const QLinkedList<T> &other) |
|
145 |
||
146 |
Assigns \a other to this list and returns a reference to this |
|
147 |
list. |
|
148 |
*/ |
|
149 |
||
150 |
/*! \fn bool QLinkedList::operator==(const QLinkedList<T> &other) const |
|
151 |
||
152 |
Returns true if \a other is equal to this list; otherwise returns |
|
153 |
false. |
|
154 |
||
155 |
Two lists are considered equal if they contain the same values in |
|
156 |
the same order. |
|
157 |
||
158 |
This function requires the value type to implement \c |
|
159 |
operator==(). |
|
160 |
||
161 |
\sa operator!=() |
|
162 |
*/ |
|
163 |
||
164 |
/*! \fn bool QLinkedList::operator!=(const QLinkedList<T> &other) const |
|
165 |
||
166 |
Returns true if \a other is not equal to this list; otherwise |
|
167 |
returns false. |
|
168 |
||
169 |
Two lists are considered equal if they contain the same values in |
|
170 |
the same order. |
|
171 |
||
172 |
This function requires the value type to implement \c |
|
173 |
operator==(). |
|
174 |
||
175 |
\sa operator==() |
|
176 |
*/ |
|
177 |
||
178 |
/*! \fn int QLinkedList::size() const |
|
179 |
||
180 |
Returns the number of items in the list. |
|
181 |
||
182 |
\sa isEmpty(), count() |
|
183 |
*/ |
|
184 |
||
185 |
/*! \fn void QLinkedList::detach() |
|
186 |
||
187 |
\internal |
|
188 |
*/ |
|
189 |
||
190 |
/*! \fn bool QLinkedList::isDetached() const |
|
191 |
||
192 |
\internal |
|
193 |
*/ |
|
194 |
||
195 |
/*! \fn void QLinkedList::setSharable(bool sharable) |
|
196 |
||
197 |
\internal |
|
198 |
*/ |
|
199 |
||
200 |
/*! \fn bool QLinkedList::isEmpty() const |
|
201 |
||
202 |
Returns true if the list contains no items; otherwise returns |
|
203 |
false. |
|
204 |
||
205 |
\sa size() |
|
206 |
*/ |
|
207 |
||
208 |
/*! \fn void QLinkedList::clear() |
|
209 |
||
210 |
Removes all the items in the list. |
|
211 |
||
212 |
\sa removeAll() |
|
213 |
*/ |
|
214 |
||
215 |
/*! \fn void QLinkedList::append(const T &value) |
|
216 |
||
217 |
Inserts \a value at the end of the list. |
|
218 |
||
219 |
Example: |
|
220 |
\snippet doc/src/snippets/code/src_corelib_tools_qlinkedlist.cpp 3 |
|
221 |
||
222 |
This is the same as list.insert(end(), \a value). |
|
223 |
||
224 |
\sa operator<<(), prepend(), insert() |
|
225 |
*/ |
|
226 |
||
227 |
/*! \fn void QLinkedList::prepend(const T &value) |
|
228 |
||
229 |
Inserts \a value at the beginning of the list. |
|
230 |
||
231 |
Example: |
|
232 |
\snippet doc/src/snippets/code/src_corelib_tools_qlinkedlist.cpp 4 |
|
233 |
||
234 |
This is the same as list.insert(begin(), \a value). |
|
235 |
||
236 |
\sa append(), insert() |
|
237 |
*/ |
|
238 |
||
239 |
/*! \fn int QLinkedList::removeAll(const T &value) |
|
240 |
||
241 |
Removes all occurrences of \a value in the list. |
|
242 |
||
243 |
Example: |
|
244 |
\snippet doc/src/snippets/code/src_corelib_tools_qlinkedlist.cpp 5 |
|
245 |
||
246 |
This function requires the value type to have an implementation of |
|
247 |
\c operator==(). |
|
248 |
||
249 |
\sa insert() |
|
250 |
*/ |
|
251 |
||
252 |
/*! |
|
253 |
\fn bool QLinkedList::removeOne(const T &value) |
|
254 |
\since 4.4 |
|
255 |
||
256 |
Removes the first occurrences of \a value in the list. Returns true on |
|
257 |
success; otherwise returns false. |
|
258 |
||
259 |
Example: |
|
260 |
\snippet doc/src/snippets/code/src_corelib_tools_qlinkedlist.cpp 6 |
|
261 |
||
262 |
This function requires the value type to have an implementation of |
|
263 |
\c operator==(). |
|
264 |
||
265 |
\sa insert() |
|
266 |
*/ |
|
267 |
||
268 |
/*! \fn bool QLinkedList::contains(const T &value) const |
|
269 |
||
270 |
Returns true if the list contains an occurrence of \a value; |
|
271 |
otherwise returns false. |
|
272 |
||
273 |
This function requires the value type to have an implementation of |
|
274 |
\c operator==(). |
|
275 |
||
276 |
\sa QLinkedListIterator::findNext(), QLinkedListIterator::findPrevious() |
|
277 |
*/ |
|
278 |
||
279 |
/*! \fn int QLinkedList::count(const T &value) const |
|
280 |
||
281 |
Returns the number of occurrences of \a value in the list. |
|
282 |
||
283 |
This function requires the value type to have an implementation of |
|
284 |
\c operator==(). |
|
285 |
||
286 |
\sa contains() |
|
287 |
*/ |
|
288 |
||
289 |
/*! \fn bool QLinkedList::startsWith(const T &value) const |
|
290 |
\since 4.5 |
|
291 |
||
292 |
Returns true if the list is not empty and its first |
|
293 |
item is equal to \a value; otherwise returns false. |
|
294 |
||
295 |
\sa isEmpty(), first() |
|
296 |
*/ |
|
297 |
||
298 |
/*! \fn bool QLinkedList::endsWith(const T &value) const |
|
299 |
\since 4.5 |
|
300 |
||
301 |
Returns true if the list is not empty and its last |
|
302 |
item is equal to \a value; otherwise returns false. |
|
303 |
||
304 |
\sa isEmpty(), last() |
|
305 |
*/ |
|
306 |
||
307 |
/*! \fn QLinkedList::iterator QLinkedList::begin() |
|
308 |
||
309 |
Returns an \l{STL-style iterator} pointing to the first item in |
|
310 |
the list. |
|
311 |
||
312 |
\sa constBegin(), end() |
|
313 |
*/ |
|
314 |
||
315 |
/*! \fn QLinkedList::const_iterator QLinkedList::begin() const |
|
316 |
||
317 |
\overload |
|
318 |
*/ |
|
319 |
||
320 |
/*! \fn QLinkedList::const_iterator QLinkedList::constBegin() const |
|
321 |
||
322 |
Returns a const \l{STL-style iterator} pointing to the first item |
|
323 |
in the list. |
|
324 |
||
325 |
\sa begin(), constEnd() |
|
326 |
*/ |
|
327 |
||
328 |
/*! \fn QLinkedList::iterator QLinkedList::end() |
|
329 |
||
330 |
Returns an \l{STL-style iterator} pointing to the imaginary item |
|
331 |
after the last item in the list. |
|
332 |
||
333 |
\sa begin(), constEnd() |
|
334 |
*/ |
|
335 |
||
336 |
/*! \fn QLinkedList::const_iterator QLinkedList::end() const |
|
337 |
||
338 |
\overload |
|
339 |
*/ |
|
340 |
||
341 |
/*! \fn QLinkedList::const_iterator QLinkedList::constEnd() const |
|
342 |
||
343 |
Returns a const \l{STL-style iterator} pointing to the imaginary |
|
344 |
item after the last item in the list. |
|
345 |
||
346 |
\sa constBegin(), end() |
|
347 |
*/ |
|
348 |
||
349 |
/*! \fn QLinkedList::iterator QLinkedList::insert(iterator before, const T &value) |
|
350 |
||
351 |
Inserts \a value in front of the item pointed to by the iterator |
|
352 |
\a before. Returns an iterator pointing at the inserted item. |
|
353 |
||
354 |
\sa erase() |
|
355 |
*/ |
|
356 |
||
357 |
/*! \fn QLinkedList::iterator QLinkedList::erase(iterator pos) |
|
358 |
||
359 |
Removes the item pointed to by the iterator \a pos from the list, |
|
360 |
and returns an iterator to the next item in the list (which may be |
|
361 |
end()). |
|
362 |
||
363 |
\sa insert() |
|
364 |
*/ |
|
365 |
||
366 |
/*! \fn QLinkedList::iterator QLinkedList::erase(iterator begin, iterator end) |
|
367 |
||
368 |
\overload |
|
369 |
||
370 |
Removes all the items from \a begin up to (but not including) \a |
|
371 |
end. |
|
372 |
*/ |
|
373 |
||
374 |
/*! \typedef QLinkedList::Iterator |
|
375 |
||
376 |
Qt-style synonym for QLinkedList::iterator. |
|
377 |
*/ |
|
378 |
||
379 |
/*! \typedef QLinkedList::ConstIterator |
|
380 |
||
381 |
Qt-style synonym for QLinkedList::const_iterator. |
|
382 |
*/ |
|
383 |
||
384 |
/*! |
|
385 |
\typedef QLinkedList::size_type |
|
386 |
||
387 |
Typedef for int. Provided for STL compatibility. |
|
388 |
*/ |
|
389 |
||
390 |
/*! |
|
391 |
\typedef QLinkedList::value_type |
|
392 |
||
393 |
Typedef for T. Provided for STL compatibility. |
|
394 |
*/ |
|
395 |
||
396 |
/*! |
|
397 |
\typedef QLinkedList::pointer |
|
398 |
||
399 |
Typedef for T *. Provided for STL compatibility. |
|
400 |
*/ |
|
401 |
||
402 |
/*! |
|
403 |
\typedef QLinkedList::const_pointer |
|
404 |
||
405 |
Typedef for const T *. Provided for STL compatibility. |
|
406 |
*/ |
|
407 |
||
408 |
/*! |
|
409 |
\typedef QLinkedList::reference |
|
410 |
||
411 |
Typedef for T &. Provided for STL compatibility. |
|
412 |
*/ |
|
413 |
||
414 |
/*! |
|
415 |
\typedef QLinkedList::const_reference |
|
416 |
||
417 |
Typedef for const T &. Provided for STL compatibility. |
|
418 |
*/ |
|
419 |
||
420 |
/*! |
|
421 |
\typedef QLinkedList::difference_type |
|
422 |
||
423 |
Typedef for ptrdiff_t. Provided for STL compatibility. |
|
424 |
*/ |
|
425 |
||
426 |
/*! \fn int QLinkedList::count() const |
|
427 |
||
428 |
Same as size(). |
|
429 |
*/ |
|
430 |
||
431 |
/*! \fn T& QLinkedList::first() |
|
432 |
||
433 |
Returns a reference to the first item in the list. This function |
|
434 |
assumes that the list isn't empty. |
|
435 |
||
436 |
\sa last(), isEmpty() |
|
437 |
*/ |
|
438 |
||
439 |
/*! \fn const T& QLinkedList::first() const |
|
440 |
||
441 |
\overload |
|
442 |
*/ |
|
443 |
||
444 |
/*! \fn T& QLinkedList::last() |
|
445 |
||
446 |
Returns a reference to the last item in the list. This function |
|
447 |
assumes that the list isn't empty. |
|
448 |
||
449 |
\sa first(), isEmpty() |
|
450 |
*/ |
|
451 |
||
452 |
/*! \fn const T& QLinkedList::last() const |
|
453 |
||
454 |
\overload |
|
455 |
*/ |
|
456 |
||
457 |
/*! \fn void QLinkedList::removeFirst() |
|
458 |
||
459 |
Removes the first item in the list. |
|
460 |
||
461 |
This is the same as erase(begin()). |
|
462 |
||
463 |
\sa removeLast(), erase() |
|
464 |
*/ |
|
465 |
||
466 |
/*! \fn void QLinkedList::removeLast() |
|
467 |
||
468 |
Removes the last item in the list. |
|
469 |
||
470 |
\sa removeFirst(), erase() |
|
471 |
*/ |
|
472 |
||
473 |
/*! \fn T QLinkedList::takeFirst() |
|
474 |
||
475 |
Removes the first item in the list and returns it. |
|
476 |
||
477 |
If you don't use the return value, removeFirst() is more |
|
478 |
efficient. |
|
479 |
||
480 |
\sa takeLast(), removeFirst() |
|
481 |
*/ |
|
482 |
||
483 |
/*! \fn T QLinkedList::takeLast() |
|
484 |
||
485 |
Removes the last item in the list and returns it. |
|
486 |
||
487 |
If you don't use the return value, removeLast() is more |
|
488 |
efficient. |
|
489 |
||
490 |
\sa takeFirst(), removeLast() |
|
491 |
*/ |
|
492 |
||
493 |
/*! \fn void QLinkedList::push_back(const T &value) |
|
494 |
||
495 |
This function is provided for STL compatibility. It is equivalent |
|
496 |
to append(\a value). |
|
497 |
*/ |
|
498 |
||
499 |
/*! \fn void QLinkedList::push_front(const T &value) |
|
500 |
||
501 |
This function is provided for STL compatibility. It is equivalent |
|
502 |
to prepend(\a value). |
|
503 |
*/ |
|
504 |
||
505 |
/*! \fn T& QLinkedList::front() |
|
506 |
||
507 |
This function is provided for STL compatibility. It is equivalent |
|
508 |
to first(). |
|
509 |
*/ |
|
510 |
||
511 |
/*! \fn const T& QLinkedList::front() const |
|
512 |
||
513 |
\overload |
|
514 |
*/ |
|
515 |
||
516 |
/*! \fn T& QLinkedList::back() |
|
517 |
||
518 |
This function is provided for STL compatibility. It is equivalent |
|
519 |
to last(). |
|
520 |
*/ |
|
521 |
||
522 |
/*! \fn const T& QLinkedList::back() const |
|
523 |
||
524 |
\overload |
|
525 |
*/ |
|
526 |
||
527 |
/*! \fn void QLinkedList::pop_front() |
|
528 |
||
529 |
This function is provided for STL compatibility. It is equivalent |
|
530 |
to removeFirst(). |
|
531 |
*/ |
|
532 |
||
533 |
/*! \fn void QLinkedList::pop_back() |
|
534 |
||
535 |
This function is provided for STL compatibility. It is equivalent |
|
536 |
to removeLast(). |
|
537 |
*/ |
|
538 |
||
539 |
/*! \fn bool QLinkedList::empty() const |
|
540 |
||
541 |
This function is provided for STL compatibility. It is equivalent |
|
542 |
to isEmpty() and returns true if the list is empty. |
|
543 |
*/ |
|
544 |
||
545 |
/*! \fn QLinkedList<T> &QLinkedList::operator+=(const QLinkedList<T> &other) |
|
546 |
||
547 |
Appends the items of the \a other list to this list and returns a |
|
548 |
reference to this list. |
|
549 |
||
550 |
\sa operator+(), append() |
|
551 |
*/ |
|
552 |
||
553 |
/*! \fn void QLinkedList::operator+=(const T &value) |
|
554 |
||
555 |
\overload |
|
556 |
||
557 |
Appends \a value to the list. |
|
558 |
*/ |
|
559 |
||
560 |
/*! \fn QLinkedList<T> QLinkedList::operator+(const QLinkedList<T> &other) const |
|
561 |
||
562 |
Returns a list that contains all the items in this list followed |
|
563 |
by all the items in the \a other list. |
|
564 |
||
565 |
\sa operator+=() |
|
566 |
*/ |
|
567 |
||
568 |
/*! \fn QLinkedList<T> &QLinkedList::operator<<(const QLinkedList<T> &other) |
|
569 |
||
570 |
Appends the items of the \a other list to this list and returns a |
|
571 |
reference to this list. |
|
572 |
||
573 |
\sa operator+=(), append() |
|
574 |
*/ |
|
575 |
||
576 |
/*! \fn QLinkedList<T> &QLinkedList::operator<<(const T &value) |
|
577 |
||
578 |
\overload |
|
579 |
||
580 |
Appends \a value to the list. |
|
581 |
*/ |
|
582 |
||
583 |
/*! \class QLinkedList::iterator |
|
584 |
\brief The QLinkedList::iterator class provides an STL-style non-const iterator for QLinkedList. |
|
585 |
||
586 |
QLinkedList features both \l{STL-style iterators} and |
|
587 |
\l{Java-style iterators}. The STL-style iterators are more |
|
588 |
low-level and more cumbersome to use; on the other hand, they are |
|
589 |
slightly faster and, for developers who already know STL, have |
|
590 |
the advantage of familiarity. |
|
591 |
||
592 |
QLinkedList\<T\>::iterator allows you to iterate over a |
|
593 |
QLinkedList\<T\> and to modify the list item associated with the |
|
594 |
iterator. If you want to iterate over a const QLinkedList, use |
|
595 |
QLinkedList::const_iterator instead. It is generally good |
|
596 |
practice to use QLinkedList::const_iterator on a non-const |
|
597 |
QLinkedList as well, unless you need to change the QLinkedList |
|
598 |
through the iterator. Const iterators are slightly faster, and |
|
599 |
can improve code readability. |
|
600 |
||
601 |
The default QLinkedList::iterator constructor creates an |
|
602 |
uninitialized iterator. You must initialize it using a |
|
603 |
function like QLinkedList::begin(), QLinkedList::end(), or |
|
604 |
QLinkedList::insert() before you can start iterating. Here's a |
|
605 |
typical loop that prints all the items stored in a list: |
|
606 |
||
607 |
\snippet doc/src/snippets/code/src_corelib_tools_qlinkedlist.cpp 7 |
|
608 |
||
609 |
STL-style iterators can be used as arguments to \l{generic |
|
610 |
algorithms}. For example, here's how to find an item in the list |
|
611 |
using the qFind() algorithm: |
|
612 |
||
613 |
\snippet doc/src/snippets/code/src_corelib_tools_qlinkedlist.cpp 8 |
|
614 |
||
615 |
Let's see a few examples of things we can do with a |
|
616 |
QLinkedList::iterator that we cannot do with a QLinkedList::const_iterator. |
|
617 |
Here's an example that increments every value stored in a |
|
618 |
QLinkedList\<int\> by 2: |
|
619 |
||
620 |
\snippet doc/src/snippets/code/src_corelib_tools_qlinkedlist.cpp 9 |
|
621 |
||
622 |
Here's an example that removes all the items that start with an |
|
623 |
underscore character in a QLinkedList\<QString\>: |
|
624 |
||
625 |
\snippet doc/src/snippets/code/src_corelib_tools_qlinkedlist.cpp 10 |
|
626 |
||
627 |
The call to QLinkedList::erase() removes the item pointed to by |
|
628 |
the iterator from the list, and returns an iterator to the next |
|
629 |
item. Here's another way of removing an item while iterating: |
|
630 |
||
631 |
\snippet doc/src/snippets/code/src_corelib_tools_qlinkedlist.cpp 11 |
|
632 |
||
633 |
It might be tempting to write code like this: |
|
634 |
||
635 |
\snippet doc/src/snippets/code/src_corelib_tools_qlinkedlist.cpp 12 |
|
636 |
||
637 |
However, this will potentially crash in \c{++i}, because \c i is |
|
638 |
a dangling iterator after the call to erase(). |
|
639 |
||
640 |
Multiple iterators can be used on the same list. If you add items |
|
641 |
to the list, existing iterators will remain valid. If you remove |
|
642 |
items from the list, iterators that point to the removed items |
|
643 |
will become dangling iterators. However, because of how \l{implicit |
|
644 |
sharing} works, you must not take a copy of a container while |
|
645 |
iterators are active on that container. |
|
646 |
||
647 |
\sa QLinkedList::const_iterator, QMutableLinkedListIterator |
|
648 |
*/ |
|
649 |
||
650 |
/*! \fn QLinkedList::iterator::iterator() |
|
651 |
||
652 |
Constructs an uninitialized iterator. |
|
653 |
||
654 |
Functions like operator*() and operator++() should not be called |
|
655 |
on an uninitialized iterator. Use operator=() to assign a value |
|
656 |
to it before using it. |
|
657 |
||
658 |
\sa QLinkedList::begin() QLinkedList::end() |
|
659 |
*/ |
|
660 |
||
661 |
/*! \fn QLinkedList::iterator::iterator(Node *node) |
|
662 |
||
663 |
\internal |
|
664 |
*/ |
|
665 |
||
666 |
/*! \typedef QLinkedList::iterator::iterator_category |
|
667 |
||
668 |
\internal |
|
669 |
*/ |
|
670 |
||
671 |
/*! \typedef QLinkedList::iterator::difference_type |
|
672 |
||
673 |
\internal |
|
674 |
*/ |
|
675 |
||
676 |
/*! \typedef QLinkedList::iterator::value_type |
|
677 |
||
678 |
\internal |
|
679 |
*/ |
|
680 |
||
681 |
/*! \typedef QLinkedList::iterator::pointer |
|
682 |
||
683 |
\internal |
|
684 |
*/ |
|
685 |
||
686 |
/*! \typedef QLinkedList::iterator::reference |
|
687 |
||
688 |
\internal |
|
689 |
*/ |
|
690 |
||
691 |
/*! \fn QLinkedList::iterator::iterator(const iterator &other) |
|
692 |
||
693 |
Constructs a copy of \a other. |
|
694 |
*/ |
|
695 |
||
696 |
/*! \fn QLinkedList::iterator &QLinkedList::iterator::operator=(const iterator &other) |
|
697 |
||
698 |
Assigns \a other to this iterator. |
|
699 |
*/ |
|
700 |
||
701 |
/*! \fn T &QLinkedList::iterator::operator*() const |
|
702 |
||
703 |
Returns a modifiable reference to the current item. |
|
704 |
||
705 |
You can change the value of an item by using operator*() on the |
|
706 |
left side of an assignment, for example: |
|
707 |
||
708 |
\snippet doc/src/snippets/code/src_corelib_tools_qlinkedlist.cpp 13 |
|
709 |
||
710 |
\sa operator->() |
|
711 |
*/ |
|
712 |
||
713 |
/*! \fn T *QLinkedList::iterator::operator->() const |
|
714 |
||
715 |
Returns a pointer to the current item. |
|
716 |
||
717 |
\sa operator*() |
|
718 |
*/ |
|
719 |
||
720 |
/*! |
|
721 |
\fn bool QLinkedList::iterator::operator==(const iterator &other) const |
|
722 |
\fn bool QLinkedList::iterator::operator==(const const_iterator &other) const |
|
723 |
||
724 |
Returns true if \a other points to the same item as this |
|
725 |
iterator; otherwise returns false. |
|
726 |
||
727 |
\sa operator!=() |
|
728 |
*/ |
|
729 |
||
730 |
/*! |
|
731 |
\fn bool QLinkedList::iterator::operator!=(const iterator &other) const |
|
732 |
\fn bool QLinkedList::iterator::operator!=(const const_iterator &other) const |
|
733 |
||
734 |
Returns true if \a other points to a different item than this |
|
735 |
iterator; otherwise returns false. |
|
736 |
||
737 |
\sa operator==() |
|
738 |
*/ |
|
739 |
||
740 |
/*! \fn QLinkedList::iterator &QLinkedList::iterator::operator++() |
|
741 |
||
742 |
The prefix ++ operator (\c{++it}) advances the iterator to the |
|
743 |
next item in the list and returns an iterator to the new current |
|
744 |
item. |
|
745 |
||
746 |
Calling this function on QLinkedList::end() leads to undefined |
|
747 |
results. |
|
748 |
||
749 |
\sa operator--() |
|
750 |
*/ |
|
751 |
||
752 |
/*! \fn QLinkedList::iterator QLinkedList::iterator::operator++(int) |
|
753 |
||
754 |
\overload |
|
755 |
||
756 |
The postfix ++ operator (\c{it++}) advances the iterator to the |
|
757 |
next item in the list and returns an iterator to the previously |
|
758 |
current item. |
|
759 |
*/ |
|
760 |
||
761 |
/*! \fn QLinkedList::iterator &QLinkedList::iterator::operator--() |
|
762 |
||
763 |
The prefix -- operator (\c{--it}) makes the preceding item |
|
764 |
current and returns an iterator to the new current item. |
|
765 |
||
766 |
Calling this function on QLinkedList::begin() leads to undefined |
|
767 |
results. |
|
768 |
||
769 |
\sa operator++() |
|
770 |
*/ |
|
771 |
||
772 |
/*! \fn QLinkedList::iterator QLinkedList::iterator::operator--(int) |
|
773 |
||
774 |
\overload |
|
775 |
||
776 |
The postfix -- operator (\c{it--}) makes the preceding item |
|
777 |
current and returns an iterator to the previously current item. |
|
778 |
*/ |
|
779 |
||
780 |
/*! \fn QLinkedList::iterator QLinkedList::iterator::operator+(int j) const |
|
781 |
||
782 |
Returns an iterator to the item at \a j positions forward from |
|
783 |
this iterator. (If \a j is negative, the iterator goes backward.) |
|
784 |
||
785 |
This operation can be slow for large \a j values. |
|
786 |
||
787 |
\sa operator-() |
|
788 |
||
789 |
*/ |
|
790 |
||
791 |
/*! \fn QLinkedList::iterator QLinkedList::iterator::operator-(int j) const |
|
792 |
||
793 |
Returns an iterator to the item at \a j positions backward from |
|
794 |
this iterator. (If \a j is negative, the iterator goes forward.) |
|
795 |
||
796 |
This operation can be slow for large \a j values. |
|
797 |
||
798 |
\sa operator+() |
|
799 |
*/ |
|
800 |
||
801 |
/*! \fn QLinkedList::iterator &QLinkedList::iterator::operator+=(int j) |
|
802 |
||
803 |
Advances the iterator by \a j items. (If \a j is negative, the |
|
804 |
iterator goes backward.) |
|
805 |
||
806 |
\sa operator-=(), operator+() |
|
807 |
*/ |
|
808 |
||
809 |
/*! \fn QLinkedList::iterator &QLinkedList::iterator::operator-=(int j) |
|
810 |
||
811 |
Makes the iterator go back by \a j items. (If \a j is negative, |
|
812 |
the iterator goes forward.) |
|
813 |
||
814 |
\sa operator+=(), operator-() |
|
815 |
*/ |
|
816 |
||
817 |
/*! \class QLinkedList::const_iterator |
|
818 |
\brief The QLinkedList::const_iterator class provides an STL-style const iterator for QLinkedList. |
|
819 |
||
820 |
QLinkedList features both \l{STL-style iterators} and |
|
821 |
\l{Java-style iterators}. The STL-style iterators are more |
|
822 |
low-level and more cumbersome to use; on the other hand, they are |
|
823 |
slightly faster and, for developers who already know STL, have |
|
824 |
the advantage of familiarity. |
|
825 |
||
826 |
QLinkedList\<T\>::const_iterator allows you to iterate over a |
|
827 |
QLinkedList\<T\>. If you want modify the QLinkedList as you iterate |
|
828 |
over it, you must use QLinkedList::const_iterator instead. It is |
|
829 |
generally good practice to use QLinkedList::const_iterator on a |
|
830 |
non-const QLinkedList as well, unless you need to change the |
|
831 |
QLinkedList through the iterator. Const iterators are slightly |
|
832 |
faster, and can improve code readability. |
|
833 |
||
834 |
The default QLinkedList::const_iterator constructor creates an |
|
835 |
uninitialized iterator. You must initialize it using a function |
|
836 |
like QLinkedList::constBegin(), QLinkedList::constEnd(), or |
|
837 |
QLinkedList::insert() before you can start iterating. Here's a |
|
838 |
typical loop that prints all the items stored in a list: |
|
839 |
||
840 |
\snippet doc/src/snippets/code/src_corelib_tools_qlinkedlist.cpp 14 |
|
841 |
||
842 |
STL-style iterators can be used as arguments to \l{generic |
|
843 |
algorithms}. For example, here's how to find an item in the list |
|
844 |
using the qFind() algorithm: |
|
845 |
||
846 |
\snippet doc/src/snippets/code/src_corelib_tools_qlinkedlist.cpp 15 |
|
847 |
||
848 |
Multiple iterators can be used on the same list. If you add items |
|
849 |
to the list, existing iterators will remain valid. If you remove |
|
850 |
items from the list, iterators that point to the removed items |
|
851 |
will become dangling iterators. |
|
852 |
||
853 |
\sa QLinkedList::iterator, QLinkedListIterator |
|
854 |
*/ |
|
855 |
||
856 |
/*! \fn QLinkedList::const_iterator::const_iterator() |
|
857 |
||
858 |
Constructs an uninitialized iterator. |
|
859 |
||
860 |
Functions like operator*() and operator++() should not be called |
|
861 |
on an uninitialized iterator. Use operator=() to assign a value |
|
862 |
to it before using it. |
|
863 |
||
864 |
\sa QLinkedList::constBegin() QLinkedList::constEnd() |
|
865 |
*/ |
|
866 |
||
867 |
/*! \fn QLinkedList::const_iterator::const_iterator(Node *node) |
|
868 |
||
869 |
\internal |
|
870 |
*/ |
|
871 |
||
872 |
/*! \typedef QLinkedList::const_iterator::iterator_category |
|
873 |
||
874 |
\internal |
|
875 |
*/ |
|
876 |
||
877 |
/*! \typedef QLinkedList::const_iterator::difference_type |
|
878 |
||
879 |
\internal |
|
880 |
*/ |
|
881 |
||
882 |
/*! \typedef QLinkedList::const_iterator::value_type |
|
883 |
||
884 |
\internal |
|
885 |
*/ |
|
886 |
||
887 |
/*! \typedef QLinkedList::const_iterator::pointer |
|
888 |
||
889 |
\internal |
|
890 |
*/ |
|
891 |
||
892 |
/*! \typedef QLinkedList::const_iterator::reference |
|
893 |
||
894 |
\internal |
|
895 |
*/ |
|
896 |
||
897 |
/*! \fn QLinkedList::const_iterator::const_iterator(const const_iterator &other) |
|
898 |
||
899 |
Constructs a copy of \a other. |
|
900 |
*/ |
|
901 |
||
902 |
/*! \fn QLinkedList::const_iterator::const_iterator(iterator other) |
|
903 |
||
904 |
Constructs a copy of \a other. |
|
905 |
*/ |
|
906 |
||
907 |
/*! \fn QLinkedList::const_iterator &QLinkedList::const_iterator::operator=( \ |
|
908 |
const const_iterator &other) |
|
909 |
||
910 |
Assigns \a other to this iterator. |
|
911 |
*/ |
|
912 |
||
913 |
/*! \fn const T &QLinkedList::const_iterator::operator*() const |
|
914 |
||
915 |
Returns a reference to the current item. |
|
916 |
||
917 |
\sa operator->() |
|
918 |
*/ |
|
919 |
||
920 |
/*! \fn const T *QLinkedList::const_iterator::operator->() const |
|
921 |
||
922 |
Returns a pointer to the current item. |
|
923 |
||
924 |
\sa operator*() |
|
925 |
*/ |
|
926 |
||
927 |
/*! \fn bool QLinkedList::const_iterator::operator==(const const_iterator &other) const |
|
928 |
||
929 |
Returns true if \a other points to the same item as this |
|
930 |
iterator; otherwise returns false. |
|
931 |
||
932 |
\sa operator!=() |
|
933 |
*/ |
|
934 |
||
935 |
/*! \fn bool QLinkedList::const_iterator::operator!=(const const_iterator &other) const |
|
936 |
||
937 |
Returns true if \a other points to a different item than this |
|
938 |
iterator; otherwise returns false. |
|
939 |
||
940 |
\sa operator==() |
|
941 |
*/ |
|
942 |
||
943 |
/*! \fn QLinkedList::const_iterator &QLinkedList::const_iterator::operator++() |
|
944 |
||
945 |
The prefix ++ operator (\c{++it}) advances the iterator to the |
|
946 |
next item in the list and returns an iterator to the new current |
|
947 |
item. |
|
948 |
||
949 |
Calling this function on QLinkedList::constEnd() leads to |
|
950 |
undefined results. |
|
951 |
||
952 |
\sa operator--() |
|
953 |
*/ |
|
954 |
||
955 |
/*! \fn QLinkedList::const_iterator QLinkedList::const_iterator::operator++(int) |
|
956 |
||
957 |
\overload |
|
958 |
||
959 |
The postfix ++ operator (\c{it++}) advances the iterator to the |
|
960 |
next item in the list and returns an iterator to the previously |
|
961 |
current item. |
|
962 |
*/ |
|
963 |
||
964 |
/*! \fn QLinkedList::const_iterator &QLinkedList::const_iterator::operator--() |
|
965 |
||
966 |
The prefix -- operator (\c{--it}) makes the preceding item |
|
967 |
current and returns an iterator to the new current item. |
|
968 |
||
969 |
Calling this function on QLinkedList::begin() leads to undefined |
|
970 |
results. |
|
971 |
||
972 |
\sa operator++() |
|
973 |
*/ |
|
974 |
||
975 |
/*! \fn QLinkedList::const_iterator QLinkedList::const_iterator::operator--(int) |
|
976 |
||
977 |
\overload |
|
978 |
||
979 |
The postfix -- operator (\c{it--}) makes the preceding item |
|
980 |
current and returns an iterator to the previously current item. |
|
981 |
*/ |
|
982 |
||
983 |
/*! \fn QLinkedList::const_iterator QLinkedList::const_iterator::operator+(int j) const |
|
984 |
||
985 |
Returns an iterator to the item at \a j positions forward from |
|
986 |
this iterator. (If \a j is negative, the iterator goes backward.) |
|
987 |
||
988 |
This operation can be slow for large \a j values. |
|
989 |
||
990 |
\sa operator-() |
|
991 |
*/ |
|
992 |
||
993 |
/*! \fn QLinkedList::const_iterator QLinkedList::const_iterator::operator-(int j) const |
|
994 |
||
995 |
This function returns an iterator to the item at \a j positions backward from |
|
996 |
this iterator. (If \a j is negative, the iterator goes forward.) |
|
997 |
||
998 |
This operation can be slow for large \a j values. |
|
999 |
||
1000 |
\sa operator+() |
|
1001 |
*/ |
|
1002 |
||
1003 |
/*! \fn QLinkedList::const_iterator &QLinkedList::const_iterator::operator+=(int j) |
|
1004 |
||
1005 |
Advances the iterator by \a j items. (If \a j is negative, the |
|
1006 |
iterator goes backward.) |
|
1007 |
||
1008 |
This operation can be slow for large \a j values. |
|
1009 |
||
1010 |
\sa operator-=(), operator+() |
|
1011 |
*/ |
|
1012 |
||
1013 |
/*! \fn QLinkedList::const_iterator &QLinkedList::const_iterator::operator-=(int j) |
|
1014 |
||
1015 |
Makes the iterator go back by \a j items. (If \a j is negative, |
|
1016 |
the iterator goes forward.) |
|
1017 |
||
1018 |
This operation can be slow for large \a j values. |
|
1019 |
||
1020 |
\sa operator+=(), operator-() |
|
1021 |
*/ |
|
1022 |
||
1023 |
/*! \fn QDataStream &operator<<(QDataStream &out, const QLinkedList<T> &list) |
|
1024 |
\relates QLinkedList |
|
1025 |
||
1026 |
Writes the linked list \a list to stream \a out. |
|
1027 |
||
1028 |
This function requires the value type to implement \c |
|
1029 |
operator<<(). |
|
1030 |
||
1031 |
\sa \link datastreamformat.html Format of the QDataStream operators \endlink |
|
1032 |
*/ |
|
1033 |
||
1034 |
/*! \fn QDataStream &operator>>(QDataStream &in, QLinkedList<T> &list) |
|
1035 |
\relates QLinkedList |
|
1036 |
||
1037 |
Reads a linked list from stream \a in into \a list. |
|
1038 |
||
1039 |
This function requires the value type to implement \c operator>>(). |
|
1040 |
||
1041 |
\sa \link datastreamformat.html Format of the QDataStream operators \endlink |
|
1042 |
*/ |
|
1043 |
||
1044 |
/*! |
|
1045 |
\fn iterator QLinkedList::remove(iterator pos) |
|
1046 |
||
1047 |
Use erase() instead. |
|
1048 |
*/ |
|
1049 |
||
1050 |
/*! |
|
1051 |
\fn int QLinkedList::findIndex(const T& t) const |
|
1052 |
||
1053 |
If you need indexes then QList or QVector are better choices than |
|
1054 |
QLinkedList. |
|
1055 |
||
1056 |
\oldcode |
|
1057 |
int index = list->findIndex(value); |
|
1058 |
\newcode |
|
1059 |
int index = 0; |
|
1060 |
bool found = false; |
|
1061 |
for (const_iterator i = list->begin(); i != list->end(); ++i; ++index) |
|
1062 |
if (*i == value) { |
|
1063 |
found = true; |
|
1064 |
break; |
|
1065 |
} |
|
1066 |
if (!found) |
|
1067 |
index = -1; |
|
1068 |
\endcode |
|
1069 |
*/ |
|
1070 |
||
1071 |
/*! |
|
1072 |
\fn iterator QLinkedList::find(iterator from, const T& t) |
|
1073 |
||
1074 |
If you need random access to a data structure then QList, QVector, |
|
1075 |
QMap, or QHash, are all better choices than QLinkedList. |
|
1076 |
||
1077 |
\oldcode |
|
1078 |
QLinkedList::iterator i = list->find(from, value); |
|
1079 |
\newcode |
|
1080 |
QLinkedList::iterator i = from; |
|
1081 |
while (i != list->end() && *i != value) |
|
1082 |
++i; |
|
1083 |
\endcode |
|
1084 |
*/ |
|
1085 |
||
1086 |
/*! |
|
1087 |
\fn iterator QLinkedList::find(const T& t) |
|
1088 |
||
1089 |
If you need random access to a data structure then QList, QVector, |
|
1090 |
QMap, or QHash, are all better choices than QLinkedList. |
|
1091 |
||
1092 |
\oldcode |
|
1093 |
QLinkedList::iterator i = list->find(value); |
|
1094 |
\newcode |
|
1095 |
QLinkedList::iterator i = list->begin(); |
|
1096 |
while (i != list->end() && *i != value) |
|
1097 |
++i; |
|
1098 |
\endcode |
|
1099 |
*/ |
|
1100 |
||
1101 |
/*! |
|
1102 |
\fn const_iterator QLinkedList::find(const_iterator from, const T& t) const |
|
1103 |
||
1104 |
If you need random access to a data structure then QList, QVector, |
|
1105 |
QMap, or QHash, are all better choices than QLinkedList. |
|
1106 |
||
1107 |
\oldcode |
|
1108 |
QLinkedList::const_iterator i = list->find(from, value); |
|
1109 |
\newcode |
|
1110 |
QLinkedList::const_iterator i = from; |
|
1111 |
while (i != list->end() && *i != value) |
|
1112 |
++i; |
|
1113 |
\endcode |
|
1114 |
*/ |
|
1115 |
||
1116 |
/*! |
|
1117 |
\fn const_iterator QLinkedList::find(const T& t) const |
|
1118 |
||
1119 |
If you need random access to a data structure then QList, QVector, |
|
1120 |
QMap, or QHash, are all better choices than QLinkedList. |
|
1121 |
||
1122 |
\oldcode |
|
1123 |
QLinkedList::const_iterator i = list->find(value); |
|
1124 |
\newcode |
|
1125 |
QLinkedList::const_iterator i = list->begin(); |
|
1126 |
while (i != list->end() && *i != value) |
|
1127 |
++i; |
|
1128 |
\endcode |
|
1129 |
*/ |
|
1130 |
||
1131 |
/*! |
|
1132 |
\since 4.1 |
|
1133 |
\fn QLinkedList<T> QLinkedList<T>::fromStdList(const std::list<T> &list) |
|
1134 |
||
1135 |
Returns a QLinkedList object with the data contained in \a list. |
|
1136 |
The order of the elements in the QLinkedList is the same as in \a |
|
1137 |
list. |
|
1138 |
||
1139 |
Example: |
|
1140 |
||
1141 |
\snippet doc/src/snippets/code/src_corelib_tools_qlinkedlist.cpp 16 |
|
1142 |
||
1143 |
\sa toStdList() |
|
1144 |
*/ |
|
1145 |
||
1146 |
/*! |
|
1147 |
\since 4.1 |
|
1148 |
\fn std::list<T> QLinkedList<T>::toStdList() const |
|
1149 |
||
1150 |
Returns a std::list object with the data contained in this |
|
1151 |
QLinkedList. Example: |
|
1152 |
||
1153 |
\snippet doc/src/snippets/code/src_corelib_tools_qlinkedlist.cpp 17 |
|
1154 |
||
1155 |
\sa fromStdList() |
|
1156 |
*/ |
|
1157 |
||
1158 |
QT_END_NAMESPACE |