summaryrefslogtreecommitdiffstats
path: root/doc/html/ntqcache.html
blob: e7f0ad16b45ec977e65bab90c704ad9bff31e717 (plain)
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/doc/qcache.doc:41 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>TQCache Class</title>
<style type="text/css"><!--
fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
--></style>
</head>
<body>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
 <a href="index.html">
<font color="#004faf">Home</font></a>
 | <a href="classes.html">
<font color="#004faf">All&nbsp;Classes</font></a>
 | <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
 | <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
 | <a href="groups.html">
<font color="#004faf">Grouped&nbsp;Classes</font></a>
 | <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>TQCache Class Reference</h1>

<p>The TQCache class is a template class that provides a cache based on TQString keys.
<a href="#details">More...</a>
<p><tt>#include &lt;<a href="qcache-h.html">ntqcache.h</a>&gt;</tt>
<p>Inherits <a href="tqptrcollection.html">TQPtrCollection</a>.
<p><a href="qcache-members.html">List of all member functions.</a>
<h2>Public Members</h2>
<ul>
<li class=fn><a href="#TQCache-2"><b>TQCache</b></a> ( int&nbsp;maxCost = 100, int&nbsp;size = 17, bool&nbsp;caseSensitive = TRUE )</li>
<li class=fn><a href="#~TQCache"><b>~TQCache</b></a> ()</li>
<li class=fn>int <a href="#maxCost"><b>maxCost</b></a> () const</li>
<li class=fn>int <a href="#totalCost"><b>totalCost</b></a> () const</li>
<li class=fn>void <a href="#setMaxCost"><b>setMaxCost</b></a> ( int&nbsp;m )</li>
<li class=fn>virtual uint <a href="#count"><b>count</b></a> () const</li>
<li class=fn>uint <a href="#size"><b>size</b></a> () const</li>
<li class=fn>bool <a href="#isEmpty"><b>isEmpty</b></a> () const</li>
<li class=fn>virtual void <a href="#clear"><b>clear</b></a> ()</li>
<li class=fn>bool <a href="#insert"><b>insert</b></a> ( const&nbsp;TQString&nbsp;&amp;&nbsp;k, const&nbsp;type&nbsp;*&nbsp;d, int&nbsp;c = 1, int&nbsp;p = 0 )</li>
<li class=fn>bool <a href="#remove"><b>remove</b></a> ( const&nbsp;TQString&nbsp;&amp;&nbsp;k )</li>
<li class=fn>type * <a href="#take"><b>take</b></a> ( const&nbsp;TQString&nbsp;&amp;&nbsp;k )</li>
<li class=fn>type * <a href="#find"><b>find</b></a> ( const&nbsp;TQString&nbsp;&amp;&nbsp;k, bool&nbsp;ref = TRUE ) const</li>
<li class=fn>type * <a href="#operator[]"><b>operator[]</b></a> ( const&nbsp;TQString&nbsp;&amp;&nbsp;k ) const</li>
<li class=fn>void <a href="#statistics"><b>statistics</b></a> () const</li>
</ul>
<h2>Important Inherited Members</h2>
<ul>
<li class=fn>bool <a href="#autoDelete"><b>autoDelete</b></a> () const</li>
<li class=fn>void <a href="#setAutoDelete"><b>setAutoDelete</b></a> ( bool&nbsp;enable )</li>
</ul>
<hr><a name="details"></a><h2>Detailed Description</h2>


The TQCache class is a template class that provides a cache based on <a href="tqstring.html">TQString</a> keys.
<p> 

<p> 
<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="#totalCost">totalCost</a>(), never exceeds the maximum cache cost, <a href="#maxCost">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> TQCache is a template class. TQCache&lt;X&gt; defines a cache that
operates on pointers to X, or X*.
<p> Apart from <a href="#insert">insert</a>(), by far the most important function is <a href="#find">find</a>()
(which also exists as <a href="#operator[]">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="#remove">remove</a>() or <a href="#take">take</a>() an object from the
cache. Calling <a href="tqptrcollection.html#setAutoDelete">setAutoDelete</a>(TRUE) for a cache tells it to delete
items that are removed. The default is to not delete items when
they 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 <a href="shclass.html#shallow-copy">shallow copy</a>. It is possible
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
function <a href="tqptrcollection.html#newItem">TQPtrCollection::newItem</a>() for the item to be inserted.
Inherit a cache and reimplement <a href="tqptrcollection.html#newItem">newItem</a>() if you want deep copies.
<p> When removing a cache item, the virtual function
<a href="tqptrcollection.html#deleteItem">TQPtrCollection::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">TQCacheIterator</a> that can be used to traverse the items
in the cache in arbitrary order.
<p> In TQCache, the cache items are accessed via <a href="tqstring.html">TQString</a> keys, which
are Unicode strings. If you want to use non-Unicode, plain 8-bit
<tt>char*</tt> keys, use the <a href="ntqasciicache.html">TQAsciiCache</a> template. A TQCache has the
same performance as a TQAsciiCache.
<p> <p>See also <a href="qcacheiterator.html">TQCacheIterator</a>, <a href="ntqasciicache.html">TQAsciiCache</a>, <a href="ntqintcache.html">TQIntCache</a>, <a href="collection.html">Collection Classes</a>, and <a href="tools.html">Non-GUI Classes</a>.

<hr><h2>Member Function Documentation</h2>
<h3 class=fn><a name="TQCache-2"></a>TQCache::TQCache ( int&nbsp;maxCost = 100, int&nbsp;size = 17, bool&nbsp;caseSensitive = TRUE )
</h3>

<p> Constructs a cache whose contents will never have a total cost
greater than <em>maxCost</em> and which is expected to contain less than
<em>size</em> items.
<p> <em>size</em> is actually the size of an internal hash array; it's
usually best to make it a <a href="primes.html#prime">prime</a> number and at least 50% bigger
than the largest expected number of items in the cache.
<p> Each inserted item has an associated cost. When inserting a new
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
recently used) items until there is enough room for the new item
to be inserted.
<p> If <em>caseSensitive</em> is TRUE (the default), the cache keys are case
sensitive; if it is FALSE, they are case-insensitive.
Case-insensitive comparison considers all Unicode letters.

<h3 class=fn><a name="~TQCache"></a>TQCache::~TQCache ()
</h3>

<p> Removes all items from the cache and destroys it. All iterators
that access this cache will be reset.

<h3 class=fn>bool <a name="autoDelete"></a>TQPtrCollection::autoDelete () const
</h3>

<p> Returns the setting of the auto-delete option. The default is FALSE.
<p> <p>See also <a href="tqptrcollection.html#setAutoDelete">setAutoDelete</a>().

<h3 class=fn>void <a name="clear"></a>TQCache::clear ()<tt> [virtual]</tt>
</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> <p>See also <a href="#remove">remove</a>() and <a href="#take">take</a>().

<p>Reimplemented from <a href="tqptrcollection.html#clear">TQPtrCollection</a>.
<h3 class=fn>uint <a name="count"></a>TQCache::count () const<tt> [virtual]</tt>
</h3>

<p> Returns the number of items in the cache.
<p> <p>See also <a href="#totalCost">totalCost</a>().

<p>Reimplemented from <a href="tqptrcollection.html#count">TQPtrCollection</a>.
<h3 class=fn>type * <a name="find"></a>TQCache::find ( const&nbsp;<a href="tqstring.html">TQString</a>&nbsp;&amp;&nbsp;k, bool&nbsp;ref = TRUE ) const
</h3>

<p> Returns the item associated with key <em>k</em>, or 0 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 least recently used list.
<p> If there are two or more items with equal keys, the one that was
inserted last is returned.

<h3 class=fn>bool <a name="insert"></a>TQCache::insert ( const&nbsp;<a href="tqstring.html">TQString</a>&nbsp;&amp;&nbsp;k, const&nbsp;type&nbsp;*&nbsp;d, int&nbsp;c = 1, int&nbsp;p = 0 )
</h3>

<p> Inserts the item <em>d</em> into the cache with key <em>k</em> and associated
cost, <em>c</em>. Returns TRUE if it is successfully inserted; otherwise
returns FALSE.
<p> The cache's size is limited, and if the total cost is too high,
TQCache will remove old, least recently 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 (which could happen, e.g.
if the cost of this item alone exceeds <a href="#maxCost">maxCost</a>()) you must delete
<em>d</em> yourself. Additionally, be very careful about using <em>d</em>
after calling this function because any other insertions into the
cache, from anywhere in the application or within TQt itself, could
cause the object to be discarded from the cache and the pointer to
become invalid.

<h3 class=fn>bool <a name="isEmpty"></a>TQCache::isEmpty () const
</h3>

<p> Returns TRUE if the cache is empty; otherwise returns FALSE.

<h3 class=fn>int <a name="maxCost"></a>TQCache::maxCost () const
</h3>

<p> Returns the maximum allowed total cost of the cache.
<p> <p>See also <a href="#setMaxCost">setMaxCost</a>() and <a href="#totalCost">totalCost</a>().

<h3 class=fn>type * <a name="operator[]"></a>TQCache::operator[] ( const&nbsp;<a href="tqstring.html">TQString</a>&nbsp;&amp;&nbsp;k ) const
</h3>

<p> Returns the item associated with key <em>k</em>, or 0 if <em>k</em> does not
exist in the cache, and moves the item to the front of the least
recently used list.
<p> If there are two or more items with equal keys, the one that was
inserted last is returned.
<p> This is the same as <a href="#find">find</a>( k, TRUE ).
<p> <p>See also <a href="#find">find</a>().

<h3 class=fn>bool <a name="remove"></a>TQCache::remove ( const&nbsp;<a href="tqstring.html">TQString</a>&nbsp;&amp;&nbsp;k )
</h3>

<p> Removes the item associated with <em>k</em>, and returns TRUE if the
item was present in the cache; otherwise returns FALSE.
<p> The item is deleted if auto-deletion has been enabled, i.e., if
you have called <a href="tqptrcollection.html#setAutoDelete">setAutoDelete</a>(TRUE).
<p> If there are two or more items with equal keys, the one that was
inserted last 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> <p>See also <a href="#take">take</a>() and <a href="#clear">clear</a>().

<h3 class=fn>void <a name="setAutoDelete"></a>TQPtrCollection::setAutoDelete ( bool&nbsp;enable )
</h3>

<p> Sets the collection to auto-delete its contents if <em>enable</em> is
TRUE and to never delete them if <em>enable</em> is FALSE.
<p> If auto-deleting is turned on, all the items in a collection are
deleted when the collection itself is deleted. This is convenient
if the collection has the only pointer to the items.
<p> The default setting is FALSE, for safety. If you turn it on, be
careful about copying the collection - you might find yourself
with two collections deleting the same items.
<p> Note that the auto-delete setting may also affect other functions
in subclasses. For example, a subclass that has a <a href="#remove">remove</a>()
function will remove the item from its data structure, and if
auto-delete is enabled, will also delete the item.
<p> <p>See also <a href="tqptrcollection.html#autoDelete">autoDelete</a>().

<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>.
<h3 class=fn>void <a name="setMaxCost"></a>TQCache::setMaxCost ( int&nbsp;m )
</h3>

<p> Sets the maximum allowed total cost of the cache to <em>m</em>. If the
current total cost is greater than <em>m</em>, some items are deleted
immediately.
<p> <p>See also <a href="#maxCost">maxCost</a>() and <a href="#totalCost">totalCost</a>().

<h3 class=fn>uint <a name="size"></a>TQCache::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="#count">count</a>() is likely to be.

<h3 class=fn>void <a name="statistics"></a>TQCache::statistics () const
</h3>

<p> A debug-only utility function. Prints out cache usage, hit/miss,
and distribution information using <a href="ntqapplication.html#qDebug">tqDebug</a>(). This function does
nothing in the release library.

<h3 class=fn>type * <a name="take"></a>TQCache::take ( const&nbsp;<a href="tqstring.html">TQString</a>&nbsp;&amp;&nbsp;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 0
if the key does not exist in the cache.
<p> If there are two or more items with equal keys, 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> <p>See also <a href="#remove">remove</a>() and <a href="#clear">clear</a>().

<h3 class=fn>int <a name="totalCost"></a>TQCache::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="#maxCost">maxCost</a>().
<p> <p>See also <a href="#setMaxCost">setMaxCost</a>().

<!-- eof -->
<hr><p>
This file is part of the <a href="index.html">TQt toolkit</a>.
Copyright &copy; 1995-2007
<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 &copy; 2007
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
<td align=right><div align=right>TQt 3.3.8</div>
</table></div></address></body>
</html>