1
/****************************************************************************
3
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
7
** This file is part of the documentation of the Qt Toolkit.
9
** $QT_BEGIN_LICENSE:LGPL$
10
** No Commercial Usage
11
** This file contains pre-release code and may not be distributed.
12
** You may use this file in accordance with the terms and conditions
13
** contained in the Technology Preview License Agreement accompanying
16
** GNU Lesser General Public License Usage
17
** Alternatively, this file may be used under the terms of the GNU Lesser
18
** General Public License version 2.1 as published by the Free Software
19
** Foundation and appearing in the file LICENSE.LGPL included in the
20
** packaging of this file. Please review the following information to
21
** ensure the GNU Lesser General Public License version 2.1 requirements
22
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
** In addition, as a special exception, Nokia gives you certain additional
25
** rights. These rights are described in the Nokia Qt LGPL Exception
26
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
40
****************************************************************************/
43
\class QVarLengthArray
44
\brief The QVarLengthArray class provides a low-level variable-length array.
49
The C++ language doesn't support variable-length arrays on the stack.
50
For example, the following code won't compile:
52
\snippet doc/src/snippets/code/doc_src_qvarlengtharray.qdoc 0
54
The alternative is to allocate the array on the heap (with
57
\snippet doc/src/snippets/code/doc_src_qvarlengtharray.qdoc 1
59
However, if myfunc() is called very frequently from the
60
application's inner loop, heap allocation can be a major source
63
QVarLengthArray is an attempt to work around this gap in the C++
64
language. It allocates a certain number of elements on the stack,
65
and if you resize the array to a larger size, it automatically
66
uses the heap instead. Stack allocation has the advantage that
67
it is much faster than heap allocation.
70
\snippet doc/src/snippets/code/doc_src_qvarlengtharray.qdoc 2
72
In the example above, QVarLengthArray will preallocate 1024
73
elements on the stack and use them unless \c{n + 1} is greater
74
than 1024. If you omit the second template argument,
75
QVarLengthArray's default of 256 is used.
77
QVarLengthArray's value type must be an \l{assignable data type}.
78
This covers most data types that are commonly used, but the
79
compiler won't let you, for example, store a QWidget as a value;
80
instead, store a QWidget *.
82
QVarLengthArray, like QVector, provides a resizable array data
83
structure. The main differences between the two classes are:
86
\o QVarLengthArray's API is much more low-level. It provides no
87
iterators and lacks much of QVector's functionality.
89
\o QVarLengthArray doesn't initialize the memory if the value is
90
a basic type. (QVector always does.)
92
\o QVector uses \l{implicit sharing} as a memory optimization.
93
QVarLengthArray doesn't provide that feature; however, it
94
usually produces slightly better performance due to reduced
95
overhead, especially in tight loops.
98
In summary, QVarLengthArray is a low-level optimization class
99
that only makes sense in very specific cases. It is used a few
100
places inside Qt and was added to Qt's public API for the
101
convenience of advanced users.
103
\sa QVector, QList, QLinkedList
106
/*! \fn QVarLengthArray::QVarLengthArray(int size)
108
Constructs an array with an initial size of \a size elements.
110
If the value type is a primitive type (e.g., char, int, float) or
111
a pointer type (e.g., QWidget *), the elements are not
112
initialized. For other types, the elements are initialized with a
113
\l{default-constructed value}.
116
/*! \fn QVarLengthArray::~QVarLengthArray()
121
/*! \fn int QVarLengthArray::size() const
123
Returns the number of elements in the array.
125
\sa isEmpty(), resize()
128
/*! \fn int QVarLengthArray::count() const
132
\sa isEmpty(), resize()
135
/*! \fn bool QVarLengthArray::isEmpty() const
137
Returns true if the array has size 0; otherwise returns false.
142
/*! \fn void QVarLengthArray::clear()
144
Removes all the elements from the array.
149
/*! \fn void QVarLengthArray::resize(int size)
151
Sets the size of the array to \a size. If \a size is greater than
152
the current size, elements are added to the end. If \a size is
153
less than the current size, elements are removed from the end.
155
If the value type is a primitive type (e.g., char, int, float) or
156
a pointer type (e.g., QWidget *), new elements are not
157
initialized. For other types, the elements are initialized with a
158
\l{default-constructed value}.
163
/*! \fn int QVarLengthArray::capacity() const
165
Returns the maximum number of elements that can be stored in the
166
array without forcing a reallocation.
168
The sole purpose of this function is to provide a means of fine
169
tuning QVarLengthArray's memory usage. In general, you will rarely ever
170
need to call this function. If you want to know how many items are
171
in the array, call size().
176
/*! \fn void QVarLengthArray::reserve(int size)
178
Attempts to allocate memory for at least \a size elements. If you
179
know in advance how large the array can get, you can call this
180
function and if you call resize() often, you are likely to get
181
better performance. If \a size is an underestimate, the worst
182
that will happen is that the QVarLengthArray will be a bit
185
The sole purpose of this function is to provide a means of fine
186
tuning QVarLengthArray's memory usage. In general, you will
187
rarely ever need to call this function. If you want to change the
188
size of the array, call resize().
193
/*! \fn T &QVarLengthArray::operator[](int i)
195
Returns a reference to the item at index position \a i.
197
\a i must be a valid index position in the array (i.e., 0 <= \a i
203
/*! \fn const T &QVarLengthArray::operator[](int i) const
210
\fn void QVarLengthArray::append(const T &t)
212
Appends item \a t to the array, extending the array if necessary.
219
\fn inline void QVarLengthArray::removeLast()
222
Decreases the size of the array by one. The allocated size is not changed.
228
\fn void QVarLengthArray::append(const T *buf, int size)
230
Appends \a size amount of items referenced by \a buf to this array.
234
/*! \fn T *QVarLengthArray::data()
236
Returns a pointer to the data stored in the array. The pointer can
237
be used to access and modify the items in the array.
240
\snippet doc/src/snippets/code/doc_src_qvarlengtharray.qdoc 3
242
The pointer remains valid as long as the array isn't reallocated.
244
This function is mostly useful to pass an array to a function
245
that accepts a plain C++ array.
247
\sa constData(), operator[]()
250
/*! \fn const T *QVarLengthArray::data() const
255
/*! \fn const T *QVarLengthArray::constData() const
257
Returns a const pointer to the data stored in the array. The
258
pointer can be used to access the items in the array. The
259
pointer remains valid as long as the array isn't reallocated.
261
This function is mostly useful to pass an array to a function
262
that accepts a plain C++ array.
264
\sa data(), operator[]()
267
/*! \fn QVarLengthArray<T, Prealloc> &QVarLengthArray::operator=(const QVarLengthArray<T, Prealloc> &other)
268
Assigns \a other to this array and returns a reference to this array.
271
/*! \fn QVarLengthArray::QVarLengthArray(const QVarLengthArray<T, Prealloc> &other)
272
Constructs a copy of \a other.