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