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