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
|
<!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 - QPtrDict 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>QPtrDict Class Reference</h1><br clear="all">
<p>
The QPtrDict class is a template class that provides a dictionary based on <code>void*</code> keys.
<a href="#details">More...</a>
<p>
<code>#include <<a href="qptrdict-h.html">qptrdict.h</a>></code>
<p>
Inherits <a href="qgdict.html">QGDict</a>.
<p><a href="qptrdict-members.html">List of all member functions.</a>
<h2>Public Members</h2>
<ul>
<li><div class="fn"><a href="#065c45"><b>QPtrDict</b></a>(intsize=17)</div>
<li><div class="fn"><a href="#a70ffe"><b>QPtrDict</b></a>(constQPtrDict<type>&dict)</div>
<li><div class="fn"><a href="#c4f925"><b>~QPtrDict</b></a>()</div>
<li><div class="fn">QPtrDict<type>&<a href="#914261"><b>operator=</b></a>(constQPtrDict<type>&dict)</div>
<li><div class="fn">virtualuint<a href="#049cf3"><b>count</b></a>()const</div>
<li><div class="fn">uint<a href="#877bc4"><b>size</b></a>()const</div>
<li><div class="fn">bool<a href="#8f7f7b"><b>isEmpty</b></a>()const</div>
<li><div class="fn">void<a href="#7c76c9"><b>insert</b></a>(void*key, consttype*item)</div>
<li><div class="fn">void<a href="#933eb0"><b>replace</b></a>(void*key, consttype*item)</div>
<li><div class="fn">bool<a href="#c8a557"><b>remove</b></a>(void*key)</div>
<li><div class="fn">type*<a href="#80109a"><b>take</b></a>(void*key)</div>
<li><div class="fn">type*<a href="#dd222f"><b>find</b></a>(void*key)const</div>
<li><div class="fn">type*<a href="#9f5508"><b>operator[]</b></a>(void*key)const</div>
<li><div class="fn">virtualvoid<a href="#346870"><b>clear</b></a>()</div>
<li><div class="fn">void<a href="#669c01"><b>resize</b></a>(uintnewsize)</div>
<li><div class="fn">void<a href="#2449dd"><b>statistics</b></a>()const</div>
</ul>
<hr><h2><a name="details"></a>Detailed Description</h2>
The QPtrDict class is a template class that provides a dictionary based on <code>void*</code> keys.
<p>
QPtrDict is implemented as a template class. Define a
template instance QPtrDict<X> to create a dictionary that operates on
pointers to X, or X*.
<p>A dictionary is a collection that associates an item with a key.
The key is used for inserting and looking up an item. QPtrDict has
<code>void*</code> keys.
<p>The dictionary has very fast insertion and lookup.
<p>Example:
<pre> #include <qptrdict.h>
#include <stdio.h>
void main()
{
int *a = new int[12];
int *b = new int[10];
int *c = new int[18];
int *d = new int[13];
<a href="qptrdict.html">QPtrDict</a><char> dict; // maps void* -> char*
dict.<a href="#7c76c9">insert</a>( a, "a is int[12]" ); // describe pointers
dict.<a href="#7c76c9">insert</a>( b, "b is int[10]" );
dict.<a href="#7c76c9">insert</a>( c, "c is int[18]" );
printf( "%s\n", dict[a] ); // print descriptions
printf( "%s\n", dict[b] );
printf( "%s\n", dict[c] );
if ( !dict[d] )
printf( "d not in dictionary\n" );
}
</pre>
<p>Program output:
<pre> a is int[12]
b is int[10]
c is int[18]
d not in dictionary
</pre>
<p>The dictionary in our example maps <code>int*</code> keys to <code>char*</code> items.
QPtrDict implements the <a href="#9f5508">[] operator</a> to lookup
an item.
<p>QPtrDict is implemented by <a href="qgdict.html">QGDict</a> as a hash array with a fixed number of
entries. Each array entry points to a singly linked list of buckets, in
which the dictionary items are stored.
<p>When an item is inserted with a key, the key is converted (hashed) to
an integer index into the hash array using the <code>mod</code> operation. The
item is inserted before the first bucket in the list of buckets.
<p>Looking up an item is normally very fast. The key is again hashed to an
array index. Then QPtrDict scans the list of buckets and returns the item
found or null if the item was not found. You cannot insert null pointers
into a dictionary.
<p>The size of the hash array is very important. In order to get good
performance, you should use a suitably large <a href="primes.html">prime
number</a>. Suitable means equal to or larger than the maximum
expected number of dictionary items.
<p>Items with equal keys are allowed. When inserting two items with the
same key, only the last inserted item will be visible (last in, first out)
until it is removed.
<p>Example:
<pre> #include <qptrdict.h>
#include <stdio.h>
void main()
{
<a href="qptrdict.html">QPtrDict</a><char> dict; // maps char* ==> char*
double *ptr = new double[28];
dict.<a href="#7c76c9">insert</a>( ptr, "first" );
dict.<a href="#7c76c9">insert</a>( ptr, "second" );
printf( "%s\n", dict[ptr] );
dict.<a href="#c8a557">remove</a>( ptr );
printf( "%s\n", dict[ptr] );
}
</pre>
<p>Program output:
<pre> second
first
</pre>
<p>The <a href="qptrdictiterator.html">QPtrDictIterator</a> class can traverse the dictionary contents, but only
in an arbitrary order. Multiple iterators may independently traverse the
same dictionary.
<p>Calling <a href="qcollection.html#a8ef9f">setAutoDelete</a>(TRUE) for a dictionary tells it to delete items
that are removed . The default is to not delete items when they are
removed.
<p>When inserting an item into a dictionary, 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. <a href="#7c76c9">insert</a>() 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 dictionary item, the virtual function
<a href="qcollection.html#8d78e7">QCollection::deleteItem</a>() is called. QPtrDict's default implementation
is to delete the item if auto-deletion is enabled.
<p>See also <a href="qptrdictiterator.html">QPtrDictIterator</a>, <a href="qdict.html">QDict</a>, <a href="qasciidict.html">QAsciiDict</a>, <a href="qintdict.html">QIntDict</a> and <a href="collection.html">Collection Classes</a>
<hr><h2>Member Function Documentation</h2>
<h3 class="fn"><a name="a70ffe"></a>QPtrDict::QPtrDict(constQPtrDict<type>&dict)</h3>
<p>Constructs a copy of <em>dict.</em>
<p>Each item in <em>dict</em> are inserted into this dictionary.
Only the pointers are copied (shallow copy).
<h3 class="fn"><a name="065c45"></a>QPtrDict::QPtrDict(intsize=17)</h3>
<p>Constructs a dictionary using an internal hash array with the size
<em>size.</em>
<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.
<h3 class="fn"><a name="c4f925"></a>QPtrDict::~QPtrDict()</h3>
<p>Removes all items from the dictionary and destroys it.
<p>All iterators that access this dictionary will be reset.
<p>See also <a href="qcollection.html#a8ef9f">setAutoDelete</a>().
<h3 class="fn">void<a name="346870"></a>QPtrDict::clear() <code>[virtual]</code></h3>
<p>Removes all items from the dictionary.
<p>The removed items are deleted if <a href="qcollection.html#a8ef9f">auto-deletion</a> is enabled.
<p>All dictionary iterators that access this dictionary will be reset.
<p>See also <a href="#c8a557">remove</a>(), <a href="#80109a">take</a>() and <a href="qcollection.html#a8ef9f">setAutoDelete</a>().
<p>Reimplemented from <a href="qcollection.html#e9c603">QCollection.</a>
<h3 class="fn">uint<a name="049cf3"></a>QPtrDict::count()const <code>[virtual]</code></h3>
<p>Returns the number of items in the dictionary.
<p>See also <a href="#8f7f7b">isEmpty</a>().
<p>Reimplemented from <a href="qcollection.html#2213fa">QCollection.</a>
<h3 class="fn">type*<a name="dd222f"></a>QPtrDict::find(void*key)const</h3>
<p>Returns the item associated with <em>key,</em> or null if the key does not
exist in the dictionary.
<p>This function uses an internal hashing algorithm to optimize lookup.
<p>If there are two or more items with equal keys, then the last inserted
of these will be found.
<p>Equivalent to the [] operator.
<p>See also <a href="#9f5508">operator[]</a>().
<h3 class="fn">void<a name="7c76c9"></a>QPtrDict::insert(void*key, consttype*item)</h3>
<p>Inserts the <em>key</em> with the <em>item</em> into the dictionary.
<p>The key does not have to be a unique dictionary key. If multiple items
are inserted with the same key, only the last item will be visible.
<p>Null items are not allowed.
<p>See also <a href="#933eb0">replace</a>().
<h3 class="fn">bool<a name="8f7f7b"></a>QPtrDict::isEmpty()const</h3>
<p>Returns TRUE if the dictionary is empty, i.e. <a href="#049cf3">count</a>() == 0. Returns FALSE
otherwise.
<p>See also <a href="#049cf3">count</a>().
<h3 class="fn">QPtrDict<type>&<a name="914261"></a>QPtrDict::operator=(constQPtrDict<type>&dict)</h3>
<p>Assigns <em>dict</em> to this dictionary and returns a reference to this
dictionary.
<p>This dictionary is first cleared, then each item in <em>dict</em> is inserted
into this dictionary.
Only the pointers are copied (shallow copy), unless <a href="qcollection.html#55065e">newItem</a>() has been
reimplemented().
<h3 class="fn">type*<a name="9f5508"></a>QPtrDict::operator[](void*key)const</h3>
<p>Returns the item associated with <em>key,</em> or null if the key does not
exist in the dictionary.
<p>This function uses an internal hashing algorithm to optimize lookup.
<p>If there are two or more items with equal keys, then the last inserted
of these will be found.
<p>Equivalent to the <a href="#dd222f">find</a>() function.
<p>See also <a href="#dd222f">find</a>().
<h3 class="fn">bool<a name="c8a557"></a>QPtrDict::remove(void*key)</h3>
<p>Removes the item associated with <em>key</em> from the dictionary.
Returns TRUE if successful, or FALSE if the key does not exist in the
dictionary.
<p>If there are two or more items with equal keys, then the last inserted
of these will be removed.
<p>The removed item is deleted if <a href="qcollection.html#a8ef9f">auto-deletion</a> is enabled.
<p>All dictionary iterators that refer to the removed item will be set to
point to the next item in the dictionary traversing order.
<p>See also <a href="#80109a">take</a>(), <a href="#346870">clear</a>() and <a href="qcollection.html#a8ef9f">setAutoDelete</a>().
<h3 class="fn">void<a name="933eb0"></a>QPtrDict::replace(void*key, consttype*item)</h3>
<p>Replaces an item which has a key equal to <em>key</em> with <em>item.</em>
<p>If the item does not already exist, it will be inserted.
<p>Null items are not allowed.
<p>Equivalent to:
<pre> <a href="qptrdict.html">QPtrDict</a><char> dict;
...
if ( dict.<a href="#dd222f">find</a>(key) )
dict.<a href="#c8a557">remove</a>( key );
dict.<a href="#7c76c9">insert</a>( key, item );
</pre>
<p>If there are two or more items with equal keys, then the last inserted
of these will be replaced.
<p>See also <a href="#7c76c9">insert</a>().
<h3 class="fn">void<a name="669c01"></a>QPtrDict::resize(uintnewsize)</h3>
<p>Changes the size of the hashtable the <em>newsize.</em>
The contents of the dictionary are preserved,
but all iterators on the dictionary become invalid.
<h3 class="fn">uint<a name="877bc4"></a>QPtrDict::size()const</h3>
<p>Returns the size of the internal hash array (as specified in the
constructor).
<p>See also <a href="#049cf3">count</a>().
<h3 class="fn">void<a name="2449dd"></a>QPtrDict::statistics()const</h3>
<p>Debugging-only function that prints out the dictionary distribution
using <a href="qapplication.html#72e78c">qDebug</a>().
<h3 class="fn">type*<a name="80109a"></a>QPtrDict::take(void*key)</h3>
<p>Takes the item associated with <em>key</em> out of the dictionary without
deleting it (even if <a href="qcollection.html#a8ef9f">auto-deletion</a> is enabled).
<p>If there are two or more items with equal keys, then the last inserted
of these will be taken.
<p>Returns a pointer to the item taken out, or null if the key does not
exist in the dictionary.
<p>All dictionary iterators that refer to the taken item will be set to
point to the next item in the dictionary traversing order.
<p>See also <a href="#c8a557">remove</a>(), <a href="#346870">clear</a>() and <a href="qcollection.html#a8ef9f">setAutoDelete</a>().
<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>
|