| 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 Q3AsciiDict
|
|---|
| 30 | \brief The Q3AsciiDict class is a template class that provides a dictionary based on char* keys.
|
|---|
| 31 | \compat
|
|---|
| 32 |
|
|---|
| 33 | Q3AsciiDict is implemented as a template class. Define a template
|
|---|
| 34 | instance Q3AsciiDict\<X\> to create a dictionary that operates on
|
|---|
| 35 | pointers to X (X*).
|
|---|
| 36 |
|
|---|
| 37 | A dictionary is a collection of key-value pairs. The key is a
|
|---|
| 38 | char* used for insertion, removal and lookup. The value is a
|
|---|
| 39 | pointer. Dictionaries provide very fast insertion and lookup.
|
|---|
| 40 |
|
|---|
| 41 | Q3AsciiDict cannot handle Unicode keys; use the Q3Dict template
|
|---|
| 42 | instead, which uses QString keys. A Q3Dict has the same
|
|---|
| 43 | performace as a Q3AsciiDict.
|
|---|
| 44 |
|
|---|
| 45 | Example:
|
|---|
| 46 | \snippet doc/src/snippets/code/doc_src_q3asciidict.qdoc 0
|
|---|
| 47 | In this example we use a dictionary to keep track of the line
|
|---|
| 48 | edits we're using. We insert each line edit into the dictionary
|
|---|
| 49 | with a unique name and then access the line edits via the
|
|---|
| 50 | dictionary. See Q3PtrDict, Q3IntDict and Q3Dict.
|
|---|
| 51 |
|
|---|
| 52 | See Q3Dict for full details, including the choice of dictionary
|
|---|
| 53 | size, and how deletions are handled.
|
|---|
| 54 |
|
|---|
| 55 | \sa Q3AsciiDictIterator, Q3Dict, Q3IntDict, Q3PtrDict
|
|---|
| 56 | */
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 | /*!
|
|---|
| 60 | \fn Q3AsciiDict::Q3AsciiDict( int size, bool caseSensitive, bool copyKeys )
|
|---|
| 61 |
|
|---|
| 62 | Constructs a dictionary optimized for less than \a size entries.
|
|---|
| 63 |
|
|---|
| 64 | We recommend setting \a size to a suitably large prime number (a
|
|---|
| 65 | bit larger than the expected number of entries). This makes the
|
|---|
| 66 | hash distribution better and will improve lookup performance.
|
|---|
| 67 |
|
|---|
| 68 | When \a caseSensitive is TRUE (the default) Q3AsciiDict treats
|
|---|
| 69 | "abc" and "Abc" as different keys; when it is FALSE "abc" and
|
|---|
| 70 | "Abc" are the same. Case-insensitive comparison only considers the
|
|---|
| 71 | 26 letters in US-ASCII.
|
|---|
| 72 |
|
|---|
| 73 | If \a copyKeys is TRUE (the default), the dictionary copies keys
|
|---|
| 74 | using strcpy(); if it is FALSE, the dictionary just copies the
|
|---|
| 75 | pointers.
|
|---|
| 76 | */
|
|---|
| 77 |
|
|---|
| 78 | /*!
|
|---|
| 79 | \fn Q3AsciiDict::Q3AsciiDict( const Q3AsciiDict<type> &dict )
|
|---|
| 80 |
|
|---|
| 81 | Constructs a copy of \a dict.
|
|---|
| 82 |
|
|---|
| 83 | Each item in \a dict is inserted into this dictionary. Only the
|
|---|
| 84 | pointers are copied (shallow copy).
|
|---|
| 85 | */
|
|---|
| 86 |
|
|---|
| 87 | /*!
|
|---|
| 88 | \fn Q3AsciiDict::~Q3AsciiDict()
|
|---|
| 89 |
|
|---|
| 90 | Removes all items from the dictionary and destroys it.
|
|---|
| 91 |
|
|---|
| 92 | The items are deleted if auto-delete is enabled.
|
|---|
| 93 |
|
|---|
| 94 | All iterators that access this dictionary will be reset.
|
|---|
| 95 |
|
|---|
| 96 | \sa setAutoDelete()
|
|---|
| 97 | */
|
|---|
| 98 |
|
|---|
| 99 | /*!
|
|---|
| 100 | \fn Q3AsciiDict<type> &Q3AsciiDict::operator=(const Q3AsciiDict<type> &dict)
|
|---|
| 101 |
|
|---|
| 102 | Assigns \a dict to this dictionary and returns a reference to this
|
|---|
| 103 | dictionary.
|
|---|
| 104 |
|
|---|
| 105 | This dictionary is first cleared and then each item in \a dict is
|
|---|
| 106 | inserted into this dictionary. Only the pointers are copied
|
|---|
| 107 | (shallow copy) unless newItem() has been reimplemented().
|
|---|
| 108 | */
|
|---|
| 109 |
|
|---|
| 110 | /*!
|
|---|
| 111 | \fn uint Q3AsciiDict::count() const
|
|---|
| 112 |
|
|---|
| 113 | Returns the number of items in the dictionary.
|
|---|
| 114 |
|
|---|
| 115 | \sa isEmpty()
|
|---|
| 116 | */
|
|---|
| 117 |
|
|---|
| 118 | /*!
|
|---|
| 119 | \fn uint Q3AsciiDict::size() const
|
|---|
| 120 |
|
|---|
| 121 | Returns the size of the internal hash array (as specified in the
|
|---|
| 122 | constructor).
|
|---|
| 123 |
|
|---|
| 124 | \sa count()
|
|---|
| 125 | */
|
|---|
| 126 |
|
|---|
| 127 | /*!
|
|---|
| 128 | \fn void Q3AsciiDict::resize( uint newsize )
|
|---|
| 129 |
|
|---|
| 130 | Changes the size of the hashtable to \a newsize. The contents of
|
|---|
| 131 | the dictionary are preserved but all iterators on the dictionary
|
|---|
| 132 | become invalid.
|
|---|
| 133 | */
|
|---|
| 134 |
|
|---|
| 135 | /*!
|
|---|
| 136 | \fn bool Q3AsciiDict::isEmpty() const
|
|---|
| 137 |
|
|---|
| 138 | Returns TRUE if the dictionary is empty, i.e. count() == 0;
|
|---|
| 139 | otherwise it returns FALSE.
|
|---|
| 140 |
|
|---|
| 141 | \sa count()
|
|---|
| 142 | */
|
|---|
| 143 |
|
|---|
| 144 | /*!
|
|---|
| 145 | \fn void Q3AsciiDict::insert( const char *key, const type *item )
|
|---|
| 146 |
|
|---|
| 147 | Inserts the \a key with the \a item into the dictionary.
|
|---|
| 148 |
|
|---|
| 149 | Multiple items can have the same key, in which case only the last
|
|---|
| 150 | item will be accessible using \l operator[]().
|
|---|
| 151 |
|
|---|
| 152 | \a item may not be 0.
|
|---|
| 153 |
|
|---|
| 154 | \sa replace()
|
|---|
| 155 | */
|
|---|
| 156 |
|
|---|
| 157 | /*!
|
|---|
| 158 | \fn void Q3AsciiDict::replace( const char *key, const type *item )
|
|---|
| 159 |
|
|---|
| 160 | Replaces an item that has a key equal to \a key with \a item.
|
|---|
| 161 |
|
|---|
| 162 | If the item does not already exist, it will be inserted.
|
|---|
| 163 |
|
|---|
| 164 | \a item may not be 0.
|
|---|
| 165 |
|
|---|
| 166 | Equivalent to:
|
|---|
| 167 | \snippet doc/src/snippets/code/doc_src_q3asciidict.qdoc 1
|
|---|
| 168 |
|
|---|
| 169 | If there are two or more items with equal keys, then the most
|
|---|
| 170 | recently inserted item will be replaced.
|
|---|
| 171 |
|
|---|
| 172 | \sa insert()
|
|---|
| 173 | */
|
|---|
| 174 |
|
|---|
| 175 | /*!
|
|---|
| 176 | \fn bool Q3AsciiDict::remove( const char *key )
|
|---|
| 177 |
|
|---|
| 178 | Removes the item associated with \a key from the dictionary.
|
|---|
| 179 | Returns TRUE if successful, i.e. if the key existed in the
|
|---|
| 180 | dictionary; otherwise returns FALSE.
|
|---|
| 181 |
|
|---|
| 182 | If there are two or more items with equal keys, then the most
|
|---|
| 183 | recently inserted item will be removed.
|
|---|
| 184 |
|
|---|
| 185 | The removed item is deleted if \link
|
|---|
| 186 | Q3PtrCollection::setAutoDelete() auto-deletion\endlink is enabled.
|
|---|
| 187 |
|
|---|
| 188 | All dictionary iterators that refer to the removed item will be
|
|---|
| 189 | set to point to the next item in the dictionary traversal order.
|
|---|
| 190 |
|
|---|
| 191 | \sa take(), clear(), setAutoDelete()
|
|---|
| 192 | */
|
|---|
| 193 |
|
|---|
| 194 | /*!
|
|---|
| 195 | \fn type *Q3AsciiDict::take( const char *key )
|
|---|
| 196 |
|
|---|
| 197 | Takes the item associated with \a key out of the dictionary
|
|---|
| 198 | without deleting it (even if \link Q3PtrCollection::setAutoDelete()
|
|---|
| 199 | auto-deletion\endlink is enabled).
|
|---|
| 200 |
|
|---|
| 201 | If there are two or more items with equal keys, then the most
|
|---|
| 202 | recently inserted item will be taken.
|
|---|
| 203 |
|
|---|
| 204 | Returns a pointer to the item taken out, or 0 if the key does not
|
|---|
| 205 | exist in the dictionary.
|
|---|
| 206 |
|
|---|
| 207 | All dictionary iterators that refer to the taken item will be set
|
|---|
| 208 | to point to the next item in the dictionary traversal order.
|
|---|
| 209 |
|
|---|
| 210 | \sa remove(), clear(), setAutoDelete()
|
|---|
| 211 | */
|
|---|
| 212 |
|
|---|
| 213 | /*!
|
|---|
| 214 | \fn void Q3AsciiDict::clear()
|
|---|
| 215 |
|
|---|
| 216 | Removes all items from the dictionary.
|
|---|
| 217 |
|
|---|
| 218 | The removed items are deleted if \link
|
|---|
| 219 | Q3PtrCollection::setAutoDelete() auto-deletion\endlink is enabled.
|
|---|
| 220 |
|
|---|
| 221 | All dictionary iterators that operate on dictionary are reset.
|
|---|
| 222 |
|
|---|
| 223 | \sa remove(), take(), setAutoDelete()
|
|---|
| 224 | */
|
|---|
| 225 |
|
|---|
| 226 | /*!
|
|---|
| 227 | \fn type *Q3AsciiDict::find( const char *key ) const
|
|---|
| 228 |
|
|---|
| 229 | Returns the item associated with \a key, or 0 if the key does not
|
|---|
| 230 | exist in the dictionary.
|
|---|
| 231 |
|
|---|
| 232 | This function uses an internal hashing algorithm to optimize
|
|---|
| 233 | lookup.
|
|---|
| 234 |
|
|---|
| 235 | If there are two or more items with equal keys, then the item that
|
|---|
| 236 | was most recently inserted will be found.
|
|---|
| 237 |
|
|---|
| 238 | Equivalent to the [] operator.
|
|---|
| 239 |
|
|---|
| 240 | \sa operator[]()
|
|---|
| 241 | */
|
|---|
| 242 |
|
|---|
| 243 | /*!
|
|---|
| 244 | \fn type *Q3AsciiDict::operator[]( const char *key ) const
|
|---|
| 245 |
|
|---|
| 246 | Returns the item associated with \a key, or 0 if the key does
|
|---|
| 247 | not exist in the dictionary.
|
|---|
| 248 |
|
|---|
| 249 | This function uses an internal hashing algorithm to optimize
|
|---|
| 250 | lookup.
|
|---|
| 251 |
|
|---|
| 252 | If there are two or more items with equal keys, then the item that
|
|---|
| 253 | was most recently inserted will be found.
|
|---|
| 254 |
|
|---|
| 255 | Equivalent to the find() function.
|
|---|
| 256 |
|
|---|
| 257 | \sa find()
|
|---|
| 258 | */
|
|---|
| 259 |
|
|---|
| 260 | /*!
|
|---|
| 261 | \fn void Q3AsciiDict::statistics() const
|
|---|
| 262 |
|
|---|
| 263 | Debugging-only function that prints out the dictionary
|
|---|
| 264 | distribution using qDebug().
|
|---|
| 265 | */
|
|---|
| 266 |
|
|---|
| 267 | /*!
|
|---|
| 268 | \fn QDataStream& Q3AsciiDict::read( QDataStream &s,
|
|---|
| 269 | Q3PtrCollection::Item &item )
|
|---|
| 270 |
|
|---|
| 271 | Reads a dictionary item from the stream \a s and returns a
|
|---|
| 272 | reference to the stream.
|
|---|
| 273 |
|
|---|
| 274 | The default implementation sets \a item to 0.
|
|---|
| 275 |
|
|---|
| 276 | \sa write()
|
|---|
| 277 | */
|
|---|
| 278 |
|
|---|
| 279 | /*!
|
|---|
| 280 | \fn QDataStream& Q3AsciiDict::write(QDataStream &s, Q3PtrCollection::Item item) const
|
|---|
| 281 |
|
|---|
| 282 | Writes a dictionary \a item to the stream \a s and returns a
|
|---|
| 283 | reference to the stream.
|
|---|
| 284 |
|
|---|
| 285 | \sa read()
|
|---|
| 286 | */
|
|---|
| 287 |
|
|---|
| 288 | /*!
|
|---|
| 289 | \class Q3AsciiDictIterator
|
|---|
| 290 | \brief The Q3AsciiDictIterator class provides an iterator for Q3AsciiDict collections.
|
|---|
| 291 | \compat
|
|---|
| 292 |
|
|---|
| 293 | Q3AsciiDictIterator is implemented as a template class. Define a
|
|---|
| 294 | template instance Q3AsciiDictIterator\<X\> to create a dictionary
|
|---|
| 295 | iterator that operates on Q3AsciiDict\<X\> (dictionary of X*).
|
|---|
| 296 |
|
|---|
| 297 | Example:
|
|---|
| 298 | \snippet doc/src/snippets/code/doc_src_q3asciidict.qdoc 2
|
|---|
| 299 | In the example we insert some line edits into a dictionary, then
|
|---|
| 300 | iterate over the dictionary printing the strings associated with
|
|---|
| 301 | those line edits.
|
|---|
| 302 |
|
|---|
| 303 | Note that the traversal order is arbitrary; you are not guaranteed
|
|---|
| 304 | any particular order.
|
|---|
| 305 |
|
|---|
| 306 | Multiple iterators may independently traverse the same dictionary.
|
|---|
| 307 | A Q3AsciiDict knows about all the iterators that are operating on
|
|---|
| 308 | the dictionary. When an item is removed from the dictionary,
|
|---|
| 309 | Q3AsciiDict updates all the iterators that are referring to the
|
|---|
| 310 | removed item to point to the next item in the (arbitrary)
|
|---|
| 311 | traversal order.
|
|---|
| 312 |
|
|---|
| 313 | \sa Q3AsciiDict
|
|---|
| 314 | */
|
|---|
| 315 |
|
|---|
| 316 | /*!
|
|---|
| 317 | \fn Q3AsciiDictIterator::Q3AsciiDictIterator( const Q3AsciiDict<type> &dict )
|
|---|
| 318 |
|
|---|
| 319 | Constructs an iterator for \a dict. The current iterator item is
|
|---|
| 320 | set to point on the first item in the \a dict.
|
|---|
| 321 | */
|
|---|
| 322 |
|
|---|
| 323 | /*!
|
|---|
| 324 | \fn Q3AsciiDictIterator::~Q3AsciiDictIterator()
|
|---|
| 325 |
|
|---|
| 326 | Destroys the iterator.
|
|---|
| 327 | */
|
|---|
| 328 |
|
|---|
| 329 | /*!
|
|---|
| 330 | \fn uint Q3AsciiDictIterator::count() const
|
|---|
| 331 |
|
|---|
| 332 | Returns the number of items in the dictionary this iterator
|
|---|
| 333 | operates over.
|
|---|
| 334 |
|
|---|
| 335 | \sa isEmpty()
|
|---|
| 336 | */
|
|---|
| 337 |
|
|---|
| 338 | /*!
|
|---|
| 339 | \fn bool Q3AsciiDictIterator::isEmpty() const
|
|---|
| 340 |
|
|---|
| 341 | Returns TRUE if the dictionary is empty, i.e. count() == 0,
|
|---|
| 342 | otherwise returns FALSE.
|
|---|
| 343 |
|
|---|
| 344 | \sa count()
|
|---|
| 345 | */
|
|---|
| 346 |
|
|---|
| 347 | /*!
|
|---|
| 348 | \fn type *Q3AsciiDictIterator::toFirst()
|
|---|
| 349 |
|
|---|
| 350 | Sets the current iterator item to point to the first item in the
|
|---|
| 351 | dictionary and returns a pointer to the item. If the dictionary is
|
|---|
| 352 | empty it sets the current item to 0 and returns 0.
|
|---|
| 353 | */
|
|---|
| 354 |
|
|---|
| 355 | /*!
|
|---|
| 356 | \fn Q3AsciiDictIterator::operator type *() const
|
|---|
| 357 |
|
|---|
| 358 | Cast operator. Returns a pointer to the current iterator item.
|
|---|
| 359 | Same as current().
|
|---|
| 360 | */
|
|---|
| 361 |
|
|---|
| 362 | /*!
|
|---|
| 363 | \fn type *Q3AsciiDictIterator::current() const
|
|---|
| 364 |
|
|---|
| 365 | Returns a pointer to the current iterator item.
|
|---|
| 366 | */
|
|---|
| 367 |
|
|---|
| 368 | /*!
|
|---|
| 369 | \fn const char *Q3AsciiDictIterator::currentKey() const
|
|---|
| 370 |
|
|---|
| 371 | Returns a pointer to the key for the current iterator item.
|
|---|
| 372 | */
|
|---|
| 373 |
|
|---|
| 374 | /*!
|
|---|
| 375 | \fn type *Q3AsciiDictIterator::operator()()
|
|---|
| 376 |
|
|---|
| 377 | Makes the succeeding item current and returns the original current
|
|---|
| 378 | item.
|
|---|
| 379 |
|
|---|
| 380 | If the current iterator item was the last item in the dictionary
|
|---|
| 381 | or if it was 0, 0 is returned.
|
|---|
| 382 | */
|
|---|
| 383 |
|
|---|
| 384 | /*!
|
|---|
| 385 | \fn type *Q3AsciiDictIterator::operator++()
|
|---|
| 386 |
|
|---|
| 387 | Prefix ++ makes the succeeding item current and returns the new
|
|---|
| 388 | current item.
|
|---|
| 389 |
|
|---|
| 390 | If the current iterator item was the last item in the dictionary
|
|---|
| 391 | or if it was 0, 0 is returned.
|
|---|
| 392 | */
|
|---|
| 393 |
|
|---|
| 394 | /*!
|
|---|
| 395 | \fn type *Q3AsciiDictIterator::operator+=( uint jump )
|
|---|
| 396 |
|
|---|
| 397 | Sets the current item to the item \a jump positions after the
|
|---|
| 398 | current item, and returns a pointer to that item.
|
|---|
| 399 |
|
|---|
| 400 | If that item is beyond the last item or if the dictionary is
|
|---|
| 401 | empty, it sets the current item to 0 and returns 0.
|
|---|
| 402 | */
|
|---|