]> git.saurik.com Git - wxWidgets.git/blame - include/wx/hash.h
added conversion to and from std::string and std::wstring (if wxUSE_STD_STRING)
[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
644cb537
MB
338typedef wxNode wxHashTable_Node;
339
bddd7a8d 340class WXDLLIMPEXP_BASE wxHashTable : public wxObject
bcaa23de
VZ
341{
342public:
7057f62a
MB
343 typedef wxNode Node;
344 typedef wxNode* compatibility_iterator;
345
bcaa23de
VZ
346 int n;
347 int current_position;
348 wxNode *current_node;
349
350 unsigned int key_type;
351 wxList **hash_table;
352
353 wxHashTable(int the_key_type = wxKEY_INTEGER,
354 int size = wxHASH_SIZE_DEFAULT);
d3c7fc99 355 virtual ~wxHashTable();
bcaa23de
VZ
356
357 // copy ctor and assignment operator
d84afea9
GD
358 wxHashTable(const wxHashTable& table) : wxObject()
359 { DoCopy(table); }
bcaa23de
VZ
360 wxHashTable& operator=(const wxHashTable& table)
361 { Clear(); DoCopy(table); return *this; }
362
363 void DoCopy(const wxHashTable& table);
364
365 void Destroy();
366
367 bool Create(int the_key_type = wxKEY_INTEGER,
368 int size = wxHASH_SIZE_DEFAULT);
369
370 // Note that there are 2 forms of Put, Get.
371 // With a key and a value, the *value* will be checked
372 // when a collision is detected. Otherwise, if there are
373 // 2 items with a different value but the same key,
374 // we'll retrieve the WRONG ONE. So where possible,
375 // supply the required value along with the key.
376 // In fact, the value-only versions make a key, and still store
377 // the value. The use of an explicit key might be required
378 // e.g. when combining several values into one key.
379 // When doing that, it's highly likely we'll get a collision,
380 // e.g. 1 + 2 = 3, 2 + 1 = 3.
381
382 // key and value are NOT necessarily the same
383 void Put(long key, long value, wxObject *object);
384 void Put(long key, const wxChar *value, wxObject *object);
385
386 // key and value are the same
387 void Put(long value, wxObject *object);
388 void Put(const wxChar *value, wxObject *object);
389
390 // key and value not the same
391 wxObject *Get(long key, long value) const;
392 wxObject *Get(long key, const wxChar *value) const;
393
394 // key and value are the same
395 wxObject *Get(long value) const;
396 wxObject *Get(const wxChar *value) const;
397
398 // Deletes entry and returns data if found
399 wxObject *Delete(long key);
400 wxObject *Delete(const wxChar *key);
401
402 wxObject *Delete(long key, int value);
403 wxObject *Delete(long key, const wxChar *value);
404
405 // Construct your own integer key from a string, e.g. in case
406 // you need to combine it with something
407 long MakeKey(const wxChar *string) const;
408
409 // Way of iterating through whole hash table (e.g. to delete everything)
410 // Not necessary, of course, if you're only storing pointers to
411 // objects maintained separately
412
413 void BeginFind();
1a6d9c76 414 Node* Next();
bcaa23de
VZ
415
416 void DeleteContents(bool flag);
417 void Clear();
418
419 // Returns number of nodes
420 size_t GetCount() const { return m_count; }
421
422private:
423 size_t m_count; // number of elements in the hashtable
424 bool m_deleteContents;
425
426 DECLARE_DYNAMIC_CLASS(wxHashTable)
427};
428
6e86701b 429#endif // wxUSE_OLD_HASH_TABLE
df5168c4 430
6e86701b 431#if !wxUSE_OLD_HASH_TABLE
df5168c4 432
bcaa23de
VZ
433// defines a new type safe hash table which stores the elements of type eltype
434// in lists of class listclass
df5168c4
MB
435#define _WX_DECLARE_HASH(eltype, dummy, hashclass, classexp) \
436 classexp hashclass : public wxHashTableBase \
437 { \
438 public: \
439 hashclass(wxKeyType keyType = wxKEY_INTEGER, \
440 size_t size = wxHASH_SIZE_DEFAULT) \
1a6d9c76 441 : wxHashTableBase() { Create(keyType, size); } \
df5168c4 442 \
6aa33060 443 virtual ~hashclass() { Destroy(); } \
df5168c4 444 \
1a6d9c76 445 void Put(long key, eltype *data) { DoPut(key, key, (void*)data); } \
222702b1
WS
446 void Put(long lhash, long key, eltype *data) \
447 { DoPut(key, lhash, (void*)data); } \
1a6d9c76 448 eltype *Get(long key) const { return (eltype*)DoGet(key, key); } \
222702b1
WS
449 eltype *Get(long lhash, long key) const \
450 { return (eltype*)DoGet(key, lhash); } \
1a6d9c76 451 eltype *Delete(long key) { return (eltype*)DoDelete(key, key); } \
222702b1
WS
452 eltype *Delete(long lhash, long key) \
453 { return (eltype*)DoDelete(key, lhash); } \
6f02a879 454 private: \
1a6d9c76
MB
455 virtual void DoDeleteContents( wxHashTableBase_Node* node ) \
456 { delete (eltype*)node->GetData(); } \
457 \
458 DECLARE_NO_COPY_CLASS(hashclass) \
df5168c4
MB
459 }
460
6e86701b 461#else // if wxUSE_OLD_HASH_TABLE
df5168c4 462
f6bcfd97
BP
463#define _WX_DECLARE_HASH(eltype, listclass, hashclass, classexp) \
464 classexp hashclass : public wxHashTableBase \
bcaa23de
VZ
465 { \
466 public: \
467 hashclass(wxKeyType keyType = wxKEY_INTEGER, \
468 size_t size = wxHASH_SIZE_DEFAULT) \
469 { Create(keyType, size); } \
470 \
d3c7fc99 471 virtual ~hashclass() { Destroy(); } \
bcaa23de
VZ
472 \
473 void Put(long key, long val, eltype *data) { DoPut(key, val, data); } \
474 void Put(long key, eltype *data) { DoPut(key, key, data); } \
475 \
476 eltype *Get(long key, long value) const \
477 { \
478 wxNodeBase *node = GetNode(key, value); \
479 return node ? ((listclass::Node *)node)->GetData() : (eltype *)0; \
480 } \
481 eltype *Get(long key) const { return Get(key, key); } \
482 \
483 eltype *Delete(long key, long value) \
484 { \
485 eltype *data; \
486 \
487 wxNodeBase *node = GetNode(key, value); \
488 if ( node ) \
489 { \
490 data = ((listclass::Node *)node)->GetData(); \
491 \
492 delete node; \
493 m_count--; \
494 } \
495 else \
496 { \
497 data = (eltype *)0; \
498 } \
499 \
500 return data; \
501 } \
502 eltype *Delete(long key) { return Delete(key, key); } \
503 \
504 protected: \
505 void DoPut(long key, long value, eltype *data) \
506 { \
6a5391af 507 size_t slot = (size_t)abs((int)(key % (long)m_hashSize)); \
bcaa23de
VZ
508 \
509 if ( !m_hashTable[slot] ) \
510 { \
511 m_hashTable[slot] = new listclass(m_keyType); \
512 if ( m_deleteContents ) \
5d3e7b52 513 m_hashTable[slot]->DeleteContents(true); \
bcaa23de
VZ
514 } \
515 \
516 ((listclass *)m_hashTable[slot])->Append(value, data); \
517 m_count++; \
518 } \
fc7a2a60
VZ
519 \
520 DECLARE_NO_COPY_CLASS(hashclass) \
bcaa23de
VZ
521 }
522
6e86701b 523#endif // wxUSE_OLD_HASH_TABLE
df5168c4 524
f6bcfd97
BP
525// this macro is to be used in the user code
526#define WX_DECLARE_HASH(el, list, hash) \
527 _WX_DECLARE_HASH(el, list, hash, class)
528
529// and this one does exactly the same thing but should be used inside the
530// library
531#define WX_DECLARE_EXPORTED_HASH(el, list, hash) \
532 _WX_DECLARE_HASH(el, list, hash, class WXDLLEXPORT)
533
0b9ab0bd
RL
534#define WX_DECLARE_USER_EXPORTED_HASH(el, list, hash, usergoo) \
535 _WX_DECLARE_HASH(el, list, hash, class usergoo)
536
ba8c1601
MB
537// delete all hash elements
538//
539// NB: the class declaration of the hash elements must be visible from the
540// place where you use this macro, otherwise the proper destructor may not
541// be called (a decent compiler should give a warning about it, but don't
542// count on it)!
df5168c4 543#define WX_CLEAR_HASH_TABLE(hash) \
ba8c1601 544 { \
df5168c4
MB
545 (hash).BeginFind(); \
546 wxHashTable::compatibility_iterator it = (hash).Next(); \
ba8c1601
MB
547 while( it ) \
548 { \
549 delete it->GetData(); \
df5168c4 550 it = (hash).Next(); \
ba8c1601 551 } \
df5168c4 552 (hash).Clear(); \
ba8c1601
MB
553 }
554
c801d85f 555#endif
bcaa23de 556 // _WX_HASH_H__