1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
14 | ** written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Free Documentation License
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
20 | ** file.
|
---|
21 | **
|
---|
22 | ** If you have questions regarding the use of this file, please contact
|
---|
23 | ** Nokia at [email protected].
|
---|
24 | ** $QT_END_LICENSE$
|
---|
25 | **
|
---|
26 | ****************************************************************************/
|
---|
27 |
|
---|
28 | /*!
|
---|
29 | \class Q3Dict
|
---|
30 | \brief The Q3Dict class is a template class that provides a
|
---|
31 | dictionary based on QString keys.
|
---|
32 | \compat
|
---|
33 |
|
---|
34 | Q3Dict is implemented as a template class. Define a template
|
---|
35 | instance Q3Dict\<X\> to create a dictionary that operates on
|
---|
36 | pointers to X (X *).
|
---|
37 |
|
---|
38 | A dictionary is a collection of key-value pairs. The key is a
|
---|
39 | QString used for insertion, removal and lookup. The value is a
|
---|
40 | pointer. Dictionaries provide very fast insertion and lookup.
|
---|
41 |
|
---|
42 | If you want to use non-Unicode, plain 8-bit \c char* keys, use the
|
---|
43 | Q3AsciiDict template. A Q3Dict has the same performance as a
|
---|
44 | Q3AsciiDict. If you want to have a dictionary that maps QStrings to
|
---|
45 | QStrings use QMap.
|
---|
46 |
|
---|
47 | The size() of the dictionary is very important. In order to get
|
---|
48 | good performance, you should use a suitably large prime number.
|
---|
49 | Suitable means equal to or larger than the maximum expected number
|
---|
50 | of dictionary items. Size is set in the constructor but may be
|
---|
51 | changed with resize().
|
---|
52 |
|
---|
53 | Items are inserted with insert(); 0 pointers cannot be inserted.
|
---|
54 | Items are removed with remove(). All the items in a dictionary can
|
---|
55 | be removed with clear(). The number of items in the dictionary is
|
---|
56 | returned by count(). If the dictionary contains no items isEmpty()
|
---|
57 | returns TRUE. You can change an item's value with replace(). Items
|
---|
58 | are looked up with operator[](), or with find() which return a
|
---|
59 | pointer to the value or 0 if the given key does not exist. You can
|
---|
60 | take an item out of the dictionary with take().
|
---|
61 |
|
---|
62 | Calling setAutoDelete(TRUE) for a dictionary tells it to delete
|
---|
63 | items that are removed. The default behavior is not to delete
|
---|
64 | items when they are removed.
|
---|
65 |
|
---|
66 | When an item is inserted, the key is converted (hashed) to an
|
---|
67 | integer index into an internal hash array. This makes lookup very
|
---|
68 | fast.
|
---|
69 |
|
---|
70 | Items with equal keys are allowed. When inserting two items with
|
---|
71 | the same key, only the last inserted item will be accessible (last
|
---|
72 | in, first out) until it is removed.
|
---|
73 |
|
---|
74 | The Q3DictIterator class can traverse the dictionary, but only in
|
---|
75 | an arbitrary order. Multiple iterators may independently traverse
|
---|
76 | the same dictionary.
|
---|
77 |
|
---|
78 | When inserting an item into a dictionary, only the pointer is
|
---|
79 | copied, not the item itself, i.e. a shallow copy is made. It is
|
---|
80 | possible to make the dictionary copy all of the item's data (a
|
---|
81 | deep copy) when an item is inserted. insert() calls the virtual
|
---|
82 | function Q3PtrCollection::newItem() for the item to be inserted.
|
---|
83 | Inherit a dictionary and reimplement newItem() if you want deep
|
---|
84 | copies.
|
---|
85 |
|
---|
86 | When removing a dictionary item, the virtual function
|
---|
87 | Q3PtrCollection::deleteItem() is called. Q3Dict's default
|
---|
88 | implementation is to delete the item if auto-deletion is enabled.
|
---|
89 |
|
---|
90 | \sa Q3DictIterator, Q3AsciiDict, Q3IntDict, Q3PtrDict
|
---|
91 | */
|
---|
92 |
|
---|
93 |
|
---|
94 | /*!
|
---|
95 | \fn Q3Dict::Q3Dict( int size, bool caseSensitive )
|
---|
96 |
|
---|
97 | Constructs a dictionary optimized for less than \a size entries.
|
---|
98 |
|
---|
99 | We recommend setting \a size to a suitably large prime number
|
---|
100 | (e.g. a prime that's slightly larger than the expected number of
|
---|
101 | entries). This makes the hash distribution better which will lead
|
---|
102 | to faster lookup.
|
---|
103 |
|
---|
104 | If \a caseSensitive is TRUE (the default), keys which differ only
|
---|
105 | by case are considered different.
|
---|
106 | */
|
---|
107 |
|
---|
108 | /*!
|
---|
109 | \fn Q3Dict::Q3Dict( const Q3Dict<type> &dict )
|
---|
110 |
|
---|
111 | Constructs a copy of \a dict.
|
---|
112 |
|
---|
113 | Each item in \a dict is inserted into this dictionary. Only the
|
---|
114 | pointers are copied (shallow copy).
|
---|
115 | */
|
---|
116 |
|
---|
117 | /*!
|
---|
118 | \fn Q3Dict::~Q3Dict()
|
---|
119 |
|
---|
120 | Removes all items from the dictionary and destroys it. If
|
---|
121 | setAutoDelete() is TRUE, each value is deleted. All iterators that
|
---|
122 | access this dictionary will be reset.
|
---|
123 |
|
---|
124 | \sa setAutoDelete()
|
---|
125 | */
|
---|
126 |
|
---|
127 | /*!
|
---|
128 | \fn Q3Dict<type> &Q3Dict::operator=(const Q3Dict<type> &dict)
|
---|
129 |
|
---|
130 | Assigns \a dict to this dictionary and returns a reference to this
|
---|
131 | dictionary.
|
---|
132 |
|
---|
133 | This dictionary is first cleared, then each item in \a dict is
|
---|
134 | inserted into this dictionary. Only the pointers are copied
|
---|
135 | (shallow copy), unless newItem() has been reimplemented.
|
---|
136 | */
|
---|
137 |
|
---|
138 | /*!
|
---|
139 | \fn uint Q3Dict::count() const
|
---|
140 |
|
---|
141 | Returns the number of items in the dictionary.
|
---|
142 |
|
---|
143 | \sa isEmpty()
|
---|
144 | */
|
---|
145 |
|
---|
146 | /*!
|
---|
147 | \fn uint Q3Dict::size() const
|
---|
148 |
|
---|
149 | Returns the size of the internal hash array (as specified in the
|
---|
150 | constructor).
|
---|
151 |
|
---|
152 | \sa count()
|
---|
153 | */
|
---|
154 |
|
---|
155 | /*!
|
---|
156 | \fn void Q3Dict::resize( uint newsize )
|
---|
157 |
|
---|
158 | Changes the size of the hash table to \a newsize. The contents of
|
---|
159 | the dictionary are preserved, but all iterators on the dictionary
|
---|
160 | become invalid.
|
---|
161 | */
|
---|
162 |
|
---|
163 | /*!
|
---|
164 | \fn bool Q3Dict::isEmpty() const
|
---|
165 |
|
---|
166 | Returns TRUE if the dictionary is empty, i.e. count() == 0;
|
---|
167 | otherwise returns FALSE.
|
---|
168 |
|
---|
169 | \sa count()
|
---|
170 | */
|
---|
171 |
|
---|
172 | /*!
|
---|
173 | \fn void Q3Dict::insert( const QString &key, const type *item )
|
---|
174 |
|
---|
175 | Inserts the key \a key with value \a item into the dictionary.
|
---|
176 |
|
---|
177 | Multiple items can have the same key, in which case only the last
|
---|
178 | item will be accessible using \l operator[]().
|
---|
179 |
|
---|
180 | \a item may not be 0.
|
---|
181 |
|
---|
182 | \sa replace()
|
---|
183 | */
|
---|
184 |
|
---|
185 | /*!
|
---|
186 | \fn void Q3Dict::replace( const QString &key, const type *item )
|
---|
187 |
|
---|
188 | Replaces the value of the key, \a key with \a item.
|
---|
189 |
|
---|
190 | If the item does not already exist, it will be inserted.
|
---|
191 |
|
---|
192 | \a item may not be 0.
|
---|
193 |
|
---|
194 | Equivalent to:
|
---|
195 | \snippet doc/src/snippets/code/doc_src_q3dict.qdoc 0
|
---|
196 |
|
---|
197 | If there are two or more items with equal keys, then the last item
|
---|
198 | that was inserted will be replaced.
|
---|
199 |
|
---|
200 | \sa insert()
|
---|
201 | */
|
---|
202 |
|
---|
203 | /*!
|
---|
204 | \fn bool Q3Dict::remove( const QString &key )
|
---|
205 |
|
---|
206 | Removes the item with \a key from the dictionary. Returns TRUE if
|
---|
207 | successful, i.e. if the item is in the dictionary; otherwise
|
---|
208 | returns FALSE.
|
---|
209 |
|
---|
210 | If there are two or more items with equal keys, then the last item
|
---|
211 | that was inserted will be removed.
|
---|
212 |
|
---|
213 | The removed item is deleted if \link
|
---|
214 | Q3PtrCollection::setAutoDelete() auto-deletion\endlink is enabled.
|
---|
215 |
|
---|
216 | All dictionary iterators that refer to the removed item will be
|
---|
217 | set to point to the next item in the dictionary's traversal order.
|
---|
218 |
|
---|
219 | \sa take(), clear(), setAutoDelete()
|
---|
220 | */
|
---|
221 |
|
---|
222 | /*!
|
---|
223 | \fn type *Q3Dict::take( const QString &key )
|
---|
224 |
|
---|
225 | Takes the item with \a key out of the dictionary without deleting
|
---|
226 | it (even if \link Q3PtrCollection::setAutoDelete()
|
---|
227 | auto-deletion\endlink is enabled).
|
---|
228 |
|
---|
229 | If there are two or more items with equal keys, then the last item
|
---|
230 | that was inserted will be taken.
|
---|
231 |
|
---|
232 | Returns a pointer to the item taken out, or 0 if the key does not
|
---|
233 | exist in the dictionary.
|
---|
234 |
|
---|
235 | All dictionary iterators that refer to the taken item will be set
|
---|
236 | to point to the next item in the dictionary traversal order.
|
---|
237 |
|
---|
238 | \sa remove(), clear(), setAutoDelete()
|
---|
239 | */
|
---|
240 |
|
---|
241 | /*!
|
---|
242 | \fn void Q3Dict::clear()
|
---|
243 |
|
---|
244 | Removes all items from the dictionary.
|
---|
245 |
|
---|
246 | The removed items are deleted if \link
|
---|
247 | Q3PtrCollection::setAutoDelete() auto-deletion\endlink is enabled.
|
---|
248 |
|
---|
249 | All dictionary iterators that operate on the dictionary are reset.
|
---|
250 |
|
---|
251 | \sa remove(), take(), setAutoDelete()
|
---|
252 | */
|
---|
253 |
|
---|
254 | /*!
|
---|
255 | \fn type *Q3Dict::find( const QString &key ) const
|
---|
256 |
|
---|
257 | Returns the item with key \a key, or 0 if the key does not exist
|
---|
258 | in the dictionary.
|
---|
259 |
|
---|
260 | If there are two or more items with equal keys, then the most
|
---|
261 | recently inserted item will be found.
|
---|
262 |
|
---|
263 | Equivalent to the [] operator.
|
---|
264 |
|
---|
265 | \sa operator[]()
|
---|
266 | */
|
---|
267 |
|
---|
268 | /*!
|
---|
269 | \fn type *Q3Dict::operator[]( const QString &key ) const
|
---|
270 |
|
---|
271 | Returns the item with key \a key, or 0 if the key does not
|
---|
272 | exist in the dictionary.
|
---|
273 |
|
---|
274 | If there are two or more items with equal keys, then the most
|
---|
275 | recently inserted item will be found.
|
---|
276 |
|
---|
277 | Equivalent to the find() function.
|
---|
278 |
|
---|
279 | \sa find()
|
---|
280 | */
|
---|
281 |
|
---|
282 | /*!
|
---|
283 | \fn void Q3Dict::statistics() const
|
---|
284 |
|
---|
285 | Debugging-only function that prints out the dictionary
|
---|
286 | distribution using qDebug().
|
---|
287 | */
|
---|
288 |
|
---|
289 | /*!
|
---|
290 | \fn QDataStream& Q3Dict::read( QDataStream &s, Q3PtrCollection::Item &item )
|
---|
291 |
|
---|
292 | Reads a dictionary item from the stream \a s and returns a
|
---|
293 | reference to the stream.
|
---|
294 |
|
---|
295 | The default implementation sets \a item to 0.
|
---|
296 |
|
---|
297 | \sa write()
|
---|
298 | */
|
---|
299 |
|
---|
300 | /*!
|
---|
301 | \fn QDataStream& Q3Dict::write( QDataStream &s, Q3PtrCollection::Item item ) const
|
---|
302 |
|
---|
303 | Writes a dictionary \a item to the stream \a s and returns a
|
---|
304 | reference to the stream.
|
---|
305 |
|
---|
306 | \sa read()
|
---|
307 | */
|
---|
308 |
|
---|
309 | /*!
|
---|
310 | \class Q3DictIterator
|
---|
311 | \brief The Q3DictIterator class provides an iterator for Q3Dict collections.
|
---|
312 | \compat
|
---|
313 |
|
---|
314 | Q3DictIterator is implemented as a template class. Define a
|
---|
315 | template instance Q3DictIterator\<X\> to create a dictionary
|
---|
316 | iterator that operates on Q3Dict\<X\> (dictionary of X*).
|
---|
317 |
|
---|
318 | The traversal order is arbitrary; when we speak of the "first",
|
---|
319 | "last" and "next" item we are talking in terms of this arbitrary
|
---|
320 | order.
|
---|
321 |
|
---|
322 | Multiple iterators may independently traverse the same dictionary.
|
---|
323 | A Q3Dict knows about all the iterators that are operating on the
|
---|
324 | dictionary. When an item is removed from the dictionary, Q3Dict
|
---|
325 | updates all iterators that are referring to the removed item to
|
---|
326 | point to the next item in the (arbitrary) traversal order.
|
---|
327 |
|
---|
328 | Example:
|
---|
329 | \snippet doc/src/snippets/code/doc_src_q3dict.qdoc 1
|
---|
330 | In the example we insert some pointers to line edits into a
|
---|
331 | dictionary, then iterate over the dictionary printing the strings
|
---|
332 | associated with the line edits.
|
---|
333 |
|
---|
334 | \sa Q3Dict
|
---|
335 | */
|
---|
336 |
|
---|
337 | /*!
|
---|
338 | \fn Q3DictIterator::Q3DictIterator( const Q3Dict<type> &dict )
|
---|
339 |
|
---|
340 | Constructs an iterator for \a dict. The current iterator item is
|
---|
341 | set to point to the first item in the dictionary, \a dict. First
|
---|
342 | in this context means first in the arbitrary traversal order.
|
---|
343 | */
|
---|
344 |
|
---|
345 | /*!
|
---|
346 | \fn Q3DictIterator::~Q3DictIterator()
|
---|
347 |
|
---|
348 | Destroys the iterator.
|
---|
349 | */
|
---|
350 |
|
---|
351 | /*!
|
---|
352 | \fn uint Q3DictIterator::count() const
|
---|
353 |
|
---|
354 | Returns the number of items in the dictionary over which the
|
---|
355 | iterator is operating.
|
---|
356 |
|
---|
357 | \sa isEmpty()
|
---|
358 | */
|
---|
359 |
|
---|
360 | /*!
|
---|
361 | \fn bool Q3DictIterator::isEmpty() const
|
---|
362 |
|
---|
363 | Returns TRUE if the dictionary is empty, i.e. count() == 0;
|
---|
364 | otherwise returns FALSE.
|
---|
365 |
|
---|
366 | \sa count()
|
---|
367 | */
|
---|
368 |
|
---|
369 | /*!
|
---|
370 | \fn type *Q3DictIterator::toFirst()
|
---|
371 |
|
---|
372 | Resets the iterator, making the first item the first current item.
|
---|
373 | First in this context means first in the arbitrary traversal
|
---|
374 | order. Returns a pointer to this item.
|
---|
375 |
|
---|
376 | If the dictionary is empty it sets the current item to 0 and
|
---|
377 | returns 0.
|
---|
378 | */
|
---|
379 |
|
---|
380 | /*!
|
---|
381 | \fn type *Q3DictIterator::operator*()
|
---|
382 | \internal
|
---|
383 | */
|
---|
384 |
|
---|
385 | /*!
|
---|
386 | \fn Q3DictIterator::operator type*() const
|
---|
387 |
|
---|
388 | Cast operator. Returns a pointer to the current iterator item.
|
---|
389 | Same as current().
|
---|
390 | */
|
---|
391 |
|
---|
392 |
|
---|
393 | /*!
|
---|
394 | \fn type *Q3DictIterator::current() const
|
---|
395 |
|
---|
396 | Returns a pointer to the current iterator item's value.
|
---|
397 | */
|
---|
398 |
|
---|
399 | /*!
|
---|
400 | \fn QString Q3DictIterator::currentKey() const
|
---|
401 |
|
---|
402 | Returns the current iterator item's key.
|
---|
403 | */
|
---|
404 |
|
---|
405 | /*!
|
---|
406 | \fn type *Q3DictIterator::operator()()
|
---|
407 |
|
---|
408 | Makes the next item current and returns the original current item.
|
---|
409 |
|
---|
410 | If the current iterator item was the last item in the dictionary
|
---|
411 | or if it was 0, 0 is returned.
|
---|
412 | */
|
---|
413 |
|
---|
414 | /*!
|
---|
415 | \fn type *Q3DictIterator::operator++()
|
---|
416 |
|
---|
417 | Prefix ++ makes the next item current and returns the new current
|
---|
418 | item.
|
---|
419 |
|
---|
420 | If the current iterator item was the last item in the dictionary
|
---|
421 | or if it was 0, 0 is returned.
|
---|
422 | */
|
---|
423 |
|
---|
424 | /*!
|
---|
425 | \fn type *Q3DictIterator::operator+=( uint jump )
|
---|
426 | \internal
|
---|
427 | Sets the current item to the item \a jump positions after the current item,
|
---|
428 | and returns a pointer to that item.
|
---|
429 |
|
---|
430 | If that item is beyond the last item or if the dictionary is empty,
|
---|
431 | it sets the current item to 0 and returns 0.
|
---|
432 | */
|
---|