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
|
'\" t
.TH QCache 3qt "2 February 2007" "Trolltech AS" \" -*- nroff -*-
.\" Copyright 1992-2007 Trolltech ASA. All rights reserved. See the
.\" license file included in the distribution for a complete license
.\" statement.
.\"
.ad l
.nh
.SH NAME
QCache \- Template class that provides a cache based on QString keys
.SH SYNOPSIS
\fC#include <ntqcache.h>\fR
.PP
Inherits QPtrCollection.
.PP
.SS "Public Members"
.in +1c
.ti -1c
.BI "\fBQCache\fR ( int maxCost = 100, int size = 17, bool caseSensitive = TRUE )"
.br
.ti -1c
.BI "\fB~QCache\fR ()"
.br
.ti -1c
.BI "int \fBmaxCost\fR () const"
.br
.ti -1c
.BI "int \fBtotalCost\fR () const"
.br
.ti -1c
.BI "void \fBsetMaxCost\fR ( int m )"
.br
.ti -1c
.BI "virtual uint \fBcount\fR () const"
.br
.ti -1c
.BI "uint \fBsize\fR () const"
.br
.ti -1c
.BI "bool \fBisEmpty\fR () const"
.br
.ti -1c
.BI "virtual void \fBclear\fR ()"
.br
.ti -1c
.BI "bool \fBinsert\fR ( const QString & k, const type * d, int c = 1, int p = 0 )"
.br
.ti -1c
.BI "bool \fBremove\fR ( const QString & k )"
.br
.ti -1c
.BI "type * \fBtake\fR ( const QString & k )"
.br
.ti -1c
.BI "type * \fBfind\fR ( const QString & k, bool ref = TRUE ) const"
.br
.ti -1c
.BI "type * \fBoperator[]\fR ( const QString & k ) const"
.br
.ti -1c
.BI "void \fBstatistics\fR () const"
.br
.in -1c
.SS "Important Inherited Members"
.in +1c
.ti -1c
.BI "bool \fBautoDelete\fR () const"
.br
.ti -1c
.BI "void \fBsetAutoDelete\fR ( bool enable )"
.br
.in -1c
.SH DESCRIPTION
The QCache class is a template class that provides a cache based on QString keys.
.PP
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, totalCost(), never exceeds the maximum cache cost, maxCost(). 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.
.PP
QCache is a template class. QCache<X> defines a cache that operates on pointers to X, or X*.
.PP
Apart from insert(), by far the most important function is find() (which also exists as operator[]()). This function looks up an item, returns it, and by default marks it as being the most recently used item.
.PP
There are also methods to remove() or take() an object from the cache. Calling setAutoDelete(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).
.PP
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 cache copy all of the item's data (known as a deep copy) when an item is inserted. insert() calls the virtual function QPtrCollection::newItem() for the item to be inserted. Inherit a cache and reimplement newItem() if you want deep copies.
.PP
When removing a cache item, the virtual function QPtrCollection::deleteItem() is called. The default implementation deletes the item if auto-deletion is enabled, and does nothing otherwise.
.PP
There is a QCacheIterator that can be used to traverse the items in the cache in arbitrary order.
.PP
In QCache, the cache items are accessed via QString keys, which are Unicode strings. If you want to use non-Unicode, plain 8-bit \fCchar*\fR keys, use the QAsciiCache template. A QCache has the same performance as a QAsciiCache.
.PP
See also QCacheIterator, QAsciiCache, QIntCache, Collection Classes, and Non-GUI Classes.
.SH MEMBER FUNCTION DOCUMENTATION
.SH "QCache::QCache ( int maxCost = 100, int size = 17, bool caseSensitive = TRUE )"
Constructs a cache whose contents will never have a total cost greater than \fImaxCost\fR and which is expected to contain less than \fIsize\fR items.
.PP
\fIsize\fR is actually the size of an internal hash array; it's usually best to make it a prime number and at least 50% bigger than the largest expected number of items in the cache.
.PP
Each inserted item has an associated cost. When inserting a new item, if the total cost of all items in the cache will exceed \fImaxCost\fR, the cache will start throwing out the older (least recently used) items until there is enough room for the new item to be inserted.
.PP
If \fIcaseSensitive\fR 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.
.SH "QCache::~QCache ()"
Removes all items from the cache and destroys it. All iterators that access this cache will be reset.
.SH "bool QPtrCollection::autoDelete () const"
Returns the setting of the auto-delete option. The default is FALSE.
.PP
See also setAutoDelete().
.SH "void QCache::clear ()\fC [virtual]\fR"
Removes all items from the cache and deletes them if auto-deletion has been enabled.
.PP
All cache iterators that operate this on cache are reset.
.PP
See also remove() and take().
.PP
Reimplemented from QPtrCollection.
.SH "uint QCache::count () const\fC [virtual]\fR"
Returns the number of items in the cache.
.PP
See also totalCost().
.PP
Reimplemented from QPtrCollection.
.SH "type * QCache::find ( const QString & k, bool ref = TRUE ) const"
Returns the item associated with key \fIk\fR, or 0 if the key does not exist in the cache. If \fIref\fR is TRUE (the default), the item is moved to the front of the least recently used list.
.PP
If there are two or more items with equal keys, the one that was inserted last is returned.
.SH "bool QCache::insert ( const QString & k, const type * d, int c = 1, int p = 0 )"
Inserts the item \fId\fR into the cache with key \fIk\fR and associated cost, \fIc\fR. Returns TRUE if it is successfully inserted; otherwise returns FALSE.
.PP
The cache's size is limited, and if the total cost is too high, QCache will remove old, least recently used items until there is room for this new item.
.PP
The parameter \fIp\fR is internal and should be left at the default value (0).
.PP
\fBWarning:\fR If this function returns FALSE (which could happen, e.g. if the cost of this item alone exceeds maxCost()) you must delete \fId\fR yourself. Additionally, be very careful about using \fId\fR after calling this function because 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.
.SH "bool QCache::isEmpty () const"
Returns TRUE if the cache is empty; otherwise returns FALSE.
.SH "int QCache::maxCost () const"
Returns the maximum allowed total cost of the cache.
.PP
See also setMaxCost() and totalCost().
.SH "type * QCache::operator[] ( const QString & k ) const"
Returns the item associated with key \fIk\fR, or 0 if \fIk\fR does not exist in the cache, and moves the item to the front of the least recently used list.
.PP
If there are two or more items with equal keys, the one that was inserted last is returned.
.PP
This is the same as find( k, TRUE ).
.PP
See also find().
.SH "bool QCache::remove ( const QString & k )"
Removes the item associated with \fIk\fR, and returns TRUE if the item was present in the cache; otherwise returns FALSE.
.PP
The item is deleted if auto-deletion has been enabled, i.e., if you have called setAutoDelete(TRUE).
.PP
If there are two or more items with equal keys, the one that was inserted last is removed.
.PP
All iterators that refer to the removed item are set to point to the next item in the cache's traversal order.
.PP
See also take() and clear().
.SH "void QPtrCollection::setAutoDelete ( bool enable )"
Sets the collection to auto-delete its contents if \fIenable\fR is TRUE and to never delete them if \fIenable\fR is FALSE.
.PP
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.
.PP
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.
.PP
Note that the auto-delete setting may also affect other functions in subclasses. For example, a subclass that has a remove() function will remove the item from its data structure, and if auto-delete is enabled, will also delete the item.
.PP
See also autoDelete().
.PP
Examples:
.)l grapher/grapher.cpp, scribble/scribble.cpp, and table/bigtable/main.cpp.
.SH "void QCache::setMaxCost ( int m )"
Sets the maximum allowed total cost of the cache to \fIm\fR. If the current total cost is greater than \fIm\fR, some items are deleted immediately.
.PP
See also maxCost() and totalCost().
.SH "uint QCache::size () const"
Returns the size of the hash array used to implement the cache. This should be a bit bigger than count() is likely to be.
.SH "void QCache::statistics () const"
A debug-only utility function. Prints out cache usage, hit/miss, and distribution information using tqDebug(). This function does nothing in the release library.
.SH "type * QCache::take ( const QString & k )"
Takes the item associated with \fIk\fR 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.
.PP
If there are two or more items with equal keys, the one that was inserted last is taken.
.PP
All iterators that refer to the taken item are set to point to the next item in the cache's traversal order.
.PP
See also remove() and clear().
.SH "int QCache::totalCost () const"
Returns the total cost of the items in the cache. This is an integer in the range 0 to maxCost().
.PP
See also setMaxCost().
.SH "SEE ALSO"
.BR http://doc.trolltech.com/ntqcache.html
.BR http://www.trolltech.com/faq/tech.html
.SH COPYRIGHT
Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the
license file included in the distribution for a complete license
statement.
.SH AUTHOR
Generated automatically from the source code.
.SH BUGS
If you find a bug in Qt, please report it as described in
.BR http://doc.trolltech.com/bughowto.html .
Good bug reports help us to help you. Thank you.
.P
The definitive Qt documentation is provided in HTML format; it is
located at $QTDIR/doc/html and can be read using Qt Assistant or with
a web browser. This man page is provided as a convenience for those
users who prefer man pages, although this format is not officially
supported by Trolltech.
.P
If you find errors in this manual page, please report them to
.BR qt-bugs@trolltech.com .
Please include the name of the manual page (tqcache.3qt) and the Qt
version (3.3.8).
|