1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHashTable class
4 // Author: Julian Smart
5 // Modified by: VZ at 25.02.00: type safe hashes with WX_DECLARE_HASH()
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
16 #pragma interface "hash.h"
21 // the default size of the hash
22 #define wxHASH_SIZE_DEFAULT (1000)
25 * A hash table is an array of user-definable size with lists
26 * of data items hanging off the array positions. Usually there'll
27 * be a hit, so no search is required; otherwise we'll have to run down
28 * the list to find the desired item.
31 // ----------------------------------------------------------------------------
32 // this is the base class for object hashes: hash tables which contain
33 // pointers to objects
34 // ----------------------------------------------------------------------------
36 class WXDLLEXPORT wxHashTableBase
: public wxObject
41 void Create(wxKeyType keyType
= wxKEY_INTEGER
,
42 size_t size
= wxHASH_SIZE_DEFAULT
);
45 size_t GetSize() const { return m_hashSize
; }
46 size_t GetCount() const { return m_count
; }
48 void DeleteContents(bool flag
);
51 // find the node for (key, value)
52 wxNodeBase
*GetNode(long key
, long value
) const;
54 // the array of lists in which we store the values for given key hash
55 wxListBase
**m_hashTable
;
57 // the size of m_lists array
60 // the type of indexing we use
63 // the total number of elements in the hash
66 // should we delete our data?
67 bool m_deleteContents
;
70 // no copy ctor/assignment operator (yet)
71 DECLARE_NO_COPY_CLASS(wxHashTableBase
);
74 // ----------------------------------------------------------------------------
75 // for compatibility only
76 // ----------------------------------------------------------------------------
78 class WXDLLEXPORT wxHashTable
: public wxObject
85 unsigned int key_type
;
88 wxHashTable(int the_key_type
= wxKEY_INTEGER
,
89 int size
= wxHASH_SIZE_DEFAULT
);
92 // copy ctor and assignment operator
93 wxHashTable(const wxHashTable
& table
) { DoCopy(table
); }
94 wxHashTable
& operator=(const wxHashTable
& table
)
95 { Clear(); DoCopy(table
); return *this; }
97 void DoCopy(const wxHashTable
& table
);
101 bool Create(int the_key_type
= wxKEY_INTEGER
,
102 int size
= wxHASH_SIZE_DEFAULT
);
104 // Note that there are 2 forms of Put, Get.
105 // With a key and a value, the *value* will be checked
106 // when a collision is detected. Otherwise, if there are
107 // 2 items with a different value but the same key,
108 // we'll retrieve the WRONG ONE. So where possible,
109 // supply the required value along with the key.
110 // In fact, the value-only versions make a key, and still store
111 // the value. The use of an explicit key might be required
112 // e.g. when combining several values into one key.
113 // When doing that, it's highly likely we'll get a collision,
114 // e.g. 1 + 2 = 3, 2 + 1 = 3.
116 // key and value are NOT necessarily the same
117 void Put(long key
, long value
, wxObject
*object
);
118 void Put(long key
, const wxChar
*value
, wxObject
*object
);
120 // key and value are the same
121 void Put(long value
, wxObject
*object
);
122 void Put(const wxChar
*value
, wxObject
*object
);
124 // key and value not the same
125 wxObject
*Get(long key
, long value
) const;
126 wxObject
*Get(long key
, const wxChar
*value
) const;
128 // key and value are the same
129 wxObject
*Get(long value
) const;
130 wxObject
*Get(const wxChar
*value
) const;
132 // Deletes entry and returns data if found
133 wxObject
*Delete(long key
);
134 wxObject
*Delete(const wxChar
*key
);
136 wxObject
*Delete(long key
, int value
);
137 wxObject
*Delete(long key
, const wxChar
*value
);
139 // Construct your own integer key from a string, e.g. in case
140 // you need to combine it with something
141 long MakeKey(const wxChar
*string
) const;
143 // Way of iterating through whole hash table (e.g. to delete everything)
144 // Not necessary, of course, if you're only storing pointers to
145 // objects maintained separately
150 void DeleteContents(bool flag
);
153 // Returns number of nodes
154 size_t GetCount() const { return m_count
; }
157 size_t m_count
; // number of elements in the hashtable
158 bool m_deleteContents
;
160 DECLARE_DYNAMIC_CLASS(wxHashTable
)
163 // defines a new type safe hash table which stores the elements of type eltype
164 // in lists of class listclass
165 #define WX_DECLARE_HASH(eltype, listclass, hashclass) \
166 class WXDLLEXPORT hashclass : public wxHashTableBase \
169 hashclass(wxKeyType keyType = wxKEY_INTEGER, \
170 size_t size = wxHASH_SIZE_DEFAULT) \
171 { Create(keyType, size); } \
173 ~hashclass() { Destroy(); } \
175 void Put(long key, long val, eltype *data) { DoPut(key, val, data); } \
176 void Put(long key, eltype *data) { DoPut(key, key, data); } \
178 eltype *Get(long key, long value) const \
180 wxNodeBase *node = GetNode(key, value); \
181 return node ? ((listclass::Node *)node)->GetData() : (eltype *)0; \
183 eltype *Get(long key) const { return Get(key, key); } \
185 eltype *Delete(long key, long value) \
189 wxNodeBase *node = GetNode(key, value); \
192 data = ((listclass::Node *)node)->GetData(); \
199 data = (eltype *)0; \
204 eltype *Delete(long key) { return Delete(key, key); } \
207 void DoPut(long key, long value, eltype *data) \
209 size_t slot = (size_t)abs(key % m_hashSize); \
211 if ( !m_hashTable[slot] ) \
213 m_hashTable[slot] = new listclass(m_keyType); \
214 if ( m_deleteContents ) \
215 m_hashTable[slot]->DeleteContents(TRUE); \
218 ((listclass *)m_hashTable[slot])->Append(value, data); \