1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the documentation of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:FDL$
9 ** GNU Free Documentation License
10 ** Alternatively, this file may be used under the terms of the GNU Free
11 ** Documentation License version 1.3 as published by the Free Software
12 ** Foundation and appearing in the file included in the packaging of
16 ** Alternatively, this file may be used in accordance with the terms
17 ** and conditions contained in a signed written agreement between you
26 ****************************************************************************/
29 \class QVarLengthArray
30 \brief The QVarLengthArray class provides a low-level variable-length array.
35 The C++ language doesn't support variable-length arrays on the stack.
36 For example, the following code won't compile:
38 \snippet doc/src/snippets/code/doc_src_qvarlengtharray.cpp 0
40 The alternative is to allocate the array on the heap (with
43 \snippet doc/src/snippets/code/doc_src_qvarlengtharray.cpp 1
45 However, if myfunc() is called very frequently from the
46 application's inner loop, heap allocation can be a major source
49 QVarLengthArray is an attempt to work around this gap in the C++
50 language. It allocates a certain number of elements on the stack,
51 and if you resize the array to a larger size, it automatically
52 uses the heap instead. Stack allocation has the advantage that
53 it is much faster than heap allocation.
56 \snippet doc/src/snippets/code/doc_src_qvarlengtharray.cpp 2
58 In the example above, QVarLengthArray will preallocate 1024
59 elements on the stack and use them unless \c{n + 1} is greater
60 than 1024. If you omit the second template argument,
61 QVarLengthArray's default of 256 is used.
63 QVarLengthArray's value type must be an \l{assignable data type}.
64 This covers most data types that are commonly used, but the
65 compiler won't let you, for example, store a QWidget as a value;
66 instead, store a QWidget *.
68 QVarLengthArray, like QVector, provides a resizable array data
69 structure. The main differences between the two classes are:
72 \li QVarLengthArray's API is much more low-level. It provides no
73 iterators and lacks much of QVector's functionality.
75 \li QVarLengthArray doesn't initialize the memory if the value is
76 a basic type. (QVector always does.)
78 \li QVector uses \l{implicit sharing} as a memory optimization.
79 QVarLengthArray doesn't provide that feature; however, it
80 usually produces slightly better performance due to reduced
81 overhead, especially in tight loops.
84 In summary, QVarLengthArray is a low-level optimization class
85 that only makes sense in very specific cases. It is used a few
86 places inside Qt and was added to Qt's public API for the
87 convenience of advanced users.
89 \sa QVector, QList, QLinkedList
92 /*! \fn QVarLengthArray::QVarLengthArray(int size)
94 Constructs an array with an initial size of \a size elements.
96 If the value type is a primitive type (e.g., char, int, float) or
97 a pointer type (e.g., QWidget *), the elements are not
98 initialized. For other types, the elements are initialized with a
99 \l{default-constructed value}.
102 /*! \fn QVarLengthArray::~QVarLengthArray()
107 /*! \fn int QVarLengthArray::size() const
109 Returns the number of elements in the array.
111 \sa isEmpty(), resize()
114 /*! \fn int QVarLengthArray::count() const
118 \sa isEmpty(), resize()
121 /*! \fn int QVarLengthArray::length() const
125 \sa isEmpty(), resize()
128 /*! \fn T& QVarLengthArray::first()
130 Returns a reference to the first item in the array. The array must
131 not be empty. If the array can be empty, check isEmpty() before
132 calling this function.
134 \sa last(), isEmpty()
137 /*! \fn const T& QVarLengthArray::first() const
142 /*! \fn T& QVarLengthArray::last()
144 Returns a reference to the last item in the array. The array must
145 not be empty. If the array can be empty, check isEmpty() before
146 calling this function.
148 \sa first(), isEmpty()
151 /*! \fn const T& QVarLengthArray::last() const
158 /*! \fn bool QVarLengthArray::isEmpty() const
160 Returns true if the array has size 0; otherwise returns false.
165 /*! \fn void QVarLengthArray::clear()
167 Removes all the elements from the array.
172 /*! \fn void QVarLengthArray::resize(int size)
174 Sets the size of the array to \a size. If \a size is greater than
175 the current size, elements are added to the end. If \a size is
176 less than the current size, elements are removed from the end.
178 If the value type is a primitive type (e.g., char, int, float) or
179 a pointer type (e.g., QWidget *), new elements are not
180 initialized. For other types, the elements are initialized with a
181 \l{default-constructed value}.
186 /*! \fn int QVarLengthArray::capacity() const
188 Returns the maximum number of elements that can be stored in the
189 array without forcing a reallocation.
191 The sole purpose of this function is to provide a means of fine
192 tuning QVarLengthArray's memory usage. In general, you will rarely ever
193 need to call this function. If you want to know how many items are
194 in the array, call size().
199 /*! \fn void QVarLengthArray::reserve(int size)
201 Attempts to allocate memory for at least \a size elements. If you
202 know in advance how large the array can get, you can call this
203 function and if you call resize() often, you are likely to get
204 better performance. If \a size is an underestimate, the worst
205 that will happen is that the QVarLengthArray will be a bit
208 The sole purpose of this function is to provide a means of fine
209 tuning QVarLengthArray's memory usage. In general, you will
210 rarely ever need to call this function. If you want to change the
211 size of the array, call resize().
216 /*! \fn T &QVarLengthArray::operator[](int i)
218 Returns a reference to the item at index position \a i.
220 \a i must be a valid index position in the array (i.e., 0 <= \a i
226 /*! \fn const T &QVarLengthArray::operator[](int i) const
233 \fn void QVarLengthArray::append(const T &t)
235 Appends item \a t to the array, extending the array if necessary.
242 \fn inline void QVarLengthArray::removeLast()
245 Decreases the size of the array by one. The allocated size is not changed.
251 \fn void QVarLengthArray::append(const T *buf, int size)
253 Appends \a size amount of items referenced by \a buf to this array.
257 /*! \fn T *QVarLengthArray::data()
259 Returns a pointer to the data stored in the array. The pointer can
260 be used to access and modify the items in the array.
263 \snippet doc/src/snippets/code/doc_src_qvarlengtharray.cpp 3
265 The pointer remains valid as long as the array isn't reallocated.
267 This function is mostly useful to pass an array to a function
268 that accepts a plain C++ array.
270 \sa constData(), operator[]()
273 /*! \fn const T *QVarLengthArray::data() const
278 /*! \fn const T *QVarLengthArray::constData() const
280 Returns a const pointer to the data stored in the array. The
281 pointer can be used to access the items in the array. The
282 pointer remains valid as long as the array isn't reallocated.
284 This function is mostly useful to pass an array to a function
285 that accepts a plain C++ array.
287 \sa data(), operator[]()
290 /*! \fn QVarLengthArray<T, Prealloc> &QVarLengthArray::operator=(const QVarLengthArray<T, Prealloc> &other)
291 Assigns \a other to this array and returns a reference to this array.
294 /*! \fn QVarLengthArray::QVarLengthArray(const QVarLengthArray<T, Prealloc> &other)
295 Constructs a copy of \a other.
298 /*! \fn const T &QVarLengthArray::at(int i) const
300 Returns a reference to the item at index position \a i.
302 \a i must be a valid index position in the array (i.e., 0 <= \a i
305 \sa value(), operator[]()
308 /*! \fn T QVarLengthArray::value(int i) const
310 Returns the value at index position \a i.
312 If the index \a i is out of bounds, the function returns
313 a \l{default-constructed value}. If you are certain that
314 \a i is within bounds, you can use at() instead, which is slightly
317 \sa at(), operator[]()
320 /*! \fn T QVarLengthArray::value(int i, const T &defaultValue) const
324 If the index \a i is out of bounds, the function returns
329 \typedef QVarLengthArray::size_type
332 Typedef for int. Provided for STL compatibility.
336 \typedef QVarLengthArray::value_type
339 Typedef for T. Provided for STL compatibility.
343 \typedef QVarLengthArray::difference_type
346 Typedef for ptrdiff_t. Provided for STL compatibility.
350 \typedef QVarLengthArray::pointer
353 Typedef for T *. Provided for STL compatibility.
357 \typedef QVarLengthArray::const_pointer
360 Typedef for const T *. Provided for STL compatibility.
364 \typedef QVarLengthArray::reference
367 Typedef for T &. Provided for STL compatibility.
371 \typedef QVarLengthArray::const_reference
374 Typedef for const T &. Provided for STL compatibility.
378 \typedef QVarLengthArray::const_iterator
381 Typedef for const T *. Provided for STL compatibility.
385 \typedef QVarLengthArray::iterator
388 Typedef for T *. Provided for STL compatibility.
391 /*! \fn void QVarLengthArray::prepend(const T &value)
394 Inserts \a value at the beginning of the array.
397 This is the same as vector.insert(0, \a value).
399 For large arrays, this operation can be slow (\l{linear time}),
400 because it requires moving all the items in the vector by one
401 position further in memory. If you want a container class that
402 provides a fast prepend() function, use QList or QLinkedList
405 \sa append(), insert()
408 /*! \fn void QVarLengthArray::replace(int i, const T &value)
411 Replaces the item at index position \a i with \a value.
413 \a i must be a valid index position in the array (i.e., 0 <= \a
416 \sa operator[](), remove()
419 /*! \fn void QVarLengthArray::remove(int i)
424 Removes the element at index position \a i.
426 \sa insert(), replace()
429 /*! \fn void QVarLengthArray::remove(int i, int count)
434 Removes \a count elements from the middle of the array, starting at
437 \sa insert(), replace()
440 /*! \fn QVarLengthArray::iterator QVarLengthArray::begin()
443 Returns an \l{STL-style iterator} pointing to the first item in
446 \sa constBegin(), end()
449 /*! \fn QVarLengthArray::const_iterator QVarLengthArray::begin() const
454 /*! \fn QVarLengthArray::const_iterator QVarLengthArray::cbegin() const
457 Returns a const \l{STL-style iterator} pointing to the first item
463 /*! \fn QVarLengthArray::const_iterator QVarLengthArray::constBegin() const
466 Returns a const \l{STL-style iterator} pointing to the first item
469 \sa begin(), constEnd()
472 /*! \fn QVarLengthArray::iterator QVarLengthArray::end()
475 Returns an \l{STL-style iterator} pointing to the imaginary item
476 after the last item in the array.
478 \sa begin(), constEnd()
481 /*! \fn QVarLengthArray::const_iterator QVarLengthArray::end() const
487 /*! \fn QVarLengthArray::const_iterator QVarLengthArray::cend() const
490 Returns a const \l{STL-style iterator} pointing to the imaginary
491 item after the last item in the array.
496 /*! \fn QVarLengthArray::const_iterator QVarLengthArray::constEnd() const
499 Returns a const \l{STL-style iterator} pointing to the imaginary
500 item after the last item in the array.
502 \sa constBegin(), end()
505 /*! \fn QVarLengthArray::iterator QVarLengthArray::erase(iterator pos)
508 Removes the item pointed to by the iterator \a pos from the
509 vector, and returns an iterator to the next item in the vector
510 (which may be end()).
512 \sa insert(), remove()
515 /*! \fn QVarLengthArray::iterator QVarLengthArray::erase(iterator begin, iterator end)
520 Removes all the items from \a begin up to (but not including) \a
521 end. Returns an iterator to the same item that \a end referred to
525 /*! \fn void QVarLengthArray::insert(int i, const T &value)
528 Inserts \a value at index position \a i in the array. If \a i is
529 0, the value is prepended to the vector. If \a i is size(), the
530 value is appended to the vector.
532 For large arrays, this operation can be slow (\l{linear time}),
533 because it requires moving all the items at indexes \a i and
534 above by one position further in memory. If you want a container
535 class that provides a fast insert() function, use QLinkedList
541 /*! \fn void QVarLengthArray::insert(int i, int count, const T &value)
546 Inserts \a count copies of \a value at index position \a i in the
550 /*! \fn QVarLengthArray::iterator QVarLengthArray::insert(iterator before, const T &value)
555 Inserts \a value in front of the item pointed to by the iterator
556 \a before. Returns an iterator pointing at the inserted item.
559 /*! \fn QVarLengthArray::iterator QVarLengthArray::insert(iterator before, int count, const T &value)
562 Inserts \a count copies of \a value in front of the item pointed to
563 by the iterator \a before. Returns an iterator pointing at the
564 first of the inserted items.
569 /*! \fn bool operator==(const QVarLengthArray<T, Prealloc1> &left, const QVarLengthArray<T, Prealloc2> &right)
571 \relates QVarLengthArray
573 Returns true if the two arrays, specified by \a left and \a right, are equal.
575 Two arrays are considered equal if they contain the same values
578 This function requires the value type to have an implementation
584 /*! \fn bool operator!=(const QVarLengthArray<T, Prealloc1> &left, const QVarLengthArray<T, Prealloc2> &right)
586 \relates QVarLengthArray
588 Returns true if the two arrays, specified by \a left and \a right, are \e not equal.
590 Two arrays are considered equal if they contain the same values
593 This function requires the value type to have an implementation
599 /*! \fn QVarLengthArray &QVarLengthArray::operator<<(const T &value)
602 Appends \a value to the array and returns a reference to this
605 \sa append(), operator+=()
608 /*! \fn QVarLengthArray &QVarLengthArray::operator+=(const T &value)
611 Appends \a value to the array and returns a reference to this
614 \sa append(), operator<<()