1QCache(3qt) QCache(3qt)
2
3
4
6 QCache - Template class that provides a cache based on QString keys
7
9 #include <qcache.h>
10
11 Inherits QPtrCollection.
12
13 Public Members
14 QCache ( int maxCost = 100, int size = 17, bool caseSensitive = TRUE )
15 ~QCache ()
16 int maxCost () const
17 int totalCost () const
18 void setMaxCost ( int m )
19 virtual uint count () const
20 uint size () const
21 bool isEmpty () const
22 virtual void clear ()
23 bool insert ( const QString & k, const type * d, int c = 1, int p = 0 )
24 bool remove ( const QString & k )
25 type * take ( const QString & k )
26 type * find ( const QString & k, bool ref = TRUE ) const
27 type * operator[] ( const QString & k ) const
28 void statistics () const
29
30 Important Inherited Members
31 bool autoDelete () const
32 void setAutoDelete ( bool enable )
33
35 The QCache class is a template class that provides a cache based on
36 QString keys.
37
38 A cache is a least recently used (LRU) list of cache items. Each cache
39 item has a key and a certain cost. The sum of item costs, totalCost(),
40 never exceeds the maximum cache cost, maxCost(). If inserting a new
41 item would cause the total cost to exceed the maximum cost, the least
42 recently used items in the cache are removed.
43
44 QCache is a template class. QCache<X> defines a cache that operates on
45 pointers to X, or X*.
46
47 Apart from insert(), by far the most important function is find()
48 (which also exists as operator[]()). This function looks up an item,
49 returns it, and by default marks it as being the most recently used
50 item.
51
52 There are also methods to remove() or take() an object from the cache.
53 Calling setAutoDelete(TRUE) for a cache tells it to delete items that
54 are removed. The default is to not delete items when they are removed
55 (i.e., remove() and take() are equivalent).
56
57 When inserting an item into the cache, only the pointer is copied, not
58 the item itself. This is called a shallow copy. It is possible to make
59 the cache copy all of the item's data (known as a deep copy) when an
60 item is inserted. insert() calls the virtual function
61 QPtrCollection::newItem() for the item to be inserted. Inherit a cache
62 and reimplement newItem() if you want deep copies.
63
64 When removing a cache item, the virtual function
65 QPtrCollection::deleteItem() is called. The default implementation
66 deletes the item if auto-deletion is enabled, and does nothing
67 otherwise.
68
69 There is a QCacheIterator that can be used to traverse the items in the
70 cache in arbitrary order.
71
72 In QCache, the cache items are accessed via QString keys, which are
73 Unicode strings. If you want to use non-Unicode, plain 8-bit char*
74 keys, use the QAsciiCache template. A QCache has the same performance
75 as a QAsciiCache.
76
77 See also QCacheIterator, QAsciiCache, QIntCache, Collection Classes,
78 and Non-GUI Classes.
79
82
83 Constructs a cache whose contents will never have a total cost greater
84 than maxCost and which is expected to contain less than size items.
85
86 size is actually the size of an internal hash array; it's usually best
87 to make it a prime number and at least 50% bigger than the largest
88 expected number of items in the cache.
89
90 Each inserted item has an associated cost. When inserting a new item,
91 if the total cost of all items in the cache will exceed maxCost, the
92 cache will start throwing out the older (least recently used) items
93 until there is enough room for the new item to be inserted.
94
95 If caseSensitive is TRUE (the default), the cache keys are case
96 sensitive; if it is FALSE, they are case-insensitive. Case-insensitive
97 comparison considers all Unicode letters.
98
100 Removes all items from the cache and destroys it. All iterators that
101 access this cache will be reset.
102
104 Returns the setting of the auto-delete option. The default is FALSE.
105
106 See also setAutoDelete().
107
109 Removes all items from the cache and deletes them if auto-deletion has
110 been enabled.
111
112 All cache iterators that operate this on cache are reset.
113
114 See also remove() and take().
115
116 Reimplemented from QPtrCollection.
117
119 Returns the number of items in the cache.
120
121 See also totalCost().
122
123 Reimplemented from QPtrCollection.
124
126 Returns the item associated with key k, or 0 if the key does not exist
127 in the cache. If ref is TRUE (the default), the item is moved to the
128 front of the least recently used list.
129
130 If there are two or more items with equal keys, the one that was
131 inserted last is returned.
132
134 )
135 Inserts the item d into the cache with key k and associated cost, c.
136 Returns TRUE if it is successfully inserted; otherwise returns FALSE.
137
138 The cache's size is limited, and if the total cost is too high, QCache
139 will remove old, least recently used items until there is room for this
140 new item.
141
142 The parameter p is internal and should be left at the default value
143 (0).
144
145 Warning: If this function returns FALSE (which could happen, e.g. if
146 the cost of this item alone exceeds maxCost()) you must delete d
147 yourself. Additionally, be very careful about using d after calling
148 this function because any other insertions into the cache, from
149 anywhere in the application or within Qt itself, could cause the object
150 to be discarded from the cache and the pointer to become invalid.
151
153 Returns TRUE if the cache is empty; otherwise returns FALSE.
154
156 Returns the maximum allowed total cost of the cache.
157
158 See also setMaxCost() and totalCost().
159
161 Returns the item associated with key k, or 0 if k does not exist in the
162 cache, and moves the item to the front of the least recently used list.
163
164 If there are two or more items with equal keys, the one that was
165 inserted last is returned.
166
167 This is the same as find( k, TRUE ).
168
169 See also find().
170
172 Removes the item associated with k, and returns TRUE if the item was
173 present in the cache; otherwise returns FALSE.
174
175 The item is deleted if auto-deletion has been enabled, i.e., if you
176 have called setAutoDelete(TRUE).
177
178 If there are two or more items with equal keys, the one that was
179 inserted last is removed.
180
181 All iterators that refer to the removed item are set to point to the
182 next item in the cache's traversal order.
183
184 See also take() and clear().
185
187 Sets the collection to auto-delete its contents if enable is TRUE and
188 to never delete them if enable is FALSE.
189
190 If auto-deleting is turned on, all the items in a collection are
191 deleted when the collection itself is deleted. This is convenient if
192 the collection has the only pointer to the items.
193
194 The default setting is FALSE, for safety. If you turn it on, be careful
195 about copying the collection - you might find yourself with two
196 collections deleting the same items.
197
198 Note that the auto-delete setting may also affect other functions in
199 subclasses. For example, a subclass that has a remove() function will
200 remove the item from its data structure, and if auto-delete is enabled,
201 will also delete the item.
202
203 See also autoDelete().
204
205 Examples:
206
208 Sets the maximum allowed total cost of the cache to m. If the current
209 total cost is greater than m, some items are deleted immediately.
210
211 See also maxCost() and totalCost().
212
214 Returns the size of the hash array used to implement the cache. This
215 should be a bit bigger than count() is likely to be.
216
218 A debug-only utility function. Prints out cache usage, hit/miss, and
219 distribution information using qDebug(). This function does nothing in
220 the release library.
221
223 Takes the item associated with k out of the cache without deleting it,
224 and returns a pointer to the item taken out, or 0 if the key does not
225 exist in the cache.
226
227 If there are two or more items with equal keys, the one that was
228 inserted last is taken.
229
230 All iterators that refer to the taken item are set to point to the next
231 item in the cache's traversal order.
232
233 See also remove() and clear().
234
236 Returns the total cost of the items in the cache. This is an integer in
237 the range 0 to maxCost().
238
239 See also setMaxCost().
240
241
243 http://doc.trolltech.com/qcache.html
244 http://www.trolltech.com/faq/tech.html
245
247 Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the
248 license file included in the distribution for a complete license
249 statement.
250
252 Generated automatically from the source code.
253
255 If you find a bug in Qt, please report it as described in
256 http://doc.trolltech.com/bughowto.html. Good bug reports help us to
257 help you. Thank you.
258
259 The definitive Qt documentation is provided in HTML format; it is
260 located at $QTDIR/doc/html and can be read using Qt Assistant or with a
261 web browser. This man page is provided as a convenience for those users
262 who prefer man pages, although this format is not officially supported
263 by Trolltech.
264
265 If you find errors in this manual page, please report them to qt-
266 bugs@trolltech.com. Please include the name of the manual page
267 (qcache.3qt) and the Qt version (3.3.8).
268
269
270
271Trolltech AS 2 February 2007 QCache(3qt)