1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
---|
2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/doc/qcache.doc:41 -->
|
---|
3 | <html>
|
---|
4 | <head>
|
---|
5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
6 | <title>QCache Class</title>
|
---|
7 | <style type="text/css"><!--
|
---|
8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
9 | a:link { color: #004faf; text-decoration: none }
|
---|
10 | a:visited { color: #672967; text-decoration: none }
|
---|
11 | body { background: #ffffff; color: black; }
|
---|
12 | --></style>
|
---|
13 | </head>
|
---|
14 | <body>
|
---|
15 |
|
---|
16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
17 | <tr bgcolor="#E5E5E5">
|
---|
18 | <td valign=center>
|
---|
19 | <a href="index.html">
|
---|
20 | <font color="#004faf">Home</font></a>
|
---|
21 | | <a href="classes.html">
|
---|
22 | <font color="#004faf">All Classes</font></a>
|
---|
23 | | <a href="mainclasses.html">
|
---|
24 | <font color="#004faf">Main Classes</font></a>
|
---|
25 | | <a href="annotated.html">
|
---|
26 | <font color="#004faf">Annotated</font></a>
|
---|
27 | | <a href="groups.html">
|
---|
28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
29 | | <a href="functions.html">
|
---|
30 | <font color="#004faf">Functions</font></a>
|
---|
31 | </td>
|
---|
32 | <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>QCache Class Reference</h1>
|
---|
33 |
|
---|
34 | <p>The QCache class is a template class that provides a cache based on QString keys.
|
---|
35 | <a href="#details">More...</a>
|
---|
36 | <p><tt>#include <<a href="qcache-h.html">qcache.h</a>></tt>
|
---|
37 | <p>Inherits <a href="qptrcollection.html">QPtrCollection</a>.
|
---|
38 | <p><a href="qcache-members.html">List of all member functions.</a>
|
---|
39 | <h2>Public Members</h2>
|
---|
40 | <ul>
|
---|
41 | <li class=fn><a href="#QCache-2"><b>QCache</b></a> ( int maxCost = 100, int size = 17, bool caseSensitive = TRUE )</li>
|
---|
42 | <li class=fn><a href="#~QCache"><b>~QCache</b></a> ()</li>
|
---|
43 | <li class=fn>int <a href="#maxCost"><b>maxCost</b></a> () const</li>
|
---|
44 | <li class=fn>int <a href="#totalCost"><b>totalCost</b></a> () const</li>
|
---|
45 | <li class=fn>void <a href="#setMaxCost"><b>setMaxCost</b></a> ( int m )</li>
|
---|
46 | <li class=fn>virtual uint <a href="#count"><b>count</b></a> () const</li>
|
---|
47 | <li class=fn>uint <a href="#size"><b>size</b></a> () const</li>
|
---|
48 | <li class=fn>bool <a href="#isEmpty"><b>isEmpty</b></a> () const</li>
|
---|
49 | <li class=fn>virtual void <a href="#clear"><b>clear</b></a> ()</li>
|
---|
50 | <li class=fn>bool <a href="#insert"><b>insert</b></a> ( const QString & k, const type * d, int c = 1, int p = 0 )</li>
|
---|
51 | <li class=fn>bool <a href="#remove"><b>remove</b></a> ( const QString & k )</li>
|
---|
52 | <li class=fn>type * <a href="#take"><b>take</b></a> ( const QString & k )</li>
|
---|
53 | <li class=fn>type * <a href="#find"><b>find</b></a> ( const QString & k, bool ref = TRUE ) const</li>
|
---|
54 | <li class=fn>type * <a href="#operator[]"><b>operator[]</b></a> ( const QString & k ) const</li>
|
---|
55 | <li class=fn>void <a href="#statistics"><b>statistics</b></a> () const</li>
|
---|
56 | </ul>
|
---|
57 | <h2>Important Inherited Members</h2>
|
---|
58 | <ul>
|
---|
59 | <li class=fn>bool <a href="#autoDelete"><b>autoDelete</b></a> () const</li>
|
---|
60 | <li class=fn>void <a href="#setAutoDelete"><b>setAutoDelete</b></a> ( bool enable )</li>
|
---|
61 | </ul>
|
---|
62 | <hr><a name="details"></a><h2>Detailed Description</h2>
|
---|
63 |
|
---|
64 |
|
---|
65 | The QCache class is a template class that provides a cache based on <a href="qstring.html">QString</a> keys.
|
---|
66 | <p>
|
---|
67 |
|
---|
68 | <p>
|
---|
69 | <p> A cache is a least recently used (LRU) list of cache items. Each
|
---|
70 | cache item has a key and a certain cost. The sum of item costs,
|
---|
71 | <a href="#totalCost">totalCost</a>(), never exceeds the maximum cache cost, <a href="#maxCost">maxCost</a>(). If
|
---|
72 | inserting a new item would cause the total cost to exceed the
|
---|
73 | maximum cost, the least recently used items in the cache are
|
---|
74 | removed.
|
---|
75 | <p> QCache is a template class. QCache<X> defines a cache that
|
---|
76 | operates on pointers to X, or X*.
|
---|
77 | <p> Apart from <a href="#insert">insert</a>(), by far the most important function is <a href="#find">find</a>()
|
---|
78 | (which also exists as <a href="#operator[]">operator[]</a>()). This function looks up an
|
---|
79 | item, returns it, and by default marks it as being the most
|
---|
80 | recently used item.
|
---|
81 | <p> There are also methods to <a href="#remove">remove</a>() or <a href="#take">take</a>() an object from the
|
---|
82 | cache. Calling <a href="qptrcollection.html#setAutoDelete">setAutoDelete</a>(TRUE) for a cache tells it to delete
|
---|
83 | items that are removed. The default is to not delete items when
|
---|
84 | they are removed (i.e., remove() and take() are equivalent).
|
---|
85 | <p> When inserting an item into the cache, only the pointer is copied,
|
---|
86 | not the item itself. This is called a <a href="shclass.html#shallow-copy">shallow copy</a>. It is possible
|
---|
87 | to make the cache copy all of the item's data (known as a <a href="shclass.html#deep-copy">deep copy</a>) when an item is inserted. insert() calls the virtual
|
---|
88 | function <a href="qptrcollection.html#newItem">QPtrCollection::newItem</a>() for the item to be inserted.
|
---|
89 | Inherit a cache and reimplement <a href="qptrcollection.html#newItem">newItem</a>() if you want deep copies.
|
---|
90 | <p> When removing a cache item, the virtual function
|
---|
91 | <a href="qptrcollection.html#deleteItem">QPtrCollection::deleteItem</a>() is called. The default
|
---|
92 | implementation deletes the item if auto-deletion is enabled, and
|
---|
93 | does nothing otherwise.
|
---|
94 | <p> There is a <a href="qcacheiterator.html">QCacheIterator</a> that can be used to traverse the items
|
---|
95 | in the cache in arbitrary order.
|
---|
96 | <p> In QCache, the cache items are accessed via <a href="qstring.html">QString</a> keys, which
|
---|
97 | are Unicode strings. If you want to use non-Unicode, plain 8-bit
|
---|
98 | <tt>char*</tt> keys, use the <a href="qasciicache.html">QAsciiCache</a> template. A QCache has the
|
---|
99 | same performance as a QAsciiCache.
|
---|
100 | <p> <p>See also <a href="qcacheiterator.html">QCacheIterator</a>, <a href="qasciicache.html">QAsciiCache</a>, <a href="qintcache.html">QIntCache</a>, <a href="collection.html">Collection Classes</a>, and <a href="tools.html">Non-GUI Classes</a>.
|
---|
101 |
|
---|
102 | <hr><h2>Member Function Documentation</h2>
|
---|
103 | <h3 class=fn><a name="QCache-2"></a>QCache::QCache ( int maxCost = 100, int size = 17, bool caseSensitive = TRUE )
|
---|
104 | </h3>
|
---|
105 |
|
---|
106 | <p> Constructs a cache whose contents will never have a total cost
|
---|
107 | greater than <em>maxCost</em> and which is expected to contain less than
|
---|
108 | <em>size</em> items.
|
---|
109 | <p> <em>size</em> is actually the size of an internal hash array; it's
|
---|
110 | usually best to make it a <a href="primes.html#prime">prime</a> number and at least 50% bigger
|
---|
111 | than the largest expected number of items in the cache.
|
---|
112 | <p> Each inserted item has an associated cost. When inserting a new
|
---|
113 | item, if the total cost of all items in the cache will exceed <em>maxCost</em>, the cache will start throwing out the older (least
|
---|
114 | recently used) items until there is enough room for the new item
|
---|
115 | to be inserted.
|
---|
116 | <p> If <em>caseSensitive</em> is TRUE (the default), the cache keys are case
|
---|
117 | sensitive; if it is FALSE, they are case-insensitive.
|
---|
118 | Case-insensitive comparison considers all Unicode letters.
|
---|
119 |
|
---|
120 | <h3 class=fn><a name="~QCache"></a>QCache::~QCache ()
|
---|
121 | </h3>
|
---|
122 |
|
---|
123 | <p> Removes all items from the cache and destroys it. All iterators
|
---|
124 | that access this cache will be reset.
|
---|
125 |
|
---|
126 | <h3 class=fn>bool <a name="autoDelete"></a>QPtrCollection::autoDelete () const
|
---|
127 | </h3>
|
---|
128 |
|
---|
129 | <p> Returns the setting of the auto-delete option. The default is FALSE.
|
---|
130 | <p> <p>See also <a href="qptrcollection.html#setAutoDelete">setAutoDelete</a>().
|
---|
131 |
|
---|
132 | <h3 class=fn>void <a name="clear"></a>QCache::clear ()<tt> [virtual]</tt>
|
---|
133 | </h3>
|
---|
134 |
|
---|
135 | <p> Removes all items from the cache and deletes them if auto-deletion
|
---|
136 | has been enabled.
|
---|
137 | <p> All cache iterators that operate this on cache are reset.
|
---|
138 | <p> <p>See also <a href="#remove">remove</a>() and <a href="#take">take</a>().
|
---|
139 |
|
---|
140 | <p>Reimplemented from <a href="qptrcollection.html#clear">QPtrCollection</a>.
|
---|
141 | <h3 class=fn>uint <a name="count"></a>QCache::count () const<tt> [virtual]</tt>
|
---|
142 | </h3>
|
---|
143 |
|
---|
144 | <p> Returns the number of items in the cache.
|
---|
145 | <p> <p>See also <a href="#totalCost">totalCost</a>().
|
---|
146 |
|
---|
147 | <p>Reimplemented from <a href="qptrcollection.html#count">QPtrCollection</a>.
|
---|
148 | <h3 class=fn>type * <a name="find"></a>QCache::find ( const <a href="qstring.html">QString</a> & k, bool ref = TRUE ) const
|
---|
149 | </h3>
|
---|
150 |
|
---|
151 | <p> Returns the item associated with key <em>k</em>, or 0 if the key does
|
---|
152 | not exist in the cache. If <em>ref</em> is TRUE (the default), the item
|
---|
153 | is moved to the front of the least recently used list.
|
---|
154 | <p> If there are two or more items with equal keys, the one that was
|
---|
155 | inserted last is returned.
|
---|
156 |
|
---|
157 | <h3 class=fn>bool <a name="insert"></a>QCache::insert ( const <a href="qstring.html">QString</a> & k, const type * d, int c = 1, int p = 0 )
|
---|
158 | </h3>
|
---|
159 |
|
---|
160 | <p> Inserts the item <em>d</em> into the cache with key <em>k</em> and associated
|
---|
161 | cost, <em>c</em>. Returns TRUE if it is successfully inserted; otherwise
|
---|
162 | returns FALSE.
|
---|
163 | <p> The cache's size is limited, and if the total cost is too high,
|
---|
164 | QCache will remove old, least recently used items until there is
|
---|
165 | room for this new item.
|
---|
166 | <p> The parameter <em>p</em> is internal and should be left at the default
|
---|
167 | value (0).
|
---|
168 | <p> <b>Warning:</b> If this function returns FALSE (which could happen, e.g.
|
---|
169 | if the cost of this item alone exceeds <a href="#maxCost">maxCost</a>()) you must delete
|
---|
170 | <em>d</em> yourself. Additionally, be very careful about using <em>d</em>
|
---|
171 | after calling this function because any other insertions into the
|
---|
172 | cache, from anywhere in the application or within Qt itself, could
|
---|
173 | cause the object to be discarded from the cache and the pointer to
|
---|
174 | become invalid.
|
---|
175 |
|
---|
176 | <h3 class=fn>bool <a name="isEmpty"></a>QCache::isEmpty () const
|
---|
177 | </h3>
|
---|
178 |
|
---|
179 | <p> Returns TRUE if the cache is empty; otherwise returns FALSE.
|
---|
180 |
|
---|
181 | <h3 class=fn>int <a name="maxCost"></a>QCache::maxCost () const
|
---|
182 | </h3>
|
---|
183 |
|
---|
184 | <p> Returns the maximum allowed total cost of the cache.
|
---|
185 | <p> <p>See also <a href="#setMaxCost">setMaxCost</a>() and <a href="#totalCost">totalCost</a>().
|
---|
186 |
|
---|
187 | <h3 class=fn>type * <a name="operator[]"></a>QCache::operator[] ( const <a href="qstring.html">QString</a> & k ) const
|
---|
188 | </h3>
|
---|
189 |
|
---|
190 | <p> Returns the item associated with key <em>k</em>, or 0 if <em>k</em> does not
|
---|
191 | exist in the cache, and moves the item to the front of the least
|
---|
192 | recently used list.
|
---|
193 | <p> If there are two or more items with equal keys, the one that was
|
---|
194 | inserted last is returned.
|
---|
195 | <p> This is the same as <a href="#find">find</a>( k, TRUE ).
|
---|
196 | <p> <p>See also <a href="#find">find</a>().
|
---|
197 |
|
---|
198 | <h3 class=fn>bool <a name="remove"></a>QCache::remove ( const <a href="qstring.html">QString</a> & k )
|
---|
199 | </h3>
|
---|
200 |
|
---|
201 | <p> Removes the item associated with <em>k</em>, and returns TRUE if the
|
---|
202 | item was present in the cache; otherwise returns FALSE.
|
---|
203 | <p> The item is deleted if auto-deletion has been enabled, i.e., if
|
---|
204 | you have called <a href="qptrcollection.html#setAutoDelete">setAutoDelete</a>(TRUE).
|
---|
205 | <p> If there are two or more items with equal keys, the one that was
|
---|
206 | inserted last is removed.
|
---|
207 | <p> All iterators that refer to the removed item are set to point to
|
---|
208 | the next item in the cache's traversal order.
|
---|
209 | <p> <p>See also <a href="#take">take</a>() and <a href="#clear">clear</a>().
|
---|
210 |
|
---|
211 | <h3 class=fn>void <a name="setAutoDelete"></a>QPtrCollection::setAutoDelete ( bool enable )
|
---|
212 | </h3>
|
---|
213 |
|
---|
214 | <p> Sets the collection to auto-delete its contents if <em>enable</em> is
|
---|
215 | TRUE and to never delete them if <em>enable</em> is FALSE.
|
---|
216 | <p> If auto-deleting is turned on, all the items in a collection are
|
---|
217 | deleted when the collection itself is deleted. This is convenient
|
---|
218 | if the collection has the only pointer to the items.
|
---|
219 | <p> The default setting is FALSE, for safety. If you turn it on, be
|
---|
220 | careful about copying the collection - you might find yourself
|
---|
221 | with two collections deleting the same items.
|
---|
222 | <p> Note that the auto-delete setting may also affect other functions
|
---|
223 | in subclasses. For example, a subclass that has a <a href="#remove">remove</a>()
|
---|
224 | function will remove the item from its data structure, and if
|
---|
225 | auto-delete is enabled, will also delete the item.
|
---|
226 | <p> <p>See also <a href="qptrcollection.html#autoDelete">autoDelete</a>().
|
---|
227 |
|
---|
228 | <p>Examples: <a href="grapher-nsplugin-example.html#x2769">grapher/grapher.cpp</a>, <a href="scribble-example.html#x924">scribble/scribble.cpp</a>, and <a href="bigtable-example.html#x1291">table/bigtable/main.cpp</a>.
|
---|
229 | <h3 class=fn>void <a name="setMaxCost"></a>QCache::setMaxCost ( int m )
|
---|
230 | </h3>
|
---|
231 |
|
---|
232 | <p> Sets the maximum allowed total cost of the cache to <em>m</em>. If the
|
---|
233 | current total cost is greater than <em>m</em>, some items are deleted
|
---|
234 | immediately.
|
---|
235 | <p> <p>See also <a href="#maxCost">maxCost</a>() and <a href="#totalCost">totalCost</a>().
|
---|
236 |
|
---|
237 | <h3 class=fn>uint <a name="size"></a>QCache::size () const
|
---|
238 | </h3>
|
---|
239 |
|
---|
240 | <p> Returns the size of the hash array used to implement the cache.
|
---|
241 | This should be a bit bigger than <a href="#count">count</a>() is likely to be.
|
---|
242 |
|
---|
243 | <h3 class=fn>void <a name="statistics"></a>QCache::statistics () const
|
---|
244 | </h3>
|
---|
245 |
|
---|
246 | <p> A debug-only utility function. Prints out cache usage, hit/miss,
|
---|
247 | and distribution information using <a href="qapplication.html#qDebug">qDebug</a>(). This function does
|
---|
248 | nothing in the release library.
|
---|
249 |
|
---|
250 | <h3 class=fn>type * <a name="take"></a>QCache::take ( const <a href="qstring.html">QString</a> & k )
|
---|
251 | </h3>
|
---|
252 |
|
---|
253 | <p> Takes the item associated with <em>k</em> out of the cache without
|
---|
254 | deleting it, and returns a pointer to the item taken out, or 0
|
---|
255 | if the key does not exist in the cache.
|
---|
256 | <p> If there are two or more items with equal keys, the one that was
|
---|
257 | inserted last is taken.
|
---|
258 | <p> All iterators that refer to the taken item are set to point to the
|
---|
259 | next item in the cache's traversal order.
|
---|
260 | <p> <p>See also <a href="#remove">remove</a>() and <a href="#clear">clear</a>().
|
---|
261 |
|
---|
262 | <h3 class=fn>int <a name="totalCost"></a>QCache::totalCost () const
|
---|
263 | </h3>
|
---|
264 |
|
---|
265 | <p> Returns the total cost of the items in the cache. This is an
|
---|
266 | integer in the range 0 to <a href="#maxCost">maxCost</a>().
|
---|
267 | <p> <p>See also <a href="#setMaxCost">setMaxCost</a>().
|
---|
268 |
|
---|
269 | <!-- eof -->
|
---|
270 | <hr><p>
|
---|
271 | This file is part of the <a href="index.html">Qt toolkit</a>.
|
---|
272 | Copyright © 1995-2007
|
---|
273 | <a href="http://www.trolltech.com/">Trolltech</a>. All Rights Reserved.<p><address><hr><div align=center>
|
---|
274 | <table width=100% cellspacing=0 border=0><tr>
|
---|
275 | <td>Copyright © 2007
|
---|
276 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
277 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
278 | </table></div></address></body>
|
---|
279 | </html>
|
---|