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