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