| 1 | /**************************************************************************** | 
|---|
| 2 | ** $Id: qgdict.h 2 2005-11-16 15:49:26Z dmik $ | 
|---|
| 3 | ** | 
|---|
| 4 | ** Definition of QGDict and QGDictIterator classes | 
|---|
| 5 | ** | 
|---|
| 6 | ** Created : 920529 | 
|---|
| 7 | ** | 
|---|
| 8 | ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved. | 
|---|
| 9 | ** | 
|---|
| 10 | ** This file is part of the tools module of the Qt GUI Toolkit. | 
|---|
| 11 | ** | 
|---|
| 12 | ** This file may be distributed under the terms of the Q Public License | 
|---|
| 13 | ** as defined by Trolltech AS of Norway and appearing in the file | 
|---|
| 14 | ** LICENSE.QPL included in the packaging of this file. | 
|---|
| 15 | ** | 
|---|
| 16 | ** This file may be distributed and/or modified under the terms of the | 
|---|
| 17 | ** GNU General Public License version 2 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 19 | ** packaging of this file. | 
|---|
| 20 | ** | 
|---|
| 21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition | 
|---|
| 22 | ** licenses may use this file in accordance with the Qt Commercial License | 
|---|
| 23 | ** Agreement provided with the Software. | 
|---|
| 24 | ** | 
|---|
| 25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 
|---|
| 26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | 
|---|
| 27 | ** | 
|---|
| 28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for | 
|---|
| 29 | **   information about Qt Commercial License Agreements. | 
|---|
| 30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information. | 
|---|
| 31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 
|---|
| 32 | ** | 
|---|
| 33 | ** Contact info@trolltech.com if any conditions of this licensing are | 
|---|
| 34 | ** not clear to you. | 
|---|
| 35 | ** | 
|---|
| 36 | **********************************************************************/ | 
|---|
| 37 |  | 
|---|
| 38 | #ifndef QGDICT_H | 
|---|
| 39 | #define QGDICT_H | 
|---|
| 40 |  | 
|---|
| 41 | #ifndef QT_H | 
|---|
| 42 | #include "qptrcollection.h" | 
|---|
| 43 | #include "qstring.h" | 
|---|
| 44 | #endif // QT_H | 
|---|
| 45 |  | 
|---|
| 46 | class QGDictIterator; | 
|---|
| 47 | class QGDItList; | 
|---|
| 48 |  | 
|---|
| 49 |  | 
|---|
| 50 | class QBaseBucket                               // internal dict node | 
|---|
| 51 | { | 
|---|
| 52 | public: | 
|---|
| 53 | QPtrCollection::Item         getData()                      { return data; } | 
|---|
| 54 | QPtrCollection::Item         setData( QPtrCollection::Item d ) { return data = d; } | 
|---|
| 55 | QBaseBucket         *getNext()                      { return next; } | 
|---|
| 56 | void                 setNext( QBaseBucket *n)       { next = n; } | 
|---|
| 57 | protected: | 
|---|
| 58 | QBaseBucket( QPtrCollection::Item d, QBaseBucket *n ) : data(d), next(n) {} | 
|---|
| 59 | QPtrCollection::Item         data; | 
|---|
| 60 | QBaseBucket         *next; | 
|---|
| 61 | }; | 
|---|
| 62 |  | 
|---|
| 63 | class QStringBucket : public QBaseBucket | 
|---|
| 64 | { | 
|---|
| 65 | public: | 
|---|
| 66 | QStringBucket( const QString &k, QPtrCollection::Item d, QBaseBucket *n ) | 
|---|
| 67 | : QBaseBucket(d,n), key(k)              {} | 
|---|
| 68 | const QString  &getKey() const              { return key; } | 
|---|
| 69 | private: | 
|---|
| 70 | QString         key; | 
|---|
| 71 | }; | 
|---|
| 72 |  | 
|---|
| 73 | class QAsciiBucket : public QBaseBucket | 
|---|
| 74 | { | 
|---|
| 75 | public: | 
|---|
| 76 | QAsciiBucket( const char *k, QPtrCollection::Item d, QBaseBucket *n ) | 
|---|
| 77 | : QBaseBucket(d,n), key(k) {} | 
|---|
| 78 | const char *getKey() const { return key; } | 
|---|
| 79 | private: | 
|---|
| 80 | const char *key; | 
|---|
| 81 | }; | 
|---|
| 82 |  | 
|---|
| 83 | class QIntBucket : public QBaseBucket | 
|---|
| 84 | { | 
|---|
| 85 | public: | 
|---|
| 86 | QIntBucket( long k, QPtrCollection::Item d, QBaseBucket *n ) | 
|---|
| 87 | : QBaseBucket(d,n), key(k) {} | 
|---|
| 88 | long  getKey() const { return key; } | 
|---|
| 89 | private: | 
|---|
| 90 | long  key; | 
|---|
| 91 | }; | 
|---|
| 92 |  | 
|---|
| 93 | class QPtrBucket : public QBaseBucket | 
|---|
| 94 | { | 
|---|
| 95 | public: | 
|---|
| 96 | QPtrBucket( void *k, QPtrCollection::Item d, QBaseBucket *n ) | 
|---|
| 97 | : QBaseBucket(d,n), key(k) {} | 
|---|
| 98 | void *getKey() const { return key; } | 
|---|
| 99 | private: | 
|---|
| 100 | void *key; | 
|---|
| 101 | }; | 
|---|
| 102 |  | 
|---|
| 103 |  | 
|---|
| 104 | class Q_EXPORT QGDict : public QPtrCollection   // generic dictionary class | 
|---|
| 105 | { | 
|---|
| 106 | public: | 
|---|
| 107 | uint        count() const   { return numItems; } | 
|---|
| 108 | uint        size()  const   { return vlen; } | 
|---|
| 109 | QPtrCollection::Item look_string( const QString& key, QPtrCollection::Item, | 
|---|
| 110 | int ); | 
|---|
| 111 | QPtrCollection::Item look_ascii( const char *key, QPtrCollection::Item, int ); | 
|---|
| 112 | QPtrCollection::Item look_int( long key, QPtrCollection::Item, int ); | 
|---|
| 113 | QPtrCollection::Item look_ptr( void *key, QPtrCollection::Item, int ); | 
|---|
| 114 | #ifndef QT_NO_DATASTREAM | 
|---|
| 115 | QDataStream &read( QDataStream & ); | 
|---|
| 116 | QDataStream &write( QDataStream & ) const; | 
|---|
| 117 | #endif | 
|---|
| 118 | protected: | 
|---|
| 119 | enum KeyType { StringKey, AsciiKey, IntKey, PtrKey }; | 
|---|
| 120 |  | 
|---|
| 121 | QGDict( uint len, KeyType kt, bool cs, bool ck ); | 
|---|
| 122 | QGDict( const QGDict & ); | 
|---|
| 123 | ~QGDict(); | 
|---|
| 124 |  | 
|---|
| 125 | QGDict     &operator=( const QGDict & ); | 
|---|
| 126 |  | 
|---|
| 127 | bool        remove_string( const QString &key, QPtrCollection::Item item=0 ); | 
|---|
| 128 | bool        remove_ascii( const char *key, QPtrCollection::Item item=0 ); | 
|---|
| 129 | bool        remove_int( long key, QPtrCollection::Item item=0 ); | 
|---|
| 130 | bool        remove_ptr( void *key, QPtrCollection::Item item=0 ); | 
|---|
| 131 | QPtrCollection::Item take_string( const QString &key ); | 
|---|
| 132 | QPtrCollection::Item take_ascii( const char *key ); | 
|---|
| 133 | QPtrCollection::Item take_int( long key ); | 
|---|
| 134 | QPtrCollection::Item take_ptr( void *key ); | 
|---|
| 135 |  | 
|---|
| 136 | void        clear(); | 
|---|
| 137 | void        resize( uint ); | 
|---|
| 138 |  | 
|---|
| 139 | int         hashKeyString( const QString & ); | 
|---|
| 140 | int         hashKeyAscii( const char * ); | 
|---|
| 141 |  | 
|---|
| 142 | void        statistics() const; | 
|---|
| 143 |  | 
|---|
| 144 | #ifndef QT_NO_DATASTREAM | 
|---|
| 145 | virtual QDataStream &read( QDataStream &, QPtrCollection::Item & ); | 
|---|
| 146 | virtual QDataStream &write( QDataStream &, QPtrCollection::Item ) const; | 
|---|
| 147 | #endif | 
|---|
| 148 | private: | 
|---|
| 149 | QBaseBucket **vec; | 
|---|
| 150 | uint        vlen; | 
|---|
| 151 | uint        numItems; | 
|---|
| 152 | uint        keytype : 2; | 
|---|
| 153 | uint        cases   : 1; | 
|---|
| 154 | uint        copyk   : 1; | 
|---|
| 155 | QGDItList  *iterators; | 
|---|
| 156 | void           unlink_common( int, QBaseBucket *, QBaseBucket * ); | 
|---|
| 157 | QStringBucket *unlink_string( const QString &, | 
|---|
| 158 | QPtrCollection::Item item = 0 ); | 
|---|
| 159 | QAsciiBucket  *unlink_ascii( const char *, QPtrCollection::Item item = 0 ); | 
|---|
| 160 | QIntBucket    *unlink_int( long, QPtrCollection::Item item = 0 ); | 
|---|
| 161 | QPtrBucket    *unlink_ptr( void *, QPtrCollection::Item item = 0 ); | 
|---|
| 162 | void        init( uint, KeyType, bool, bool ); | 
|---|
| 163 | friend class QGDictIterator; | 
|---|
| 164 | }; | 
|---|
| 165 |  | 
|---|
| 166 |  | 
|---|
| 167 | class Q_EXPORT QGDictIterator                   // generic dictionary iterator | 
|---|
| 168 | { | 
|---|
| 169 | friend class QGDict; | 
|---|
| 170 | public: | 
|---|
| 171 | QGDictIterator( const QGDict & ); | 
|---|
| 172 | QGDictIterator( const QGDictIterator & ); | 
|---|
| 173 | QGDictIterator &operator=( const QGDictIterator & ); | 
|---|
| 174 | ~QGDictIterator(); | 
|---|
| 175 |  | 
|---|
| 176 | QPtrCollection::Item toFirst(); | 
|---|
| 177 |  | 
|---|
| 178 | QPtrCollection::Item get()       const; | 
|---|
| 179 | QString           getKeyString() const; | 
|---|
| 180 | const char       *getKeyAscii()  const; | 
|---|
| 181 | long              getKeyInt()    const; | 
|---|
| 182 | void             *getKeyPtr()    const; | 
|---|
| 183 |  | 
|---|
| 184 | QPtrCollection::Item operator()(); | 
|---|
| 185 | QPtrCollection::Item operator++(); | 
|---|
| 186 | QPtrCollection::Item operator+=(uint); | 
|---|
| 187 |  | 
|---|
| 188 | protected: | 
|---|
| 189 | QGDict           *dict; | 
|---|
| 190 |  | 
|---|
| 191 | private: | 
|---|
| 192 | QBaseBucket      *curNode; | 
|---|
| 193 | uint              curIndex; | 
|---|
| 194 | }; | 
|---|
| 195 |  | 
|---|
| 196 | inline QPtrCollection::Item QGDictIterator::get() const | 
|---|
| 197 | { | 
|---|
| 198 | return curNode ? curNode->getData() : 0; | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 | inline QString QGDictIterator::getKeyString() const | 
|---|
| 202 | { | 
|---|
| 203 | return curNode ? ((QStringBucket*)curNode)->getKey() : QString::null; | 
|---|
| 204 | } | 
|---|
| 205 |  | 
|---|
| 206 | inline const char *QGDictIterator::getKeyAscii() const | 
|---|
| 207 | { | 
|---|
| 208 | return curNode ? ((QAsciiBucket*)curNode)->getKey() : 0; | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 | inline long QGDictIterator::getKeyInt() const | 
|---|
| 212 | { | 
|---|
| 213 | return curNode ? ((QIntBucket*)curNode)->getKey() : 0; | 
|---|
| 214 | } | 
|---|
| 215 |  | 
|---|
| 216 | inline void *QGDictIterator::getKeyPtr() const | 
|---|
| 217 | { | 
|---|
| 218 | return curNode ? ((QPtrBucket*)curNode)->getKey() : 0; | 
|---|
| 219 | } | 
|---|
| 220 |  | 
|---|
| 221 |  | 
|---|
| 222 | #endif // QGDICT_H | 
|---|