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