source: trunk/doc/src/q3valuelist.qdoc@ 348

Last change on this file since 348 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 18.6 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the documentation of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/*!
43 \class Q3ValueList
44 \brief The Q3ValueList class is a value-based template class that
45 provides lists.
46 \compat
47
48 Q3ValueList is a Qt implementation of an STL-like list container.
49 It can be used in your application if the standard \c list is not
50 available for your target platforms.
51
52 Q3ValueList\<T\> defines a template instance to create a list of
53 values that all have the class T. Note that Q3ValueList does not
54 store pointers to the members of the list; it holds a copy of
55 every member. This is why these kinds of classes are called "value
56 based"; Q3PtrList and Q3Dict are "pointer based".
57
58 Q3ValueList contains and manages a collection of objects of type T
59 and provides iterators that allow the contained objects to be
60 addressed. Q3ValueList owns the contained items. For more relaxed
61 ownership semantics, see Q3PtrCollection and friends which are
62 pointer-based containers.
63
64 Some classes cannot be used within a Q3ValueList, for example, all
65 classes derived from QObject and thus all classes that implement
66 widgets. Only values can be used in a Q3ValueList. To qualify as a
67 value the class must provide:
68 \list
69 \i a copy constructor;
70 \i an assignment operator;
71 \i a default constructor, i.e. a constructor that does not take any arguments.
72 \endlist
73
74 Note that C++ defaults to field-by-field assignment operators and
75 copy constructors if no explicit version is supplied. In many
76 cases this is sufficient.
77
78 In addition, some compilers (e.g. Sun CC) might require that the
79 class provides an equality operator (operator==()).
80
81 Q3ValueList's function naming is consistent with the other Qt
82 classes (e.g. count(), isEmpty()). Q3ValueList also provides extra
83 functions for compatibility with STL algorithms, such as size()
84 and empty(). Programmers already familiar with the STL \c list may
85 prefer to use the STL-compatible functions.
86
87 Example:
88 \snippet doc/src/snippets/code/doc_src_q3valuelist.qdoc 0
89
90
91 Notice that the latest changes to Mary's salary did not affect the
92 value in the list because the list created a copy of Mary's entry.
93
94 There are several ways to find items in the list. The begin() and
95 end() functions return iterators to the beginning and end of the
96 list. The advantage of getting an iterator is that you can move
97 forward or backward from this position by
98 incrementing/decrementing the iterator. The iterator returned by
99 end() points to the item which is one \e past the last item in the
100 container. The past-the-end iterator is still associated with the
101 list it belongs to, however it is \e not dereferenceable;
102 operator*() will not return a well-defined value. If the list is
103 empty(), the iterator returned by begin() will equal the iterator
104 returned by end().
105
106 It is safe to have multiple iterators a the list at the same
107 time. If some member of the list is removed, only iterators
108 pointing to the removed member become invalid. Inserting into the
109 list does not invalidate any iterator. For convenience, the
110 function last() returns a reference to the last item in the list,
111 and first() returns a reference to the the first item. If the
112 list is empty(), both last() and first() have undefined behavior
113 (your application will crash or do unpredictable things). Use
114 last() and first() with caution, for example:
115
116 \snippet doc/src/snippets/code/doc_src_q3valuelist.qdoc 1
117
118 Because Q3ValueList is value-based there is no need to be careful
119 about deleting items in the list. The list holds its own copies
120 and will free them if the corresponding member or the list itself
121 is deleted. You can force the list to free all of its items with
122 clear().
123
124 Q3ValueList is shared implicitly, which means it can be copied in
125 constant time, i.e. O(1). If multiple Q3ValueList instances share
126 the same data and one needs to modify its contents, this modifying
127 instance makes a copy and modifies its private copy; therefore it
128 does not affect the other instances; this takes O(n) time. This is
129 often called "copy on write". If a Q3ValueList is being used in a
130 multi-threaded program, you must protect all access to the list.
131 See \l QMutex.
132
133 There are several ways to insert items into the list. The
134 prepend() and append() functions insert items at the beginning and
135 the end of the list respectively. The insert() function comes in
136 several flavors and can be used to add one or more items at
137 specific positions within the list.
138
139 Items can also be removed from the list in several ways. There
140 are several variants of the remove() function, which removes a
141 specific item from the list. The remove() function will find and
142 remove items according to a specific item value.
143
144 \sa Q3ValueListIterator
145*/
146
147/*! \typedef Q3ValueList::iterator
148 The list's iterator type, Q3ValueListIterator. */
149/*! \typedef Q3ValueList::const_iterator
150 The list's const iterator type, Q3ValueListConstIterator. */
151/*! \typedef Q3ValueList::value_type
152 The type of the object stored in the list, T. */
153/*! \typedef Q3ValueList::pointer
154 The pointer to T type. */
155/*! \typedef Q3ValueList::const_pointer
156 The const pointer to T type. */
157/*! \typedef Q3ValueList::reference
158 The reference to T type. */
159/*! \typedef Q3ValueList::const_reference
160 The const reference to T type. */
161/*! \typedef Q3ValueList::size_type
162 An unsigned integral type, used to represent various sizes. */
163/*! \typedef Q3ValueList::difference_type
164 \internal
165*/
166/*!
167 \fn Q3ValueList::Q3ValueList()
168
169 Constructs an empty list.
170*/
171
172/*!
173 \fn Q3ValueList::Q3ValueList( const Q3ValueList<T>& l )
174 \fn Q3ValueList::Q3ValueList( const QList<T>& l )
175 \fn Q3ValueList::Q3ValueList( const QLinkedList<T>& l )
176
177 Constructs a copy of \a l.
178*/
179
180/*!
181 \fn Q3ValueList::Q3ValueList( const std::list<T>& l )
182
183 Contructs a copy of \a l.
184
185 This constructor is provided for compatibility with STL
186 containers.
187*/
188
189/*!
190 \fn Q3ValueList::~Q3ValueList()
191
192 Destroys the list. References to the values in the list and all
193 iterators of this list become invalidated. Note that it is
194 impossible for an iterator to check whether or not it is valid:
195 Q3ValueList is highly tuned for performance, not for error
196 checking.
197*/
198
199/*!
200 \fn bool Q3ValueList::operator== ( const Q3ValueList<T>& l ) const
201
202 Compares both lists.
203
204 Returns TRUE if this list and \a l are equal; otherwise returns
205 FALSE.
206*/
207
208/*!
209 \fn bool Q3ValueList::operator== ( const std::list<T>& l ) const
210
211 \overload
212
213 Returns TRUE if this list and \a l are equal; otherwise returns
214 FALSE.
215
216 This operator is provided for compatibility with STL containers.
217*/
218
219/*!
220 \fn Q3ValueList<T>& Q3ValueList::operator= ( const Q3ValueList<T>& l )
221
222 Assigns \a l to this list and returns a reference to this list.
223
224 All iterators of the current list become invalidated by this
225 operation. The cost of such an assignment is O(1) since Q3ValueList
226 is implicitly shared.
227*/
228
229/*!
230 \fn Q3ValueList<T>& Q3ValueList::operator= ( const QList<T>& l )
231
232 Assigns \a l to this list and returns a reference to this list.
233
234 All iterators of the current list become invalidated by this
235 operation.
236*/
237
238/*!
239 \fn Q3ValueList<T>& Q3ValueList::operator= ( const std::list<T>& l )
240
241 \overload
242
243 Assigns the contents of \a l to the list.
244
245 All iterators of the current list become invalidated by this
246 operation.
247*/
248
249/*!
250 \fn bool Q3ValueList::operator!= ( const Q3ValueList<T>& l ) const
251
252 Compares both lists.
253
254 Returns TRUE if this list and \a l are unequal; otherwise returns
255 FALSE.
256*/
257
258/*!
259 \fn iterator Q3ValueList::insert( typename Q3ValueList<T>::Iterator it, const T& x )
260
261 Inserts the value \a x in front of the item pointed to by the
262 iterator, \a it.
263
264 Returns an iterator pointing at the inserted item.
265
266 \sa append(), prepend()
267*/
268
269/*!
270 \fn uint Q3ValueList::remove( const T& x )
271
272 \overload
273
274 Removes all items that have value \a x and returns the number of
275 removed items.
276*/
277
278/*!
279 \fn QDataStream& operator>>( QDataStream& s, Q3ValueList<T>& l )
280
281 \relates Q3ValueList
282
283 Reads a list, \a l, from the stream \a s. The type T stored in the
284 list must implement the streaming operator.
285*/
286
287/*!
288 \fn QDataStream& operator<<( QDataStream& s, const Q3ValueList<T>& l )
289
290 \overload
291 \relates Q3ValueList
292
293 Writes a list, \a l, to the stream \a s. The type T stored in the
294 list must implement the streaming operator.
295*/
296
297/*!
298 \fn void Q3ValueList::insert( typename Q3ValueList<T>::Iterator pos,
299 typename Q3ValueList<T>::size_type n, const T& x )
300
301 \overload
302
303 Inserts \a n copies of \a x before position \a pos.
304*/
305
306/*!
307 \fn Q3ValueList<T>& Q3ValueList::operator<< ( const T& x )
308
309 Adds the value \a x to the end of the list.
310
311 Returns a reference to the list.
312*/
313
314/*!
315 \fn const T& Q3ValueList::operator[] ( typename Q3ValueList<T>::size_type i ) const
316
317 Returns a const reference to the item with index \a i in the list.
318 It is up to you to check whether this item really exists. You can
319 do that easily with the count() function. However this operator
320 does not check whether \a i is in range and will deliver undefined
321 results if it does not exist.
322
323 \warning This function uses a linear search and can be extremely
324 slow for large lists. Q3ValueList is not optimized for random item
325 access. If you need random access use a different container, such
326 as Q3ValueVector.
327*/
328
329/*!
330 \fn T& Q3ValueList::operator[] ( typename Q3ValueList<T>::size_type i )
331
332 \overload
333
334 Returns a non-const reference to the item with index \a i.
335*/
336
337/*!
338 \fn const_iterator Q3ValueList::at( typename Q3ValueList<T>::size_type i ) const
339
340 Returns an iterator pointing to the item at position \a i in the
341 list, or an undefined value if the index is out of range.
342
343 \warning This function uses a linear search and can be extremely
344 slow for large lists. Q3ValueList is not optimized for random item
345 access. If you need random access use a different container, such
346 as Q3ValueVector.
347*/
348
349/*!
350 \fn iterator Q3ValueList::at( typename Q3ValueList<T>::size_type i )
351
352 \overload
353
354 Returns an iterator pointing to the item at position \a i in the
355 list, or an undefined value if the index is out of range.
356
357*/
358
359/*!
360 \fn iterator Q3ValueList::fromLast()
361
362 \overload
363
364 Returns an iterator to the last item in the list, or end() if
365 there is no last item.
366
367 Use the end() function instead. For example:
368
369 \snippet doc/src/snippets/code/doc_src_q3valuelist.qdoc 2
370
371*/
372
373/*!
374 \fn const_iterator Q3ValueList::fromLast() const
375
376 Returns an iterator to the last item in the list, or end() if
377 there is no last item.
378
379 Use the end() function instead. For example:
380
381 \snippet doc/src/snippets/code/doc_src_q3valuelist.qdoc 3
382
383*/
384
385/*!
386 \fn Q3ValueList<T> Q3ValueList::operator+( const Q3ValueList<T>& l ) const
387
388 Creates a new list and fills it with the items of this list. Then
389 the items of \a l are appended. Returns the new list.
390*/
391
392/*!
393 \fn Q3ValueList<T>& Q3ValueList::operator+= ( const Q3ValueList<T>& l )
394
395 Appends the items of \a l to this list. Returns a reference to
396 this list.
397*/
398
399/*!
400 \fn Q3ValueList<T>& Q3ValueList::operator+= ( const T& x )
401
402 \overload
403
404 Appends the value \a x to the list. Returns a reference to the
405 list.
406*/
407
408/*!
409 \fn iterator Q3ValueList::append( const T& x )
410
411 Inserts \a x at the end of the list.
412
413 \sa insert(), prepend()
414*/
415
416/*!
417 \fn iterator Q3ValueList::prepend( const T& x )
418
419 Inserts \a x at the beginning of the list.
420
421 \sa insert(), append()
422*/
423
424/*!
425 \fn iterator Q3ValueList::remove( typename Q3ValueList<T>::Iterator it )
426
427 Removes the item pointed to by \a it from the list. No iterators
428 other than \a it or other iterators pointing at the same item as
429 \a it are invalidated. Returns an iterator to the next item after
430 \a it, or end() if there is no such item.
431
432 \sa clear()
433*/
434
435/*!
436 \fn uint Q3ValueList::contains( const T& x ) const
437
438 Returns the number of occurrences of the value \a x in the list.
439*/
440
441/*!
442 \class Q3ValueListIterator
443 \brief The Q3ValueListIterator class provides an iterator for Q3ValueList.
444 \compat
445
446 An iterator is a class for accessing the items of a container
447 class: a generalization of the index in an array. A pointer
448 into a "const char *" and an index into an "int[]" are both
449 iterators, and the general idea is to provide that functionality
450 for any data structure.
451
452 The Q3ValueListIterator class is an iterator for Q3ValueList
453 instantiations. You can create the appropriate iterator type by
454 using the \c iterator typedef provided by Q3ValueList.
455
456 The only way to access the items in a Q3ValueList is to use an
457 iterator.
458
459 Example (see Q3ValueList for the complete code):
460 \snippet doc/src/snippets/code/doc_src_q3valuelist.qdoc 4
461
462 Q3ValueList is highly optimized for performance and memory usage.
463 This means that you must be careful: Q3ValueList does not know
464 about all its iterators and the iterators don't know to which list
465 they belong. This makes things very fast, but if you're not
466 careful, you can get spectacular bugs. Always make sure iterators
467 are valid before dereferencing them or using them as parameters to
468 generic algorithms in the STL.
469
470 Using an invalid iterator is undefined (your application will
471 probably crash). Many Qt functions return const value lists; to
472 iterate over these you should make a copy and iterate over the
473 copy.
474
475 For every Iterator there is a ConstIterator. When accessing a
476 Q3ValueList in a const environment or if the reference or pointer
477 to the list is itself const, then you must use the ConstIterator.
478 Its semantics are the same as the Iterator, but it only returns
479 const references.
480
481 \sa Q3ValueList, Q3ValueListConstIterator
482*/
483
484/*!
485 \fn Q3ValueListIterator::Q3ValueListIterator()
486
487 Constructs an unitialized iterator.
488*/
489
490/*!
491 \fn Q3ValueListIterator::Q3ValueListIterator(const Q3ValueListIterator &o)
492 \fn Q3ValueListIterator::Q3ValueListIterator(const typename QLinkedList<T>::iterator &o)
493
494 Constucts a copy of iterator \a o.
495*/
496
497/*!
498 \class Q3ValueListConstIterator
499 \brief The Q3ValueListConstIterator class provides a const iterator
500 for Q3ValueList.
501 \compat
502
503 In contrast to Q3ValueListIterator, this class is used to iterate
504 over a const list. It does not allow modification of the values of
505 the list since that would break const semantics.
506
507 You can create the appropriate const iterator type by using the \c
508 const_iterator typedef provided by Q3ValueList.
509
510 For more information on Q3ValueList iterators, see
511 Q3ValueListIterator.
512
513 \sa Q3ValueListIterator, Q3ValueList
514*/
515
516/*!
517 \fn Q3ValueListConstIterator::Q3ValueListConstIterator()
518
519 Constructs an unitialized iterator.
520*/
521
522/*!
523 \fn Q3ValueListConstIterator::Q3ValueListConstIterator(const Q3ValueListConstIterator &o)
524 \fn Q3ValueListConstIterator::Q3ValueListConstIterator(const typename QLinkedList<T>::const_iterator &o)
525 \fn Q3ValueListConstIterator::Q3ValueListConstIterator(const typename QLinkedList<T>::iterator &o)
526
527 Constructs a copy of iterator \a o.
528*/
529
530/*!
531 \typedef Q3ValueList::Iterator
532
533 This iterator is an instantiation of Q3ValueListIterator for the
534 same type as this Q3ValueList. In other words, if you instantiate
535 Q3ValueList<int>, Iterator is a Q3ValueListIterator<int>. Several
536 member function use it, such as Q3ValueList::begin(), which returns
537 an iterator pointing to the first item in the list.
538
539 Functionally, this is almost the same as ConstIterator. The only
540 difference is that you cannot use ConstIterator for non-const
541 operations, and that the compiler can often generate better code
542 if you use ConstIterator.
543
544 \sa Q3ValueListIterator ConstIterator
545*/
546
547/*!
548 \typedef Q3ValueList::ConstIterator
549
550 This iterator is an instantiation of Q3ValueListConstIterator for
551 the same type as this Q3ValueList. In other words, if you
552 instantiate Q3ValueList<int>, ConstIterator is a
553 Q3ValueListConstIterator<int>. Several member function use it, such
554 as Q3ValueList::begin(), which returns an iterator pointing to the
555 first item in the list.
556
557 Functionally, this is almost the same as Iterator. The only
558 difference is you cannot use ConstIterator for non-const
559 operations, and that the compiler can often generate better code
560 if you use ConstIterator.
561
562 \sa Q3ValueListIterator Iterator
563*/
564
565/*!
566 \fn Q3ValueList::operator QList<T>() const
567
568 Automatically converts a Q3ValueList<T> into a QList<T>.
569*/
Note: See TracBrowser for help on using the repository browser.