]>
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((int)(key
% (long)m_hashSize
));
110 if ( m_hashTable
[slot
] )
112 node
= m_hashTable
[slot
]->Find(wxListKey(value
));
116 node
= (wxNodeBase
*)NULL
;
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 wxHashTableLong::~wxHashTableLong()
131 void wxHashTableLong::Init(size_t size
)
134 m_values
= new wxArrayLong
*[size
];
135 m_keys
= new wxArrayLong
*[size
];
137 for ( size_t n
= 0; n
< m_hashSize
; n
++ )
140 m_keys
[n
] = (wxArrayLong
*)NULL
;
146 void wxHashTableLong::Create(size_t size
)
151 void wxHashTableLong::Destroy()
153 for ( size_t n
= 0; n
< m_hashSize
; n
++ )
165 void wxHashTableLong::Put(long key
, long value
)
167 wxCHECK_RET( m_hashSize
, _T("must call Create() first") );
169 size_t slot
= (size_t)abs((int)(key
% (long)m_hashSize
));
173 m_keys
[slot
] = new wxArrayLong
;
174 m_values
[slot
] = new wxArrayLong
;
177 m_keys
[slot
]->Add(key
);
178 m_values
[slot
]->Add(value
);
183 long wxHashTableLong::Get(long key
) const
185 wxCHECK_MSG( m_hashSize
, wxNOT_FOUND
, _T("must call Create() first") );
187 size_t slot
= (size_t)abs((int)(key
% (long)m_hashSize
));
189 wxArrayLong
*keys
= m_keys
[slot
];
192 size_t count
= keys
->GetCount();
193 for ( size_t n
= 0; n
< count
; n
++ )
195 if ( keys
->Item(n
) == key
)
197 return m_values
[slot
]->Item(n
);
205 long wxHashTableLong::Delete(long key
)
207 wxCHECK_MSG( m_hashSize
, wxNOT_FOUND
, _T("must call Create() first") );
209 size_t slot
= (size_t)abs((int)(key
% (long)m_hashSize
));
211 wxArrayLong
*keys
= m_keys
[slot
];
214 size_t count
= keys
->GetCount();
215 for ( size_t n
= 0; n
< count
; n
++ )
217 if ( keys
->Item(n
) == key
)
219 long val
= m_values
[slot
]->Item(n
);
222 m_values
[slot
]->RemoveAt(n
);
234 // ----------------------------------------------------------------------------
235 // wxStringHashTable: more efficient than storing strings in a list
236 // ----------------------------------------------------------------------------
238 wxStringHashTable::wxStringHashTable(size_t sizeTable
)
240 m_keys
= new wxArrayLong
*[sizeTable
];
241 m_values
= new wxArrayString
*[sizeTable
];
243 m_hashSize
= sizeTable
;
244 for ( size_t n
= 0; n
< m_hashSize
; n
++ )
246 m_values
[n
] = (wxArrayString
*)NULL
;
247 m_keys
[n
] = (wxArrayLong
*)NULL
;
251 wxStringHashTable::~wxStringHashTable()
256 void wxStringHashTable::Destroy()
258 for ( size_t n
= 0; n
< m_hashSize
; n
++ )
269 void wxStringHashTable::Put(long key
, const wxString
& value
)
271 wxCHECK_RET( m_hashSize
, _T("must call Create() first") );
273 size_t slot
= (size_t)abs((int)(key
% (long)m_hashSize
));
277 m_keys
[slot
] = new wxArrayLong
;
278 m_values
[slot
] = new wxArrayString
;
281 m_keys
[slot
]->Add(key
);
282 m_values
[slot
]->Add(value
);
285 wxString
wxStringHashTable::Get(long key
, bool *wasFound
) const
287 wxCHECK_MSG( m_hashSize
, _T(""), _T("must call Create() first") );
289 size_t slot
= (size_t)abs((int)(key
% (long)m_hashSize
));
291 wxArrayLong
*keys
= m_keys
[slot
];
294 size_t count
= keys
->GetCount();
295 for ( size_t n
= 0; n
< count
; n
++ )
297 if ( keys
->Item(n
) == key
)
302 return m_values
[slot
]->Item(n
);
313 // ----------------------------------------------------------------------------
314 // old not type safe wxHashTable
315 // ----------------------------------------------------------------------------
317 wxHashTable::wxHashTable (int the_key_type
, int size
)
320 hash_table
= (wxList
**) NULL
;
321 Create(the_key_type
, size
);
323 m_deleteContents
= FALSE
;
326 current_position = -1;
327 current_node = (wxNode *) NULL;
329 key_type = the_key_type;
330 hash_table = new wxList *[size];
332 for (i = 0; i < size; i++)
333 hash_table[i] = (wxList *) NULL;
337 wxHashTable::~wxHashTable ()
342 void wxHashTable::Destroy()
344 if (!hash_table
) return;
346 for (i
= 0; i
< n
; i
++)
348 delete hash_table
[i
];
353 bool wxHashTable::Create(int the_key_type
, int size
)
358 current_position
= -1;
359 current_node
= (wxNode
*) NULL
;
361 key_type
= the_key_type
;
362 hash_table
= new wxList
*[size
];
364 for (i
= 0; i
< size
; i
++)
365 hash_table
[i
] = (wxList
*) NULL
;
370 void wxHashTable::DoCopy(const wxHashTable
& table
)
373 m_count
= table
.m_count
;
374 current_position
= table
.current_position
;
375 current_node
= NULL
; // doesn't matter - Next() will reconstruct it
376 key_type
= table
.key_type
;
378 hash_table
= new wxList
*[n
];
379 for (int i
= 0; i
< n
; i
++) {
380 if (table
.hash_table
[i
] == NULL
)
381 hash_table
[i
] = NULL
;
383 hash_table
[i
] = new wxList(key_type
);
384 *(hash_table
[i
]) = *(table
.hash_table
[i
]);
389 void wxHashTable::Put (long key
, long value
, wxObject
* object
)
394 int position
= (int) (k
% n
);
395 if (position
< 0) position
= -position
;
397 if (!hash_table
[position
])
399 hash_table
[position
] = new wxList (wxKEY_INTEGER
);
400 if (m_deleteContents
) hash_table
[position
]->DeleteContents(TRUE
);
403 hash_table
[position
]->Append (value
, object
);
407 void wxHashTable::Put (long key
, const wxChar
*value
, wxObject
* object
)
412 int position
= (int) (k
% n
);
413 if (position
< 0) position
= -position
;
415 if (!hash_table
[position
])
417 hash_table
[position
] = new wxList (wxKEY_STRING
);
418 if (m_deleteContents
) hash_table
[position
]->DeleteContents(TRUE
);
421 hash_table
[position
]->Append (value
, object
);
425 void wxHashTable::Put (long key
, wxObject
* object
)
430 int position
= (int) (k
% n
);
431 if (position
< 0) position
= -position
;
433 if (!hash_table
[position
])
435 hash_table
[position
] = new wxList (wxKEY_INTEGER
);
436 if (m_deleteContents
) hash_table
[position
]->DeleteContents(TRUE
);
439 hash_table
[position
]->Append (k
, object
);
443 void wxHashTable::Put (const wxChar
*key
, wxObject
* object
)
445 int position
= (int) (MakeKey (key
) % n
);
446 if (position
< 0) position
= -position
;
448 if (!hash_table
[position
])
450 hash_table
[position
] = new wxList (wxKEY_STRING
);
451 if (m_deleteContents
) hash_table
[position
]->DeleteContents(TRUE
);
454 hash_table
[position
]->Append (key
, object
);
458 wxObject
*wxHashTable::Get (long key
, long value
) const
463 int position
= (int) (k
% n
);
464 if (position
< 0) position
= -position
;
466 if (!hash_table
[position
])
467 return (wxObject
*) NULL
;
470 wxNode
*node
= hash_table
[position
]->Find (value
);
472 return node
->Data ();
474 return (wxObject
*) NULL
;
478 wxObject
*wxHashTable::Get (long key
, const wxChar
*value
) const
483 int position
= (int) (k
% n
);
484 if (position
< 0) position
= -position
;
486 if (!hash_table
[position
])
487 return (wxObject
*) NULL
;
490 wxNode
*node
= hash_table
[position
]->Find (value
);
492 return node
->Data ();
494 return (wxObject
*) NULL
;
498 wxObject
*wxHashTable::Get (long key
) const
503 int position
= (int) (k
% n
);
504 if (position
< 0) position
= -position
;
506 if (!hash_table
[position
])
507 return (wxObject
*) NULL
;
510 wxNode
*node
= hash_table
[position
]->Find (k
);
511 return node
? node
->Data () : (wxObject
*)NULL
;
515 wxObject
*wxHashTable::Get (const wxChar
*key
) const
517 int position
= (int) (MakeKey (key
) % n
);
518 if (position
< 0) position
= -position
;
520 if (!hash_table
[position
])
521 return (wxObject
*) NULL
;
524 wxNode
*node
= hash_table
[position
]->Find (key
);
525 return node
? node
->Data () : (wxObject
*)NULL
;
529 wxObject
*wxHashTable::Delete (long key
)
534 int position
= (int) (k
% n
);
535 if (position
< 0) position
= -position
;
537 if (!hash_table
[position
])
538 return (wxObject
*) NULL
;
541 wxNode
*node
= hash_table
[position
]->Find (k
);
544 wxObject
*data
= node
->Data ();
550 return (wxObject
*) NULL
;
554 wxObject
*wxHashTable::Delete (const wxChar
*key
)
556 int position
= (int) (MakeKey (key
) % n
);
557 if (position
< 0) position
= -position
;
559 if (!hash_table
[position
])
560 return (wxObject
*) NULL
;
563 wxNode
*node
= hash_table
[position
]->Find (key
);
566 wxObject
*data
= node
->Data ();
572 return (wxObject
*) NULL
;
576 wxObject
*wxHashTable::Delete (long key
, int value
)
581 int position
= (int) (k
% n
);
582 if (position
< 0) position
= -position
;
584 if (!hash_table
[position
])
585 return (wxObject
*) NULL
;
588 wxNode
*node
= hash_table
[position
]->Find (value
);
591 wxObject
*data
= node
->Data ();
597 return (wxObject
*) NULL
;
601 wxObject
*wxHashTable::Delete (long key
, const wxChar
*value
)
603 int position
= (int) (key
% n
);
604 if (position
< 0) position
= -position
;
606 if (!hash_table
[position
])
607 return (wxObject
*) NULL
;
610 wxNode
*node
= hash_table
[position
]->Find (value
);
613 wxObject
*data
= node
->Data ();
619 return (wxObject
*) NULL
;
623 long wxHashTable::MakeKey (const wxChar
*string
) const
628 int_key
+= (wxUChar
) *string
++;
633 void wxHashTable::BeginFind ()
635 current_position
= -1;
636 current_node
= (wxNode
*) NULL
;
639 wxNode
*wxHashTable::Next ()
641 wxNode
*found
= (wxNode
*) NULL
;
643 while (!end
&& !found
)
648 if (current_position
>= n
)
650 current_position
= -1;
651 current_node
= (wxNode
*) NULL
;
656 if (hash_table
[current_position
])
658 current_node
= hash_table
[current_position
]->First ();
659 found
= current_node
;
665 current_node
= current_node
->Next ();
666 found
= current_node
;
672 void wxHashTable::DeleteContents (bool flag
)
675 m_deleteContents
= flag
;
676 for (i
= 0; i
< n
; i
++)
679 hash_table
[i
]->DeleteContents (flag
);
683 void wxHashTable::Clear ()
688 for (i
= 0; i
< n
; i
++)
691 hash_table
[i
]->Clear ();