File: qdict.html

package info (click to toggle)
qt-embedded 2.3.2-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 68,608 kB
  • ctags: 45,998
  • sloc: cpp: 276,654; ansic: 71,987; makefile: 29,074; sh: 12,305; yacc: 2,465; python: 1,863; perl: 481; lex: 480; xml: 68; lisp: 15
file content (272 lines) | stat: -rw-r--r-- 15,309 bytes parent folder | download
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
<!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 - QDict 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>QDict Class Reference</h1><br clear="all">
<p>
The QDict class is a template class that provides a dictionary based on <code><a href="qstring.html">QString</a></code> keys.
<a href="#details">More...</a>
<p>
<code>#include &lt;<a href="qdict-h.html">qdict.h</a>&gt;</code>
<p>
Inherits <a href="qgdict.html">QGDict</a>.
<p><a href="qdict-members.html">List of all member functions.</a>
<h2>Public Members</h2>
<ul>
<li><div class="fn"><a href="#88a01f"><b>QDict</b></a>(intsize=17, boolcaseSensitive=TRUE)</div>
<li><div class="fn"><a href="#bcb6c3"><b>QDict</b></a>(constQDict&lt;type&gt;&amp;dict)</div>
<li><div class="fn"><a href="#138d0b"><b>~QDict</b></a>()</div>
<li><div class="fn">QDict&lt;type&gt;&amp;<a href="#d1e069"><b>operator=</b></a>(constQDict&lt;type&gt;&amp;dict)</div>
<li><div class="fn">virtualuint<a href="#36f642"><b>count</b></a>()const</div>
<li><div class="fn">uint<a href="#19f6da"><b>size</b></a>()const</div>
<li><div class="fn">bool<a href="#672f16"><b>isEmpty</b></a>()const</div>
<li><div class="fn">void<a href="#17a374"><b>insert</b></a>(constQString&amp;key, consttype*item)</div>
<li><div class="fn">void<a href="#f6f4ac"><b>replace</b></a>(constQString&amp;key, consttype*item)</div>
<li><div class="fn">bool<a href="#896101"><b>remove</b></a>(constQString&amp;key)</div>
<li><div class="fn">type*<a href="#265476"><b>take</b></a>(constQString&amp;key)</div>
<li><div class="fn">type*<a href="#1cdfba"><b>find</b></a>(constQString&amp;key)const</div>
<li><div class="fn">type*<a href="#4c069b"><b>operator[]</b></a>(constQString&amp;key)const</div>
<li><div class="fn">virtualvoid<a href="#2ff49d"><b>clear</b></a>()</div>
<li><div class="fn">void<a href="#3c4b73"><b>resize</b></a>(uintnewsize)</div>
<li><div class="fn">void<a href="#c36af7"><b>statistics</b></a>()const</div>
</ul>
<hr><h2><a name="details"></a>Detailed Description</h2>
The QDict class is a template class that provides a dictionary based on <code><a href="qstring.html">QString</a></code> keys.
<p>
QDict is implemented as a template class. Define a template instance
QDict&lt;X&gt; 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. QDict has
<a href="qstring.html">QString</a> keys, which are Unicode strings.  If you want to use
non-Unicode, plain 8-bit <code>char*</code> keys, use the <a href="qasciidict.html">QAsciiDict</a> template.
A QDict has the same performace as a QAsciiDict.
<p>The dictionary has very fast insertion and lookup.
<p>Example:
<pre>    #include &lt;qdict.h&gt;
    #include &lt;stdio.h&gt;

    void main()
    {
      // Creates a dictionary that maps QString ==&gt; char* (case insensitive)
        <a href="qdict.html">QDict</a>&lt;char&gt; dict( 17, FALSE );

        dict.<a href="#17a374">insert</a>( "France", "Paris" );
        dict.<a href="#17a374">insert</a>( "Russia", "Moscow" );
        dict.<a href="#17a374">insert</a>( "Norway", "Oslo" );

        printf( "%s\n", dict["Norway"] );
        printf( "%s\n", dict["FRANCE"] );
        printf( "%s\n", dict["russia"] );

        if ( !dict["Italy"] )
            printf( "Italy not defined\n" );
    }
</pre>
<p>Program output:
<pre>        Oslo
        Paris
        Moscow
        Italy not defined
</pre>
<p>The dictionary in our example maps <code>QString</code> keys to <code>char*</code> items.
Note that the mapping is case insensitive (specified in the
<a href="#88a01f">constructor</a>).
QDict implements the <a href="#4c069b">[] operator</a> to lookup an item.
<p>QDict 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. 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 QDict 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 &lt;qdict.h&gt;
    #include &lt;stdio.h&gt;

    void main()
    {
      // Creates a dictionary that maps QString ==&gt; char* (case sensitive)
        <a href="qdict.html">QDict</a>&lt;char&gt; dict;

        dict.<a href="#17a374">insert</a>( "Germany", "Berlin" );
        dict.<a href="#17a374">insert</a>( "Germany", "Bonn" );

        printf( "%s\n", dict["Germany"] );
        dict.<a href="#896101">remove</a>( "Germany" );       // Oct 3rd 1990
        printf( "%s\n", dict["Germany"] );
    }
</pre>
<p>Program output:
<pre>        Bonn
        Berlin
</pre>
<p>The <a href="qdictiterator.html">QDictIterator</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="#17a374">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.  QDict's default implementation
is to delete the item if auto-deletion is enabled.
<p>See also  <a href="qdictiterator.html">QDictIterator</a>, <a href="qasciidict.html">QAsciiDict</a>, <a href="qintdict.html">QIntDict</a>, <a href="qptrdict.html">QPtrDict</a> and <a href="collection.html">Collection Classes</a>

<hr><h2>Member Function Documentation</h2>
<h3 class="fn"><a name="bcb6c3"></a>QDict::QDict(constQDict&lt;type&gt;&amp;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="88a01f"></a>QDict::QDict(intsize=17, boolcaseSensitive=TRUE)</h3>
<p>Constructs a dictionary with the following properties:
<p>Arguments:
<ul>
<li><em>size</em> is the size of the internal hash array.
<li><em>caseSensitive</em> specifies whether to use case sensitive lookup or not.
</ul>
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.
<p>Setting <em>caseSensitive</em> to TRUE will treat "abc" and "Abc" as different
keys.  Setting it to FALSE will make the dictionary ignore case.
Case insensitive comparison includes the whole Unicode alphabeth.
<h3 class="fn"><a name="138d0b"></a>QDict::~QDict()</h3>
<p>Removes all items from the dictionary and destroys it.
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="2ff49d"></a>QDict::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 operate on dictionary are reset.
<p>See also  <a href="#896101">remove</a>(), <a href="#265476">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="36f642"></a>QDict::count()const <code>[virtual]</code></h3>
<p>Returns the number of items in the dictionary.
<p>See also  <a href="#672f16">isEmpty</a>().
<p>Reimplemented from <a href="qcollection.html#2213fa">QCollection.</a>
<h3 class="fn">type*<a name="1cdfba"></a>QDict::find(const<a href="qstring.html">QString</a>&amp;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="#4c069b">operator[]</a>().
<h3 class="fn">void<a name="17a374"></a>QDict::insert(const<a href="qstring.html">QString</a>&amp;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="#f6f4ac">replace</a>().
<h3 class="fn">bool<a name="672f16"></a>QDict::isEmpty()const</h3>
<p>Returns TRUE if the dictionary is empty, i.e. <a href="#36f642">count</a>() == 0. Returns FALSE
otherwise.
<p>See also  <a href="#36f642">count</a>().
<h3 class="fn">QDict&lt;type&gt;&amp;<a name="d1e069"></a>QDict::operator=(constQDict&lt;type&gt;&amp;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="4c069b"></a>QDict::operator[](const<a href="qstring.html">QString</a>&amp;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="#1cdfba">find</a>() function.
<p>See also  <a href="#1cdfba">find</a>().
<h3 class="fn">bool<a name="896101"></a>QDict::remove(const<a href="qstring.html">QString</a>&amp;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="#265476">take</a>(), <a href="#2ff49d">clear</a>() and <a href="qcollection.html#a8ef9f">setAutoDelete</a>().
<h3 class="fn">void<a name="f6f4ac"></a>QDict::replace(const<a href="qstring.html">QString</a>&amp;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="qdict.html">QDict</a>&lt;char&gt; dict;
        ...
    if ( dict.<a href="#1cdfba">find</a>(key) )
        dict.<a href="#896101">remove</a>( key );
    dict.<a href="#17a374">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="#17a374">insert</a>().
<h3 class="fn">void<a name="3c4b73"></a>QDict::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="19f6da"></a>QDict::size()const</h3>
<p>Returns the size of the internal hash array (as specified in the
constructor).
<p>See also  <a href="#36f642">count</a>().
<h3 class="fn">void<a name="c36af7"></a>QDict::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="265476"></a>QDict::take(const<a href="qstring.html">QString</a>&amp;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 traversal order.
<p>See also  <a href="#896101">remove</a>(), <a href="#2ff49d">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 &copy; 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>