]> git.saurik.com Git - wxWidgets.git/blame - include/wx/hash.h
wxMessageBox off the main thread lost result code.
[wxWidgets.git] / include / wx / hash.h
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
bcaa23de 2// Name: wx/hash.h
c801d85f
KB
3// Purpose: wxHashTable class
4// Author: Julian Smart
bcaa23de 5// Modified by: VZ at 25.02.00: type safe hashes with WX_DECLARE_HASH()
c801d85f 6// Created: 01/02/97
99d80019 7// Copyright: (c) Julian Smart
65571936 8// Licence: wxWindows licence
c801d85f
KB
9/////////////////////////////////////////////////////////////////////////////
10
bcaa23de
VZ
11#ifndef _WX_HASH_H__
12#define _WX_HASH_H__
c801d85f 13
df5168c4 14#include "wx/defs.h"
1918a4c4 15#include "wx/string.h"
df5168c4 16
01871bf6 17#if !wxUSE_STD_CONTAINERS
1a6d9c76 18 #include "wx/object.h"
1a6d9c76 19#else
b5dbe15d 20 class WXDLLIMPEXP_FWD_BASE wxObject;
df5168c4 21#endif
df5168c4 22
bcaa23de
VZ
23// the default size of the hash
24#define wxHASH_SIZE_DEFAULT (1000)
25
c801d85f
KB
26/*
27 * A hash table is an array of user-definable size with lists
28 * of data items hanging off the array positions. Usually there'll
29 * be a hit, so no search is required; otherwise we'll have to run down
30 * the list to find the desired item.
31*/
32
df5168c4
MB
33union wxHashKeyValue
34{
35 long integer;
0518029c 36 wxString *string;
df5168c4
MB
37};
38
5a2ec1b8
VZ
39// for some compilers (AIX xlC), defining it as friend inside the class is not
40// enough, so provide a real forward declaration
b5dbe15d 41class WXDLLIMPEXP_FWD_BASE wxHashTableBase;
5a2ec1b8 42
1a6d9c76 43class WXDLLIMPEXP_BASE wxHashTableBase_Node
df5168c4 44{
b5dbe15d
VS
45 friend class WXDLLIMPEXP_FWD_BASE wxHashTableBase;
46 typedef class WXDLLIMPEXP_FWD_BASE wxHashTableBase_Node _Node;
1a6d9c76
MB
47public:
48 wxHashTableBase_Node( long key, void* value,
49 wxHashTableBase* table );
0518029c 50 wxHashTableBase_Node( const wxString& key, void* value,
1a6d9c76
MB
51 wxHashTableBase* table );
52 ~wxHashTableBase_Node();
df5168c4 53
1a6d9c76 54 long GetKeyInteger() const { return m_key.integer; }
0518029c 55 const wxString& GetKeyString() const { return *m_key.string; }
df5168c4 56
1a6d9c76
MB
57 void* GetData() const { return m_value; }
58 void SetData( void* data ) { m_value = data; }
df5168c4 59
1a6d9c76
MB
60protected:
61 _Node* GetNext() const { return m_next; }
df5168c4 62
1a6d9c76
MB
63protected:
64 // next node in the chain
65 wxHashTableBase_Node* m_next;
df5168c4 66
1a6d9c76
MB
67 // key
68 wxHashKeyValue m_key;
760d3c7c 69
1a6d9c76
MB
70 // value
71 void* m_value;
760d3c7c 72
1a6d9c76
MB
73 // pointer to the hash containing the node, used to remove the
74 // node from the hash when the user deletes the node iterating
75 // through it
76 // TODO: move it to wxHashTable_Node (only wxHashTable supports
77 // iteration)
78 wxHashTableBase* m_hashPtr;
760d3c7c 79};
df5168c4
MB
80
81class WXDLLIMPEXP_BASE wxHashTableBase
01871bf6 82#if !wxUSE_STD_CONTAINERS
1a6d9c76
MB
83 : public wxObject
84#endif
df5168c4 85{
b5dbe15d 86 friend class WXDLLIMPEXP_FWD_BASE wxHashTableBase_Node;
df5168c4 87public:
1a6d9c76 88 typedef wxHashTableBase_Node Node;
df5168c4 89
1a6d9c76 90 wxHashTableBase();
47b378bd 91 virtual ~wxHashTableBase() { }
d8274c92 92
1a6d9c76
MB
93 void Create( wxKeyType keyType = wxKEY_INTEGER,
94 size_t size = wxHASH_SIZE_DEFAULT );
95 void Clear();
96 void Destroy();
df5168c4 97
1a6d9c76
MB
98 size_t GetSize() const { return m_size; }
99 size_t GetCount() const { return m_count; }
d8274c92 100
1a6d9c76 101 void DeleteContents( bool flag ) { m_deleteContents = flag; }
df5168c4 102
0518029c 103 static long MakeKey(const wxString& string);
df5168c4 104
1a6d9c76
MB
105protected:
106 void DoPut( long key, long hash, void* data );
0518029c 107 void DoPut( const wxString& key, long hash, void* data );
1a6d9c76 108 void* DoGet( long key, long hash ) const;
0518029c 109 void* DoGet( const wxString& key, long hash ) const;
1a6d9c76 110 void* DoDelete( long key, long hash );
0518029c 111 void* DoDelete( const wxString& key, long hash );
d8274c92 112
1a6d9c76
MB
113private:
114 // Remove the node from the hash, *only called from
115 // ~wxHashTable*_Node destructor
116 void DoRemoveNode( wxHashTableBase_Node* node );
df5168c4 117
1a6d9c76
MB
118 // destroys data contained in the node if appropriate:
119 // deletes the key if it is a string and destrys
120 // the value if m_deleteContents is true
121 void DoDestroyNode( wxHashTableBase_Node* node );
df5168c4 122
1a6d9c76
MB
123 // inserts a node in the table (at the end of the chain)
124 void DoInsertNode( size_t bucket, wxHashTableBase_Node* node );
d8274c92 125
1a6d9c76
MB
126 // removes a node from the table (fiven a pointer to the previous
127 // but does not delete it (only deletes its contents)
128 void DoUnlinkNode( size_t bucket, wxHashTableBase_Node* node,
129 wxHashTableBase_Node* prev );
df5168c4 130
1a6d9c76
MB
131 // unconditionally deletes node value (invoking the
132 // correct destructor)
133 virtual void DoDeleteContents( wxHashTableBase_Node* node ) = 0;
df5168c4 134
1a6d9c76
MB
135protected:
136 // number of buckets
137 size_t m_size;
df5168c4 138
1a6d9c76
MB
139 // number of nodes (key/value pairs)
140 size_t m_count;
d8274c92 141
1a6d9c76
MB
142 // table
143 Node** m_table;
df5168c4 144
1a6d9c76
MB
145 // key typ (INTEGER/STRING)
146 wxKeyType m_keyType;
df5168c4 147
1a6d9c76
MB
148 // delete contents when hash is cleared
149 bool m_deleteContents;
df5168c4 150
1a6d9c76 151private:
c0c133e1 152 wxDECLARE_NO_COPY_CLASS(wxHashTableBase);
df5168c4
MB
153};
154
bcaa23de
VZ
155// ----------------------------------------------------------------------------
156// for compatibility only
157// ----------------------------------------------------------------------------
158
1a6d9c76 159class WXDLLIMPEXP_BASE wxHashTable_Node : public wxHashTableBase_Node
df5168c4 160{
b5dbe15d 161 friend class WXDLLIMPEXP_FWD_BASE wxHashTable;
df5168c4 162public:
1a6d9c76
MB
163 wxHashTable_Node( long key, void* value,
164 wxHashTableBase* table )
165 : wxHashTableBase_Node( key, value, table ) { }
0518029c 166 wxHashTable_Node( const wxString& key, void* value,
1a6d9c76
MB
167 wxHashTableBase* table )
168 : wxHashTableBase_Node( key, value, table ) { }
169
170 wxObject* GetData() const
171 { return (wxObject*)wxHashTableBase_Node::GetData(); }
172 void SetData( wxObject* data )
173 { wxHashTableBase_Node::SetData( data ); }
174
175 wxHashTable_Node* GetNext() const
176 { return (wxHashTable_Node*)wxHashTableBase_Node::GetNext(); }
177};
178
179// should inherit protectedly, but it is public for compatibility in
180// order to publicly inherit from wxObject
181class WXDLLIMPEXP_BASE wxHashTable : public wxHashTableBase
182{
183 typedef wxHashTableBase hash;
184public:
185 typedef wxHashTable_Node Node;
186 typedef wxHashTable_Node* compatibility_iterator;
df5168c4
MB
187public:
188 wxHashTable( wxKeyType keyType = wxKEY_INTEGER,
189 size_t size = wxHASH_SIZE_DEFAULT )
1a6d9c76
MB
190 : wxHashTableBase() { Create( keyType, size ); BeginFind(); }
191 wxHashTable( const wxHashTable& table );
192
6aa33060
MB
193 virtual ~wxHashTable() { Destroy(); }
194
1a6d9c76 195 const wxHashTable& operator=( const wxHashTable& );
df5168c4 196
df5168c4 197 // key and value are the same
1a6d9c76
MB
198 void Put(long value, wxObject *object)
199 { DoPut( value, value, object ); }
222702b1
WS
200 void Put(long lhash, long value, wxObject *object)
201 { DoPut( value, lhash, object ); }
11aac4ba
VS
202 void Put(const wxString& value, wxObject *object)
203 { DoPut( value, MakeKey( value ), object ); }
0518029c 204 void Put(long lhash, const wxString& value, wxObject *object)
222702b1 205 { DoPut( value, lhash, object ); }
df5168c4
MB
206
207 // key and value are the same
1a6d9c76
MB
208 wxObject *Get(long value) const
209 { return (wxObject*)DoGet( value, value ); }
222702b1
WS
210 wxObject *Get(long lhash, long value) const
211 { return (wxObject*)DoGet( value, lhash ); }
11aac4ba
VS
212 wxObject *Get(const wxString& value) const
213 { return (wxObject*)DoGet( value, MakeKey( value ) ); }
0518029c 214 wxObject *Get(long lhash, const wxString& value) const
222702b1 215 { return (wxObject*)DoGet( value, lhash ); }
df5168c4
MB
216
217 // Deletes entry and returns data if found
1a6d9c76
MB
218 wxObject *Delete(long key)
219 { return (wxObject*)DoDelete( key, key ); }
222702b1
WS
220 wxObject *Delete(long lhash, long key)
221 { return (wxObject*)DoDelete( key, lhash ); }
11aac4ba
VS
222 wxObject *Delete(const wxString& key)
223 { return (wxObject*)DoDelete( key, MakeKey( key ) ); }
0518029c 224 wxObject *Delete(long lhash, const wxString& key)
222702b1 225 { return (wxObject*)DoDelete( key, lhash ); }
df5168c4 226
df5168c4
MB
227 // Way of iterating through whole hash table (e.g. to delete everything)
228 // Not necessary, of course, if you're only storing pointers to
229 // objects maintained separately
1a6d9c76
MB
230 void BeginFind() { m_curr = NULL; m_currBucket = 0; }
231 Node* Next();
df5168c4 232
d8274c92 233 void Clear() { wxHashTableBase::Clear(); }
72eef316
MB
234
235 size_t GetCount() const { return wxHashTableBase::GetCount(); }
1a6d9c76 236protected:
1a6d9c76
MB
237 // copy helper
238 void DoCopy( const wxHashTable& copy );
239
240 // searches the next node starting from bucket bucketStart and sets
241 // m_curr to it and m_currBucket to its bucket
242 void GetNextNode( size_t bucketStart );
df5168c4 243private:
6f02a879
VZ
244 virtual void DoDeleteContents( wxHashTableBase_Node* node );
245
1a6d9c76
MB
246 // current node
247 Node* m_curr;
248
249 // bucket the current node belongs to
250 size_t m_currBucket;
df5168c4
MB
251};
252
bcaa23de
VZ
253// defines a new type safe hash table which stores the elements of type eltype
254// in lists of class listclass
df5168c4
MB
255#define _WX_DECLARE_HASH(eltype, dummy, hashclass, classexp) \
256 classexp hashclass : public wxHashTableBase \
257 { \
258 public: \
259 hashclass(wxKeyType keyType = wxKEY_INTEGER, \
260 size_t size = wxHASH_SIZE_DEFAULT) \
1a6d9c76 261 : wxHashTableBase() { Create(keyType, size); } \
df5168c4 262 \
6aa33060 263 virtual ~hashclass() { Destroy(); } \
df5168c4 264 \
1a6d9c76 265 void Put(long key, eltype *data) { DoPut(key, key, (void*)data); } \
222702b1
WS
266 void Put(long lhash, long key, eltype *data) \
267 { DoPut(key, lhash, (void*)data); } \
1a6d9c76 268 eltype *Get(long key) const { return (eltype*)DoGet(key, key); } \
222702b1
WS
269 eltype *Get(long lhash, long key) const \
270 { return (eltype*)DoGet(key, lhash); } \
1a6d9c76 271 eltype *Delete(long key) { return (eltype*)DoDelete(key, key); } \
222702b1
WS
272 eltype *Delete(long lhash, long key) \
273 { return (eltype*)DoDelete(key, lhash); } \
6f02a879 274 private: \
1a6d9c76
MB
275 virtual void DoDeleteContents( wxHashTableBase_Node* node ) \
276 { delete (eltype*)node->GetData(); } \
277 \
278 DECLARE_NO_COPY_CLASS(hashclass) \
df5168c4
MB
279 }
280
df5168c4 281
f6bcfd97
BP
282// this macro is to be used in the user code
283#define WX_DECLARE_HASH(el, list, hash) \
284 _WX_DECLARE_HASH(el, list, hash, class)
285
286// and this one does exactly the same thing but should be used inside the
287// library
288#define WX_DECLARE_EXPORTED_HASH(el, list, hash) \
53a2db12 289 _WX_DECLARE_HASH(el, list, hash, class WXDLLIMPEXP_CORE)
f6bcfd97 290
0b9ab0bd
RL
291#define WX_DECLARE_USER_EXPORTED_HASH(el, list, hash, usergoo) \
292 _WX_DECLARE_HASH(el, list, hash, class usergoo)
293
ba8c1601
MB
294// delete all hash elements
295//
296// NB: the class declaration of the hash elements must be visible from the
297// place where you use this macro, otherwise the proper destructor may not
298// be called (a decent compiler should give a warning about it, but don't
299// count on it)!
df5168c4 300#define WX_CLEAR_HASH_TABLE(hash) \
ba8c1601 301 { \
df5168c4
MB
302 (hash).BeginFind(); \
303 wxHashTable::compatibility_iterator it = (hash).Next(); \
ba8c1601
MB
304 while( it ) \
305 { \
306 delete it->GetData(); \
df5168c4 307 it = (hash).Next(); \
ba8c1601 308 } \
df5168c4 309 (hash).Clear(); \
ba8c1601
MB
310 }
311
0518029c 312#endif // _WX_HASH_H__