]> git.saurik.com Git - wxWidgets.git/blame - src/common/hash.cpp
Sorry folks, a lot of changes to remedy GetFont, GetBrush etc.
[wxWidgets.git] / src / common / hash.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: hash.cpp
3// Purpose: wxHashTable implementation
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "hash.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20#pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
24#include "wx/list.h"
25#endif
26
27#include "wx/hash.h"
28
29#include <string.h>
30#include <stdarg.h>
31
32#if !USE_SHARED_LIBRARY
33IMPLEMENT_DYNAMIC_CLASS(wxHashTable, wxObject)
34#endif
35
debe6624 36wxHashTable::wxHashTable (int the_key_type, int size)
c801d85f
KB
37{
38 n = size;
39 current_position = -1;
c67daf87 40 current_node = (wxNode *) NULL;
c801d85f
KB
41
42 key_type = the_key_type;
43 hash_table = new wxList *[size];
44 int i;
45 for (i = 0; i < size; i++)
c67daf87 46 hash_table[i] = (wxList *) NULL;
c801d85f
KB
47}
48
49wxHashTable::~wxHashTable (void)
50{
e55ad60e
RR
51 Destroy();
52}
53
54void wxHashTable::Destroy(void)
55{
56 if (!hash_table) return;
c801d85f
KB
57 int i;
58 for (i = 0; i < n; i++)
59 if (hash_table[i])
60 delete hash_table[i];
61 delete[] hash_table;
e55ad60e 62 hash_table = NULL;
c801d85f
KB
63}
64
debe6624 65bool wxHashTable::Create(int the_key_type, int size)
c801d85f
KB
66{
67 n = size;
68 current_position = -1;
c67daf87 69 current_node = (wxNode *) NULL;
c801d85f
KB
70
71 key_type = the_key_type;
72 if (hash_table)
73 delete[] hash_table;
74 hash_table = new wxList *[size];
75 int i;
76 for (i = 0; i < size; i++)
c67daf87 77 hash_table[i] = (wxList *) NULL;
c801d85f
KB
78 return TRUE;
79}
80
debe6624 81void wxHashTable::Put (long key, long value, wxObject * object)
c801d85f
KB
82{
83 // Should NEVER be
84 long k = (long) key;
85 if (k < 0)
86 k = -k;
87
88 int position = (int) (k % n);
89 if (!hash_table[position])
90 hash_table[position] = new wxList (wxKEY_INTEGER);
91
92 hash_table[position]->Append (value, object);
93}
94
debe6624 95void wxHashTable::Put (long key, const char *value, wxObject * object)
c801d85f
KB
96{
97 // Should NEVER be
98 long k = (long) key;
99 if (k < 0)
100 k = -k;
101
102 int position = (int) (k % n);
103 if (!hash_table[position])
104 hash_table[position] = new wxList (wxKEY_INTEGER);
105
106 hash_table[position]->Append (value, object);
107}
108
debe6624 109void wxHashTable::Put (long key, wxObject * object)
c801d85f
KB
110{
111 // Should NEVER be
112 long k = (long) key;
113 if (k < 0)
114 k = -k;
115
116 int position = (int) (k % n);
117 if (!hash_table[position])
118 hash_table[position] = new wxList (wxKEY_INTEGER);
119
120 hash_table[position]->Append (k, object);
121}
122
123void wxHashTable::Put (const char *key, wxObject * object)
124{
125 int position = (int) (MakeKey (key) % n);
126
127 if (!hash_table[position])
128 hash_table[position] = new wxList (wxKEY_STRING);
129
130 hash_table[position]->Append (key, object);
131}
132
debe6624 133wxObject *wxHashTable::Get (long key, long value) const
c801d85f
KB
134{
135 // Should NEVER be
136 long k = (long) key;
137 if (k < 0)
138 k = -k;
139
140 int position = (int) (k % n);
141 if (!hash_table[position])
c67daf87 142 return (wxObject *) NULL;
c801d85f
KB
143 else
144 {
145 wxNode *node = hash_table[position]->Find (value);
146 if (node)
147 return node->Data ();
148 else
c67daf87 149 return (wxObject *) NULL;
c801d85f
KB
150 }
151}
152
debe6624 153wxObject *wxHashTable::Get (long key, const char *value) const
c801d85f
KB
154{
155 // Should NEVER be
156 long k = (long) key;
157 if (k < 0)
158 k = -k;
159
160 int position = (int) (k % n);
161 if (!hash_table[position])
c67daf87 162 return (wxObject *) NULL;
c801d85f
KB
163 else
164 {
165 wxNode *node = hash_table[position]->Find (value);
166 if (node)
167 return node->Data ();
168 else
c67daf87 169 return (wxObject *) NULL;
c801d85f
KB
170 }
171}
172
debe6624 173wxObject *wxHashTable::Get (long key) const
c801d85f
KB
174{
175 // Should NEVER be
176 long k = (long) key;
177 if (k < 0)
178 k = -k;
179
180 int position = (int) (k % n);
181 if (!hash_table[position])
c67daf87 182 return (wxObject *) NULL;
c801d85f
KB
183 else
184 {
185 wxNode *node = hash_table[position]->Find (k);
186 return node ? node->Data () : (wxObject*)NULL;
187 }
188}
189
190wxObject *wxHashTable::Get (const char *key) const
191{
192 int position = (int) (MakeKey (key) % n);
193
194 if (!hash_table[position])
c67daf87 195 return (wxObject *) NULL;
c801d85f
KB
196 else
197 {
198 wxNode *node = hash_table[position]->Find (key);
199 return node ? node->Data () : (wxObject*)NULL;
200 }
201}
202
debe6624 203wxObject *wxHashTable::Delete (long key)
c801d85f
KB
204{
205 // Should NEVER be
206 long k = (long) key;
207 if (k < 0)
208 k = -k;
209
210 int position = (int) (k % n);
211 if (!hash_table[position])
c67daf87 212 return (wxObject *) NULL;
c801d85f
KB
213 else
214 {
215 wxNode *node = hash_table[position]->Find (k);
216 if (node)
217 {
218 wxObject *data = node->Data ();
219 delete node;
220 return data;
221 }
222 else
c67daf87 223 return (wxObject *) NULL;
c801d85f
KB
224 }
225}
226
227wxObject *wxHashTable::Delete (const char *key)
228{
229 int position = (int) (MakeKey (key) % n);
230 if (!hash_table[position])
c67daf87 231 return (wxObject *) NULL;
c801d85f
KB
232 else
233 {
234 wxNode *node = hash_table[position]->Find (key);
235 if (node)
236 {
237 wxObject *data = node->Data ();
238 delete node;
239 return data;
240 }
241 else
c67daf87 242 return (wxObject *) NULL;
c801d85f
KB
243 }
244}
245
debe6624 246wxObject *wxHashTable::Delete (long key, int value)
c801d85f
KB
247{
248 // Should NEVER be
249 long k = (long) key;
250 if (k < 0)
251 k = -k;
252
253 int position = (int) (k % n);
254 if (!hash_table[position])
c67daf87 255 return (wxObject *) NULL;
c801d85f
KB
256 else
257 {
258 wxNode *node = hash_table[position]->Find (value);
259 if (node)
260 {
261 wxObject *data = node->Data ();
262 delete node;
263 return data;
264 }
265 else
c67daf87 266 return (wxObject *) NULL;
c801d85f
KB
267 }
268}
269
debe6624 270wxObject *wxHashTable::Delete (long key, const char *value)
c801d85f
KB
271{
272 int position = (int) (key % n);
273 if (!hash_table[position])
c67daf87 274 return (wxObject *) NULL;
c801d85f
KB
275 else
276 {
277 wxNode *node = hash_table[position]->Find (value);
278 if (node)
279 {
280 wxObject *data = node->Data ();
281 delete node;
282 return data;
283 }
284 else
c67daf87 285 return (wxObject *) NULL;
c801d85f
KB
286 }
287}
288
289long wxHashTable::MakeKey (const char *string) const
290{
291 long int_key = 0;
292
293 while (*string)
294 int_key += (unsigned char) *string++;
295
296 return int_key;
297}
298
299void wxHashTable::BeginFind (void)
300{
301 current_position = -1;
c67daf87 302 current_node = (wxNode *) NULL;
c801d85f
KB
303}
304
305wxNode *wxHashTable::Next (void)
306{
c67daf87 307 wxNode *found = (wxNode *) NULL;
c801d85f
KB
308 bool end = FALSE;
309 while (!end && !found)
310 {
311 if (!current_node)
312 {
313 current_position++;
314 if (current_position >= n)
315 {
316 current_position = -1;
c67daf87 317 current_node = (wxNode *) NULL;
c801d85f
KB
318 end = TRUE;
319 }
320 else
321 {
322 if (hash_table[current_position])
323 {
324 current_node = hash_table[current_position]->First ();
325 found = current_node;
326 }
327 }
328 }
329 else
330 {
331 current_node = current_node->Next ();
332 found = current_node;
333 }
334 }
335 return found;
336}
337
debe6624 338void wxHashTable::DeleteContents (bool flag)
c801d85f
KB
339{
340 int i;
341 for (i = 0; i < n; i++)
342 {
343 if (hash_table[i])
344 hash_table[i]->DeleteContents (flag);
345 }
346}
347
348void wxHashTable::Clear (void)
349{
350 int i;
351 for (i = 0; i < n; i++)
352 {
353 if (hash_table[i])
354 hash_table[i]->Clear ();
355 }
356}
357