1QIntCache(3qt) QIntCache(3qt)
2
3
4
6 QIntCache - Template class that provides a cache based on long keys
7
9 #include <qintcache.h>
10
11 Inherits QPtrCollection.
12
13 Public Members
14 QIntCache ( int maxCost = 100, int size = 17 )
15 ~QIntCache ()
16 int maxCost () const
17 int totalCost () const
18 void setMaxCost ( int m )
19 virtual uint count () const
20 uint size () const
21 bool isEmpty () const
22 bool insert ( long k, const type * d, int c = 1, int p = 0 )
23 bool remove ( long k )
24 type * take ( long k )
25 virtual void clear ()
26 type * find ( long k, bool ref = TRUE ) const
27 type * operator[] ( long k ) const
28 void statistics () const
29
31 The QIntCache class is a template class that provides a cache based on
32 long keys.
33
34 QIntCache is implemented as a template class. Define a template
35 instance QIntCache<X> to create a cache that operates on pointers to X,
36 or X*.
37
38 A cache is a least recently used (LRU) list of cache items, accessed
39 via long keys. Each cache item has a cost. The sum of item costs,
40 totalCost(), will not exceed the maximum cache cost, maxCost(). If
41 inserting a new item would cause the total cost to exceed the maximum
42 cost, the least recently used items in the cache are removed.
43
44 Apart from insert(), by far the most important function is find()
45 (which also exists as operator[]). This function looks up an item,
46 returns it, and by default marks it as being the most recently used
47 item.
48
49 There are also methods to remove() or take() an object from the cache.
50 Calling setAutoDelete(TRUE) for a cache tells it to delete items that
51 are removed. The default is to not delete items when they are removed
52 (i.e. remove() and take() are equivalent).
53
54 When inserting an item into the cache, only the pointer is copied, not
55 the item itself. This is called a shallow copy. It is possible to make
56 the cache copy all of the item's data (known as a deep copy) when an
57 item is inserted. insert() calls the virtual function
58 QPtrCollection::newItem() for the item to be inserted. Inherit a
59 dictionary and reimplement newItem() if you want deep copies.
60
61 When removing a cache item, the item will be automatically deleted if
62 auto-deletion is enabled.
63
64 There is a QIntCacheIterator which may be used to traverse the items in
65 the cache in arbitrary order.
66
67 See also QIntCacheIterator, QCache, QAsciiCache, Collection Classes,
68 and Non-GUI Classes.
69
72 Constructs a cache whose contents will never have a total cost greater
73 than maxCost and which is expected to contain less than size items.
74
75 size is actually the size of an internal hash array; it's usually best
76 to make it prime and at least 50% bigger than the largest expected
77 number of items in the cache.
78
79 Each inserted item is associated with a cost. When inserting a new
80 item, if the total cost of all items in the cache will exceed maxCost,
81 the cache will start throwing out the older (least recently used) items
82 until there is enough room for the new item to be inserted.
83
85 Removes all items from the cache and then destroys the int cache. If
86 auto-deletion is enabled the cache's items are deleted. All iterators
87 that access this cache will be reset.
88
90 Removes all items from the cache, and deletes them if auto-deletion has
91 been enabled.
92
93 All cache iterators that operate this on cache are reset.
94
95 See also remove() and take().
96
97 Reimplemented from QPtrCollection.
98
100 Returns the number of items in the cache.
101
102 See also totalCost().
103
104 Reimplemented from QPtrCollection.
105
107 Returns the item associated with k, or 0 if the key does not exist in
108 the cache. If ref is TRUE (the default), the item is moved to the front
109 of the least recently used list.
110
111 If there are two or more items with equal keys, the one that was
112 inserted most recently is returned.
113
115 Inserts the item d into the cache with key k and assigns it a cost of c
116 (default 1). Returns TRUE if it succeeds; otherwise returns FALSE.
117
118 The cache's size is limited, and if the total cost is too high,
119 QIntCache will remove old, least-used items until there is room for
120 this new item.
121
122 The parameter p is internal and should be left at the default value
123 (0).
124
125 Warning: If this function returns FALSE (for example, the cost ,
126 exceeds maxCost()), you must delete d yourself. Additionally, be very
127 careful about using d after calling this function. Any other insertions
128 into the cache, from anywhere in the application or within Qt itself,
129 could cause the object to be discarded from the cache and the pointer
130 to become invalid.
131
133 Returns TRUE if the cache is empty; otherwise returns FALSE.
134
136 Returns the maximum allowed total cost of the cache.
137
138 See also setMaxCost() and totalCost().
139
141 Returns the item associated with k, or 0 if k does not exist in the
142 cache, and moves the item to the front of the least recently used list.
143
144 If there are two or more items with equal keys, the one that was
145 inserted most recently is returned.
146
147 This is the same as find( k, TRUE ).
148
149 See also find().
150
152 Removes the item associated with k, and returns TRUE if the item was
153 present in the cache; otherwise returns FALSE.
154
155 The item is deleted if auto-deletion has been enabled, i.e. if you have
156 called setAutoDelete(TRUE).
157
158 If there are two or more items with equal keys, the one that was
159 inserted most recently is removed.
160
161 All iterators that refer to the removed item are set to point to the
162 next item in the cache's traversal order.
163
164 See also take() and clear().
165
167 Sets the maximum allowed total cost of the cache to m. If the current
168 total cost is greater than m, some items are removed immediately.
169
170 See also maxCost() and totalCost().
171
173 Returns the size of the hash array used to implement the cache. This
174 should be a bit larger than count() is likely to be.
175
177 A debug-only utility function. Prints out cache usage, hit/miss, and
178 distribution information using qDebug(). This function does nothing in
179 the release library.
180
182 Takes the item associated with k out of the cache without deleting it,
183 and returns a pointer to the item taken out or 0 if the key does not
184 exist in the cache.
185
186 If there are two or more items with equal keys, the one that was
187 inserted most recently is taken.
188
189 All iterators that refer to the taken item are set to point to the next
190 item in the cache's traversal order.
191
192 See also remove() and clear().
193
195 Returns the total cost of the items in the cache. This is an integer in
196 the range 0 to maxCost().
197
198 See also setMaxCost().
199
200
202 http://doc.trolltech.com/qintcache.html
203 http://www.trolltech.com/faq/tech.html
204
206 Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the
207 license file included in the distribution for a complete license
208 statement.
209
211 Generated automatically from the source code.
212
214 If you find a bug in Qt, please report it as described in
215 http://doc.trolltech.com/bughowto.html. Good bug reports help us to
216 help you. Thank you.
217
218 The definitive Qt documentation is provided in HTML format; it is
219 located at $QTDIR/doc/html and can be read using Qt Assistant or with a
220 web browser. This man page is provided as a convenience for those users
221 who prefer man pages, although this format is not officially supported
222 by Trolltech.
223
224 If you find errors in this manual page, please report them to qt-
225 bugs@trolltech.com. Please include the name of the manual page
226 (qintcache.3qt) and the Qt version (3.3.8).
227
228
229
230Trolltech AS 2 February 2007 QIntCache(3qt)