source: trunk/doc/html/qintcache.html@ 203

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

reference documentation added

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