summaryrefslogtreecommitdiffstats
path: root/src/tools/ntqgdict.h
blob: ace621ab34607aa9c7f5f8aea7312c036116ac10 (plain)
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
/****************************************************************************
**
** Definition of TQGDict and TQGDictIterator classes
**
** Created : 920529
**
** Copyright (C) 1992-2008 Trolltech ASA.  All rights reserved.
**
** This file is part of the tools module of the TQt GUI Toolkit.
**
** This file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free
** Software Foundation and appearing in the files LICENSE.GPL2
** and LICENSE.GPL3 included in the packaging of this file.
** Alternatively you may (at your option) use any later version
** of the GNU General Public License if such license has been
** publicly approved by Trolltech ASA (or its successors, if any)
** and the KDE Free TQt Foundation.
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/.
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at [email protected].
**
** This file may be used under the terms of the Q Public License as
** defined by Trolltech ASA and appearing in the file LICENSE.TQPL
** included in the packaging of this file.  Licensees holding valid TQt
** Commercial licenses may use this file in accordance with the TQt
** Commercial License Agreement provided with the Software.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted
** herein.
**
**********************************************************************/

#ifndef TQGDICT_H
#define TQGDICT_H

#ifndef QT_H
#include "ntqptrcollection.h"
#include "ntqstring.h"
#endif // QT_H

class TQGDictIterator;
class TQGDItList;


class TQBaseBucket				// internal dict node
{
public:
    TQPtrCollection::Item	 getData()			{ return data; }
    TQPtrCollection::Item	 setData( TQPtrCollection::Item d ) { return data = d; }
    TQBaseBucket		*getNext()			{ return next; }
    void		 setNext( TQBaseBucket *n)	{ next = n; }
protected:
    TQBaseBucket( TQPtrCollection::Item d, TQBaseBucket *n ) : data(d), next(n) {}
    TQPtrCollection::Item	 data;
    TQBaseBucket		*next;
};

class TQStringBucket : public TQBaseBucket
{
public:
    TQStringBucket( const TQString &k, TQPtrCollection::Item d, TQBaseBucket *n )
	: TQBaseBucket(d,n), key(k)		{}
    const TQString  &getKey() const		{ return key; }
private:
    TQString	    key;
};

class TQAsciiBucket : public TQBaseBucket
{
public:
    TQAsciiBucket( const char *k, TQPtrCollection::Item d, TQBaseBucket *n )
	: TQBaseBucket(d,n), key(k) {}
    const char *getKey() const { return key; }
private:
    const char *key;
};

class TQIntBucket : public TQBaseBucket
{
public:
    TQIntBucket( long k, TQPtrCollection::Item d, TQBaseBucket *n )
	: TQBaseBucket(d,n), key(k) {}
    long  getKey() const { return key; }
private:
    long  key;
};

class TQPtrBucket : public TQBaseBucket
{
public:
    TQPtrBucket( void *k, TQPtrCollection::Item d, TQBaseBucket *n )
	: TQBaseBucket(d,n), key(k) {}
    void *getKey() const { return key; }
private:
    void *key;
};


class Q_EXPORT TQGDict : public TQPtrCollection	// generic dictionary class
{
public:
    uint	count() const	{ return numItems; }
    uint	size()	const	{ return vlen; }
    TQPtrCollection::Item look_string( const TQString& key, TQPtrCollection::Item,
				   int );
    TQPtrCollection::Item look_ascii( const char *key, TQPtrCollection::Item, int );
    TQPtrCollection::Item look_int( long key, TQPtrCollection::Item, int );
    TQPtrCollection::Item look_ptr( void *key, TQPtrCollection::Item, int );
#ifndef TQT_NO_DATASTREAM
    TQDataStream &read( TQDataStream & );
    TQDataStream &write( TQDataStream & ) const;
#endif
protected:
    enum KeyType { StringKey, AsciiKey, IntKey, PtrKey };

    TQGDict( uint len, KeyType kt, bool cs, bool ck );
    TQGDict( const TQGDict & );
   ~TQGDict();

    TQGDict     &operator=( const TQGDict & );

    bool	remove_string( const TQString &key, TQPtrCollection::Item item=0 );
    bool	remove_ascii( const char *key, TQPtrCollection::Item item=0 );
    bool	remove_int( long key, TQPtrCollection::Item item=0 );
    bool	remove_ptr( void *key, TQPtrCollection::Item item=0 );
    TQPtrCollection::Item take_string( const TQString &key );
    TQPtrCollection::Item take_ascii( const char *key );
    TQPtrCollection::Item take_int( long key );
    TQPtrCollection::Item take_ptr( void *key );

    void	clear();
    void	resize( uint );

    int		hashKeyString( const TQString & );
    int		hashKeyAscii( const char * );

    void	statistics() const;

#ifndef TQT_NO_DATASTREAM
    virtual TQDataStream &read( TQDataStream &, TQPtrCollection::Item & );
    virtual TQDataStream &write( TQDataStream &, TQPtrCollection::Item ) const;
#endif
private:
    TQBaseBucket **vec;
    uint	vlen;
    uint	numItems;
    uint	keytype	: 2;
    uint	cases	: 1;
    uint	copyk	: 1;
    TQGDItList  *iterators;
    void	   unlink_common( int, TQBaseBucket *, TQBaseBucket * );
    TQStringBucket *unlink_string( const TQString &,
				  TQPtrCollection::Item item = 0 );
    TQAsciiBucket  *unlink_ascii( const char *, TQPtrCollection::Item item = 0 );
    TQIntBucket    *unlink_int( long, TQPtrCollection::Item item = 0 );
    TQPtrBucket    *unlink_ptr( void *, TQPtrCollection::Item item = 0 );
    void	init( uint, KeyType, bool, bool );
    friend class TQGDictIterator;
};


class Q_EXPORT TQGDictIterator			// generic dictionary iterator
{
friend class TQGDict;
public:
    TQGDictIterator( const TQGDict & );
    TQGDictIterator( const TQGDictIterator & );
    TQGDictIterator &operator=( const TQGDictIterator & );
   ~TQGDictIterator();

    TQPtrCollection::Item toFirst();

    TQPtrCollection::Item get()	     const;
    TQString	      getKeyString() const;
    const char	     *getKeyAscii()  const;
    long	      getKeyInt()    const;
    void	     *getKeyPtr()    const;

    TQPtrCollection::Item operator()();
    TQPtrCollection::Item operator++();
    TQPtrCollection::Item operator+=(uint);

protected:
    TQGDict	     *dict;

private:
    TQBaseBucket      *curNode;
    uint	      curIndex;
};

inline TQPtrCollection::Item TQGDictIterator::get() const
{
    return curNode ? curNode->getData() : 0;
}

inline TQString TQGDictIterator::getKeyString() const
{
    return curNode ? ((TQStringBucket*)curNode)->getKey() : TQString::null;
}

inline const char *TQGDictIterator::getKeyAscii() const
{
    return curNode ? ((TQAsciiBucket*)curNode)->getKey() : 0;
}

inline long TQGDictIterator::getKeyInt() const
{
    return curNode ? ((TQIntBucket*)curNode)->getKey() : 0;
}

inline void *TQGDictIterator::getKeyPtr() const
{
    return curNode ? ((TQPtrBucket*)curNode)->getKey() : 0;
}


#endif // TQGDICT_H