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