X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/c801d85f158c4cba50b588807daabdcbd0ed3853..c6e62f74fcac5b62889b59e7ce7a41d0ce884d7b:/src/common/hash.cpp?ds=sidebyside diff --git a/src/common/hash.cpp b/src/common/hash.cpp index 1aba64563c..4c6ad97c6d 100644 --- a/src/common/hash.cpp +++ b/src/common/hash.cpp @@ -29,49 +29,79 @@ #include #include -#if !USE_SHARED_LIBRARY IMPLEMENT_DYNAMIC_CLASS(wxHashTable, wxObject) -#endif -wxHashTable::wxHashTable (const int the_key_type, const int size) +wxHashTable::wxHashTable (int the_key_type, int size) { + n = 0; + hash_table = (wxList**) NULL; + Create(the_key_type, size); + m_count = 0; +/* n = size; current_position = -1; - current_node = NULL; + current_node = (wxNode *) NULL; key_type = the_key_type; hash_table = new wxList *[size]; int i; for (i = 0; i < size; i++) - hash_table[i] = NULL; + hash_table[i] = (wxList *) NULL; +*/ } wxHashTable::~wxHashTable (void) { + Destroy(); +} + +void wxHashTable::Destroy(void) +{ + if (!hash_table) return; int i; for (i = 0; i < n; i++) if (hash_table[i]) delete hash_table[i]; delete[] hash_table; + hash_table = NULL; } -bool wxHashTable::Create(const int the_key_type, const int size) +bool wxHashTable::Create(int the_key_type, int size) { + Destroy(); + n = size; current_position = -1; - current_node = NULL; + current_node = (wxNode *) NULL; key_type = the_key_type; - if (hash_table) - delete[] hash_table; hash_table = new wxList *[size]; int i; for (i = 0; i < size; i++) - hash_table[i] = NULL; + hash_table[i] = (wxList *) NULL; return TRUE; } -void wxHashTable::Put (const long key, const long value, wxObject * object) + +void wxHashTable::DoCopy(const wxHashTable& table) +{ + n = table.n; + current_position = table.current_position; + current_node = NULL; // doesn't matter - Next() will reconstruct it + key_type = table.key_type; + + hash_table = new wxList *[n]; + for (int i = 0; i < n; i++) { + if (table.hash_table[i] == NULL) + hash_table[i] = NULL; + else { + hash_table[i] = new wxList(key_type); + *(hash_table[i]) = *(table.hash_table[i]); + } + } +} + +void wxHashTable::Put (long key, long value, wxObject * object) { // Should NEVER be long k = (long) key; @@ -83,9 +113,10 @@ void wxHashTable::Put (const long key, const long value, wxObject * object) hash_table[position] = new wxList (wxKEY_INTEGER); hash_table[position]->Append (value, object); + m_count++; } -void wxHashTable::Put (const long key, const char *value, wxObject * object) +void wxHashTable::Put (long key, const wxChar *value, wxObject * object) { // Should NEVER be long k = (long) key; @@ -97,9 +128,10 @@ void wxHashTable::Put (const long key, const char *value, wxObject * object) hash_table[position] = new wxList (wxKEY_INTEGER); hash_table[position]->Append (value, object); + m_count++; } -void wxHashTable::Put (const long key, wxObject * object) +void wxHashTable::Put (long key, wxObject * object) { // Should NEVER be long k = (long) key; @@ -111,9 +143,10 @@ void wxHashTable::Put (const long key, wxObject * object) hash_table[position] = new wxList (wxKEY_INTEGER); hash_table[position]->Append (k, object); + m_count++; } -void wxHashTable::Put (const char *key, wxObject * object) +void wxHashTable::Put (const wxChar *key, wxObject * object) { int position = (int) (MakeKey (key) % n); @@ -121,9 +154,10 @@ void wxHashTable::Put (const char *key, wxObject * object) hash_table[position] = new wxList (wxKEY_STRING); hash_table[position]->Append (key, object); + m_count++; } -wxObject *wxHashTable::Get (const long key, const long value) const +wxObject *wxHashTable::Get (long key, long value) const { // Should NEVER be long k = (long) key; @@ -132,18 +166,18 @@ wxObject *wxHashTable::Get (const long key, const long value) const int position = (int) (k % n); if (!hash_table[position]) - return NULL; + return (wxObject *) NULL; else { wxNode *node = hash_table[position]->Find (value); if (node) return node->Data (); else - return NULL; + return (wxObject *) NULL; } } -wxObject *wxHashTable::Get (const long key, const char *value) const +wxObject *wxHashTable::Get (long key, const wxChar *value) const { // Should NEVER be long k = (long) key; @@ -152,18 +186,18 @@ wxObject *wxHashTable::Get (const long key, const char *value) const int position = (int) (k % n); if (!hash_table[position]) - return NULL; + return (wxObject *) NULL; else { wxNode *node = hash_table[position]->Find (value); if (node) return node->Data (); else - return NULL; + return (wxObject *) NULL; } } -wxObject *wxHashTable::Get (const long key) const +wxObject *wxHashTable::Get (long key) const { // Should NEVER be long k = (long) key; @@ -172,7 +206,7 @@ wxObject *wxHashTable::Get (const long key) const int position = (int) (k % n); if (!hash_table[position]) - return NULL; + return (wxObject *) NULL; else { wxNode *node = hash_table[position]->Find (k); @@ -180,12 +214,12 @@ wxObject *wxHashTable::Get (const long key) const } } -wxObject *wxHashTable::Get (const char *key) const +wxObject *wxHashTable::Get (const wxChar *key) const { int position = (int) (MakeKey (key) % n); if (!hash_table[position]) - return NULL; + return (wxObject *) NULL; else { wxNode *node = hash_table[position]->Find (key); @@ -193,7 +227,7 @@ wxObject *wxHashTable::Get (const char *key) const } } -wxObject *wxHashTable::Delete (const long key) +wxObject *wxHashTable::Delete (long key) { // Should NEVER be long k = (long) key; @@ -202,7 +236,7 @@ wxObject *wxHashTable::Delete (const long key) int position = (int) (k % n); if (!hash_table[position]) - return NULL; + return (wxObject *) NULL; else { wxNode *node = hash_table[position]->Find (k); @@ -210,18 +244,19 @@ wxObject *wxHashTable::Delete (const long key) { wxObject *data = node->Data (); delete node; + m_count--; return data; } else - return NULL; + return (wxObject *) NULL; } } -wxObject *wxHashTable::Delete (const char *key) +wxObject *wxHashTable::Delete (const wxChar *key) { int position = (int) (MakeKey (key) % n); if (!hash_table[position]) - return NULL; + return (wxObject *) NULL; else { wxNode *node = hash_table[position]->Find (key); @@ -229,14 +264,15 @@ wxObject *wxHashTable::Delete (const char *key) { wxObject *data = node->Data (); delete node; + m_count--; return data; } else - return NULL; + return (wxObject *) NULL; } } -wxObject *wxHashTable::Delete (const long key, const int value) +wxObject *wxHashTable::Delete (long key, int value) { // Should NEVER be long k = (long) key; @@ -245,7 +281,7 @@ wxObject *wxHashTable::Delete (const long key, const int value) int position = (int) (k % n); if (!hash_table[position]) - return NULL; + return (wxObject *) NULL; else { wxNode *node = hash_table[position]->Find (value); @@ -253,18 +289,19 @@ wxObject *wxHashTable::Delete (const long key, const int value) { wxObject *data = node->Data (); delete node; + m_count--; return data; } else - return NULL; + return (wxObject *) NULL; } } -wxObject *wxHashTable::Delete (const long key, const char *value) +wxObject *wxHashTable::Delete (long key, const wxChar *value) { int position = (int) (key % n); if (!hash_table[position]) - return NULL; + return (wxObject *) NULL; else { wxNode *node = hash_table[position]->Find (value); @@ -272,19 +309,20 @@ wxObject *wxHashTable::Delete (const long key, const char *value) { wxObject *data = node->Data (); delete node; + m_count--; return data; } else - return NULL; + return (wxObject *) NULL; } } -long wxHashTable::MakeKey (const char *string) const +long wxHashTable::MakeKey (const wxChar *string) const { long int_key = 0; while (*string) - int_key += (unsigned char) *string++; + int_key += (wxUChar) *string++; return int_key; } @@ -292,12 +330,12 @@ long wxHashTable::MakeKey (const char *string) const void wxHashTable::BeginFind (void) { current_position = -1; - current_node = NULL; + current_node = (wxNode *) NULL; } wxNode *wxHashTable::Next (void) { - wxNode *found = NULL; + wxNode *found = (wxNode *) NULL; bool end = FALSE; while (!end && !found) { @@ -307,7 +345,7 @@ wxNode *wxHashTable::Next (void) if (current_position >= n) { current_position = -1; - current_node = NULL; + current_node = (wxNode *) NULL; end = TRUE; } else @@ -328,7 +366,7 @@ wxNode *wxHashTable::Next (void) return found; } -void wxHashTable::DeleteContents (const bool flag) +void wxHashTable::DeleteContents (bool flag) { int i; for (i = 0; i < n; i++) @@ -346,5 +384,6 @@ void wxHashTable::Clear (void) if (hash_table[i]) hash_table[i]->Clear (); } + m_count = 0; }