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