]> git.saurik.com Git - wxWidgets.git/blame - src/common/hash.cpp
Added #if wxUSE_OWNER_DRAWN
[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
c801d85f 32IMPLEMENT_DYNAMIC_CLASS(wxHashTable, wxObject)
c801d85f 33
debe6624 34wxHashTable::wxHashTable (int the_key_type, int size)
c801d85f
KB
35{
36 n = size;
37 current_position = -1;
c67daf87 38 current_node = (wxNode *) NULL;
c801d85f
KB
39
40 key_type = the_key_type;
41 hash_table = new wxList *[size];
42 int i;
43 for (i = 0; i < size; i++)
c67daf87 44 hash_table[i] = (wxList *) NULL;
c801d85f
KB
45}
46
47wxHashTable::~wxHashTable (void)
48{
e55ad60e
RR
49 Destroy();
50}
51
52void wxHashTable::Destroy(void)
53{
54 if (!hash_table) return;
c801d85f
KB
55 int i;
56 for (i = 0; i < n; i++)
57 if (hash_table[i])
58 delete hash_table[i];
59 delete[] hash_table;
e55ad60e 60 hash_table = NULL;
c801d85f
KB
61}
62
debe6624 63bool wxHashTable::Create(int the_key_type, int size)
c801d85f
KB
64{
65 n = size;
66 current_position = -1;
c67daf87 67 current_node = (wxNode *) NULL;
c801d85f
KB
68
69 key_type = the_key_type;
70 if (hash_table)
71 delete[] hash_table;
72 hash_table = new wxList *[size];
73 int i;
74 for (i = 0; i < size; i++)
c67daf87 75 hash_table[i] = (wxList *) NULL;
c801d85f
KB
76 return TRUE;
77}
78
4e67bfc7
VS
79
80void wxHashTable::DoCopy(const wxHashTable& table)
81{
82 n = table.n;
83 current_position = table.current_position;
84 current_node = NULL; // doesn't matter - Next() will reconstruct it
85 key_type = table.key_type;
86
87 hash_table = new wxList *[n];
88 for (int i = 0; i < n; i++) {
89 if (table.hash_table[i] == NULL)
90 hash_table[i] = NULL;
91 else {
92 hash_table[i] = new wxList(key_type);
93 *(hash_table[i]) = *(table.hash_table[i]);
94 }
95 }
96}
97
debe6624 98void wxHashTable::Put (long key, long value, wxObject * object)
c801d85f
KB
99{
100 // Should NEVER be
101 long k = (long) key;
102 if (k < 0)
103 k = -k;
104
105 int position = (int) (k % n);
106 if (!hash_table[position])
107 hash_table[position] = new wxList (wxKEY_INTEGER);
108
109 hash_table[position]->Append (value, object);
110}
111
50920146 112void wxHashTable::Put (long key, const wxChar *value, wxObject * object)
c801d85f
KB
113{
114 // Should NEVER be
115 long k = (long) key;
116 if (k < 0)
117 k = -k;
118
119 int position = (int) (k % n);
120 if (!hash_table[position])
121 hash_table[position] = new wxList (wxKEY_INTEGER);
122
123 hash_table[position]->Append (value, object);
124}
125
debe6624 126void wxHashTable::Put (long key, wxObject * object)
c801d85f
KB
127{
128 // Should NEVER be
129 long k = (long) key;
130 if (k < 0)
131 k = -k;
132
133 int position = (int) (k % n);
134 if (!hash_table[position])
135 hash_table[position] = new wxList (wxKEY_INTEGER);
136
137 hash_table[position]->Append (k, object);
138}
139
50920146 140void wxHashTable::Put (const wxChar *key, wxObject * object)
c801d85f
KB
141{
142 int position = (int) (MakeKey (key) % n);
143
144 if (!hash_table[position])
145 hash_table[position] = new wxList (wxKEY_STRING);
146
147 hash_table[position]->Append (key, object);
148}
149
debe6624 150wxObject *wxHashTable::Get (long key, long value) const
c801d85f
KB
151{
152 // Should NEVER be
153 long k = (long) key;
154 if (k < 0)
155 k = -k;
156
157 int position = (int) (k % n);
158 if (!hash_table[position])
c67daf87 159 return (wxObject *) NULL;
c801d85f
KB
160 else
161 {
162 wxNode *node = hash_table[position]->Find (value);
163 if (node)
164 return node->Data ();
165 else
c67daf87 166 return (wxObject *) NULL;
c801d85f
KB
167 }
168}
169
50920146 170wxObject *wxHashTable::Get (long key, const wxChar *value) const
c801d85f
KB
171{
172 // Should NEVER be
173 long k = (long) key;
174 if (k < 0)
175 k = -k;
176
177 int position = (int) (k % n);
178 if (!hash_table[position])
c67daf87 179 return (wxObject *) NULL;
c801d85f
KB
180 else
181 {
182 wxNode *node = hash_table[position]->Find (value);
183 if (node)
184 return node->Data ();
185 else
c67daf87 186 return (wxObject *) NULL;
c801d85f
KB
187 }
188}
189
debe6624 190wxObject *wxHashTable::Get (long key) const
c801d85f
KB
191{
192 // Should NEVER be
193 long k = (long) key;
194 if (k < 0)
195 k = -k;
196
197 int position = (int) (k % n);
198 if (!hash_table[position])
c67daf87 199 return (wxObject *) NULL;
c801d85f
KB
200 else
201 {
202 wxNode *node = hash_table[position]->Find (k);
203 return node ? node->Data () : (wxObject*)NULL;
204 }
205}
206
50920146 207wxObject *wxHashTable::Get (const wxChar *key) const
c801d85f
KB
208{
209 int position = (int) (MakeKey (key) % n);
210
211 if (!hash_table[position])
c67daf87 212 return (wxObject *) NULL;
c801d85f
KB
213 else
214 {
215 wxNode *node = hash_table[position]->Find (key);
216 return node ? node->Data () : (wxObject*)NULL;
217 }
218}
219
debe6624 220wxObject *wxHashTable::Delete (long key)
c801d85f
KB
221{
222 // Should NEVER be
223 long k = (long) key;
224 if (k < 0)
225 k = -k;
226
227 int position = (int) (k % n);
228 if (!hash_table[position])
c67daf87 229 return (wxObject *) NULL;
c801d85f
KB
230 else
231 {
232 wxNode *node = hash_table[position]->Find (k);
233 if (node)
234 {
235 wxObject *data = node->Data ();
236 delete node;
237 return data;
238 }
239 else
c67daf87 240 return (wxObject *) NULL;
c801d85f
KB
241 }
242}
243
50920146 244wxObject *wxHashTable::Delete (const wxChar *key)
c801d85f
KB
245{
246 int position = (int) (MakeKey (key) % n);
247 if (!hash_table[position])
c67daf87 248 return (wxObject *) NULL;
c801d85f
KB
249 else
250 {
251 wxNode *node = hash_table[position]->Find (key);
252 if (node)
253 {
254 wxObject *data = node->Data ();
255 delete node;
256 return data;
257 }
258 else
c67daf87 259 return (wxObject *) NULL;
c801d85f
KB
260 }
261}
262
debe6624 263wxObject *wxHashTable::Delete (long key, int value)
c801d85f
KB
264{
265 // Should NEVER be
266 long k = (long) key;
267 if (k < 0)
268 k = -k;
269
270 int position = (int) (k % n);
271 if (!hash_table[position])
c67daf87 272 return (wxObject *) NULL;
c801d85f
KB
273 else
274 {
275 wxNode *node = hash_table[position]->Find (value);
276 if (node)
277 {
278 wxObject *data = node->Data ();
279 delete node;
280 return data;
281 }
282 else
c67daf87 283 return (wxObject *) NULL;
c801d85f
KB
284 }
285}
286
50920146 287wxObject *wxHashTable::Delete (long key, const wxChar *value)
c801d85f
KB
288{
289 int position = (int) (key % n);
290 if (!hash_table[position])
c67daf87 291 return (wxObject *) NULL;
c801d85f
KB
292 else
293 {
294 wxNode *node = hash_table[position]->Find (value);
295 if (node)
296 {
297 wxObject *data = node->Data ();
298 delete node;
299 return data;
300 }
301 else
c67daf87 302 return (wxObject *) NULL;
c801d85f
KB
303 }
304}
305
50920146 306long wxHashTable::MakeKey (const wxChar *string) const
c801d85f
KB
307{
308 long int_key = 0;
309
310 while (*string)
50920146 311 int_key += (wxUChar) *string++;
c801d85f
KB
312
313 return int_key;
314}
315
316void wxHashTable::BeginFind (void)
317{
318 current_position = -1;
c67daf87 319 current_node = (wxNode *) NULL;
c801d85f
KB
320}
321
322wxNode *wxHashTable::Next (void)
323{
c67daf87 324 wxNode *found = (wxNode *) NULL;
c801d85f
KB
325 bool end = FALSE;
326 while (!end && !found)
327 {
328 if (!current_node)
329 {
330 current_position++;
331 if (current_position >= n)
332 {
333 current_position = -1;
c67daf87 334 current_node = (wxNode *) NULL;
c801d85f
KB
335 end = TRUE;
336 }
337 else
338 {
339 if (hash_table[current_position])
340 {
341 current_node = hash_table[current_position]->First ();
342 found = current_node;
343 }
344 }
345 }
346 else
347 {
348 current_node = current_node->Next ();
349 found = current_node;
350 }
351 }
352 return found;
353}
354
debe6624 355void wxHashTable::DeleteContents (bool flag)
c801d85f
KB
356{
357 int i;
358 for (i = 0; i < n; i++)
359 {
360 if (hash_table[i])
361 hash_table[i]->DeleteContents (flag);
362 }
363}
364
365void wxHashTable::Clear (void)
366{
367 int i;
368 for (i = 0; i < n; i++)
369 {
370 if (hash_table[i])
371 hash_table[i]->Clear ();
372 }
373}
374