| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the documentation of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:FDL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in a
|
|---|
| 14 | ** written agreement between you and Nokia.
|
|---|
| 15 | **
|
|---|
| 16 | ** GNU Free Documentation License
|
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
|---|
| 18 | ** Documentation License version 1.3 as published by the Free Software
|
|---|
| 19 | ** Foundation and appearing in the file included in the packaging of this
|
|---|
| 20 | ** file.
|
|---|
| 21 | **
|
|---|
| 22 | ** If you have questions regarding the use of this file, please contact
|
|---|
| 23 | ** Nokia at [email protected].
|
|---|
| 24 | ** $QT_END_LICENSE$
|
|---|
| 25 | **
|
|---|
| 26 | ****************************************************************************/
|
|---|
| 27 |
|
|---|
| 28 | /*!
|
|---|
| 29 | \class QVarLengthArray
|
|---|
| 30 | \brief The QVarLengthArray class provides a low-level variable-length array.
|
|---|
| 31 |
|
|---|
| 32 | \ingroup tools
|
|---|
| 33 | \reentrant
|
|---|
| 34 |
|
|---|
| 35 | The C++ language doesn't support variable-length arrays on the stack.
|
|---|
| 36 | For example, the following code won't compile:
|
|---|
| 37 |
|
|---|
| 38 | \snippet doc/src/snippets/code/doc_src_qvarlengtharray.qdoc 0
|
|---|
| 39 |
|
|---|
| 40 | The alternative is to allocate the array on the heap (with
|
|---|
| 41 | \c{new}):
|
|---|
| 42 |
|
|---|
| 43 | \snippet doc/src/snippets/code/doc_src_qvarlengtharray.qdoc 1
|
|---|
| 44 |
|
|---|
| 45 | However, if myfunc() is called very frequently from the
|
|---|
| 46 | application's inner loop, heap allocation can be a major source
|
|---|
| 47 | of slowdown.
|
|---|
| 48 |
|
|---|
| 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.
|
|---|
| 54 |
|
|---|
| 55 | Example:
|
|---|
| 56 | \snippet doc/src/snippets/code/doc_src_qvarlengtharray.qdoc 2
|
|---|
| 57 |
|
|---|
| 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.
|
|---|
| 62 |
|
|---|
| 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 *.
|
|---|
| 67 |
|
|---|
| 68 | QVarLengthArray, like QVector, provides a resizable array data
|
|---|
| 69 | structure. The main differences between the two classes are:
|
|---|
| 70 |
|
|---|
| 71 | \list
|
|---|
| 72 | \o QVarLengthArray's API is much more low-level. It provides no
|
|---|
| 73 | iterators and lacks much of QVector's functionality.
|
|---|
| 74 |
|
|---|
| 75 | \o QVarLengthArray doesn't initialize the memory if the value is
|
|---|
| 76 | a basic type. (QVector always does.)
|
|---|
| 77 |
|
|---|
| 78 | \o 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.
|
|---|
| 82 | \endlist
|
|---|
| 83 |
|
|---|
| 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.
|
|---|
| 88 |
|
|---|
| 89 | \sa QVector, QList, QLinkedList
|
|---|
| 90 | */
|
|---|
| 91 |
|
|---|
| 92 | /*! \fn QVarLengthArray::QVarLengthArray(int size)
|
|---|
| 93 |
|
|---|
| 94 | Constructs an array with an initial size of \a size elements.
|
|---|
| 95 |
|
|---|
| 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}.
|
|---|
| 100 | */
|
|---|
| 101 |
|
|---|
| 102 | /*! \fn QVarLengthArray::~QVarLengthArray()
|
|---|
| 103 |
|
|---|
| 104 | Destroys the array.
|
|---|
| 105 | */
|
|---|
| 106 |
|
|---|
| 107 | /*! \fn int QVarLengthArray::size() const
|
|---|
| 108 |
|
|---|
| 109 | Returns the number of elements in the array.
|
|---|
| 110 |
|
|---|
| 111 | \sa isEmpty(), resize()
|
|---|
| 112 | */
|
|---|
| 113 |
|
|---|
| 114 | /*! \fn int QVarLengthArray::count() const
|
|---|
| 115 |
|
|---|
| 116 | Same as size().
|
|---|
| 117 |
|
|---|
| 118 | \sa isEmpty(), resize()
|
|---|
| 119 | */
|
|---|
| 120 |
|
|---|
| 121 | /*! \fn bool QVarLengthArray::isEmpty() const
|
|---|
| 122 |
|
|---|
| 123 | Returns true if the array has size 0; otherwise returns false.
|
|---|
| 124 |
|
|---|
| 125 | \sa size(), resize()
|
|---|
| 126 | */
|
|---|
| 127 |
|
|---|
| 128 | /*! \fn void QVarLengthArray::clear()
|
|---|
| 129 |
|
|---|
| 130 | Removes all the elements from the array.
|
|---|
| 131 |
|
|---|
| 132 | Same as resize(0).
|
|---|
| 133 | */
|
|---|
| 134 |
|
|---|
| 135 | /*! \fn void QVarLengthArray::resize(int size)
|
|---|
| 136 |
|
|---|
| 137 | Sets the size of the array to \a size. If \a size is greater than
|
|---|
| 138 | the current size, elements are added to the end. If \a size is
|
|---|
| 139 | less than the current size, elements are removed from the end.
|
|---|
| 140 |
|
|---|
| 141 | If the value type is a primitive type (e.g., char, int, float) or
|
|---|
| 142 | a pointer type (e.g., QWidget *), new elements are not
|
|---|
| 143 | initialized. For other types, the elements are initialized with a
|
|---|
| 144 | \l{default-constructed value}.
|
|---|
| 145 |
|
|---|
| 146 | \sa size()
|
|---|
| 147 | */
|
|---|
| 148 |
|
|---|
| 149 | /*! \fn int QVarLengthArray::capacity() const
|
|---|
| 150 |
|
|---|
| 151 | Returns the maximum number of elements that can be stored in the
|
|---|
| 152 | array without forcing a reallocation.
|
|---|
| 153 |
|
|---|
| 154 | The sole purpose of this function is to provide a means of fine
|
|---|
| 155 | tuning QVarLengthArray's memory usage. In general, you will rarely ever
|
|---|
| 156 | need to call this function. If you want to know how many items are
|
|---|
| 157 | in the array, call size().
|
|---|
| 158 |
|
|---|
| 159 | \sa reserve()
|
|---|
| 160 | */
|
|---|
| 161 |
|
|---|
| 162 | /*! \fn void QVarLengthArray::reserve(int size)
|
|---|
| 163 |
|
|---|
| 164 | Attempts to allocate memory for at least \a size elements. If you
|
|---|
| 165 | know in advance how large the array can get, you can call this
|
|---|
| 166 | function and if you call resize() often, you are likely to get
|
|---|
| 167 | better performance. If \a size is an underestimate, the worst
|
|---|
| 168 | that will happen is that the QVarLengthArray will be a bit
|
|---|
| 169 | slower.
|
|---|
| 170 |
|
|---|
| 171 | The sole purpose of this function is to provide a means of fine
|
|---|
| 172 | tuning QVarLengthArray's memory usage. In general, you will
|
|---|
| 173 | rarely ever need to call this function. If you want to change the
|
|---|
| 174 | size of the array, call resize().
|
|---|
| 175 |
|
|---|
| 176 | \sa capacity()
|
|---|
| 177 | */
|
|---|
| 178 |
|
|---|
| 179 | /*! \fn T &QVarLengthArray::operator[](int i)
|
|---|
| 180 |
|
|---|
| 181 | Returns a reference to the item at index position \a i.
|
|---|
| 182 |
|
|---|
| 183 | \a i must be a valid index position in the array (i.e., 0 <= \a i
|
|---|
| 184 | < size()).
|
|---|
| 185 |
|
|---|
| 186 | \sa data(), at()
|
|---|
| 187 | */
|
|---|
| 188 |
|
|---|
| 189 | /*! \fn const T &QVarLengthArray::operator[](int i) const
|
|---|
| 190 |
|
|---|
| 191 | \overload
|
|---|
| 192 | */
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 | /*!
|
|---|
| 196 | \fn void QVarLengthArray::append(const T &t)
|
|---|
| 197 |
|
|---|
| 198 | Appends item \a t to the array, extending the array if necessary.
|
|---|
| 199 |
|
|---|
| 200 | \sa removeLast()
|
|---|
| 201 | */
|
|---|
| 202 |
|
|---|
| 203 |
|
|---|
| 204 | /*!
|
|---|
| 205 | \fn inline void QVarLengthArray::removeLast()
|
|---|
| 206 | \since 4.5
|
|---|
| 207 |
|
|---|
| 208 | Decreases the size of the array by one. The allocated size is not changed.
|
|---|
| 209 |
|
|---|
| 210 | \sa append()
|
|---|
| 211 | */
|
|---|
| 212 |
|
|---|
| 213 | /*!
|
|---|
| 214 | \fn void QVarLengthArray::append(const T *buf, int size)
|
|---|
| 215 |
|
|---|
| 216 | Appends \a size amount of items referenced by \a buf to this array.
|
|---|
| 217 | */
|
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 | /*! \fn T *QVarLengthArray::data()
|
|---|
| 221 |
|
|---|
| 222 | Returns a pointer to the data stored in the array. The pointer can
|
|---|
| 223 | be used to access and modify the items in the array.
|
|---|
| 224 |
|
|---|
| 225 | Example:
|
|---|
| 226 | \snippet doc/src/snippets/code/doc_src_qvarlengtharray.qdoc 3
|
|---|
| 227 |
|
|---|
| 228 | The pointer remains valid as long as the array isn't reallocated.
|
|---|
| 229 |
|
|---|
| 230 | This function is mostly useful to pass an array to a function
|
|---|
| 231 | that accepts a plain C++ array.
|
|---|
| 232 |
|
|---|
| 233 | \sa constData(), operator[]()
|
|---|
| 234 | */
|
|---|
| 235 |
|
|---|
| 236 | /*! \fn const T *QVarLengthArray::data() const
|
|---|
| 237 |
|
|---|
| 238 | \overload
|
|---|
| 239 | */
|
|---|
| 240 |
|
|---|
| 241 | /*! \fn const T *QVarLengthArray::constData() const
|
|---|
| 242 |
|
|---|
| 243 | Returns a const pointer to the data stored in the array. The
|
|---|
| 244 | pointer can be used to access the items in the array. The
|
|---|
| 245 | pointer remains valid as long as the array isn't reallocated.
|
|---|
| 246 |
|
|---|
| 247 | This function is mostly useful to pass an array to a function
|
|---|
| 248 | that accepts a plain C++ array.
|
|---|
| 249 |
|
|---|
| 250 | \sa data(), operator[]()
|
|---|
| 251 | */
|
|---|
| 252 |
|
|---|
| 253 | /*! \fn QVarLengthArray<T, Prealloc> &QVarLengthArray::operator=(const QVarLengthArray<T, Prealloc> &other)
|
|---|
| 254 | Assigns \a other to this array and returns a reference to this array.
|
|---|
| 255 | */
|
|---|
| 256 |
|
|---|
| 257 | /*! \fn QVarLengthArray::QVarLengthArray(const QVarLengthArray<T, Prealloc> &other)
|
|---|
| 258 | Constructs a copy of \a other.
|
|---|
| 259 | */
|
|---|
| 260 |
|
|---|
| 261 | /*! \fn const T &QVarLengthArray::at(int i) const
|
|---|
| 262 |
|
|---|
| 263 | Returns a reference to the item at index position \a i.
|
|---|
| 264 |
|
|---|
| 265 | \a i must be a valid index position in the array (i.e., 0 <= \a i
|
|---|
| 266 | < size()).
|
|---|
| 267 |
|
|---|
| 268 | \sa value(), operator[]()
|
|---|
| 269 | */
|
|---|
| 270 |
|
|---|
| 271 | /*! \fn T QVarLengthArray::value(int i) const
|
|---|
| 272 |
|
|---|
| 273 | Returns the value at index position \a i.
|
|---|
| 274 |
|
|---|
| 275 | If the index \a i is out of bounds, the function returns
|
|---|
| 276 | a \l{default-constructed value}. If you are certain that
|
|---|
| 277 | \a i is within bounds, you can use at() instead, which is slightly
|
|---|
| 278 | faster.
|
|---|
| 279 |
|
|---|
| 280 | \sa at(), operator[]()
|
|---|
| 281 | */
|
|---|
| 282 |
|
|---|
| 283 | /*! \fn T QVarLengthArray::value(int i, const T &defaultValue) const
|
|---|
| 284 |
|
|---|
| 285 | \overload
|
|---|
| 286 |
|
|---|
| 287 | If the index \a i is out of bounds, the function returns
|
|---|
| 288 | \a defaultValue.
|
|---|
| 289 | */
|
|---|
| 290 |
|
|---|
| 291 | /*!
|
|---|
| 292 | \typedef QVarLengthArray::size_type
|
|---|
| 293 | \since 4.7
|
|---|
| 294 |
|
|---|
| 295 | Typedef for int. Provided for STL compatibility.
|
|---|
| 296 | */
|
|---|
| 297 |
|
|---|
| 298 | /*!
|
|---|
| 299 | \typedef QVarLengthArray::value_type
|
|---|
| 300 | \since 4.7
|
|---|
| 301 |
|
|---|
| 302 | Typedef for T. Provided for STL compatibility.
|
|---|
| 303 | */
|
|---|
| 304 |
|
|---|
| 305 | /*!
|
|---|
| 306 | \typedef QVarLengthArray::difference_type
|
|---|
| 307 | \since 4.7
|
|---|
| 308 |
|
|---|
| 309 | Typedef for ptrdiff_t. Provided for STL compatibility.
|
|---|
| 310 | */
|
|---|
| 311 |
|
|---|
| 312 | /*!
|
|---|
| 313 | \typedef QVarLengthArray::pointer
|
|---|
| 314 | \since 4.7
|
|---|
| 315 |
|
|---|
| 316 | Typedef for T *. Provided for STL compatibility.
|
|---|
| 317 | */
|
|---|
| 318 |
|
|---|
| 319 | /*!
|
|---|
| 320 | \typedef QVarLengthArray::const_pointer
|
|---|
| 321 | \since 4.7
|
|---|
| 322 |
|
|---|
| 323 | Typedef for const T *. Provided for STL compatibility.
|
|---|
| 324 | */
|
|---|
| 325 |
|
|---|
| 326 | /*!
|
|---|
| 327 | \typedef QVarLengthArray::reference
|
|---|
| 328 | \since 4.7
|
|---|
| 329 |
|
|---|
| 330 | Typedef for T &. Provided for STL compatibility.
|
|---|
| 331 | */
|
|---|
| 332 |
|
|---|
| 333 | /*!
|
|---|
| 334 | \typedef QVarLengthArray::const_reference
|
|---|
| 335 | \since 4.7
|
|---|
| 336 |
|
|---|
| 337 | Typedef for const T &. Provided for STL compatibility.
|
|---|
| 338 | */
|
|---|
| 339 |
|
|---|