source: trunk/doc/html/qasciicache.html

Last change on this file was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 13.1 KB
RevLine 
[190]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/qasciicache.doc:41 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>QAsciiCache Class</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { 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&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;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&nbsp;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>QAsciiCache Class Reference</h1>
33
34<p>The QAsciiCache class is a template class that provides a cache based on char* keys.
35<a href="#details">More...</a>
36<p><tt>#include &lt;<a href="qasciicache-h.html">qasciicache.h</a>&gt;</tt>
37<p>Inherits <a href="qptrcollection.html">QPtrCollection</a>.
38<p><a href="qasciicache-members.html">List of all member functions.</a>
39<h2>Public Members</h2>
40<ul>
41<li class=fn><a href="#QAsciiCache-2"><b>QAsciiCache</b></a> ( int&nbsp;maxCost = 100, int&nbsp;size = 17, bool&nbsp;caseSensitive = TRUE, bool&nbsp;copyKeys = TRUE )</li>
42<li class=fn><a href="#~QAsciiCache"><b>~QAsciiCache</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&nbsp;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&nbsp;char&nbsp;*&nbsp;k, const&nbsp;type&nbsp;*&nbsp;d, int&nbsp;c = 1, int&nbsp;p = 0 )</li>
51<li class=fn>bool <a href="#remove"><b>remove</b></a> ( const&nbsp;char&nbsp;*&nbsp;k )</li>
52<li class=fn>type * <a href="#take"><b>take</b></a> ( const&nbsp;char&nbsp;*&nbsp;k )</li>
53<li class=fn>type * <a href="#find"><b>find</b></a> ( const&nbsp;char&nbsp;*&nbsp;k, bool&nbsp;ref = TRUE ) const</li>
54<li class=fn>type * <a href="#operator[]"><b>operator[]</b></a> ( const&nbsp;char&nbsp;*&nbsp;k ) const</li>
55<li class=fn>void <a href="#statistics"><b>statistics</b></a> () const</li>
56</ul>
57<hr><a name="details"></a><h2>Detailed Description</h2>
58
59
60<p> The QAsciiCache class is a template class that provides a cache based on char* keys.
61<p>
62
63<p> QAsciiCache is implemented as a template class. Define a template
64instance QAsciiCache&lt;X&gt; to create a cache that operates on
65pointers to X (X*).
66<p> A cache is a least recently used (LRU) list of cache items. The
67cache items are accessed via <tt>char*</tt> keys. For Unicode keys use
68the <a href="qcache.html">QCache</a> template instead, which uses <a href="qstring.html">QString</a> keys. A QCache
69has the same performace as a QAsciiCache.
70<p> Each cache item has a cost. The sum of item costs, <a href="#totalCost">totalCost</a>(),
71will not exceed the maximum cache cost, <a href="#maxCost">maxCost</a>(). If inserting a
72new item would cause the total cost to exceed the maximum cost,
73the least recently used items in the cache are removed.
74<p> Apart from <a href="#insert">insert</a>(), by far the most important function is <a href="#find">find</a>()
75(which also exists as <a href="#operator[]">operator[]</a>()). This function looks up an
76item, returns it, and by default marks it as being the most
77recently used item.
78<p> There are also methods to <a href="#remove">remove</a>() or <a href="#take">take</a>() an object from the
79cache. Calling <a href="qptrcollection.html#setAutoDelete">setAutoDelete(TRUE)</a> tells the cache to delete items that
80are removed. The default is to not delete items when then are
81removed (i.e., remove() and take() are equivalent).
82<p> When inserting an item into the cache, only the pointer is copied,
83not the item itself. This is called a <a href="shclass.html#shallow-copy">shallow copy</a>. It is possible
84to 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
85function <a href="qptrcollection.html#newItem">QPtrCollection::newItem</a>() for the item to be inserted.
86Inherit a cache and reimplement <a href="qptrcollection.html#newItem">newItem</a>() if you want deep copies.
87<p> When removing a cache item the virtual function
88<a href="qptrcollection.html#deleteItem">QPtrCollection::deleteItem</a>() is called. Its default implementation
89in QAsciiCache is to delete the item if <a href="qptrcollection.html#setAutoDelete">auto-deletion</a> is enabled.
90<p> There is a <a href="qasciicacheiterator.html">QAsciiCacheIterator</a> which may be used to traverse the
91items in the cache in arbitrary order.
92<p> <p>See also <a href="qasciicacheiterator.html">QAsciiCacheIterator</a>, <a href="qcache.html">QCache</a>, <a href="qintcache.html">QIntCache</a>, <a href="collection.html">Collection Classes</a>, and <a href="tools.html">Non-GUI Classes</a>.
93
94<hr><h2>Member Function Documentation</h2>
95<h3 class=fn><a name="QAsciiCache-2"></a>QAsciiCache::QAsciiCache ( int&nbsp;maxCost = 100, int&nbsp;size = 17, bool&nbsp;caseSensitive = TRUE, bool&nbsp;copyKeys = TRUE )
96</h3>
97
98<p> Constructs a cache whose contents will never have a total cost
99greater than <em>maxCost</em> and which is expected to contain less than
100<em>size</em> items.
101<p> <em>size</em> is actually the size of an internal hash array; it's
102usually best to make it <a href="primes.html#prime">prime</a> and at least 50% bigger than the
103largest expected number of items in the cache.
104<p> Each inserted item has an associated cost. When inserting a new
105item, if the total cost of all items in the cache will exceed <em>maxCost</em>, the cache will start throwing out the older (least
106recently used) items until there is enough room for the new item
107to be inserted.
108<p> If <em>caseSensitive</em> is TRUE (the default), the cache keys are case
109sensitive; if it is FALSE, they are case-insensitive.
110Case-insensitive comparison only affects the 26 letters in
111US-ASCII. If <em>copyKeys</em> is TRUE (the default), QAsciiCache makes
112a copy of the cache keys, otherwise it copies just the const char
113* pointer - slightly faster if you can guarantee that the keys
114will never change, but very risky.
115
116<h3 class=fn><a name="~QAsciiCache"></a>QAsciiCache::~QAsciiCache ()
117</h3>
118
119<p> Removes all items from the cache and destroys it.
120All iterators that access this cache will be reset.
121
122<h3 class=fn>void <a name="clear"></a>QAsciiCache::clear ()<tt> [virtual]</tt>
123</h3>
124
125<p> Removes all items from the cache, and deletes them if <a href="qptrcollection.html#setAutoDelete">auto-deletion</a> has been
126enabled.
127<p> All cache iterators that operate on this cache are reset.
128<p> <p>See also <a href="#remove">remove</a>() and <a href="#take">take</a>().
129
130<p>Reimplemented from <a href="qptrcollection.html#clear">QPtrCollection</a>.
131<h3 class=fn>uint <a name="count"></a>QAsciiCache::count () const<tt> [virtual]</tt>
132</h3>
133
134<p> Returns the number of items in the cache.
135<p> <p>See also <a href="#totalCost">totalCost</a>() and <a href="#size">size</a>().
136
137<p>Reimplemented from <a href="qptrcollection.html#count">QPtrCollection</a>.
138<h3 class=fn>type * <a name="find"></a>QAsciiCache::find ( const&nbsp;char&nbsp;*&nbsp;k, bool&nbsp;ref = TRUE ) const
139</h3>
140
141<p> Returns the item with key <em>k</em>, or 0 if the key does not exist
142in the cache. If <em>ref</em> is TRUE (the default), the item is moved
143to the front of the least recently used list.
144<p> If there are two or more items with equal keys, the one that was
145inserted last is returned.
146
147<h3 class=fn>bool <a name="insert"></a>QAsciiCache::insert ( const&nbsp;char&nbsp;*&nbsp;k, const&nbsp;type&nbsp;*&nbsp;d, int&nbsp;c = 1, int&nbsp;p = 0 )
148</h3>
149
150<p> Inserts the item <em>d</em> into the cache using key <em>k</em>, and with an
151associated cost of <em>c</em>. Returns TRUE if the item is successfully
152inserted. Returns FALSE if the item is not inserted, for example,
153if the cost of the item exceeds <a href="#maxCost">maxCost</a>().
154<p> The cache's size is limited, and if the total cost is too high,
155QAsciiCache will remove old, least recently used items until there
156is room for this new item.
157<p> Items with duplicate keys can be inserted.
158<p> The parameter <em>p</em> is internal and should be left at the default
159value (0).
160<p> <b>Warning:</b> If this function returns FALSE, you must delete <em>d</em>
161yourself. Additionally, be very careful about using <em>d</em> after
162calling this function, because any other insertions into the
163cache, from anywhere in the application or within Qt itself, could
164cause the object to be discarded from the cache and the pointer to
165become invalid.
166
167<h3 class=fn>bool <a name="isEmpty"></a>QAsciiCache::isEmpty () const
168</h3>
169
170<p> Returns TRUE if the cache is empty; otherwise returns FALSE.
171
172<h3 class=fn>int <a name="maxCost"></a>QAsciiCache::maxCost () const
173</h3>
174
175<p> Returns the maximum allowed total cost of the cache.
176<p> <p>See also <a href="#setMaxCost">setMaxCost</a>() and <a href="#totalCost">totalCost</a>().
177
178<h3 class=fn>type * <a name="operator[]"></a>QAsciiCache::operator[] ( const&nbsp;char&nbsp;*&nbsp;k ) const
179</h3>
180
181<p> Returns the item with key <em>k</em>, or 0 if <em>k</em> does not exist in
182the cache, and moves the item to the front of the least recently
183used list.
184<p> If there are two or more items with equal keys, the one that was
185inserted last is returned.
186<p> This is the same as <a href="#find">find</a>( k, TRUE ).
187<p> <p>See also <a href="#find">find</a>().
188
189<h3 class=fn>bool <a name="remove"></a>QAsciiCache::remove ( const&nbsp;char&nbsp;*&nbsp;k )
190</h3>
191
192<p> Removes the item with key <em>k</em> and returns TRUE if the item was
193present in the cache; otherwise returns FALSE.
194<p> The item is deleted if auto-deletion has been enabled, i.e., if
195you have called <a href="qptrcollection.html#setAutoDelete">setAutoDelete(TRUE)</a>.
196<p> If there are two or more items with equal keys, the one that was
197inserted last is removed.
198<p> All iterators that refer to the removed item are set to point to
199the next item in the cache's traversal order.
200<p> <p>See also <a href="#take">take</a>() and <a href="#clear">clear</a>().
201
202<h3 class=fn>void <a name="setMaxCost"></a>QAsciiCache::setMaxCost ( int&nbsp;m )
203</h3>
204
205<p> Sets the maximum allowed total cost of the cache to <em>m</em>. If the
206current total cost is greater than <em>m</em>, some items are removed
207immediately.
208<p> <p>See also <a href="#maxCost">maxCost</a>() and <a href="#totalCost">totalCost</a>().
209
210<h3 class=fn>uint <a name="size"></a>QAsciiCache::size () const
211</h3>
212
213<p> Returns the size of the hash array used to implement the cache.
214This should be a bit bigger than <a href="#count">count</a>() is likely to be.
215
216<h3 class=fn>void <a name="statistics"></a>QAsciiCache::statistics () const
217</h3>
218
219<p> A debug-only utility function. Prints out cache usage, hit/miss,
220and distribution information using <a href="qapplication.html#qDebug">qDebug</a>(). This function does
221nothing in the release library.
222
223<h3 class=fn>type * <a name="take"></a>QAsciiCache::take ( const&nbsp;char&nbsp;*&nbsp;k )
224</h3>
225
226<p> Takes the item associated with <em>k</em> out of the cache without
227deleting it and returns a pointer to the item taken out, or 0
228if the key does not exist in the cache.
229<p> If there are two or more items with equal keys, the one that was
230inserted last is taken.
231<p> All iterators that refer to the taken item are set to point to the
232next item in the cache's traversal order.
233<p> <p>See also <a href="#remove">remove</a>() and <a href="#clear">clear</a>().
234
235<h3 class=fn>int <a name="totalCost"></a>QAsciiCache::totalCost () const
236</h3>
237
238<p> Returns the total cost of the items in the cache. This is an
239integer in the range 0 to <a href="#maxCost">maxCost</a>().
240<p> <p>See also <a href="#setMaxCost">setMaxCost</a>().
241
242<!-- eof -->
243<hr><p>
244This file is part of the <a href="index.html">Qt toolkit</a>.
245Copyright &copy; 1995-2007
246<a href="http://www.trolltech.com/">Trolltech</a>. All Rights Reserved.<p><address><hr><div align=center>
247<table width=100% cellspacing=0 border=0><tr>
248<td>Copyright &copy; 2007
249<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
250<td align=right><div align=right>Qt 3.3.8</div>
251</table></div></address></body>
252</html>
Note: See TracBrowser for help on using the repository browser.