1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Qt Toolkit - QCache Class</title><style type="text/css"><!--
h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }body { background: white; color: black; }
--></style>
</head><body bgcolor="#ffffff">
<table width="100%">
<tr><td><a href="index.html">
<img width="100" height="100" src="qtlogo.png"
alt="Home" border="0"><img width="100"
height="100" src="face.png" alt="Home" border="0">
</a><td valign=top><div align=right><img src="dochead.png" width="472" height="27"><br>
<a href="classes.html"><b>Classes</b></a>
-<a href="annotated.html">Annotated</a>
- <a href="hierarchy.html">Tree</a>
-<a href="functions.html">Functions</a>
-<a href="index.html">Home</a>
-<a href="topicals.html"><b>Structure</b></a>
</div>
</table>
<h1 align=center>QCache Class Reference</h1><br clear="all">
<p>
The QCache class is a template class that provides a cache based on <code><a href="qstring.html">QString</a></code> keys.
<a href="#details">More...</a>
<p>
<code>#include <<a href="qcache-h.html">qcache.h</a>></code>
<p>
Inherits <a href="qgcache.html">QGCache</a>.
<p><a href="qcache-members.html">List of all member functions.</a>
<h2>Public Members</h2>
<ul>
<li><div class="fn"><b>QCache</b>(constQCache<type>&c)(internal)</div>
<li><div class="fn"><a href="#e88ea4"><b>QCache</b></a>(intmaxCost=100, intsize=17, boolcaseSensitive=TRUE)</div>
<li><div class="fn"><a href="#8973a6"><b>~QCache</b></a>()</div>
<li><div class="fn">QCache<type>&<b>operator=</b>(constQCache<type>&c)(internal)</div>
<li><div class="fn">int<a href="#29d27e"><b>maxCost</b></a>()const</div>
<li><div class="fn">int<a href="#aff367"><b>totalCost</b></a>()const</div>
<li><div class="fn">void<a href="#e8ff12"><b>setMaxCost</b></a>(intm)</div>
<li><div class="fn">virtualuint<a href="#d77b4a"><b>count</b></a>()const</div>
<li><div class="fn">uint<a href="#3be05e"><b>size</b></a>()const</div>
<li><div class="fn">bool<a href="#aee159"><b>isEmpty</b></a>()const</div>
<li><div class="fn">virtualvoid<a href="#0557ab"><b>clear</b></a>()</div>
<li><div class="fn">bool<a href="#2607c8"><b>insert</b></a>(constQString&k, consttype*d, intc=1, intp=0)</div>
<li><div class="fn">bool<a href="#6365d6"><b>remove</b></a>(constQString&k)</div>
<li><div class="fn">type*<a href="#4e8d41"><b>take</b></a>(constQString&k)</div>
<li><div class="fn">type*<a href="#d77456"><b>find</b></a>(constQString&k, boolref=TRUE)const</div>
<li><div class="fn">type*<a href="#35dea7"><b>operator[]</b></a>(constQString&k)const</div>
<li><div class="fn">void<a href="#746d80"><b>statistics</b></a>()const</div>
</ul>
<hr><h2><a name="details"></a>Detailed Description</h2>
The QCache class is a template class that provides a cache based on <code><a href="qstring.html">QString</a></code> keys.
<p>
A cache is a least recently used (LRU) list of cache items. Each
cache item has a key and a certain cost. The sum of item costs,
<a href="#aff367">totalCost</a>(), never exceeds the maximum cache cost, <a href="#29d27e">maxCost</a>(). If
inserting a new item would cause the total cost to exceed the
maximum cost, the least recently used items in the cache are
removed.
<p>QCache is a template class. QCache<X> defines a cache that
operates on pointers to X, or X*.
<p>Apart from <a href="#2607c8">insert</a>(), by far the most important function is <a href="#d77456">find</a>()
(which also exists as <a href="#35dea7">operator[]</a>()). This function looks up an item,
returns it, and by default marks it as being the most recently used
item.
<p>There are also methods to <a href="#6365d6">remove</a>() or <a href="#4e8d41">take</a>() an object from the
cache. Calling <a href="qcollection.html#a8ef9f">setAutoDelete</a>(TRUE) for a cache tells it to delete
items that are removed. The default is to not delete items when
then are removed (i.e. remove() and take() are equivalent).
<p>When inserting an item into the cache, only the pointer is copied, not
the item itself. This is called a shallow copy. It is possible to make the
dictionary copy all of the item's data (known as a deep copy) when an
item is inserted. insert() calls the virtual function
<a href="qcollection.html#55065e">QCollection::newItem</a>() for the item to be inserted.
Inherit a dictionary and reimplement it if you want deep copies.
<p>When removing a cache item, the virtual function
<a href="qcollection.html#8d78e7">QCollection::deleteItem</a>() is called. The default implementation
deletes the item if auto-deletion is enabled, and does nothing
otherwise.
<p>There is a <a href="qcacheiterator.html">QCacheIterator</a> which may be used to traverse the
items in the cache in arbitrary order.
<p>In QCache, the cache items are accessed via <a href="qstring.html">QString</a> keys, which
are Unicode strings. If you want to use non-Unicode, plain 8-bit <code>char*</code> keys, use the <a href="qasciicache.html">QAsciiCache</a> template. A QCache has the same
performace as a QAsciiCache.
<p>See also <a href="qcacheiterator.html">QCacheIterator</a>, <a href="qasciicache.html">QAsciiCache</a>, <a href="qintcache.html">QIntCache</a> and <a href="collection.html">Collection Classes</a>
<hr><h2>Member Function Documentation</h2>
<h3 class="fn"><a name="e88ea4"></a>QCache::QCache(intmaxCost=100, intsize=17, boolcaseSensitive=TRUE)</h3>
<p>Constructs a cache with the following properties:
<p>Arguments:
<ul>
<li><em>maxCost</em> is the maximum allowed total cost.
<li><em>size</em> is the size of the internal hash array.
<li><em>caseSensitive</em> specifies whether to use case sensitive lookup or not.
</ul>
Each inserted item is associated with a cost. When inserting a new
item, if the total cost of all items in the cache will exceeds
<em>maxCost,</em> the cache will start throwing out the older (recently least
used) items until there is room enough for the new item to be inserted.
<p>Setting <em>size</em> to a suitably large <a href="primes.html">prime
number</a> (equal to or greater than the expected number of entries)
makes the hash distribution better and hence the loopup faster.
<p>Setting <em>caseSensitive</em> to TRUE will treat "abc" and "Abc" as different
keys. Setting it to FALSE will make the dictionary ignore case.
Case insensitive comparison includes the whole Unicode alphabeth.
<h3 class="fn"><a name="8973a6"></a>QCache::~QCache()</h3>
<p>Removes all items from the cache and destroys it.
All iterators that access this cache will be reset.
<h3 class="fn">void<a name="0557ab"></a>QCache::clear() <code>[virtual]</code></h3>
<p>Removes all items from the cache, and deletes them if auto-deletion
has been enabled.
<p>All cache iterators that operate this on cache are reset.
<p>See also <a href="#6365d6">remove</a>() and <a href="#4e8d41">take</a>().
<p>Reimplemented from <a href="qcollection.html#e9c603">QCollection.</a>
<h3 class="fn">uint<a name="d77b4a"></a>QCache::count()const <code>[virtual]</code></h3>
<p>Returns the number of items in the cache.
<p>See also <a href="#aff367">totalCost</a>().
<p>Reimplemented from <a href="qcollection.html#2213fa">QCollection.</a>
<h3 class="fn">type*<a name="d77456"></a>QCache::find(const<a href="qstring.html">QString</a>&k, boolref=TRUE)const</h3>
<p>Returns the item associated with <em>k,</em> or null if the key does not
exist in the cache. If <em>ref</em> is TRUE (the default), the item is
moved to the front of the LRU list.
<p>If there are two or more items with equal keys, then the one that
was inserted last is returned.
<h3 class="fn">bool<a name="2607c8"></a>QCache::insert(const<a href="qstring.html">QString</a>&k, consttype*d, intc=1, intp=0)</h3>
<p>Inserts the item <em>d</em> into the cache with key <em>k</em> and cost <em>c.</em> Returns TRUE if it is successful and FALSE if it fails.
<p>The cache's size is limited, and if the total cost is too high,
QCache will remove old, least-used items until there is room for this
new item.
<p>The parameter <em>p</em> is internal and should be left at the default
value (0).<p><b>Warning:</b> If this function returns FALSE, you must delete <em>d</em>
yourself. Additionally, be very careful about using <em>d</em> after
calling this function, as any other insertions into the cache, from
anywhere in the application, or within Qt itself, could cause the
object to be discarded from the cache, and the pointer to become
invalid.
<h3 class="fn">bool<a name="aee159"></a>QCache::isEmpty()const</h3>
<p>Returns TRUE if the cache is empty, or FALSE if there is at least one
object in it.
<h3 class="fn">int<a name="29d27e"></a>QCache::maxCost()const</h3>
<p>Returns the maximum allowed total cost of the cache.
<p>See also <a href="#e8ff12">setMaxCost</a>() and <a href="#aff367">totalCost</a>().
<h3 class="fn">type*<a name="35dea7"></a>QCache::operator[](const<a href="qstring.html">QString</a>&k)const</h3>
<p>Returns the item associated with <em>k,</em> or null if <em>k</em> does not
exist in the cache, and moves the item to the front of the LRU list.
<p>If there are two or more items with equal keys, then the one that
was inserted last is returned.
<p>This is the same as <a href="#d77456">find</a>( k, TRUE ).
<p>See also <a href="#d77456">find</a>().
<h3 class="fn">bool<a name="6365d6"></a>QCache::remove(const<a href="qstring.html">QString</a>&k)</h3>
<p>Removes the item associated with <em>k,</em> and returns TRUE if the item
was present in the cache or FALSE if it was not.
<p>The item is deleted if auto-deletion has been enabled, i.e. you have
called <a href="qcollection.html#a8ef9f">setAutoDelete</a>(TRUE).
<p>If there are two or more items with equal keys, then the one that
was inserted last is is removed.
<p>All iterators that refer to the removed item are set to point to the
next item in the cache's traversal order.
<p>See also <a href="#4e8d41">take</a>() and <a href="#0557ab">clear</a>().
<h3 class="fn">void<a name="e8ff12"></a>QCache::setMaxCost(intm)</h3>
<p>Sets the maximum allowed total cost of the cache to <em>m.</em> If the
current total cost is above <em>m,</em> some items are deleted
immediately.
<p>See also <a href="#29d27e">maxCost</a>() and <a href="#aff367">totalCost</a>().
<h3 class="fn">uint<a name="3be05e"></a>QCache::size()const</h3>
<p>Returns the size of the hash array used to implement the cache.
This should be a bit bigger than <a href="#d77b4a">count</a>() is likely to be.
<h3 class="fn">void<a name="746d80"></a>QCache::statistics()const</h3>
<p>A debug-only utility function. Prints out cache usage, hit/miss, and
distribution information using <a href="qapplication.html#72e78c">qDebug</a>(). This function does nothing
in the release library.
<h3 class="fn">type*<a name="4e8d41"></a>QCache::take(const<a href="qstring.html">QString</a>&k)</h3>
<p>Takes the item associated with <em>k</em> out of the cache without
deleting it, and returns a pointer to the item taken out, or null if
the key does not exist in the cache.
<p>If there are two or more items with equal keys, then the one that
was inserted last is taken.
<p>All iterators that refer to the taken item are set to point to the
next item in the cache's traversal order.
<p>See also <a href="#6365d6">remove</a>() and <a href="#0557ab">clear</a>().
<h3 class="fn">int<a name="aff367"></a>QCache::totalCost()const</h3>
<p>Returns the total cost of the items in the cache. This is an
integer in the range 0 to <a href="#29d27e">maxCost</a>().
<p>See also <a href="#e8ff12">setMaxCost</a>().
<h3 class="fn"><a name="4eff65"></a>QCache::QCache(constQCache<type>&c)</h3>
<p>For internal use only.
<h3 class="fn">QCache<type>&<a name="850026"></a>QCache::operator=(constQCache<type>&c)</h3>
<p>For internal use only.
<hr><p>
Search the documentation, FAQ, qt-interest archive and more (uses
<a href="http://www.trolltech.com">www.trolltech.com</a>):<br>
<form method=post action="http://www.trolltech.com/search.cgi">
<input type=hidden name="version" value="2.3.2"><nobr>
<input size="50" name="search"><input type=submit value="Search">
</nobr></form><hr><p>
This file is part of the <a href="index.html">Qt toolkit</a>,
copyright © 1995-2001
<a href="http://www.trolltech.com">Trolltech</a>, all rights reserved.<p><address><hr><div align="center">
<table width="100%" cellspacing="0" border="0"><tr>
<td>Copyright 2001 Trolltech<td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td align="right"><div align="right">Qt version 2.3.2</div>
</table></div></address></body></html>
|