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