]> git.saurik.com Git - wxWidgets.git/blame - include/wx/hash.h
added wxAPPLY and wxCLOSE flags to CreateStdDialogButtonSizer() (Marcin Wojdyr)
[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 17
c1dc9f83 18#define wxUSE_OLD_HASH_TABLE 0
6e86701b 19
df5168c4 20#if !wxUSE_STL
1a6d9c76 21 #include "wx/object.h"
1a6d9c76
MB
22#else
23 class WXDLLIMPEXP_BASE wxObject;
df5168c4 24#endif
6e86701b
MB
25#if wxUSE_OLD_HASH_TABLE
26 #include "wx/list.h"
27#endif
df5168c4 28
bcaa23de
VZ
29// the default size of the hash
30#define wxHASH_SIZE_DEFAULT (1000)
31
c801d85f
KB
32/*
33 * A hash table is an array of user-definable size with lists
34 * of data items hanging off the array positions. Usually there'll
35 * be a hit, so no search is required; otherwise we'll have to run down
36 * the list to find the desired item.
37*/
38
bcaa23de
VZ
39// ----------------------------------------------------------------------------
40// this is the base class for object hashes: hash tables which contain
41// pointers to objects
42// ----------------------------------------------------------------------------
43
6e86701b 44#if wxUSE_OLD_HASH_TABLE
df5168c4 45
bddd7a8d 46class WXDLLIMPEXP_BASE wxHashTableBase : public wxObject
c801d85f 47{
bcaa23de
VZ
48public:
49 wxHashTableBase();
50
51 void Create(wxKeyType keyType = wxKEY_INTEGER,
52 size_t size = wxHASH_SIZE_DEFAULT);
53 void Destroy();
54
55 size_t GetSize() const { return m_hashSize; }
56 size_t GetCount() const { return m_count; }
57
58 void DeleteContents(bool flag);
59
60protected:
61 // find the node for (key, value)
62 wxNodeBase *GetNode(long key, long value) const;
63
64 // the array of lists in which we store the values for given key hash
65 wxListBase **m_hashTable;
66
67 // the size of m_lists array
68 size_t m_hashSize;
69
70 // the type of indexing we use
71 wxKeyType m_keyType;
72
73 // the total number of elements in the hash
74 size_t m_count;
75
76 // should we delete our data?
77 bool m_deleteContents;
78
79private:
80 // no copy ctor/assignment operator (yet)
1a0d517e 81 DECLARE_NO_COPY_CLASS(wxHashTableBase)
c801d85f
KB
82};
83
6e86701b 84#else // if !wxUSE_OLD_HASH_TABLE
df5168c4
MB
85
86#if !defined(wxENUM_KEY_TYPE_DEFINED)
87#define wxENUM_KEY_TYPE_DEFINED
88
89enum wxKeyType
90{
91 wxKEY_NONE,
92 wxKEY_INTEGER,
93 wxKEY_STRING
94};
95
96#endif
97
98union wxHashKeyValue
99{
100 long integer;
101 wxChar *string;
102};
103
5a2ec1b8
VZ
104// for some compilers (AIX xlC), defining it as friend inside the class is not
105// enough, so provide a real forward declaration
106class WXDLLIMPEXP_BASE wxHashTableBase;
107
1a6d9c76 108class WXDLLIMPEXP_BASE wxHashTableBase_Node
df5168c4 109{
1a6d9c76
MB
110 friend class WXDLLIMPEXP_BASE wxHashTableBase;
111 typedef class WXDLLIMPEXP_BASE wxHashTableBase_Node _Node;
112public:
113 wxHashTableBase_Node( long key, void* value,
114 wxHashTableBase* table );
115 wxHashTableBase_Node( const wxChar* key, void* value,
116 wxHashTableBase* table );
117 ~wxHashTableBase_Node();
df5168c4 118
1a6d9c76
MB
119 long GetKeyInteger() const { return m_key.integer; }
120 const wxChar* GetKeyString() const { return m_key.string; }
df5168c4 121
1a6d9c76
MB
122 void* GetData() const { return m_value; }
123 void SetData( void* data ) { m_value = data; }
df5168c4 124
1a6d9c76
MB
125protected:
126 _Node* GetNext() const { return m_next; }
df5168c4 127
1a6d9c76
MB
128protected:
129 // next node in the chain
130 wxHashTableBase_Node* m_next;
df5168c4 131
1a6d9c76
MB
132 // key
133 wxHashKeyValue m_key;
760d3c7c 134
1a6d9c76
MB
135 // value
136 void* m_value;
760d3c7c 137
1a6d9c76
MB
138 // pointer to the hash containing the node, used to remove the
139 // node from the hash when the user deletes the node iterating
140 // through it
141 // TODO: move it to wxHashTable_Node (only wxHashTable supports
142 // iteration)
143 wxHashTableBase* m_hashPtr;
760d3c7c 144};
df5168c4
MB
145
146class WXDLLIMPEXP_BASE wxHashTableBase
1a6d9c76
MB
147#if !wxUSE_STL
148 : public wxObject
149#endif
df5168c4 150{
1a6d9c76 151 friend class WXDLLIMPEXP_BASE wxHashTableBase_Node;
df5168c4 152public:
1a6d9c76 153 typedef wxHashTableBase_Node Node;
df5168c4 154
1a6d9c76 155 wxHashTableBase();
47b378bd 156 virtual ~wxHashTableBase() { }
d8274c92 157
1a6d9c76
MB
158 void Create( wxKeyType keyType = wxKEY_INTEGER,
159 size_t size = wxHASH_SIZE_DEFAULT );
160 void Clear();
161 void Destroy();
df5168c4 162
1a6d9c76
MB
163 size_t GetSize() const { return m_size; }
164 size_t GetCount() const { return m_count; }
d8274c92 165
1a6d9c76 166 void DeleteContents( bool flag ) { m_deleteContents = flag; }
df5168c4 167
1a6d9c76 168 static long MakeKey(const wxChar *string);
df5168c4 169
1a6d9c76
MB
170protected:
171 void DoPut( long key, long hash, void* data );
172 void DoPut( const wxChar* key, long hash, void* data );
173 void* DoGet( long key, long hash ) const;
174 void* DoGet( const wxChar* key, long hash ) const;
175 void* DoDelete( long key, long hash );
176 void* DoDelete( const wxChar* key, long hash );
d8274c92 177
1a6d9c76
MB
178private:
179 // Remove the node from the hash, *only called from
180 // ~wxHashTable*_Node destructor
181 void DoRemoveNode( wxHashTableBase_Node* node );
df5168c4 182
1a6d9c76
MB
183 // destroys data contained in the node if appropriate:
184 // deletes the key if it is a string and destrys
185 // the value if m_deleteContents is true
186 void DoDestroyNode( wxHashTableBase_Node* node );
df5168c4 187
1a6d9c76
MB
188 // inserts a node in the table (at the end of the chain)
189 void DoInsertNode( size_t bucket, wxHashTableBase_Node* node );
d8274c92 190
1a6d9c76
MB
191 // removes a node from the table (fiven a pointer to the previous
192 // but does not delete it (only deletes its contents)
193 void DoUnlinkNode( size_t bucket, wxHashTableBase_Node* node,
194 wxHashTableBase_Node* prev );
df5168c4 195
1a6d9c76
MB
196 // unconditionally deletes node value (invoking the
197 // correct destructor)
198 virtual void DoDeleteContents( wxHashTableBase_Node* node ) = 0;
df5168c4 199
1a6d9c76
MB
200protected:
201 // number of buckets
202 size_t m_size;
df5168c4 203
1a6d9c76
MB
204 // number of nodes (key/value pairs)
205 size_t m_count;
d8274c92 206
1a6d9c76
MB
207 // table
208 Node** m_table;
df5168c4 209
1a6d9c76
MB
210 // key typ (INTEGER/STRING)
211 wxKeyType m_keyType;
df5168c4 212
1a6d9c76
MB
213 // delete contents when hash is cleared
214 bool m_deleteContents;
df5168c4 215
1a6d9c76
MB
216private:
217 DECLARE_NO_COPY_CLASS(wxHashTableBase)
df5168c4
MB
218};
219
6e86701b 220#endif // wxUSE_OLD_HASH_TABLE
df5168c4 221
bcaa23de
VZ
222// ----------------------------------------------------------------------------
223// for compatibility only
224// ----------------------------------------------------------------------------
225
6e86701b 226#if !wxUSE_OLD_HASH_TABLE
df5168c4 227
1a6d9c76 228class WXDLLIMPEXP_BASE wxHashTable_Node : public wxHashTableBase_Node
df5168c4 229{
1a6d9c76 230 friend class WXDLLIMPEXP_BASE wxHashTable;
df5168c4 231public:
1a6d9c76
MB
232 wxHashTable_Node( long key, void* value,
233 wxHashTableBase* table )
234 : wxHashTableBase_Node( key, value, table ) { }
235 wxHashTable_Node( const wxChar* key, void* value,
236 wxHashTableBase* table )
237 : wxHashTableBase_Node( key, value, table ) { }
238
239 wxObject* GetData() const
240 { return (wxObject*)wxHashTableBase_Node::GetData(); }
241 void SetData( wxObject* data )
242 { wxHashTableBase_Node::SetData( data ); }
243
244 wxHashTable_Node* GetNext() const
245 { return (wxHashTable_Node*)wxHashTableBase_Node::GetNext(); }
246};
247
248// should inherit protectedly, but it is public for compatibility in
249// order to publicly inherit from wxObject
250class WXDLLIMPEXP_BASE wxHashTable : public wxHashTableBase
251{
252 typedef wxHashTableBase hash;
253public:
254 typedef wxHashTable_Node Node;
255 typedef wxHashTable_Node* compatibility_iterator;
df5168c4
MB
256public:
257 wxHashTable( wxKeyType keyType = wxKEY_INTEGER,
258 size_t size = wxHASH_SIZE_DEFAULT )
1a6d9c76
MB
259 : wxHashTableBase() { Create( keyType, size ); BeginFind(); }
260 wxHashTable( const wxHashTable& table );
261
6aa33060
MB
262 virtual ~wxHashTable() { Destroy(); }
263
1a6d9c76 264 const wxHashTable& operator=( const wxHashTable& );
df5168c4 265
df5168c4 266 // key and value are the same
1a6d9c76
MB
267 void Put(long value, wxObject *object)
268 { DoPut( value, value, object ); }
222702b1
WS
269 void Put(long lhash, long value, wxObject *object)
270 { DoPut( value, lhash, object ); }
1a6d9c76
MB
271 void Put(const wxChar *value, wxObject *object)
272 { DoPut( value, MakeKey( value ), object ); }
11aac4ba
VS
273 // FIXME-UTF8: have only wxString forms here
274 void Put(const wxString& value, wxObject *object)
275 { DoPut( value, MakeKey( value ), object ); }
222702b1
WS
276 void Put(long lhash, const wxChar *value, wxObject *object)
277 { DoPut( value, lhash, object ); }
df5168c4
MB
278
279 // key and value are the same
1a6d9c76
MB
280 wxObject *Get(long value) const
281 { return (wxObject*)DoGet( value, value ); }
222702b1
WS
282 wxObject *Get(long lhash, long value) const
283 { return (wxObject*)DoGet( value, lhash ); }
1a6d9c76
MB
284 wxObject *Get(const wxChar *value) const
285 { return (wxObject*)DoGet( value, MakeKey( value ) ); }
11aac4ba
VS
286 // FIXME-UTF8: have only wxString forms here
287 wxObject *Get(const wxString& value) const
288 { return (wxObject*)DoGet( value, MakeKey( value ) ); }
222702b1
WS
289 wxObject *Get(long lhash, const wxChar *value) const
290 { return (wxObject*)DoGet( value, lhash ); }
df5168c4
MB
291
292 // Deletes entry and returns data if found
1a6d9c76
MB
293 wxObject *Delete(long key)
294 { return (wxObject*)DoDelete( key, key ); }
222702b1
WS
295 wxObject *Delete(long lhash, long key)
296 { return (wxObject*)DoDelete( key, lhash ); }
1a6d9c76
MB
297 wxObject *Delete(const wxChar *key)
298 { return (wxObject*)DoDelete( key, MakeKey( key ) ); }
11aac4ba
VS
299 // FIXME-UTF8: have only wxString forms here
300 wxObject *Delete(const wxString& key)
301 { return (wxObject*)DoDelete( key, MakeKey( key ) ); }
222702b1
WS
302 wxObject *Delete(long lhash, const wxChar *key)
303 { return (wxObject*)DoDelete( key, lhash ); }
df5168c4 304
df5168c4
MB
305 // Construct your own integer key from a string, e.g. in case
306 // you need to combine it with something
1a6d9c76
MB
307 long MakeKey(const wxChar *string) const
308 { return wxHashTableBase::MakeKey(string); }
309
df5168c4
MB
310 // Way of iterating through whole hash table (e.g. to delete everything)
311 // Not necessary, of course, if you're only storing pointers to
312 // objects maintained separately
1a6d9c76
MB
313 void BeginFind() { m_curr = NULL; m_currBucket = 0; }
314 Node* Next();
df5168c4 315
d8274c92 316 void Clear() { wxHashTableBase::Clear(); }
72eef316
MB
317
318 size_t GetCount() const { return wxHashTableBase::GetCount(); }
1a6d9c76 319protected:
1a6d9c76
MB
320 // copy helper
321 void DoCopy( const wxHashTable& copy );
322
323 // searches the next node starting from bucket bucketStart and sets
324 // m_curr to it and m_currBucket to its bucket
325 void GetNextNode( size_t bucketStart );
df5168c4 326private:
6f02a879
VZ
327 virtual void DoDeleteContents( wxHashTableBase_Node* node );
328
1a6d9c76
MB
329 // current node
330 Node* m_curr;
331
332 // bucket the current node belongs to
333 size_t m_currBucket;
df5168c4
MB
334};
335
6e86701b 336#else // if wxUSE_OLD_HASH_TABLE
df5168c4 337
bddd7a8d 338class WXDLLIMPEXP_BASE wxHashTable : public wxObject
bcaa23de
VZ
339{
340public:
7057f62a
MB
341 typedef wxNode Node;
342 typedef wxNode* compatibility_iterator;
343
bcaa23de
VZ
344 int n;
345 int current_position;
346 wxNode *current_node;
347
348 unsigned int key_type;
349 wxList **hash_table;
350
351 wxHashTable(int the_key_type = wxKEY_INTEGER,
352 int size = wxHASH_SIZE_DEFAULT);
d3c7fc99 353 virtual ~wxHashTable();
bcaa23de
VZ
354
355 // copy ctor and assignment operator
d84afea9
GD
356 wxHashTable(const wxHashTable& table) : wxObject()
357 { DoCopy(table); }
bcaa23de
VZ
358 wxHashTable& operator=(const wxHashTable& table)
359 { Clear(); DoCopy(table); return *this; }
360
361 void DoCopy(const wxHashTable& table);
362
363 void Destroy();
364
365 bool Create(int the_key_type = wxKEY_INTEGER,
366 int size = wxHASH_SIZE_DEFAULT);
367
368 // Note that there are 2 forms of Put, Get.
369 // With a key and a value, the *value* will be checked
370 // when a collision is detected. Otherwise, if there are
371 // 2 items with a different value but the same key,
372 // we'll retrieve the WRONG ONE. So where possible,
373 // supply the required value along with the key.
374 // In fact, the value-only versions make a key, and still store
375 // the value. The use of an explicit key might be required
376 // e.g. when combining several values into one key.
377 // When doing that, it's highly likely we'll get a collision,
378 // e.g. 1 + 2 = 3, 2 + 1 = 3.
379
380 // key and value are NOT necessarily the same
381 void Put(long key, long value, wxObject *object);
382 void Put(long key, const wxChar *value, wxObject *object);
383
384 // key and value are the same
385 void Put(long value, wxObject *object);
386 void Put(const wxChar *value, wxObject *object);
387
388 // key and value not the same
389 wxObject *Get(long key, long value) const;
390 wxObject *Get(long key, const wxChar *value) const;
391
392 // key and value are the same
393 wxObject *Get(long value) const;
394 wxObject *Get(const wxChar *value) const;
395
396 // Deletes entry and returns data if found
397 wxObject *Delete(long key);
398 wxObject *Delete(const wxChar *key);
399
400 wxObject *Delete(long key, int value);
401 wxObject *Delete(long key, const wxChar *value);
402
403 // Construct your own integer key from a string, e.g. in case
404 // you need to combine it with something
405 long MakeKey(const wxChar *string) const;
406
407 // Way of iterating through whole hash table (e.g. to delete everything)
408 // Not necessary, of course, if you're only storing pointers to
409 // objects maintained separately
410
411 void BeginFind();
1a6d9c76 412 Node* Next();
bcaa23de
VZ
413
414 void DeleteContents(bool flag);
415 void Clear();
416
417 // Returns number of nodes
418 size_t GetCount() const { return m_count; }
419
420private:
421 size_t m_count; // number of elements in the hashtable
422 bool m_deleteContents;
423
424 DECLARE_DYNAMIC_CLASS(wxHashTable)
425};
426
6e86701b 427#endif // wxUSE_OLD_HASH_TABLE
df5168c4 428
6e86701b 429#if !wxUSE_OLD_HASH_TABLE
df5168c4 430
bcaa23de
VZ
431// defines a new type safe hash table which stores the elements of type eltype
432// in lists of class listclass
df5168c4
MB
433#define _WX_DECLARE_HASH(eltype, dummy, hashclass, classexp) \
434 classexp hashclass : public wxHashTableBase \
435 { \
436 public: \
437 hashclass(wxKeyType keyType = wxKEY_INTEGER, \
438 size_t size = wxHASH_SIZE_DEFAULT) \
1a6d9c76 439 : wxHashTableBase() { Create(keyType, size); } \
df5168c4 440 \
6aa33060 441 virtual ~hashclass() { Destroy(); } \
df5168c4 442 \
1a6d9c76 443 void Put(long key, eltype *data) { DoPut(key, key, (void*)data); } \
222702b1
WS
444 void Put(long lhash, long key, eltype *data) \
445 { DoPut(key, lhash, (void*)data); } \
1a6d9c76 446 eltype *Get(long key) const { return (eltype*)DoGet(key, key); } \
222702b1
WS
447 eltype *Get(long lhash, long key) const \
448 { return (eltype*)DoGet(key, lhash); } \
1a6d9c76 449 eltype *Delete(long key) { return (eltype*)DoDelete(key, key); } \
222702b1
WS
450 eltype *Delete(long lhash, long key) \
451 { return (eltype*)DoDelete(key, lhash); } \
6f02a879 452 private: \
1a6d9c76
MB
453 virtual void DoDeleteContents( wxHashTableBase_Node* node ) \
454 { delete (eltype*)node->GetData(); } \
455 \
456 DECLARE_NO_COPY_CLASS(hashclass) \
df5168c4
MB
457 }
458
6e86701b 459#else // if wxUSE_OLD_HASH_TABLE
df5168c4 460
f6bcfd97
BP
461#define _WX_DECLARE_HASH(eltype, listclass, hashclass, classexp) \
462 classexp hashclass : public wxHashTableBase \
bcaa23de
VZ
463 { \
464 public: \
465 hashclass(wxKeyType keyType = wxKEY_INTEGER, \
466 size_t size = wxHASH_SIZE_DEFAULT) \
467 { Create(keyType, size); } \
468 \
d3c7fc99 469 virtual ~hashclass() { Destroy(); } \
bcaa23de
VZ
470 \
471 void Put(long key, long val, eltype *data) { DoPut(key, val, data); } \
472 void Put(long key, eltype *data) { DoPut(key, key, data); } \
473 \
474 eltype *Get(long key, long value) const \
475 { \
476 wxNodeBase *node = GetNode(key, value); \
477 return node ? ((listclass::Node *)node)->GetData() : (eltype *)0; \
478 } \
479 eltype *Get(long key) const { return Get(key, key); } \
480 \
481 eltype *Delete(long key, long value) \
482 { \
483 eltype *data; \
484 \
485 wxNodeBase *node = GetNode(key, value); \
486 if ( node ) \
487 { \
488 data = ((listclass::Node *)node)->GetData(); \
489 \
490 delete node; \
491 m_count--; \
492 } \
493 else \
494 { \
495 data = (eltype *)0; \
496 } \
497 \
498 return data; \
499 } \
500 eltype *Delete(long key) { return Delete(key, key); } \
501 \
502 protected: \
503 void DoPut(long key, long value, eltype *data) \
504 { \
6a5391af 505 size_t slot = (size_t)abs((int)(key % (long)m_hashSize)); \
bcaa23de
VZ
506 \
507 if ( !m_hashTable[slot] ) \
508 { \
509 m_hashTable[slot] = new listclass(m_keyType); \
510 if ( m_deleteContents ) \
5d3e7b52 511 m_hashTable[slot]->DeleteContents(true); \
bcaa23de
VZ
512 } \
513 \
514 ((listclass *)m_hashTable[slot])->Append(value, data); \
515 m_count++; \
516 } \
fc7a2a60
VZ
517 \
518 DECLARE_NO_COPY_CLASS(hashclass) \
bcaa23de
VZ
519 }
520
6e86701b 521#endif // wxUSE_OLD_HASH_TABLE
df5168c4 522
f6bcfd97
BP
523// this macro is to be used in the user code
524#define WX_DECLARE_HASH(el, list, hash) \
525 _WX_DECLARE_HASH(el, list, hash, class)
526
527// and this one does exactly the same thing but should be used inside the
528// library
529#define WX_DECLARE_EXPORTED_HASH(el, list, hash) \
530 _WX_DECLARE_HASH(el, list, hash, class WXDLLEXPORT)
531
0b9ab0bd
RL
532#define WX_DECLARE_USER_EXPORTED_HASH(el, list, hash, usergoo) \
533 _WX_DECLARE_HASH(el, list, hash, class usergoo)
534
ba8c1601
MB
535// delete all hash elements
536//
537// NB: the class declaration of the hash elements must be visible from the
538// place where you use this macro, otherwise the proper destructor may not
539// be called (a decent compiler should give a warning about it, but don't
540// count on it)!
df5168c4 541#define WX_CLEAR_HASH_TABLE(hash) \
ba8c1601 542 { \
df5168c4
MB
543 (hash).BeginFind(); \
544 wxHashTable::compatibility_iterator it = (hash).Next(); \
ba8c1601
MB
545 while( it ) \
546 { \
547 delete it->GetData(); \
df5168c4 548 it = (hash).Next(); \
ba8c1601 549 } \
df5168c4 550 (hash).Clear(); \
ba8c1601
MB
551 }
552
c801d85f 553#endif
bcaa23de 554 // _WX_HASH_H__