]>
git.saurik.com Git - wxWidgets.git/blob - src/common/hash.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHashTable implementation
4 // Author: Julian Smart
5 // Modified by: VZ at 25.02.00: type safe hashes with WX_DECLARE_HASH()
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "hash.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 IMPLEMENT_DYNAMIC_CLASS(wxHashTable
, wxObject
)
46 // ============================================================================
48 // ============================================================================
50 // ----------------------------------------------------------------------------
51 // wxHashTablleBase for working with "void *" data
52 // ----------------------------------------------------------------------------
54 wxHashTableBase::wxHashTableBase()
56 m_deleteContents
= FALSE
;
57 m_hashTable
= (wxListBase
**)NULL
;
60 m_keyType
= wxKEY_NONE
;
63 void wxHashTableBase::Create(wxKeyType keyType
, size_t size
)
69 m_hashTable
= new wxListBase
*[size
];
70 for ( size_t n
= 0; n
< m_hashSize
; n
++ )
72 m_hashTable
[n
] = (wxListBase
*) NULL
;
76 void wxHashTableBase::Destroy()
80 for ( size_t n
= 0; n
< m_hashSize
; n
++ )
82 delete m_hashTable
[n
];
85 delete [] m_hashTable
;
87 m_hashTable
= (wxListBase
**)NULL
;
93 void wxHashTableBase::DeleteContents(bool flag
)
95 m_deleteContents
= flag
;
96 for ( size_t n
= 0; n
< m_hashSize
; n
++ )
100 m_hashTable
[n
]->DeleteContents(flag
);
105 wxNodeBase
*wxHashTableBase::GetNode(long key
, long value
) const
107 size_t slot
= (size_t)abs(key
% m_hashSize
);
110 if ( m_hashTable
[slot
] )
112 node
= m_hashTable
[slot
]->Find(wxListKey(value
));
116 node
= (wxNodeBase
*)NULL
;
122 // ----------------------------------------------------------------------------
123 // old not type safe wxHashTable
124 // ----------------------------------------------------------------------------
126 wxHashTable::wxHashTable (int the_key_type
, int size
)
129 hash_table
= (wxList
**) NULL
;
130 Create(the_key_type
, size
);
132 m_deleteContents
= FALSE
;
135 current_position = -1;
136 current_node = (wxNode *) NULL;
138 key_type = the_key_type;
139 hash_table = new wxList *[size];
141 for (i = 0; i < size; i++)
142 hash_table[i] = (wxList *) NULL;
146 wxHashTable::~wxHashTable ()
151 void wxHashTable::Destroy()
153 if (!hash_table
) return;
155 for (i
= 0; i
< n
; i
++)
157 delete hash_table
[i
];
162 bool wxHashTable::Create(int the_key_type
, int size
)
167 current_position
= -1;
168 current_node
= (wxNode
*) NULL
;
170 key_type
= the_key_type
;
171 hash_table
= new wxList
*[size
];
173 for (i
= 0; i
< size
; i
++)
174 hash_table
[i
] = (wxList
*) NULL
;
179 void wxHashTable::DoCopy(const wxHashTable
& table
)
182 current_position
= table
.current_position
;
183 current_node
= NULL
; // doesn't matter - Next() will reconstruct it
184 key_type
= table
.key_type
;
186 hash_table
= new wxList
*[n
];
187 for (int i
= 0; i
< n
; i
++) {
188 if (table
.hash_table
[i
] == NULL
)
189 hash_table
[i
] = NULL
;
191 hash_table
[i
] = new wxList(key_type
);
192 *(hash_table
[i
]) = *(table
.hash_table
[i
]);
197 void wxHashTable::Put (long key
, long value
, wxObject
* object
)
202 int position
= (int) (k
% n
);
203 if (position
< 0) position
= -position
;
205 if (!hash_table
[position
])
207 hash_table
[position
] = new wxList (wxKEY_INTEGER
);
208 if (m_deleteContents
) hash_table
[position
]->DeleteContents(TRUE
);
211 hash_table
[position
]->Append (value
, object
);
215 void wxHashTable::Put (long key
, const wxChar
*value
, wxObject
* object
)
220 int position
= (int) (k
% n
);
221 if (position
< 0) position
= -position
;
223 if (!hash_table
[position
])
225 hash_table
[position
] = new wxList (wxKEY_INTEGER
);
226 if (m_deleteContents
) hash_table
[position
]->DeleteContents(TRUE
);
229 hash_table
[position
]->Append (value
, object
);
233 void wxHashTable::Put (long key
, wxObject
* object
)
238 int position
= (int) (k
% n
);
239 if (position
< 0) position
= -position
;
241 if (!hash_table
[position
])
243 hash_table
[position
] = new wxList (wxKEY_INTEGER
);
244 if (m_deleteContents
) hash_table
[position
]->DeleteContents(TRUE
);
247 hash_table
[position
]->Append (k
, object
);
251 void wxHashTable::Put (const wxChar
*key
, wxObject
* object
)
253 int position
= (int) (MakeKey (key
) % n
);
254 if (position
< 0) position
= -position
;
256 if (!hash_table
[position
])
258 hash_table
[position
] = new wxList (wxKEY_STRING
);
259 if (m_deleteContents
) hash_table
[position
]->DeleteContents(TRUE
);
262 hash_table
[position
]->Append (key
, object
);
266 wxObject
*wxHashTable::Get (long key
, long value
) const
271 int position
= (int) (k
% n
);
272 if (position
< 0) position
= -position
;
274 if (!hash_table
[position
])
275 return (wxObject
*) NULL
;
278 wxNode
*node
= hash_table
[position
]->Find (value
);
280 return node
->Data ();
282 return (wxObject
*) NULL
;
286 wxObject
*wxHashTable::Get (long key
, const wxChar
*value
) const
291 int position
= (int) (k
% n
);
292 if (position
< 0) position
= -position
;
294 if (!hash_table
[position
])
295 return (wxObject
*) NULL
;
298 wxNode
*node
= hash_table
[position
]->Find (value
);
300 return node
->Data ();
302 return (wxObject
*) NULL
;
306 wxObject
*wxHashTable::Get (long key
) const
311 int position
= (int) (k
% n
);
312 if (position
< 0) position
= -position
;
314 if (!hash_table
[position
])
315 return (wxObject
*) NULL
;
318 wxNode
*node
= hash_table
[position
]->Find (k
);
319 return node
? node
->Data () : (wxObject
*)NULL
;
323 wxObject
*wxHashTable::Get (const wxChar
*key
) const
325 int position
= (int) (MakeKey (key
) % n
);
326 if (position
< 0) position
= -position
;
328 if (!hash_table
[position
])
329 return (wxObject
*) NULL
;
332 wxNode
*node
= hash_table
[position
]->Find (key
);
333 return node
? node
->Data () : (wxObject
*)NULL
;
337 wxObject
*wxHashTable::Delete (long key
)
342 int position
= (int) (k
% n
);
343 if (position
< 0) position
= -position
;
345 if (!hash_table
[position
])
346 return (wxObject
*) NULL
;
349 wxNode
*node
= hash_table
[position
]->Find (k
);
352 wxObject
*data
= node
->Data ();
358 return (wxObject
*) NULL
;
362 wxObject
*wxHashTable::Delete (const wxChar
*key
)
364 int position
= (int) (MakeKey (key
) % n
);
365 if (position
< 0) position
= -position
;
367 if (!hash_table
[position
])
368 return (wxObject
*) NULL
;
371 wxNode
*node
= hash_table
[position
]->Find (key
);
374 wxObject
*data
= node
->Data ();
380 return (wxObject
*) NULL
;
384 wxObject
*wxHashTable::Delete (long key
, int value
)
389 int position
= (int) (k
% n
);
390 if (position
< 0) position
= -position
;
392 if (!hash_table
[position
])
393 return (wxObject
*) NULL
;
396 wxNode
*node
= hash_table
[position
]->Find (value
);
399 wxObject
*data
= node
->Data ();
405 return (wxObject
*) NULL
;
409 wxObject
*wxHashTable::Delete (long key
, const wxChar
*value
)
411 int position
= (int) (key
% n
);
412 if (position
< 0) position
= -position
;
414 if (!hash_table
[position
])
415 return (wxObject
*) NULL
;
418 wxNode
*node
= hash_table
[position
]->Find (value
);
421 wxObject
*data
= node
->Data ();
427 return (wxObject
*) NULL
;
431 long wxHashTable::MakeKey (const wxChar
*string
) const
436 int_key
+= (wxUChar
) *string
++;
441 void wxHashTable::BeginFind ()
443 current_position
= -1;
444 current_node
= (wxNode
*) NULL
;
447 wxNode
*wxHashTable::Next ()
449 wxNode
*found
= (wxNode
*) NULL
;
451 while (!end
&& !found
)
456 if (current_position
>= n
)
458 current_position
= -1;
459 current_node
= (wxNode
*) NULL
;
464 if (hash_table
[current_position
])
466 current_node
= hash_table
[current_position
]->First ();
467 found
= current_node
;
473 current_node
= current_node
->Next ();
474 found
= current_node
;
480 void wxHashTable::DeleteContents (bool flag
)
483 m_deleteContents
= flag
;
484 for (i
= 0; i
< n
; i
++)
487 hash_table
[i
]->DeleteContents (flag
);
491 void wxHashTable::Clear ()
494 for (i
= 0; i
< n
; i
++)
497 hash_table
[i
]->Clear ();