[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/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"><!--
|
---|
| 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>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 <<a href="qintcache-h.html">qintcache.h</a>></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 maxCost = 100, int 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 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 k, const type * d, int c = 1, int p = 0 )</li>
|
---|
| 50 | <li class=fn>bool <a href="#remove"><b>remove</b></a> ( long k )</li>
|
---|
| 51 | <li class=fn>type * <a href="#take"><b>take</b></a> ( long 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 k, bool ref = TRUE ) const</li>
|
---|
| 54 | <li class=fn>type * <a href="#operator[]"><b>operator[]</b></a> ( long 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 | The 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
|
---|
| 64 | instance QIntCache<X> to create a cache that operates on
|
---|
| 65 | pointers to X, or X*.
|
---|
| 66 | <p> A cache is a least recently used (LRU) list of cache items,
|
---|
| 67 | accessed via <tt>long</tt> keys. Each cache item has a cost. The sum
|
---|
| 68 | of item costs, <a href="#totalCost">totalCost</a>(), will not exceed the maximum cache
|
---|
| 69 | cost, <a href="#maxCost">maxCost</a>(). If inserting a new item would cause the total
|
---|
| 70 | cost to exceed the maximum cost, the least recently used items in
|
---|
| 71 | the 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
|
---|
| 74 | item, returns it, and by default marks it as being the most
|
---|
| 75 | recently used item.
|
---|
| 76 | <p> There are also methods to <a href="#remove">remove</a>() or <a href="#take">take</a>() an object from the
|
---|
| 77 | cache. Calling <a href="qptrcollection.html#setAutoDelete">setAutoDelete</a>(TRUE) for a cache tells it to delete
|
---|
| 78 | items that are removed. The default is to not delete items when
|
---|
| 79 | they are removed (i.e. remove() and take() are equivalent).
|
---|
| 80 | <p> When inserting an item into the cache, only the pointer is copied,
|
---|
| 81 | not the item itself. This is called a <a href="shclass.html#shallow-copy">shallow copy</a>. It is possible
|
---|
| 82 | 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
|
---|
| 83 | function <a href="qptrcollection.html#newItem">QPtrCollection::newItem</a>() for the item to be inserted.
|
---|
| 84 | Inherit a dictionary and reimplement <a href="qptrcollection.html#newItem">newItem</a>() if you want deep
|
---|
| 85 | copies.
|
---|
| 86 | <p> When removing a cache item, the item will be automatically
|
---|
| 87 | deleted if auto-deletion is enabled.
|
---|
| 88 | <p> There is a <a href="qintcacheiterator.html">QIntCacheIterator</a> which may be used to traverse the
|
---|
| 89 | items 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 maxCost = 100, int size = 17 )
|
---|
| 94 | </h3>
|
---|
| 95 |
|
---|
| 96 | <p> Constructs a cache whose contents will never have a total cost
|
---|
| 97 | greater 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
|
---|
| 100 | usually best to make it <a href="primes.html#prime">prime</a> and at least 50% bigger than the
|
---|
| 101 | largest expected number of items in the cache.
|
---|
| 102 | <p> Each inserted item is associated with a cost. When inserting a new
|
---|
| 103 | 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
|
---|
| 104 | recently used) items until there is enough room for the new item
|
---|
| 105 | to 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.
|
---|
| 111 | If auto-deletion is enabled the cache's items are deleted. All
|
---|
| 112 | iterators 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
|
---|
| 118 | auto-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 k, bool ref = TRUE ) const
|
---|
| 131 | </h3>
|
---|
| 132 |
|
---|
| 133 | <p> Returns the item associated with <em>k</em>, or 0 if the key does not
|
---|
| 134 | exist in the cache. If <em>ref</em> is TRUE (the default), the item is
|
---|
| 135 | moved 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
|
---|
| 137 | inserted most recently is returned.
|
---|
| 138 |
|
---|
| 139 | <h3 class=fn>bool <a name="insert"></a>QIntCache::insert ( long k, const type * d, int c = 1, int p = 0 )
|
---|
| 140 | </h3>
|
---|
| 141 |
|
---|
| 142 | <p> Inserts the item <em>d</em> into the cache with key <em>k</em> and assigns it
|
---|
| 143 | a cost of <em>c</em> (default 1). Returns TRUE if it succeeds; otherwise
|
---|
| 144 | returns FALSE.
|
---|
| 145 | <p> The cache's size is limited, and if the total cost is too high,
|
---|
| 146 | QIntCache will remove old, least-used items until there is room
|
---|
| 147 | for this new item.
|
---|
| 148 | <p> The parameter <em>p</em> is internal and should be left at the default
|
---|
| 149 | value (0).
|
---|
| 150 | <p> <b>Warning:</b> If this function returns FALSE (for example, the cost <tt>,</tt>
|
---|
| 151 | exceeds <a href="#maxCost">maxCost</a>()), you must delete <em>d</em> yourself. Additionally,
|
---|
| 152 | be very careful about using <em>d</em> after calling this function. Any
|
---|
| 153 | other insertions into the cache, from anywhere in the application
|
---|
| 154 | or within Qt itself, could cause the object to be discarded from
|
---|
| 155 | the 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 k ) const
|
---|
| 169 | </h3>
|
---|
| 170 |
|
---|
| 171 | <p> Returns the item associated with <em>k</em>, or 0 if <em>k</em> does not exist
|
---|
| 172 | in the cache, and moves the item to the front of the least
|
---|
| 173 | recently used list.
|
---|
| 174 | <p> If there are two or more items with equal keys, the one that was
|
---|
| 175 | inserted 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 k )
|
---|
| 180 | </h3>
|
---|
| 181 |
|
---|
| 182 | <p> Removes the item associated with <em>k</em>, and returns TRUE if the
|
---|
| 183 | item was present in the cache; otherwise returns FALSE.
|
---|
| 184 | <p> The item is deleted if auto-deletion has been enabled, i.e. if you
|
---|
| 185 | have 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
|
---|
| 187 | inserted most recently is removed.
|
---|
| 188 | <p> All iterators that refer to the removed item are set to point to
|
---|
| 189 | the 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 m )
|
---|
| 193 | </h3>
|
---|
| 194 |
|
---|
| 195 | <p> Sets the maximum allowed total cost of the cache to <em>m</em>. If the
|
---|
| 196 | current total cost is greater than <em>m</em>, some items are removed
|
---|
| 197 | immediately.
|
---|
| 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.
|
---|
| 204 | This 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,
|
---|
| 210 | and distribution information using <a href="qapplication.html#qDebug">qDebug</a>(). This function does
|
---|
| 211 | nothing in the release library.
|
---|
| 212 |
|
---|
| 213 | <h3 class=fn>type * <a name="take"></a>QIntCache::take ( long k )
|
---|
| 214 | </h3>
|
---|
| 215 |
|
---|
| 216 | <p> Takes the item associated with <em>k</em> out of the cache without
|
---|
| 217 | deleting it, and returns a pointer to the item taken out or 0 if
|
---|
| 218 | the key does not exist in the cache.
|
---|
| 219 | <p> If there are two or more items with equal keys, the one that was
|
---|
| 220 | inserted most recently is taken.
|
---|
| 221 | <p> All iterators that refer to the taken item are set to point to the
|
---|
| 222 | next 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
|
---|
| 229 | integer 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>
|
---|
| 234 | This file is part of the <a href="index.html">Qt toolkit</a>.
|
---|
| 235 | Copyright © 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 © 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>
|
---|