author | eckhart.koppen@nokia.com |
Wed, 31 Mar 2010 11:06:36 +0300 | |
changeset 7 | f7bc934e204c |
parent 3 | 41300fa6a67c |
child 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
7
f7bc934e204c
5cabc75a39ca2f064f70b40f72ed93c74c4dc19b
eckhart.koppen@nokia.com
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtCore module of the Qt Toolkit. |
|
8 |
** |
|
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 |
|
14 |
** this package. |
|
15 |
** |
|
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. |
|
23 |
** |
|
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. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qvector.h" |
|
43 |
#include "qtools_p.h" |
|
44 |
#include <string.h> |
|
45 |
||
46 |
QT_BEGIN_NAMESPACE |
|
47 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
48 |
static inline int alignmentThreshold() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
49 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
50 |
// malloc on 32-bit platforms should return pointers that are 8-byte aligned or more |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
51 |
// while on 64-bit platforms they should be 16-byte aligned or more |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
52 |
return 2 * sizeof(void*); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
53 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
54 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
55 |
QVectorData QVectorData::shared_null = { Q_BASIC_ATOMIC_INITIALIZER(1), 0, 0, true, false, 0 }; |
0 | 56 |
|
57 |
QVectorData *QVectorData::malloc(int sizeofTypedData, int size, int sizeofT, QVectorData *init) |
|
58 |
{ |
|
59 |
QVectorData* p = (QVectorData *)qMalloc(sizeofTypedData + (size - 1) * sizeofT); |
|
60 |
Q_CHECK_PTR(p); |
|
61 |
::memcpy(p, init, sizeofTypedData + (qMin(size, init->alloc) - 1) * sizeofT); |
|
62 |
return p; |
|
63 |
} |
|
64 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
65 |
QVectorData *QVectorData::allocate(int size, int alignment) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
66 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
67 |
return static_cast<QVectorData *>(alignment > alignmentThreshold() ? qMallocAligned(size, alignment) : qMalloc(size)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
68 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
69 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
70 |
QVectorData *QVectorData::reallocate(QVectorData *x, int newsize, int oldsize, int alignment) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
71 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
72 |
if (alignment > alignmentThreshold()) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
73 |
return static_cast<QVectorData *>(qReallocAligned(x, newsize, oldsize, alignment)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
74 |
return static_cast<QVectorData *>(qRealloc(x, newsize)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
75 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
76 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
77 |
void QVectorData::free(QVectorData *x, int alignment) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
78 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
79 |
if (alignment > alignmentThreshold()) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
80 |
qFreeAligned(x); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
81 |
else |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
82 |
qFree(x); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
83 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
84 |
|
0 | 85 |
int QVectorData::grow(int sizeofTypedData, int size, int sizeofT, bool excessive) |
86 |
{ |
|
87 |
if (excessive) |
|
88 |
return size + size / 2; |
|
89 |
return qAllocMore(size * sizeofT, sizeofTypedData - sizeofT) / sizeofT; |
|
90 |
} |
|
91 |
||
92 |
/*! |
|
93 |
\class QVector |
|
94 |
\brief The QVector class is a template class that provides a dynamic array. |
|
95 |
||
96 |
\ingroup tools |
|
97 |
\ingroup shared |
|
98 |
||
99 |
\reentrant |
|
100 |
||
101 |
QVector\<T\> is one of Qt's generic \l{container classes}. It |
|
102 |
stores its items in adjacent memory locations and provides fast |
|
103 |
index-based access. |
|
104 |
||
105 |
QList\<T\>, QLinkedList\<T\>, and QVarLengthArray\<T\> provide |
|
106 |
similar functionality. Here's an overview: |
|
107 |
||
108 |
\list |
|
109 |
\i For most purposes, QList is the right class to use. Operations |
|
110 |
like prepend() and insert() are usually faster than with |
|
111 |
QVector because of the way QList stores its items in memory |
|
112 |
(see \l{Algorithmic Complexity} for details), |
|
113 |
and its index-based API is more convenient than QLinkedList's |
|
114 |
iterator-based API. It also expands to less code in your |
|
115 |
executable. |
|
116 |
\i If you need a real linked list, with guarantees of \l{constant |
|
117 |
time} insertions in the middle of the list and iterators to |
|
118 |
items rather than indexes, use QLinkedList. |
|
119 |
\i If you want the items to occupy adjacent memory positions, or |
|
120 |
if your items are larger than a pointer and you want to avoid |
|
121 |
the overhead of allocating them on the heap individually at |
|
122 |
insertion time, then use QVector. |
|
123 |
\i If you want a low-level variable-size array, QVarLengthArray |
|
124 |
may be sufficient. |
|
125 |
\endlist |
|
126 |
||
127 |
Here's an example of a QVector that stores integers and a QVector |
|
128 |
that stores QString values: |
|
129 |
||
130 |
\snippet doc/src/snippets/code/src_corelib_tools_qvector.cpp 0 |
|
131 |
||
132 |
QVector stores a vector (or array) of items. Typically, vectors |
|
133 |
are created with an initial size. For example, the following code |
|
134 |
constructs a QVector with 200 elements: |
|
135 |
||
136 |
\snippet doc/src/snippets/code/src_corelib_tools_qvector.cpp 1 |
|
137 |
||
138 |
The elements are automatically initialized with a |
|
139 |
\l{default-constructed value}. If you want to initialize the |
|
140 |
vector with a different value, pass that value as the second |
|
141 |
argument to the constructor: |
|
142 |
||
143 |
\snippet doc/src/snippets/code/src_corelib_tools_qvector.cpp 2 |
|
144 |
||
145 |
You can also call fill() at any time to fill the vector with a |
|
146 |
value. |
|
147 |
||
148 |
QVector uses 0-based indexes, just like C++ arrays. To access the |
|
149 |
item at a particular index position, you can use operator[](). On |
|
150 |
non-const vectors, operator[]() returns a reference to the item |
|
151 |
that can be used on the left side of an assignment: |
|
152 |
||
153 |
\snippet doc/src/snippets/code/src_corelib_tools_qvector.cpp 3 |
|
154 |
||
155 |
For read-only access, an alternative syntax is to use at(): |
|
156 |
||
157 |
\snippet doc/src/snippets/code/src_corelib_tools_qvector.cpp 4 |
|
158 |
||
159 |
at() can be faster than operator[](), because it never causes a |
|
160 |
\l{deep copy} to occur. |
|
161 |
||
162 |
Another way to access the data stored in a QVector is to call |
|
163 |
data(). The function returns a pointer to the first item in the |
|
164 |
vector. You can use the pointer to directly access and modify the |
|
165 |
elements stored in the vector. The pointer is also useful if you |
|
166 |
need to pass a QVector to a function that accepts a plain C++ |
|
167 |
array. |
|
168 |
||
169 |
If you want to find all occurrences of a particular value in a |
|
170 |
vector, use indexOf() or lastIndexOf(). The former searches |
|
171 |
forward starting from a given index position, the latter searches |
|
172 |
backward. Both return the index of the matching item if they found |
|
173 |
one; otherwise, they return -1. For example: |
|
174 |
||
175 |
\snippet doc/src/snippets/code/src_corelib_tools_qvector.cpp 5 |
|
176 |
||
177 |
If you simply want to check whether a vector contains a |
|
178 |
particular value, use contains(). If you want to find out how |
|
179 |
many times a particular value occurs in the vector, use count(). |
|
180 |
||
181 |
QVector provides these basic functions to add, move, and remove |
|
182 |
items: insert(), replace(), remove(), prepend(), append(). With |
|
183 |
the exception of append() and replace(), these functions can be slow |
|
184 |
(\l{linear time}) for large vectors, because they require moving many |
|
185 |
items in the vector by one position in memory. If you want a container |
|
186 |
class that provides fast insertion/removal in the middle, use |
|
187 |
QList or QLinkedList instead. |
|
188 |
||
189 |
Unlike plain C++ arrays, QVectors can be resized at any time by |
|
190 |
calling resize(). If the new size is larger than the old size, |
|
191 |
QVector might need to reallocate the whole vector. QVector tries |
|
192 |
to reduce the number of reallocations by preallocating up to twice |
|
193 |
as much memory as the actual data needs. |
|
194 |
||
195 |
If you know in advance approximately how many items the QVector |
|
196 |
will contain, you can call reserve(), asking QVector to |
|
197 |
preallocate a certain amount of memory. You can also call |
|
198 |
capacity() to find out how much memory QVector actually |
|
199 |
allocated. |
|
200 |
||
201 |
Note that using non-const operators and functions can cause |
|
202 |
QVector to do a deep copy of the data. This is due to \l{implicit sharing}. |
|
203 |
||
204 |
QVector's value type must be an \l{assignable data type}. This |
|
205 |
covers most data types that are commonly used, but the compiler |
|
206 |
won't let you, for example, store a QWidget as a value; instead, |
|
207 |
store a QWidget *. A few functions have additional requirements; |
|
208 |
for example, indexOf() and lastIndexOf() expect the value type to |
|
209 |
support \c operator==(). These requirements are documented on a |
|
210 |
per-function basis. |
|
211 |
||
212 |
Like the other container classes, QVector provides \l{Java-style |
|
213 |
iterators} (QVectorIterator and QMutableVectorIterator) and |
|
214 |
\l{STL-style iterators} (QVector::const_iterator and |
|
215 |
QVector::iterator). In practice, these are rarely used, because |
|
216 |
you can use indexes into the QVector. |
|
217 |
||
218 |
In addition to QVector, Qt also provides QVarLengthArray, a very |
|
219 |
low-level class with little functionality that is optimized for |
|
220 |
speed. |
|
221 |
||
222 |
QVector does \e not support inserting, prepending, appending or replacing |
|
223 |
with references to its own values. Doing so will cause your application to |
|
224 |
abort with an error message. |
|
225 |
||
226 |
\sa QVectorIterator, QMutableVectorIterator, QList, QLinkedList |
|
227 |
*/ |
|
228 |
||
229 |
/*! |
|
230 |
\fn QVector<T> QVector::mid(int pos, int length = -1) const |
|
231 |
||
232 |
Returns a vector whose elements are copied from this vector, |
|
233 |
starting at position \a pos. If \a length is -1 (the default), all |
|
234 |
elements after \a pos are copied; otherwise \a length elements (or |
|
235 |
all remaining elements if there are less than \a length elements) |
|
236 |
are copied. |
|
237 |
*/ |
|
238 |
||
239 |
||
240 |
/*! \fn QVector::QVector() |
|
241 |
||
242 |
Constructs an empty vector. |
|
243 |
||
244 |
\sa resize() |
|
245 |
*/ |
|
246 |
||
247 |
/*! \fn QVector::QVector(int size) |
|
248 |
||
249 |
Constructs a vector with an initial size of \a size elements. |
|
250 |
||
251 |
The elements are initialized with a \l{default-constructed |
|
252 |
value}. |
|
253 |
||
254 |
\sa resize() |
|
255 |
*/ |
|
256 |
||
257 |
/*! \fn QVector::QVector(int size, const T &value) |
|
258 |
||
259 |
Constructs a vector with an initial size of \a size elements. |
|
260 |
Each element is initialized with \a value. |
|
261 |
||
262 |
\sa resize(), fill() |
|
263 |
*/ |
|
264 |
||
265 |
/*! \fn QVector::QVector(const QVector<T> &other) |
|
266 |
||
267 |
Constructs a copy of \a other. |
|
268 |
||
269 |
This operation takes \l{constant time}, because QVector is |
|
270 |
\l{implicitly shared}. This makes returning a QVector from a |
|
271 |
function very fast. If a shared instance is modified, it will be |
|
272 |
copied (copy-on-write), and that takes \l{linear time}. |
|
273 |
||
274 |
\sa operator=() |
|
275 |
*/ |
|
276 |
||
277 |
/*! \fn QVector::~QVector() |
|
278 |
||
279 |
Destroys the vector. |
|
280 |
*/ |
|
281 |
||
282 |
/*! \fn QVector<T> &QVector::operator=(const QVector<T> &other) |
|
283 |
||
284 |
Assigns \a other to this vector and returns a reference to this |
|
285 |
vector. |
|
286 |
*/ |
|
287 |
||
288 |
/*! \fn bool QVector::operator==(const QVector<T> &other) const |
|
289 |
||
290 |
Returns true if \a other is equal to this vector; otherwise |
|
291 |
returns false. |
|
292 |
||
293 |
Two vectors are considered equal if they contain the same values |
|
294 |
in the same order. |
|
295 |
||
296 |
This function requires the value type to have an implementation |
|
297 |
of \c operator==(). |
|
298 |
||
299 |
\sa operator!=() |
|
300 |
*/ |
|
301 |
||
302 |
/*! \fn bool QVector::operator!=(const QVector<T> &other) const |
|
303 |
||
304 |
Returns true if \a other is not equal to this vector; otherwise |
|
305 |
returns false. |
|
306 |
||
307 |
Two vectors are considered equal if they contain the same values |
|
308 |
in the same order. |
|
309 |
||
310 |
This function requires the value type to have an implementation |
|
311 |
of \c operator==(). |
|
312 |
||
313 |
\sa operator==() |
|
314 |
*/ |
|
315 |
||
316 |
/*! \fn int QVector::size() const |
|
317 |
||
318 |
Returns the number of items in the vector. |
|
319 |
||
320 |
\sa isEmpty(), resize() |
|
321 |
*/ |
|
322 |
||
323 |
/*! \fn bool QVector::isEmpty() const |
|
324 |
||
325 |
Returns true if the vector has size 0; otherwise returns false. |
|
326 |
||
327 |
\sa size(), resize() |
|
328 |
*/ |
|
329 |
||
330 |
/*! \fn void QVector::resize(int size) |
|
331 |
||
332 |
Sets the size of the vector to \a size. If \a size is greater than the |
|
333 |
current size, elements are added to the end; the new elements are |
|
334 |
initialized with a \l{default-constructed value}. If \a size is less |
|
335 |
than the current size, elements are removed from the end. |
|
336 |
||
337 |
\sa size() |
|
338 |
*/ |
|
339 |
||
340 |
/*! \fn int QVector::capacity() const |
|
341 |
||
342 |
Returns the maximum number of items that can be stored in the |
|
343 |
vector without forcing a reallocation. |
|
344 |
||
345 |
The sole purpose of this function is to provide a means of fine |
|
346 |
tuning QVector's memory usage. In general, you will rarely ever |
|
347 |
need to call this function. If you want to know how many items are |
|
348 |
in the vector, call size(). |
|
349 |
||
350 |
\sa reserve(), squeeze() |
|
351 |
*/ |
|
352 |
||
353 |
/*! \fn void QVector::reserve(int size) |
|
354 |
||
355 |
Attempts to allocate memory for at least \a size elements. If you |
|
356 |
know in advance how large the vector will be, you can call this |
|
357 |
function, and if you call resize() often you are likely to get |
|
358 |
better performance. If \a size is an underestimate, the worst |
|
359 |
that will happen is that the QVector will be a bit slower. |
|
360 |
||
361 |
The sole purpose of this function is to provide a means of fine |
|
362 |
tuning QVector's memory usage. In general, you will rarely ever |
|
363 |
need to call this function. If you want to change the size of the |
|
364 |
vector, call resize(). |
|
365 |
||
366 |
\sa squeeze(), capacity() |
|
367 |
*/ |
|
368 |
||
369 |
/*! \fn void QVector::squeeze() |
|
370 |
||
371 |
Releases any memory not required to store the items. |
|
372 |
||
373 |
The sole purpose of this function is to provide a means of fine |
|
374 |
tuning QVector's memory usage. In general, you will rarely ever |
|
375 |
need to call this function. |
|
376 |
||
377 |
\sa reserve(), capacity() |
|
378 |
*/ |
|
379 |
||
380 |
/*! \fn void QVector::detach() |
|
381 |
||
382 |
\internal |
|
383 |
*/ |
|
384 |
||
385 |
/*! \fn bool QVector::isDetached() const |
|
386 |
||
387 |
\internal |
|
388 |
*/ |
|
389 |
||
390 |
/*! \fn void QVector::setSharable(bool sharable) |
|
391 |
||
392 |
\internal |
|
393 |
*/ |
|
394 |
||
395 |
/*! \fn T *QVector::data() |
|
396 |
||
397 |
Returns a pointer to the data stored in the vector. The pointer |
|
398 |
can be used to access and modify the items in the vector. |
|
399 |
||
400 |
Example: |
|
401 |
\snippet doc/src/snippets/code/src_corelib_tools_qvector.cpp 6 |
|
402 |
||
403 |
The pointer remains valid as long as the vector isn't |
|
404 |
reallocated. |
|
405 |
||
406 |
This function is mostly useful to pass a vector to a function |
|
407 |
that accepts a plain C++ array. |
|
408 |
||
409 |
\sa constData(), operator[]() |
|
410 |
*/ |
|
411 |
||
412 |
/*! \fn const T *QVector::data() const |
|
413 |
||
414 |
\overload |
|
415 |
*/ |
|
416 |
||
417 |
/*! \fn const T *QVector::constData() const |
|
418 |
||
419 |
Returns a const pointer to the data stored in the vector. The |
|
420 |
pointer can be used to access the items in the vector. |
|
421 |
The pointer remains valid as long as the vector isn't |
|
422 |
reallocated. |
|
423 |
||
424 |
This function is mostly useful to pass a vector to a function |
|
425 |
that accepts a plain C++ array. |
|
426 |
||
427 |
\sa data(), operator[]() |
|
428 |
*/ |
|
429 |
||
430 |
/*! \fn void QVector::clear() |
|
431 |
||
432 |
Removes all the elements from the vector and releases the memory used by |
|
433 |
the vector. |
|
434 |
*/ |
|
435 |
||
436 |
/*! \fn const T &QVector::at(int i) const |
|
437 |
||
438 |
Returns the item at index position \a i in the vector. |
|
439 |
||
440 |
\a i must be a valid index position in the vector (i.e., 0 <= \a |
|
441 |
i < size()). |
|
442 |
||
443 |
\sa value(), operator[]() |
|
444 |
*/ |
|
445 |
||
446 |
/*! \fn T &QVector::operator[](int i) |
|
447 |
||
448 |
Returns the item at index position \a i as a modifiable reference. |
|
449 |
||
450 |
\a i must be a valid index position in the vector (i.e., 0 <= \a i |
|
451 |
< size()). |
|
452 |
||
453 |
Note that using non-const operators can cause QVector to do a deep |
|
454 |
copy. |
|
455 |
||
456 |
\sa at(), value() |
|
457 |
*/ |
|
458 |
||
459 |
/*! \fn const T &QVector::operator[](int i) const |
|
460 |
||
461 |
\overload |
|
462 |
||
463 |
Same as at(\a i). |
|
464 |
*/ |
|
465 |
||
466 |
/*! |
|
467 |
\fn void QVector::append(const T &value) |
|
468 |
||
469 |
Inserts \a value at the end of the vector. |
|
470 |
||
471 |
Example: |
|
472 |
\snippet doc/src/snippets/code/src_corelib_tools_qvector.cpp 7 |
|
473 |
||
474 |
This is the same as calling resize(size() + 1) and assigning \a |
|
475 |
value to the new last element in the vector. |
|
476 |
||
477 |
This operation is relatively fast, because QVector typically |
|
478 |
allocates more memory than necessary, so it can grow without |
|
479 |
reallocating the entire vector each time. |
|
480 |
||
481 |
\sa operator<<(), prepend(), insert() |
|
482 |
*/ |
|
483 |
||
484 |
/*! \fn void QVector::prepend(const T &value) |
|
485 |
||
486 |
Inserts \a value at the beginning of the vector. |
|
487 |
||
488 |
Example: |
|
489 |
\snippet doc/src/snippets/code/src_corelib_tools_qvector.cpp 8 |
|
490 |
||
491 |
This is the same as vector.insert(0, \a value). |
|
492 |
||
493 |
For large vectors, this operation can be slow (\l{linear time}), |
|
494 |
because it requires moving all the items in the vector by one |
|
495 |
position further in memory. If you want a container class that |
|
496 |
provides a fast prepend() function, use QList or QLinkedList |
|
497 |
instead. |
|
498 |
||
499 |
\sa append(), insert() |
|
500 |
*/ |
|
501 |
||
502 |
/*! \fn void QVector::insert(int i, const T &value) |
|
503 |
||
504 |
Inserts \a value at index position \a i in the vector. If \a i is |
|
505 |
0, the value is prepended to the vector. If \a i is size(), the |
|
506 |
value is appended to the vector. |
|
507 |
||
508 |
Example: |
|
509 |
\snippet doc/src/snippets/code/src_corelib_tools_qvector.cpp 9 |
|
510 |
||
511 |
For large vectors, this operation can be slow (\l{linear time}), |
|
512 |
because it requires moving all the items at indexes \a i and |
|
513 |
above by one position further in memory. If you want a container |
|
514 |
class that provides a fast insert() function, use QLinkedList |
|
515 |
instead. |
|
516 |
||
517 |
\sa append(), prepend(), remove() |
|
518 |
*/ |
|
519 |
||
520 |
/*! \fn void QVector::insert(int i, int count, const T &value) |
|
521 |
||
522 |
\overload |
|
523 |
||
524 |
Inserts \a count copies of \a value at index position \a i in the |
|
525 |
vector. |
|
526 |
||
527 |
Example: |
|
528 |
\snippet doc/src/snippets/code/src_corelib_tools_qvector.cpp 10 |
|
529 |
*/ |
|
530 |
||
531 |
/*! \fn QVector::iterator QVector::insert(iterator before, const T &value) |
|
532 |
||
533 |
\overload |
|
534 |
||
535 |
Inserts \a value in front of the item pointed to by the iterator |
|
536 |
\a before. Returns an iterator pointing at the inserted item. |
|
537 |
*/ |
|
538 |
||
539 |
/*! \fn QVector::iterator QVector::insert(iterator before, int count, const T &value) |
|
540 |
||
541 |
Inserts \a count copies of \a value in front of the item pointed to |
|
542 |
by the iterator \a before. Returns an iterator pointing at the |
|
543 |
first of the inserted items. |
|
544 |
*/ |
|
545 |
||
546 |
/*! \fn void QVector::replace(int i, const T &value) |
|
547 |
||
548 |
Replaces the item at index position \a i with \a value. |
|
549 |
||
550 |
\a i must be a valid index position in the vector (i.e., 0 <= \a |
|
551 |
i < size()). |
|
552 |
||
553 |
\sa operator[](), remove() |
|
554 |
*/ |
|
555 |
||
556 |
/*! \fn void QVector::remove(int i) |
|
557 |
||
558 |
\overload |
|
559 |
||
560 |
Removes the element at index position \a i. |
|
561 |
||
562 |
\sa insert(), replace(), fill() |
|
563 |
*/ |
|
564 |
||
565 |
/*! \fn void QVector::remove(int i, int count) |
|
566 |
||
567 |
\overload |
|
568 |
||
569 |
Removes \a count elements from the middle of the vector, starting at |
|
570 |
index position \a i. |
|
571 |
||
572 |
\sa insert(), replace(), fill() |
|
573 |
*/ |
|
574 |
||
575 |
/*! \fn QVector &QVector::fill(const T &value, int size = -1) |
|
576 |
||
577 |
Assigns \a value to all items in the vector. If \a size is |
|
578 |
different from -1 (the default), the vector is resized to size \a |
|
579 |
size beforehand. |
|
580 |
||
581 |
Example: |
|
582 |
\snippet doc/src/snippets/code/src_corelib_tools_qvector.cpp 11 |
|
583 |
||
584 |
\sa resize() |
|
585 |
*/ |
|
586 |
||
587 |
/*! \fn int QVector::indexOf(const T &value, int from = 0) const |
|
588 |
||
589 |
Returns the index position of the first occurrence of \a value in |
|
590 |
the vector, searching forward from index position \a from. |
|
591 |
Returns -1 if no item matched. |
|
592 |
||
593 |
Example: |
|
594 |
\snippet doc/src/snippets/code/src_corelib_tools_qvector.cpp 12 |
|
595 |
||
596 |
This function requires the value type to have an implementation of |
|
597 |
\c operator==(). |
|
598 |
||
599 |
\sa lastIndexOf(), contains() |
|
600 |
*/ |
|
601 |
||
602 |
/*! \fn int QVector::lastIndexOf(const T &value, int from = -1) const |
|
603 |
||
604 |
Returns the index position of the last occurrence of the value \a |
|
605 |
value in the vector, searching backward from index position \a |
|
606 |
from. If \a from is -1 (the default), the search starts at the |
|
607 |
last item. Returns -1 if no item matched. |
|
608 |
||
609 |
Example: |
|
610 |
\snippet doc/src/snippets/code/src_corelib_tools_qvector.cpp 13 |
|
611 |
||
612 |
This function requires the value type to have an implementation of |
|
613 |
\c operator==(). |
|
614 |
||
615 |
\sa indexOf() |
|
616 |
*/ |
|
617 |
||
618 |
/*! \fn bool QVector::contains(const T &value) const |
|
619 |
||
620 |
Returns true if the vector contains an occurrence of \a value; |
|
621 |
otherwise returns false. |
|
622 |
||
623 |
This function requires the value type to have an implementation of |
|
624 |
\c operator==(). |
|
625 |
||
626 |
\sa indexOf(), count() |
|
627 |
*/ |
|
628 |
||
629 |
/*! \fn bool QVector::startsWith(const T &value) const |
|
630 |
\since 4.5 |
|
631 |
||
632 |
Returns true if this vector is not empty and its first |
|
633 |
item is equal to \a value; otherwise returns false. |
|
634 |
||
635 |
\sa isEmpty(), first() |
|
636 |
*/ |
|
637 |
||
638 |
/*! \fn bool QVector::endsWith(const T &value) const |
|
639 |
\since 4.5 |
|
640 |
||
641 |
Returns true if this vector is not empty and its last |
|
642 |
item is equal to \a value; otherwise returns false. |
|
643 |
||
644 |
\sa isEmpty(), last() |
|
645 |
*/ |
|
646 |
||
647 |
||
648 |
/*! \fn int QVector::count(const T &value) const |
|
649 |
||
650 |
Returns the number of occurrences of \a value in the vector. |
|
651 |
||
652 |
This function requires the value type to have an implementation of |
|
653 |
\c operator==(). |
|
654 |
||
655 |
\sa contains(), indexOf() |
|
656 |
*/ |
|
657 |
||
658 |
/*! \fn int QVector::count() const |
|
659 |
||
660 |
\overload |
|
661 |
||
662 |
Same as size(). |
|
663 |
*/ |
|
664 |
||
665 |
/*! \fn QVector::iterator QVector::begin() |
|
666 |
||
667 |
Returns an \l{STL-style iterator} pointing to the first item in |
|
668 |
the vector. |
|
669 |
||
670 |
\sa constBegin(), end() |
|
671 |
*/ |
|
672 |
||
673 |
/*! \fn QVector::const_iterator QVector::begin() const |
|
674 |
||
675 |
\overload |
|
676 |
*/ |
|
677 |
||
678 |
/*! \fn QVector::const_iterator QVector::constBegin() const |
|
679 |
||
680 |
Returns a const \l{STL-style iterator} pointing to the first item |
|
681 |
in the vector. |
|
682 |
||
683 |
\sa begin(), constEnd() |
|
684 |
*/ |
|
685 |
||
686 |
/*! \fn QVector::iterator QVector::end() |
|
687 |
||
688 |
Returns an \l{STL-style iterator} pointing to the imaginary item |
|
689 |
after the last item in the vector. |
|
690 |
||
691 |
\sa begin(), constEnd() |
|
692 |
*/ |
|
693 |
||
694 |
/*! \fn QVector::const_iterator QVector::end() const |
|
695 |
||
696 |
\overload |
|
697 |
*/ |
|
698 |
||
699 |
/*! \fn QVector::const_iterator QVector::constEnd() const |
|
700 |
||
701 |
Returns a const \l{STL-style iterator} pointing to the imaginary |
|
702 |
item after the last item in the vector. |
|
703 |
||
704 |
\sa constBegin(), end() |
|
705 |
*/ |
|
706 |
||
707 |
/*! \fn QVector::iterator QVector::erase(iterator pos) |
|
708 |
||
709 |
Removes the item pointed to by the iterator \a pos from the |
|
710 |
vector, and returns an iterator to the next item in the vector |
|
711 |
(which may be end()). |
|
712 |
||
713 |
\sa insert(), remove() |
|
714 |
*/ |
|
715 |
||
716 |
/*! \fn QVector::iterator QVector::erase(iterator begin, iterator end) |
|
717 |
||
718 |
\overload |
|
719 |
||
720 |
Removes all the items from \a begin up to (but not including) \a |
|
721 |
end. Returns an iterator to the same item that \a end referred to |
|
722 |
before the call. |
|
723 |
*/ |
|
724 |
||
725 |
/*! \fn T& QVector::first() |
|
726 |
||
727 |
Returns a reference to the first item in the vector. This |
|
728 |
function assumes that the vector isn't empty. |
|
729 |
||
730 |
\sa last(), isEmpty() |
|
731 |
*/ |
|
732 |
||
733 |
/*! \fn const T& QVector::first() const |
|
734 |
||
735 |
\overload |
|
736 |
*/ |
|
737 |
||
738 |
/*! \fn T& QVector::last() |
|
739 |
||
740 |
Returns a reference to the last item in the vector. This function |
|
741 |
assumes that the vector isn't empty. |
|
742 |
||
743 |
\sa first(), isEmpty() |
|
744 |
*/ |
|
745 |
||
746 |
/*! \fn const T& QVector::last() const |
|
747 |
||
748 |
\overload |
|
749 |
*/ |
|
750 |
||
751 |
/*! \fn T QVector::value(int i) const |
|
752 |
||
753 |
Returns the value at index position \a i in the vector. |
|
754 |
||
755 |
If the index \a i is out of bounds, the function returns |
|
756 |
a \l{default-constructed value}. If you are certain that |
|
757 |
\a i is within bounds, you can use at() instead, which is slightly |
|
758 |
faster. |
|
759 |
||
760 |
\sa at(), operator[]() |
|
761 |
*/ |
|
762 |
||
763 |
/*! \fn T QVector::value(int i, const T &defaultValue) const |
|
764 |
||
765 |
\overload |
|
766 |
||
767 |
If the index \a i is out of bounds, the function returns |
|
768 |
\a defaultValue. |
|
769 |
*/ |
|
770 |
||
771 |
/*! \fn void QVector::push_back(const T &value) |
|
772 |
||
773 |
This function is provided for STL compatibility. It is equivalent |
|
774 |
to append(\a value). |
|
775 |
*/ |
|
776 |
||
777 |
/*! \fn void QVector::push_front(const T &value) |
|
778 |
||
779 |
This function is provided for STL compatibility. It is equivalent |
|
780 |
to prepend(\a value). |
|
781 |
*/ |
|
782 |
||
783 |
/*! \fn void QVector::pop_front() |
|
784 |
||
785 |
This function is provided for STL compatibility. It is equivalent |
|
786 |
to erase(begin()). |
|
787 |
*/ |
|
788 |
||
789 |
/*! \fn void QVector::pop_back() |
|
790 |
||
791 |
This function is provided for STL compatibility. It is equivalent |
|
792 |
to erase(end() - 1). |
|
793 |
*/ |
|
794 |
||
795 |
/*! \fn T& QVector::front() |
|
796 |
||
797 |
This function is provided for STL compatibility. It is equivalent |
|
798 |
to first(). |
|
799 |
*/ |
|
800 |
||
801 |
/*! \fn QVector::const_reference QVector::front() const |
|
802 |
||
803 |
\overload |
|
804 |
*/ |
|
805 |
||
806 |
/*! \fn QVector::reference QVector::back() |
|
807 |
||
808 |
This function is provided for STL compatibility. It is equivalent |
|
809 |
to last(). |
|
810 |
*/ |
|
811 |
||
812 |
/*! \fn QVector::const_reference QVector::back() const |
|
813 |
||
814 |
\overload |
|
815 |
*/ |
|
816 |
||
817 |
/*! \fn bool QVector::empty() const |
|
818 |
||
819 |
This function is provided for STL compatibility. It is equivalent |
|
820 |
to isEmpty(), returning true if the vector is empty; otherwise |
|
821 |
returns false. |
|
822 |
*/ |
|
823 |
||
824 |
/*! \fn QVector<T> &QVector::operator+=(const QVector<T> &other) |
|
825 |
||
826 |
Appends the items of the \a other vector to this vector and |
|
827 |
returns a reference to this vector. |
|
828 |
||
829 |
\sa operator+(), append() |
|
830 |
*/ |
|
831 |
||
832 |
/*! \fn void QVector::operator+=(const T &value) |
|
833 |
||
834 |
\overload |
|
835 |
||
836 |
Appends \a value to the vector. |
|
837 |
||
838 |
\sa append(), operator<<() |
|
839 |
*/ |
|
840 |
||
841 |
/*! \fn QVector<T> QVector::operator+(const QVector<T> &other) const |
|
842 |
||
843 |
Returns a vector that contains all the items in this vector |
|
844 |
followed by all the items in the \a other vector. |
|
845 |
||
846 |
\sa operator+=() |
|
847 |
*/ |
|
848 |
||
849 |
/*! \fn QVector<T> &QVector::operator<<(const T &value) |
|
850 |
||
851 |
Appends \a value to the vector and returns a reference to this |
|
852 |
vector. |
|
853 |
||
854 |
\sa append(), operator+=() |
|
855 |
*/ |
|
856 |
||
857 |
/*! \fn QVector<T> &QVector::operator<<(const QVector<T> &other) |
|
858 |
||
859 |
Appends \a other to the vector and returns a reference to the |
|
860 |
vector. |
|
861 |
*/ |
|
862 |
||
863 |
/*! \typedef QVector::iterator |
|
864 |
||
865 |
The QVector::iterator typedef provides an STL-style non-const |
|
866 |
iterator for QVector and QStack. |
|
867 |
||
868 |
QVector provides both \l{STL-style iterators} and \l{Java-style |
|
869 |
iterators}. The STL-style non-const iterator is simply a typedef |
|
870 |
for "T *" (pointer to T). |
|
871 |
||
872 |
\sa QVector::begin(), QVector::end(), QVector::const_iterator, QMutableVectorIterator |
|
873 |
*/ |
|
874 |
||
875 |
/*! \typedef QVector::const_iterator |
|
876 |
||
877 |
The QVector::const_iterator typedef provides an STL-style const |
|
878 |
iterator for QVector and QStack. |
|
879 |
||
880 |
QVector provides both \l{STL-style iterators} and \l{Java-style |
|
881 |
iterators}. The STL-style const iterator is simply a typedef for |
|
882 |
"const T *" (pointer to const T). |
|
883 |
||
884 |
\sa QVector::constBegin(), QVector::constEnd(), QVector::iterator, QVectorIterator |
|
885 |
*/ |
|
886 |
||
887 |
/*! \typedef QVector::Iterator |
|
888 |
||
889 |
Qt-style synonym for QVector::iterator. |
|
890 |
*/ |
|
891 |
||
892 |
/*! \typedef QVector::ConstIterator |
|
893 |
||
894 |
Qt-style synonym for QVector::const_iterator. |
|
895 |
*/ |
|
896 |
||
897 |
/*! \typedef QVector::const_pointer |
|
898 |
||
899 |
Typedef for const T *. Provided for STL compatibility. |
|
900 |
*/ |
|
901 |
||
902 |
/*! \typedef QVector::const_reference |
|
903 |
||
904 |
Typedef for T &. Provided for STL compatibility. |
|
905 |
*/ |
|
906 |
||
907 |
/*! \typedef QVector::difference_type |
|
908 |
||
909 |
Typedef for ptrdiff_t. Provided for STL compatibility. |
|
910 |
*/ |
|
911 |
||
912 |
/*! \typedef QVector::pointer |
|
913 |
||
914 |
Typedef for T *. Provided for STL compatibility. |
|
915 |
*/ |
|
916 |
||
917 |
/*! \typedef QVector::reference |
|
918 |
||
919 |
Typedef for T &. Provided for STL compatibility. |
|
920 |
*/ |
|
921 |
||
922 |
/*! \typedef QVector::size_type |
|
923 |
||
924 |
Typedef for int. Provided for STL compatibility. |
|
925 |
*/ |
|
926 |
||
927 |
/*! \typedef QVector::value_type |
|
928 |
||
929 |
Typedef for T. Provided for STL compatibility. |
|
930 |
*/ |
|
931 |
||
932 |
/*! \fn QList<T> QVector<T>::toList() const |
|
933 |
||
934 |
Returns a QList object with the data contained in this QVector. |
|
935 |
||
936 |
Example: |
|
937 |
||
938 |
\snippet doc/src/snippets/code/src_corelib_tools_qvector.cpp 14 |
|
939 |
||
940 |
\sa fromList(), QList::fromVector() |
|
941 |
*/ |
|
942 |
||
943 |
/*! \fn QVector<T> QVector<T>::fromList(const QList<T> &list) |
|
944 |
||
945 |
Returns a QVector object with the data contained in \a list. |
|
946 |
||
947 |
Example: |
|
948 |
||
949 |
\snippet doc/src/snippets/code/src_corelib_tools_qvector.cpp 15 |
|
950 |
||
951 |
\sa toList(), QList::toVector() |
|
952 |
*/ |
|
953 |
||
954 |
/*! \fn QVector<T> QVector<T>::fromStdVector(const std::vector<T> &vector) |
|
955 |
||
956 |
Returns a QVector object with the data contained in \a vector. The |
|
957 |
order of the elements in the QVector is the same as in \a vector. |
|
958 |
||
959 |
Example: |
|
960 |
||
961 |
\snippet doc/src/snippets/code/src_corelib_tools_qvector.cpp 16 |
|
962 |
||
963 |
\sa toStdVector(), QList::fromStdList() |
|
964 |
*/ |
|
965 |
||
966 |
/*! \fn std::vector<T> QVector<T>::toStdVector() const |
|
967 |
||
968 |
Returns a std::vector object with the data contained in this QVector. |
|
969 |
Example: |
|
970 |
||
971 |
\snippet doc/src/snippets/code/src_corelib_tools_qvector.cpp 17 |
|
972 |
||
973 |
\sa fromStdVector(), QList::toStdList() |
|
974 |
*/ |
|
975 |
||
976 |
/*! \fn QDataStream &operator<<(QDataStream &out, const QVector<T> &vector) |
|
977 |
\relates QVector |
|
978 |
||
979 |
Writes the vector \a vector to stream \a out. |
|
980 |
||
981 |
This function requires the value type to implement \c operator<<(). |
|
982 |
||
983 |
\sa \link datastreamformat.html Format of the QDataStream operators \endlink |
|
984 |
*/ |
|
985 |
||
986 |
/*! \fn QDataStream &operator>>(QDataStream &in, QVector<T> &vector) |
|
987 |
\relates QVector |
|
988 |
||
989 |
Reads a vector from stream \a in into \a vector. |
|
990 |
||
991 |
This function requires the value type to implement \c operator>>(). |
|
992 |
||
993 |
\sa \link datastreamformat.html Format of the QDataStream operators \endlink |
|
994 |
*/ |
|
995 |
||
996 |
QT_END_NAMESPACE |