1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
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 qt-info@nokia.com.
|
---|
24 | ** $QT_END_LICENSE$
|
---|
25 | **
|
---|
26 | ****************************************************************************/
|
---|
27 |
|
---|
28 | /*!
|
---|
29 | \class Q3IntCache
|
---|
30 | \brief The Q3IntCache class is a template class that provides a cache based on long keys.
|
---|
31 | \compat
|
---|
32 |
|
---|
33 | Q3IntCache is implemented as a template class. Define a template
|
---|
34 | instance Q3IntCache\<X\> to create a cache that operates on
|
---|
35 | pointers to X, or X*.
|
---|
36 |
|
---|
37 | A cache is a least recently used (LRU) list of cache items,
|
---|
38 | accessed via \c long keys. Each cache item has a cost. The sum
|
---|
39 | of item costs, totalCost(), will not exceed the maximum cache
|
---|
40 | cost, maxCost(). If inserting a new item would cause the total
|
---|
41 | cost to exceed the maximum cost, the least recently used items in
|
---|
42 | the cache are removed.
|
---|
43 |
|
---|
44 | Apart from insert(), by far the most important function is find()
|
---|
45 | (which also exists as operator[]). This function looks up an
|
---|
46 | item, returns it, and by default marks it as being the most
|
---|
47 | recently used item.
|
---|
48 |
|
---|
49 | There are also methods to remove() or take() an object from the
|
---|
50 | cache. Calling setAutoDelete(TRUE) for a cache tells it to delete
|
---|
51 | items that are removed. The default is to not delete items when
|
---|
52 | they are removed (i.e. remove() and take() are equivalent).
|
---|
53 |
|
---|
54 | When inserting an item into the cache, only the pointer is copied,
|
---|
55 | not the item itself. This is called a shallow copy. It is possible
|
---|
56 | to make the cache copy all of the item's data (known as a deep
|
---|
57 | copy) when an item is inserted. insert() calls the virtual
|
---|
58 | function Q3PtrCollection::newItem() for the item to be inserted.
|
---|
59 | Inherit a dictionary and reimplement newItem() if you want deep
|
---|
60 | copies.
|
---|
61 |
|
---|
62 | When removing a cache item, the item will be automatically
|
---|
63 | deleted if auto-deletion is enabled.
|
---|
64 |
|
---|
65 | There is a Q3IntCacheIterator which may be used to traverse the
|
---|
66 | items in the cache in arbitrary order.
|
---|
67 |
|
---|
68 | \sa Q3IntCacheIterator, Q3Cache, Q3AsciiCache
|
---|
69 | */
|
---|
70 |
|
---|
71 | /*!
|
---|
72 | \fn Q3IntCache::Q3IntCache( const Q3IntCache<type> &c )
|
---|
73 |
|
---|
74 | \internal
|
---|
75 |
|
---|
76 | Do not use. A Q3IntCache cannot be copied. Calls qFatal() in debug version.
|
---|
77 | */
|
---|
78 |
|
---|
79 | /*!
|
---|
80 | \fn Q3IntCache::Q3IntCache( int maxCost, int size )
|
---|
81 |
|
---|
82 | Constructs a cache whose contents will never have a total cost
|
---|
83 | greater than \a maxCost and which is expected to contain less than
|
---|
84 | \a size items.
|
---|
85 |
|
---|
86 | \a size is actually the size of an internal hash array; it's
|
---|
87 | usually best to make it prime and at least 50% bigger than the
|
---|
88 | largest expected number of items in the cache.
|
---|
89 |
|
---|
90 | Each inserted item is associated with a cost. When inserting a new
|
---|
91 | item, if the total cost of all items in the cache will exceed \a
|
---|
92 | maxCost, the cache will start throwing out the older (least
|
---|
93 | recently used) items until there is enough room for the new item
|
---|
94 | to be inserted.
|
---|
95 | */
|
---|
96 |
|
---|
97 | /*!
|
---|
98 | \fn Q3IntCache::~Q3IntCache()
|
---|
99 |
|
---|
100 | Removes all items from the cache and then destroys the int cache.
|
---|
101 | If auto-deletion is enabled the cache's items are deleted. All
|
---|
102 | iterators that access this cache will be reset.
|
---|
103 | */
|
---|
104 |
|
---|
105 | /*!
|
---|
106 | \fn Q3IntCache<type>& Q3IntCache::operator=( const Q3IntCache<type> &c )
|
---|
107 |
|
---|
108 | \internal
|
---|
109 |
|
---|
110 | Do not use. A Q3IntCache cannot be copied. Calls qFatal() in debug version.
|
---|
111 | */
|
---|
112 |
|
---|
113 | /*!
|
---|
114 | \fn int Q3IntCache::maxCost() const
|
---|
115 |
|
---|
116 | Returns the maximum allowed total cost of the cache.
|
---|
117 |
|
---|
118 | \sa setMaxCost() totalCost()
|
---|
119 | */
|
---|
120 |
|
---|
121 | /*!
|
---|
122 | \fn int Q3IntCache::totalCost() const
|
---|
123 |
|
---|
124 | Returns the total cost of the items in the cache. This is an
|
---|
125 | integer in the range 0 to maxCost().
|
---|
126 |
|
---|
127 | \sa setMaxCost()
|
---|
128 | */
|
---|
129 |
|
---|
130 | /*!
|
---|
131 | \fn void Q3IntCache::setMaxCost( int m )
|
---|
132 |
|
---|
133 | Sets the maximum allowed total cost of the cache to \a m. If the
|
---|
134 | current total cost is greater than \a m, some items are removed
|
---|
135 | immediately.
|
---|
136 |
|
---|
137 | \sa maxCost() totalCost()
|
---|
138 | */
|
---|
139 |
|
---|
140 | /*!
|
---|
141 | \fn uint Q3IntCache::count() const
|
---|
142 |
|
---|
143 | Returns the number of items in the cache.
|
---|
144 |
|
---|
145 | \sa totalCost()
|
---|
146 | */
|
---|
147 |
|
---|
148 | /*!
|
---|
149 | \fn uint Q3IntCache::size() const
|
---|
150 |
|
---|
151 | Returns the size of the hash array used to implement the cache.
|
---|
152 | This should be a bit larger than count() is likely to be.
|
---|
153 | */
|
---|
154 |
|
---|
155 | /*!
|
---|
156 | \fn bool Q3IntCache::isEmpty() const
|
---|
157 |
|
---|
158 | Returns TRUE if the cache is empty; otherwise returns FALSE.
|
---|
159 | */
|
---|
160 |
|
---|
161 | /*!
|
---|
162 | \fn bool Q3IntCache::insert( long k, const type *d, int c, int p )
|
---|
163 |
|
---|
164 | Inserts the item \a d into the cache with key \a k and assigns it
|
---|
165 | a cost of \a c (default 1). Returns TRUE if it succeeds; otherwise
|
---|
166 | returns FALSE.
|
---|
167 |
|
---|
168 | The cache's size is limited, and if the total cost is too high,
|
---|
169 | Q3IntCache will remove old, least-used items until there is room
|
---|
170 | for this new item.
|
---|
171 |
|
---|
172 | The parameter \a p is internal and should be left at the default
|
---|
173 | value (0).
|
---|
174 |
|
---|
175 | \warning If this function returns FALSE (for example, the cost \c,
|
---|
176 | exceeds maxCost()), you must delete \a d yourself. Additionally,
|
---|
177 | be very careful about using \a d after calling this function. Any
|
---|
178 | other insertions into the cache, from anywhere in the application
|
---|
179 | or within Qt itself, could cause the object to be discarded from
|
---|
180 | the cache and the pointer to become invalid.
|
---|
181 | */
|
---|
182 |
|
---|
183 | /*!
|
---|
184 | \fn bool Q3IntCache::remove( long k )
|
---|
185 |
|
---|
186 | Removes the item associated with \a k, and returns TRUE if the
|
---|
187 | item was present in the cache; otherwise returns FALSE.
|
---|
188 |
|
---|
189 | The item is deleted if auto-deletion has been enabled, i.e. if you
|
---|
190 | have called setAutoDelete(TRUE).
|
---|
191 |
|
---|
192 | If there are two or more items with equal keys, the one that was
|
---|
193 | inserted most recently is removed.
|
---|
194 |
|
---|
195 | All iterators that refer to the removed item are set to point to
|
---|
196 | the next item in the cache's traversal order.
|
---|
197 |
|
---|
198 | \sa take(), clear()
|
---|
199 | */
|
---|
200 |
|
---|
201 | /*!
|
---|
202 | \fn type * Q3IntCache::take( long k )
|
---|
203 |
|
---|
204 | Takes the item associated with \a k out of the cache without
|
---|
205 | deleting it, and returns a pointer to the item taken out or 0 if
|
---|
206 | the key does not exist in the cache.
|
---|
207 |
|
---|
208 | If there are two or more items with equal keys, the one that was
|
---|
209 | inserted most recently is taken.
|
---|
210 |
|
---|
211 | All iterators that refer to the taken item are set to point to the
|
---|
212 | next item in the cache's traversal order.
|
---|
213 |
|
---|
214 | \sa remove(), clear()
|
---|
215 | */
|
---|
216 |
|
---|
217 | /*!
|
---|
218 | \fn void Q3IntCache::clear()
|
---|
219 |
|
---|
220 | Removes all items from the cache, and deletes them if
|
---|
221 | auto-deletion has been enabled.
|
---|
222 |
|
---|
223 | All cache iterators that operate this on cache are reset.
|
---|
224 |
|
---|
225 | \sa remove() take()
|
---|
226 | */
|
---|
227 |
|
---|
228 | /*!
|
---|
229 | \fn type * Q3IntCache::find( long k, bool ref ) const
|
---|
230 |
|
---|
231 | Returns the item associated with \a k, or 0 if the key does not
|
---|
232 | exist in the cache. If \a ref is TRUE (the default), the item is
|
---|
233 | moved to the front of the least recently used list.
|
---|
234 |
|
---|
235 | If there are two or more items with equal keys, the one that was
|
---|
236 | inserted most recently is returned.
|
---|
237 | */
|
---|
238 |
|
---|
239 | /*!
|
---|
240 | \fn type * Q3IntCache::operator[]( long k ) const
|
---|
241 |
|
---|
242 | Returns the item associated with \a k, or 0 if \a k does not exist
|
---|
243 | in the cache, and moves the item to the front of the least
|
---|
244 | recently used list.
|
---|
245 |
|
---|
246 | If there are two or more items with equal keys, the one that was
|
---|
247 | inserted most recently is returned.
|
---|
248 |
|
---|
249 | This is the same as find( k, TRUE ).
|
---|
250 |
|
---|
251 | \sa find()
|
---|
252 | */
|
---|
253 |
|
---|
254 | /*!
|
---|
255 | \fn void Q3IntCache::statistics() const
|
---|
256 |
|
---|
257 | A debug-only utility function. Prints out cache usage, hit/miss,
|
---|
258 | and distribution information using qDebug(). This function does
|
---|
259 | nothing in the release library.
|
---|
260 | */
|
---|
261 |
|
---|
262 | /*!
|
---|
263 | \class Q3IntCacheIterator
|
---|
264 | \brief The Q3IntCacheIterator class provides an iterator for Q3IntCache collections.
|
---|
265 | \compat
|
---|
266 |
|
---|
267 | Note that the traversal order is arbitrary; you are not guaranteed
|
---|
268 | any particular order. If new objects are inserted into the cache
|
---|
269 | while the iterator is active, the iterator may or may not see
|
---|
270 | them.
|
---|
271 |
|
---|
272 | Multiple iterators are completely independent, even when they
|
---|
273 | operate on the same Q3IntCache. Q3IntCache updates all iterators
|
---|
274 | that refer an item when that item is removed.
|
---|
275 |
|
---|
276 | Q3IntCacheIterator provides an operator++(), and an operator+=() to
|
---|
277 | traverse the cache; current() and currentKey() to access the
|
---|
278 | current cache item and its key; atFirst() atLast(), which return
|
---|
279 | TRUE if the iterator points to the first/last item in the cache;
|
---|
280 | isEmpty(), which returns TRUE if the cache is empty; and count(),
|
---|
281 | which returns the number of items in the cache.
|
---|
282 |
|
---|
283 | Note that atFirst() and atLast() refer to the iterator's arbitrary
|
---|
284 | ordering, not to the cache's internal least recently used list.
|
---|
285 |
|
---|
286 | \sa Q3IntCache
|
---|
287 | */
|
---|
288 |
|
---|
289 | /*!
|
---|
290 | \fn Q3IntCacheIterator::Q3IntCacheIterator( const Q3IntCache<type> &cache )
|
---|
291 |
|
---|
292 | Constructs an iterator for \a cache. The current iterator item is
|
---|
293 | set to point to the first item in the \a cache (or rather, the
|
---|
294 | first item is defined to be the item at which this constructor
|
---|
295 | sets the iterator to point).
|
---|
296 | */
|
---|
297 |
|
---|
298 | /*!
|
---|
299 | \fn Q3IntCacheIterator::Q3IntCacheIterator (const Q3IntCacheIterator<type> & ci)
|
---|
300 |
|
---|
301 | Constructs an iterator for the same cache as \a ci. The new
|
---|
302 | iterator starts at the same item as ci.current(), but moves
|
---|
303 | independently from there on.
|
---|
304 | */
|
---|
305 |
|
---|
306 | /*!
|
---|
307 | \fn Q3IntCacheIterator<type>& Q3IntCacheIterator::operator=( const Q3IntCacheIterator<type> &ci )
|
---|
308 |
|
---|
309 | Makes this an iterator for the same cache as \a ci. The new
|
---|
310 | iterator starts at the same item as ci.current(), but moves
|
---|
311 | independently thereafter.
|
---|
312 | */
|
---|
313 |
|
---|
314 | /*!
|
---|
315 | \fn uint Q3IntCacheIterator::count() const
|
---|
316 |
|
---|
317 | Returns the number of items in the cache on which this iterator
|
---|
318 | operates.
|
---|
319 |
|
---|
320 | \sa isEmpty()
|
---|
321 | */
|
---|
322 |
|
---|
323 | /*!
|
---|
324 | \fn bool Q3IntCacheIterator::isEmpty() const
|
---|
325 |
|
---|
326 | Returns TRUE if the cache is empty; otherwise returns FALSE.
|
---|
327 |
|
---|
328 | \sa count()
|
---|
329 | */
|
---|
330 |
|
---|
331 | /*!
|
---|
332 | \fn bool Q3IntCacheIterator::atFirst() const
|
---|
333 |
|
---|
334 | Returns TRUE if the iterator points to the first item in the
|
---|
335 | cache; otherwise returns FALSE. Note that this refers to the
|
---|
336 | iterator's arbitrary ordering, not to the cache's internal least
|
---|
337 | recently used list.
|
---|
338 |
|
---|
339 | \sa toFirst(), atLast()
|
---|
340 | */
|
---|
341 |
|
---|
342 | /*!
|
---|
343 | \fn bool Q3IntCacheIterator::atLast() const
|
---|
344 |
|
---|
345 | Returns TRUE if the iterator points to the last item in the cache;
|
---|
346 | otherwise returns FALSE. Note that this refers to the iterator's
|
---|
347 | arbitrary ordering, not to the cache's internal least recently
|
---|
348 | used list.
|
---|
349 |
|
---|
350 | \sa toLast(), atFirst()
|
---|
351 | */
|
---|
352 |
|
---|
353 | /*!
|
---|
354 | \fn type *Q3IntCacheIterator::toFirst()
|
---|
355 |
|
---|
356 | Sets the iterator to point to the first item in the cache and
|
---|
357 | returns a pointer to the item.
|
---|
358 |
|
---|
359 | Sets the iterator to 0, and returns 0, if the cache is empty.
|
---|
360 |
|
---|
361 | \sa toLast() isEmpty()
|
---|
362 | */
|
---|
363 |
|
---|
364 | /*!
|
---|
365 | \fn type *Q3IntCacheIterator::toLast()
|
---|
366 |
|
---|
367 | Sets the iterator to point to the last item in the cache and
|
---|
368 | returns a pointer to the item.
|
---|
369 |
|
---|
370 | Sets the iterator to 0, and returns 0, if the cache is empty.
|
---|
371 |
|
---|
372 | \sa toFirst() isEmpty()
|
---|
373 | */
|
---|
374 |
|
---|
375 | /*!
|
---|
376 | \fn Q3IntCacheIterator::operator type *() const
|
---|
377 |
|
---|
378 | Cast operator. Returns a pointer to the current iterator item.
|
---|
379 | Same as current().
|
---|
380 | */
|
---|
381 |
|
---|
382 | /*!
|
---|
383 | \fn type *Q3IntCacheIterator::current() const
|
---|
384 |
|
---|
385 | Returns a pointer to the current iterator item.
|
---|
386 | */
|
---|
387 |
|
---|
388 | /*!
|
---|
389 | \fn long Q3IntCacheIterator::currentKey() const
|
---|
390 |
|
---|
391 | Returns the key for the current iterator item.
|
---|
392 | */
|
---|
393 |
|
---|
394 | /*!
|
---|
395 | \fn type *Q3IntCacheIterator::operator()()
|
---|
396 |
|
---|
397 | Makes the succeeding item current and returns the original current
|
---|
398 | item.
|
---|
399 |
|
---|
400 | If the current iterator item was the last item in the cache or if
|
---|
401 | it was 0, 0 is returned.
|
---|
402 | */
|
---|
403 |
|
---|
404 | /*!
|
---|
405 | \fn type *Q3IntCacheIterator::operator+=( uint jump )
|
---|
406 |
|
---|
407 | Returns the item \a jump positions after the current item, or 0 if
|
---|
408 | it is beyond the last item. Makes this the current item.
|
---|
409 | */
|
---|
410 |
|
---|
411 | /*!
|
---|
412 | \fn type *Q3IntCacheIterator::operator-=( uint jump )
|
---|
413 |
|
---|
414 | Returns the item \a jump positions before the current item, or 0
|
---|
415 | if it is beyond the first item. Makes this the current item.
|
---|
416 | */
|
---|
417 |
|
---|
418 | /*!
|
---|
419 | \fn type *Q3IntCacheIterator::operator++()
|
---|
420 |
|
---|
421 | Prefix ++ makes the iterator point to the item just after
|
---|
422 | current(), and makes it the new current item for the iterator. If
|
---|
423 | current() was the last item, operator--() returns 0.
|
---|
424 | */
|
---|
425 |
|
---|
426 | /*!
|
---|
427 | \fn type *Q3IntCacheIterator::operator--()
|
---|
428 |
|
---|
429 | Prefix -- makes the iterator point to the item just before
|
---|
430 | current(), and makes it the new current item for the iterator. If
|
---|
431 | current() was the first item, operator--() returns 0.
|
---|
432 | */
|
---|