| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qcontiguouscache.h" |
| 5 | #ifdef QT_QCONTIGUOUSCACHE_DEBUG |
| 6 | #include <QDebug> |
| 7 | #endif |
| 8 | |
| 9 | #include <QtCore/qmalloc.h> |
| 10 | |
| 11 | QT_BEGIN_NAMESPACE |
| 12 | |
| 13 | #ifdef QT_QCONTIGUOUSCACHE_DEBUG |
| 14 | void QContiguousCacheData::dump() const |
| 15 | { |
| 16 | qDebug() << "capacity:" << alloc; |
| 17 | qDebug() << "count:" << count; |
| 18 | qDebug() << "start:" << start; |
| 19 | qDebug() << "offset:" << offset; |
| 20 | } |
| 21 | #endif |
| 22 | |
| 23 | QContiguousCacheData *QContiguousCacheData::allocateData(qsizetype size, qsizetype alignment) |
| 24 | { |
| 25 | void *mem = qMallocAligned(size: size_t(size), alignment: size_t(alignment)); |
| 26 | return new (mem) QContiguousCacheData{/*ref=*/1, .alloc: 0, .count: 0, .start: 0, .offset: 0}; |
| 27 | } |
| 28 | |
| 29 | void QContiguousCacheData::freeData(QContiguousCacheData *data) |
| 30 | { |
| 31 | qFreeAligned(ptr: data); |
| 32 | } |
| 33 | |
| 34 | /*! \class QContiguousCache |
| 35 | \inmodule QtCore |
| 36 | \brief The QContiguousCache class is a template class that provides a contiguous cache. |
| 37 | \ingroup tools |
| 38 | \ingroup shared |
| 39 | \reentrant |
| 40 | \since 4.6 |
| 41 | |
| 42 | The QContiguousCache class provides an efficient way of caching items for |
| 43 | display in a user interface view. Unlike QCache, it adds a restriction |
| 44 | that elements within the cache are contiguous. This has the advantage |
| 45 | of matching how user interface views most commonly request data, as |
| 46 | a set of rows localized around the current scrolled position. This |
| 47 | restriction allows the cache to consume less memory and processor |
| 48 | cycles than QCache. |
| 49 | |
| 50 | QContiguousCache operates on a fixed capacity, set with setCapacity() or |
| 51 | passed as a parameter to the constructor. This capacity is the upper bound |
| 52 | on memory usage by the cache itself, not including the memory allocated by |
| 53 | the elements themselves. Note that a cache with a capacity of zero (the |
| 54 | default) means no items will be stored: the insert(), append() and |
| 55 | prepend() operations will effectively be no-ops. Therefore, it's important |
| 56 | to set the capacity to a reasonable value before adding items to the cache. |
| 57 | |
| 58 | The simplest way of using a contiguous cache is to use the append() |
| 59 | and prepend(). |
| 60 | |
| 61 | \snippet code/src_corelib_tools_qcontiguouscache.cpp 0 |
| 62 | |
| 63 | If the cache is full then the item at the opposite end of the cache from |
| 64 | where the new item is appended or prepended will be removed. |
| 65 | |
| 66 | This usage can be further optimized by using the insert() function |
| 67 | in the case where the requested row is a long way from the currently cached |
| 68 | items. If there is a gap between where the new item is inserted and the currently |
| 69 | cached items then the existing cached items are first removed to retain |
| 70 | the contiguous nature of the cache. Hence it is important to take some care then |
| 71 | when using insert() in order to avoid unwanted clearing of the cache. |
| 72 | |
| 73 | The range of valid indexes for the QContiguousCache class are from |
| 74 | 0 to INT_MAX. Calling prepend() such that the first index would become less |
| 75 | than 0 or append() such that the last index would become greater |
| 76 | than INT_MAX can result in the indexes of the cache being invalid. |
| 77 | When the cache indexes are invalid it is important to call |
| 78 | normalizeIndexes() before calling any of containsIndex(), firstIndex(), |
| 79 | lastIndex(), at() or \l{QContiguousCache::operator[]()}{operator[]()}. |
| 80 | Calling these functions when the cache has invalid indexes will result in |
| 81 | undefined behavior. The indexes can be checked by using areIndexesValid() |
| 82 | |
| 83 | In most cases the indexes will not exceed 0 to INT_MAX, and |
| 84 | normalizeIndexes() will not need to be used. |
| 85 | |
| 86 | See the \l{Contiguous Cache Example}{Contiguous Cache} example. |
| 87 | */ |
| 88 | |
| 89 | /*! \fn template<typename T> QContiguousCache<T>::QContiguousCache(qsizetype capacity) |
| 90 | |
| 91 | Constructs a cache with the given \a capacity. |
| 92 | |
| 93 | \sa setCapacity() |
| 94 | */ |
| 95 | |
| 96 | /*! \fn template<typename T> QContiguousCache<T>::QContiguousCache(const QContiguousCache<T> &other) |
| 97 | |
| 98 | Constructs a copy of \a other. |
| 99 | |
| 100 | This operation takes \l{constant time}, because QContiguousCache is |
| 101 | \l{implicitly shared}. This makes returning a QContiguousCache from a |
| 102 | function very fast. If a shared instance is modified, it will be |
| 103 | copied (copy-on-write), and that takes \l{linear time}. |
| 104 | |
| 105 | \sa operator=() |
| 106 | */ |
| 107 | |
| 108 | /*! \fn template<typename T> QContiguousCache<T>::~QContiguousCache() |
| 109 | |
| 110 | Destroys the cache. |
| 111 | */ |
| 112 | |
| 113 | /*! \fn template<typename T> void QContiguousCache<T>::detach() |
| 114 | \internal |
| 115 | */ |
| 116 | |
| 117 | /*! \fn template<typename T> bool QContiguousCache<T>::isDetached() const |
| 118 | \internal |
| 119 | */ |
| 120 | |
| 121 | /*! \fn template<typename T> void QContiguousCache<T>::setSharable(bool sharable) |
| 122 | \internal |
| 123 | */ |
| 124 | |
| 125 | /*! \typedef QContiguousCache::value_type |
| 126 | \internal |
| 127 | */ |
| 128 | |
| 129 | /*! \typedef QContiguousCache::pointer |
| 130 | \internal |
| 131 | */ |
| 132 | |
| 133 | /*! \typedef QContiguousCache::const_pointer |
| 134 | \internal |
| 135 | */ |
| 136 | |
| 137 | /*! \typedef QContiguousCache::reference |
| 138 | \internal |
| 139 | */ |
| 140 | |
| 141 | /*! \typedef QContiguousCache::const_reference |
| 142 | \internal |
| 143 | */ |
| 144 | |
| 145 | /*! \typedef QContiguousCache::difference_type |
| 146 | \internal |
| 147 | */ |
| 148 | |
| 149 | /*! \typedef QContiguousCache::size_type |
| 150 | \internal |
| 151 | */ |
| 152 | |
| 153 | /*! \fn template<typename T> QContiguousCache<T> &QContiguousCache<T>::operator=(const QContiguousCache<T> &other) |
| 154 | |
| 155 | Assigns \a other to this cache and returns a reference to this cache. |
| 156 | */ |
| 157 | |
| 158 | /*! |
| 159 | \fn template<typename T> QContiguousCache<T> &QContiguousCache<T>::operator=(QContiguousCache<T> &&other) |
| 160 | |
| 161 | Move-assigns \a other to this QContiguousCache instance. |
| 162 | |
| 163 | \since 5.2 |
| 164 | */ |
| 165 | |
| 166 | /*! \fn template<typename T> void QContiguousCache<T>::swap(QContiguousCache<T> &other) |
| 167 | \since 4.8 |
| 168 | \memberswap{cache} |
| 169 | */ |
| 170 | |
| 171 | /*! \fn template<typename T> bool QContiguousCache<T>::operator==(const QContiguousCache<T> &other) const |
| 172 | |
| 173 | Returns \c true if \a other is equal to this cache; otherwise returns \c false. |
| 174 | |
| 175 | Two caches are considered equal if they contain the same values at the same |
| 176 | indexes. This function requires the value type to implement the \c operator==(). |
| 177 | |
| 178 | \sa operator!=() |
| 179 | */ |
| 180 | |
| 181 | /*! \fn template<typename T> bool QContiguousCache<T>::operator!=(const QContiguousCache<T> &other) const |
| 182 | |
| 183 | Returns \c true if \a other is not equal to this cache; otherwise |
| 184 | returns \c false. |
| 185 | |
| 186 | Two caches are considered equal if they contain the same values at the same |
| 187 | indexes. This function requires the value type to implement the \c operator==(). |
| 188 | |
| 189 | \sa operator==() |
| 190 | */ |
| 191 | |
| 192 | /*! \fn template<typename T> qsizetype QContiguousCache<T>::capacity() const |
| 193 | |
| 194 | Returns the number of items the cache can store before it is full. |
| 195 | When a cache contains a number of items equal to its capacity, adding new |
| 196 | items will cause items farthest from the added item to be removed. |
| 197 | |
| 198 | \sa setCapacity(), size() |
| 199 | */ |
| 200 | |
| 201 | /*! \fn template<typename T> qsizetype QContiguousCache<T>::count() const |
| 202 | |
| 203 | Same as size(). |
| 204 | */ |
| 205 | |
| 206 | /*! \fn template<typename T> qsizetype QContiguousCache<T>::size() const |
| 207 | |
| 208 | Returns the number of items contained within the cache. |
| 209 | |
| 210 | \sa capacity() |
| 211 | */ |
| 212 | |
| 213 | /*! \fn template<typename T> bool QContiguousCache<T>::isEmpty() const |
| 214 | |
| 215 | Returns \c true if no items are stored within the cache. |
| 216 | |
| 217 | \sa size(), capacity() |
| 218 | */ |
| 219 | |
| 220 | /*! \fn template<typename T> bool QContiguousCache<T>::isFull() const |
| 221 | |
| 222 | Returns \c true if the number of items stored within the cache is equal |
| 223 | to the capacity of the cache. |
| 224 | |
| 225 | \sa size(), capacity() |
| 226 | */ |
| 227 | |
| 228 | /*! \fn template<typename T> qsizetype QContiguousCache<T>::available() const |
| 229 | |
| 230 | Returns the number of items that can be added to the cache before it becomes full. |
| 231 | |
| 232 | \sa size(), capacity(), isFull() |
| 233 | */ |
| 234 | |
| 235 | /*! \fn template<typename T> void QContiguousCache<T>::clear() |
| 236 | |
| 237 | Removes all items from the cache. The capacity is unchanged. |
| 238 | */ |
| 239 | |
| 240 | /*! \fn template<typename T> void QContiguousCache<T>::setCapacity(qsizetype size) |
| 241 | |
| 242 | Sets the capacity of the cache to the given \a size. A cache can hold a |
| 243 | number of items equal to its capacity. When inserting, appending or prepending |
| 244 | items to the cache, if the cache is already full then the item farthest from |
| 245 | the added item will be removed. |
| 246 | |
| 247 | If the given \a size is smaller than the current count of items in the cache |
| 248 | then only the last \a size items from the cache will remain. |
| 249 | |
| 250 | \sa capacity(), isFull() |
| 251 | */ |
| 252 | |
| 253 | /*! \fn template<typename T> const T &QContiguousCache<T>::at(qsizetype i) const |
| 254 | |
| 255 | Returns the item at index position \a i in the cache. \a i must |
| 256 | be a valid index position in the cache (i.e, firstIndex() <= \a i <= lastIndex()). |
| 257 | |
| 258 | The indexes in the cache refer to the number of positions the item is from the |
| 259 | first item appended into the cache. That is to say a cache with a capacity of |
| 260 | 100, that has had 150 items appended will have a valid index range of |
| 261 | 50 to 149. This allows inserting and retrieving items into the cache based |
| 262 | on a theoretical infinite list |
| 263 | |
| 264 | \sa firstIndex(), lastIndex(), insert(), operator[]() |
| 265 | */ |
| 266 | |
| 267 | /*! \fn template<typename T> T &QContiguousCache<T>::operator[](qsizetype i) |
| 268 | |
| 269 | Returns the item at index position \a i as a modifiable reference. If |
| 270 | the cache does not contain an item at the given index position \a i |
| 271 | then it will first insert an empty item at that position. |
| 272 | |
| 273 | In most cases it is better to use either at() or insert(). |
| 274 | |
| 275 | \note This non-const overload of operator[] requires QContiguousCache |
| 276 | to make a deep copy. Use at() for read-only access to a non-const |
| 277 | QContiguousCache. |
| 278 | |
| 279 | \sa insert(), at() |
| 280 | */ |
| 281 | |
| 282 | /*! \fn template<typename T> const T &QContiguousCache<T>::operator[](qsizetype i) const |
| 283 | |
| 284 | \overload |
| 285 | |
| 286 | Same as at(\a i). |
| 287 | */ |
| 288 | |
| 289 | /*! \fn template<typename T> void QContiguousCache<T>::append(const T &value) |
| 290 | |
| 291 | Inserts \a value at the end of the cache. If the cache is already full |
| 292 | the item at the start of the cache will be removed. |
| 293 | |
| 294 | \sa prepend(), insert(), isFull() |
| 295 | */ |
| 296 | |
| 297 | /*! \fn template<typename T> void QContiguousCache<T>::prepend(const T &value) |
| 298 | |
| 299 | Inserts \a value at the start of the cache. If the cache is already full |
| 300 | the item at the end of the cache will be removed. |
| 301 | |
| 302 | \sa append(), insert(), isFull() |
| 303 | */ |
| 304 | |
| 305 | /*! \fn template<typename T> void QContiguousCache<T>::insert(qsizetype i, const T &value) |
| 306 | |
| 307 | Inserts the \a value at the index position \a i. If the cache already contains |
| 308 | an item at \a i then that value is replaced. If \a i is either one more than |
| 309 | lastIndex() or one less than firstIndex() it is the equivalent to an append() |
| 310 | or a prepend(). |
| 311 | |
| 312 | If the given index \a i is not within the current range of the cache nor adjacent |
| 313 | to the bounds of the cache's index range, the cache is first cleared before |
| 314 | inserting the item. At this point the cache will have a size of 1. It is |
| 315 | worthwhile taking effort to insert items in an order that starts adjacent |
| 316 | to the current index range for the cache. |
| 317 | |
| 318 | The range of valid indexes for the QContiguousCache class are from |
| 319 | 0 to INT_MAX. Inserting outside of this range has undefined behavior. |
| 320 | |
| 321 | |
| 322 | \sa prepend(), append(), isFull(), firstIndex(), lastIndex() |
| 323 | */ |
| 324 | |
| 325 | /*! \fn template<typename T> bool QContiguousCache<T>::containsIndex(qsizetype i) const |
| 326 | |
| 327 | Returns \c true if the cache's index range includes the given index \a i. |
| 328 | |
| 329 | \sa firstIndex(), lastIndex() |
| 330 | */ |
| 331 | |
| 332 | /*! \fn template<typename T> qsizetype QContiguousCache<T>::firstIndex() const |
| 333 | |
| 334 | Returns the first valid index in the cache. The index will be invalid if the |
| 335 | cache is empty. |
| 336 | |
| 337 | \sa capacity(), size(), lastIndex() |
| 338 | */ |
| 339 | |
| 340 | /*! \fn template<typename T> qsizetype QContiguousCache<T>::lastIndex() const |
| 341 | |
| 342 | Returns the last valid index in the cache. The index will be invalid if the cache is empty. |
| 343 | |
| 344 | \sa capacity(), size(), firstIndex() |
| 345 | */ |
| 346 | |
| 347 | |
| 348 | /*! \fn template<typename T> T &QContiguousCache<T>::first() |
| 349 | |
| 350 | Returns a reference to the first item in the cache. This function |
| 351 | assumes that the cache isn't empty. |
| 352 | |
| 353 | \sa last(), isEmpty() |
| 354 | */ |
| 355 | |
| 356 | /*! \fn template<typename T> T &QContiguousCache<T>::last() |
| 357 | |
| 358 | Returns a reference to the last item in the cache. This function |
| 359 | assumes that the cache isn't empty. |
| 360 | |
| 361 | \sa first(), isEmpty() |
| 362 | */ |
| 363 | |
| 364 | /*! \fn template<typename T> const T& QContiguousCache<T>::first() const |
| 365 | |
| 366 | \overload |
| 367 | */ |
| 368 | |
| 369 | /*! \fn template<typename T> const T& QContiguousCache<T>::last() const |
| 370 | |
| 371 | \overload |
| 372 | */ |
| 373 | |
| 374 | /*! \fn template<typename T> void QContiguousCache<T>::removeFirst() |
| 375 | |
| 376 | Removes the first item from the cache. This function assumes that |
| 377 | the cache isn't empty. |
| 378 | |
| 379 | \sa removeLast() |
| 380 | */ |
| 381 | |
| 382 | /*! \fn template<typename T> void QContiguousCache<T>::removeLast() |
| 383 | |
| 384 | Removes the last item from the cache. This function assumes that |
| 385 | the cache isn't empty. |
| 386 | |
| 387 | \sa removeFirst() |
| 388 | */ |
| 389 | |
| 390 | /*! \fn template<typename T> T QContiguousCache<T>::takeFirst() |
| 391 | |
| 392 | Removes the first item in the cache and returns it. This function |
| 393 | assumes that the cache isn't empty. |
| 394 | |
| 395 | If you don't use the return value, removeFirst() is more efficient. |
| 396 | |
| 397 | \sa takeLast(), removeFirst() |
| 398 | */ |
| 399 | |
| 400 | /*! \fn template<typename T> T QContiguousCache<T>::takeLast() |
| 401 | |
| 402 | Removes the last item in the cache and returns it. This function |
| 403 | assumes that the cache isn't empty. |
| 404 | |
| 405 | If you don't use the return value, removeLast() is more efficient. |
| 406 | |
| 407 | \sa takeFirst(), removeLast() |
| 408 | */ |
| 409 | |
| 410 | /*! \fn template<typename T> void QContiguousCache<T>::normalizeIndexes() |
| 411 | |
| 412 | Moves the first index and last index of the cache |
| 413 | such that they point to valid indexes. The function does not modify |
| 414 | the contents of the cache or the ordering of elements within the cache. |
| 415 | |
| 416 | It is provided so that index overflows can be corrected when using the |
| 417 | cache as a circular buffer. |
| 418 | |
| 419 | \snippet code/src_corelib_tools_qcontiguouscache.cpp 1 |
| 420 | |
| 421 | \sa areIndexesValid(), append(), prepend() |
| 422 | */ |
| 423 | |
| 424 | /*! \fn template<typename T> bool QContiguousCache<T>::areIndexesValid() const |
| 425 | |
| 426 | Returns whether the indexes for items stored in the cache are valid. |
| 427 | Indexes can become invalid if items are appended after the index position |
| 428 | INT_MAX or prepended before the index position 0. This is only expected |
| 429 | to occur in very long lived circular buffer style usage of the |
| 430 | contiguous cache. Indexes can be made valid again by calling |
| 431 | normalizeIndexes(). |
| 432 | |
| 433 | \sa normalizeIndexes(), append(), prepend() |
| 434 | */ |
| 435 | |
| 436 | QT_END_NAMESPACE |
| 437 | |