source: trunk/doc/html/qptrdict.html@ 208

Last change on this file since 208 was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 15.6 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/doc/qptrdict.doc:41 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>QPtrDict Class</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { background: #ffffff; color: black; }
12--></style>
13</head>
14<body>
15
16<table border="0" cellpadding="0" cellspacing="0" width="100%">
17<tr bgcolor="#E5E5E5">
18<td valign=center>
19 <a href="index.html">
20<font color="#004faf">Home</font></a>
21 | <a href="classes.html">
22<font color="#004faf">All&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;Classes</font></a>
25 | <a href="annotated.html">
26<font color="#004faf">Annotated</font></a>
27 | <a href="groups.html">
28<font color="#004faf">Grouped&nbsp;Classes</font></a>
29 | <a href="functions.html">
30<font color="#004faf">Functions</font></a>
31</td>
32<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>QPtrDict Class Reference</h1>
33
34<p>The QPtrDict class is a template class that provides a dictionary based on void* keys.
35<a href="#details">More...</a>
36<p><tt>#include &lt;<a href="qptrdict-h.html">qptrdict.h</a>&gt;</tt>
37<p>Inherits <a href="qptrcollection.html">QPtrCollection</a>.
38<p><a href="qptrdict-members.html">List of all member functions.</a>
39<h2>Public Members</h2>
40<ul>
41<li class=fn><a href="#QPtrDict"><b>QPtrDict</b></a> ( int&nbsp;size = 17 )</li>
42<li class=fn><a href="#QPtrDict-2"><b>QPtrDict</b></a> ( const&nbsp;QPtrDict&lt;type&gt;&nbsp;&amp;&nbsp;dict )</li>
43<li class=fn><a href="#~QPtrDict"><b>~QPtrDict</b></a> ()</li>
44<li class=fn>QPtrDict&lt;type&gt; &amp; <a href="#operator-eq"><b>operator=</b></a> ( const&nbsp;QPtrDict&lt;type&gt;&nbsp;&amp;&nbsp;dict )</li>
45<li class=fn>virtual uint <a href="#count"><b>count</b></a> () const</li>
46<li class=fn>uint <a href="#size"><b>size</b></a> () const</li>
47<li class=fn>bool <a href="#isEmpty"><b>isEmpty</b></a> () const</li>
48<li class=fn>void <a href="#insert"><b>insert</b></a> ( void&nbsp;*&nbsp;key, const&nbsp;type&nbsp;*&nbsp;item )</li>
49<li class=fn>void <a href="#replace"><b>replace</b></a> ( void&nbsp;*&nbsp;key, const&nbsp;type&nbsp;*&nbsp;item )</li>
50<li class=fn>bool <a href="#remove"><b>remove</b></a> ( void&nbsp;*&nbsp;key )</li>
51<li class=fn>type * <a href="#take"><b>take</b></a> ( void&nbsp;*&nbsp;key )</li>
52<li class=fn>type * <a href="#find"><b>find</b></a> ( void&nbsp;*&nbsp;key ) const</li>
53<li class=fn>type * <a href="#operator[]"><b>operator[]</b></a> ( void&nbsp;*&nbsp;key ) const</li>
54<li class=fn>virtual void <a href="#clear"><b>clear</b></a> ()</li>
55<li class=fn>void <a href="#resize"><b>resize</b></a> ( uint&nbsp;newsize )</li>
56<li class=fn>void <a href="#statistics"><b>statistics</b></a> () const</li>
57</ul>
58<h2>Important Inherited Members</h2>
59<ul>
60<li class=fn>bool <a href="#autoDelete"><b>autoDelete</b></a> () const</li>
61<li class=fn>void <a href="#setAutoDelete"><b>setAutoDelete</b></a> ( bool&nbsp;enable )</li>
62</ul>
63<h2>Protected Members</h2>
64<ul>
65<li class=fn>virtual QDataStream &amp; <a href="#read"><b>read</b></a> ( QDataStream&nbsp;&amp;&nbsp;s, QPtrCollection::Item&nbsp;&amp;&nbsp;item )</li>
66<li class=fn>virtual QDataStream &amp; <a href="#write"><b>write</b></a> ( QDataStream&nbsp;&amp;&nbsp;s, QPtrCollection::Item ) const</li>
67</ul>
68<hr><a name="details"></a><h2>Detailed Description</h2>
69
70
71The QPtrDict class is a template class that provides a dictionary based on void* keys.
72<p>
73
74<p>
75<p> QPtrDict is implemented as a template class. Define a template
76instance QPtrDict&lt;X&gt; to create a dictionary that operates on
77pointers to X (X*).
78<p> A dictionary is a collection of key-value pairs. The key is a
79void* used for insertion, removal and lookup. The value is a
80pointer. Dictionaries provide very fast insertion and lookup.
81<p> Example:
82<pre>
83 QPtrDict&lt;char&gt; fields; // void* keys, char* values
84
85 <a href="qlineedit.html">QLineEdit</a> *le1 = new <a href="qlineedit.html">QLineEdit</a>( this );
86 le1-&gt;<a href="qlineedit.html#setText">setText</a>( "Simpson" );
87 <a href="qlineedit.html">QLineEdit</a> *le2 = new <a href="qlineedit.html">QLineEdit</a>( this );
88 le2-&gt;<a href="qlineedit.html#setText">setText</a>( "Homer" );
89 <a href="qlineedit.html">QLineEdit</a> *le3 = new <a href="qlineedit.html">QLineEdit</a>( this );
90 le3-&gt;<a href="qlineedit.html#setText">setText</a>( "45" );
91
92 fields.<a href="#insert">insert</a>( le1, "Surname" );
93 fields.<a href="#insert">insert</a>( le2, "Forename" );
94 fields.<a href="#insert">insert</a>( le3, "Age" );
95
96 <a href="qptrdictiterator.html">QPtrDictIterator</a>&lt;char&gt; it( fields );
97 for( ; it.<a href="qptrdictiterator.html#current">current</a>(); ++it )
98 cout &lt;&lt; it.<a href="qptrdictiterator.html#current">current</a>() &lt;&lt; endl;
99 cout &lt;&lt; endl;
100
101 if ( fields[le1] ) // Prints "Surname: Simpson"
102 cout &lt;&lt; fields[le1] &lt;&lt; ": " &lt;&lt; le1-&gt;<a href="qlineedit.html#text">text</a>() &lt;&lt; endl;
103 if ( fields[le2] ) // Prints "Forename: Homer"
104 cout &lt;&lt; fields[le2] &lt;&lt; ": " &lt;&lt; le2-&gt;<a href="qlineedit.html#text">text</a>() &lt;&lt; endl;
105
106 fields.<a href="#remove">remove</a>( le1 ); // Removes le1 from the dictionary
107 cout &lt;&lt; le1-&gt;<a href="qlineedit.html#text">text</a>() &lt;&lt; endl; // Prints "Simpson"
108 </pre>
109
110In this example we use a dictionary to add an extra property (a
111char*) to the line edits we're using.
112<p> See <a href="qdict.html">QDict</a> for full details, including the choice of dictionary
113size, and how deletions are handled.
114<p> <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>, <a href="collection.html">Collection Classes</a>, <a href="collection.html">Collection Classes</a>, and <a href="tools.html">Non-GUI Classes</a>.
115
116<hr><h2>Member Function Documentation</h2>
117<h3 class=fn><a name="QPtrDict"></a>QPtrDict::QPtrDict ( int&nbsp;size = 17 )
118</h3>
119
120<p> Constructs a dictionary using an internal hash array with the size
121<em>size</em>.
122<p> Setting <em>size</em> to a suitably large <a href="primes.html#prime">prime</a> number (equal to or
123greater than the expected number of entries) makes the hash
124distribution better and improves lookup performance.
125
126<h3 class=fn><a name="QPtrDict-2"></a>QPtrDict::QPtrDict ( const&nbsp;<a href="qptrdict.html">QPtrDict</a>&lt;type&gt;&nbsp;&amp;&nbsp;dict )
127</h3>
128
129<p> Constructs a copy of <em>dict</em>.
130<p> Each item in <em>dict</em> is inserted into this dictionary. Only the
131pointers are copied (shallow copy).
132
133<h3 class=fn><a name="~QPtrDict"></a>QPtrDict::~QPtrDict ()
134</h3>
135
136<p> Removes all items from the dictionary and destroys it.
137<p> All iterators that access this dictionary will be reset.
138<p> <p>See also <a href="qptrcollection.html#setAutoDelete">setAutoDelete</a>().
139
140<h3 class=fn>bool <a name="autoDelete"></a>QPtrCollection::autoDelete () const
141</h3>
142
143<p> Returns the setting of the auto-delete option. The default is FALSE.
144<p> <p>See also <a href="qptrcollection.html#setAutoDelete">setAutoDelete</a>().
145
146<h3 class=fn>void <a name="clear"></a>QPtrDict::clear ()<tt> [virtual]</tt>
147</h3>
148
149<p> Removes all items from the dictionary.
150<p> The removed items are deleted if <a href="qptrcollection.html#setAutoDelete">auto-deletion</a> is enabled.
151<p> All dictionary iterators that access this dictionary will be
152reset.
153<p> <p>See also <a href="#remove">remove</a>(), <a href="#take">take</a>(), and <a href="qptrcollection.html#setAutoDelete">setAutoDelete</a>().
154
155<p>Reimplemented from <a href="qptrcollection.html#clear">QPtrCollection</a>.
156<h3 class=fn>uint <a name="count"></a>QPtrDict::count () const<tt> [virtual]</tt>
157</h3>
158
159<p> Returns the number of items in the dictionary.
160<p> <p>See also <a href="#isEmpty">isEmpty</a>().
161
162<p>Reimplemented from <a href="qptrcollection.html#count">QPtrCollection</a>.
163<h3 class=fn>type * <a name="find"></a>QPtrDict::find ( void&nbsp;*&nbsp;key ) const
164</h3>
165
166<p> Returns the item associated with <em>key</em>, or 0 if the key does not
167exist in the dictionary.
168<p> If there are two or more items with equal keys, then the most
169recently inserted item will be found.
170<p> Equivalent to operator[].
171<p> <p>See also <a href="#operator[]">operator[]</a>().
172
173<h3 class=fn>void <a name="insert"></a>QPtrDict::insert ( void&nbsp;*&nbsp;key, const&nbsp;type&nbsp;*&nbsp;item )
174</h3>
175
176<p> Inserts the <em>key</em> with the <em>item</em> into the dictionary.
177<p> Multiple items can have the same key, in which case only the last
178item will be accessible using <a href="#operator[]">operator[]</a>().
179<p> <em>item</em> may not be 0.
180<p> <p>See also <a href="#replace">replace</a>().
181
182<h3 class=fn>bool <a name="isEmpty"></a>QPtrDict::isEmpty () const
183</h3>
184
185<p> Returns TRUE if the dictionary is empty; otherwise returns FALSE.
186<p> <p>See also <a href="#count">count</a>().
187
188<h3 class=fn><a href="qptrdict.html">QPtrDict</a>&lt;type&gt;&nbsp;&amp; <a name="operator-eq"></a>QPtrDict::operator= ( const&nbsp;<a href="qptrdict.html">QPtrDict</a>&lt;type&gt;&nbsp;&amp;&nbsp;dict )
189</h3>
190
191<p> Assigns <em>dict</em> to this dictionary and returns a reference to this
192dictionary.
193<p> This dictionary is first cleared and then each item in <em>dict</em> is
194inserted into the dictionary. Only the pointers are copied
195(shallow copy), unless <a href="qptrcollection.html#newItem">newItem</a>() has been reimplemented.
196
197<h3 class=fn>type * <a name="operator[]"></a>QPtrDict::operator[] ( void&nbsp;*&nbsp;key ) const
198</h3>
199
200<p> Returns the item associated with <em>key</em>, or 0 if the key does not
201exist in the dictionary.
202<p> If there are two or more items with equal keys, then the most
203recently inserted item will be found.
204<p> Equivalent to the <a href="#find">find</a>() function.
205<p> <p>See also <a href="#find">find</a>().
206
207<h3 class=fn><a href="qdatastream.html">QDataStream</a>&nbsp;&amp; <a name="read"></a>QPtrDict::read ( <a href="qdatastream.html">QDataStream</a>&nbsp;&amp;&nbsp;s, <a href="qptrcollection.html#Item">QPtrCollection::Item</a>&nbsp;&amp;&nbsp;item )<tt> [virtual protected]</tt>
208</h3>
209
210<p> Reads a dictionary item from the stream <em>s</em> and returns a
211reference to the stream.
212<p> The default implementation sets <em>item</em> to 0.
213<p> <p>See also <a href="#write">write</a>().
214
215<h3 class=fn>bool <a name="remove"></a>QPtrDict::remove ( void&nbsp;*&nbsp;key )
216</h3>
217
218<p> Removes the item associated with <em>key</em> from the dictionary.
219Returns TRUE if successful, i.e. if <em>key</em> is in the dictionary;
220otherwise returns FALSE.
221<p> If there are two or more items with equal keys, then the most
222recently inserted item will be removed.
223<p> The removed item is deleted if <a href="qptrcollection.html#setAutoDelete">auto-deletion</a> is enabled.
224<p> All dictionary iterators that refer to the removed item will be
225set to point to the next item in the dictionary traversal order.
226<p> <p>See also <a href="#take">take</a>(), <a href="#clear">clear</a>(), and <a href="qptrcollection.html#setAutoDelete">setAutoDelete</a>().
227
228<h3 class=fn>void <a name="replace"></a>QPtrDict::replace ( void&nbsp;*&nbsp;key, const&nbsp;type&nbsp;*&nbsp;item )
229</h3>
230
231<p> If the dictionary has key <em>key</em>, this key's item is replaced with
232<em>item</em>. If the dictionary doesn't contain key <em>key</em>, <em>item</em> is
233inserted into the dictionary using key <em>key</em>.
234<p> <em>item</em> may not be 0.
235<p> Equivalent to
236<pre>
237 QPtrDict&lt;ItemType&gt; dict;
238 ...
239 if ( dict.<a href="#find">find</a>( key ) )
240 dict.<a href="#remove">remove</a>( key );
241 dict.<a href="#insert">insert</a>( key, item );
242 </pre>
243
244<p> If there are two or more items with equal keys, then the most
245recently inserted item will be replaced.
246<p> <p>See also <a href="#insert">insert</a>().
247
248<h3 class=fn>void <a name="resize"></a>QPtrDict::resize ( uint&nbsp;newsize )
249</h3>
250
251<p> Changes the size of the hash table to <em>newsize</em>. The contents of
252the dictionary are preserved, but all iterators on the dictionary
253become invalid.
254
255<h3 class=fn>void <a name="setAutoDelete"></a>QPtrCollection::setAutoDelete ( bool&nbsp;enable )
256</h3>
257
258<p> Sets the collection to auto-delete its contents if <em>enable</em> is
259TRUE and to never delete them if <em>enable</em> is FALSE.
260<p> If auto-deleting is turned on, all the items in a collection are
261deleted when the collection itself is deleted. This is convenient
262if the collection has the only pointer to the items.
263<p> The default setting is FALSE, for safety. If you turn it on, be
264careful about copying the collection - you might find yourself
265with two collections deleting the same items.
266<p> Note that the auto-delete setting may also affect other functions
267in subclasses. For example, a subclass that has a <a href="#remove">remove</a>()
268function will remove the item from its data structure, and if
269auto-delete is enabled, will also delete the item.
270<p> <p>See also <a href="qptrcollection.html#autoDelete">autoDelete</a>().
271
272<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>.
273<h3 class=fn>uint <a name="size"></a>QPtrDict::size () const
274</h3>
275
276<p> Returns the size of the internal hash table (as specified in the
277constructor).
278<p> <p>See also <a href="#count">count</a>().
279
280<h3 class=fn>void <a name="statistics"></a>QPtrDict::statistics () const
281</h3>
282
283<p> Debugging-only function that prints out the dictionary
284distribution using <a href="qapplication.html#qDebug">qDebug</a>().
285
286<h3 class=fn>type * <a name="take"></a>QPtrDict::take ( void&nbsp;*&nbsp;key )
287</h3>
288
289<p> Takes the item associated with <em>key</em> out of the dictionary
290without deleting it (even if <a href="qptrcollection.html#setAutoDelete">auto-deletion</a> is enabled).
291<p> If there are two or more items with equal keys, then the most
292recently inserted item will be removed.
293<p> Returns a pointer to the item taken out, or 0 if the key does not
294exist in the dictionary.
295<p> All dictionary iterators that refer to the taken item will be set
296to point to the next item in the dictionary traversal order.
297<p> <p>See also <a href="#remove">remove</a>(), <a href="#clear">clear</a>(), and <a href="qptrcollection.html#setAutoDelete">setAutoDelete</a>().
298
299<h3 class=fn><a href="qdatastream.html">QDataStream</a>&nbsp;&amp; <a name="write"></a>QPtrDict::write ( <a href="qdatastream.html">QDataStream</a>&nbsp;&amp;&nbsp;s, <a href="qptrcollection.html#Item">QPtrCollection::Item</a> ) const<tt> [virtual protected]</tt>
300</h3>
301
302<p> Writes a dictionary item to the stream <em>s</em> and returns a
303reference to the stream.
304<p> <p>See also <a href="#read">read</a>().
305
306<!-- eof -->
307<hr><p>
308This file is part of the <a href="index.html">Qt toolkit</a>.
309Copyright &copy; 1995-2007
310<a href="http://www.trolltech.com/">Trolltech</a>. All Rights Reserved.<p><address><hr><div align=center>
311<table width=100% cellspacing=0 border=0><tr>
312<td>Copyright &copy; 2007
313<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
314<td align=right><div align=right>Qt 3.3.8</div>
315</table></div></address></body>
316</html>
Note: See TracBrowser for help on using the repository browser.