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