/* * File: wb_hash.cc * Purpose: Hash table implementation * Author: Julian Smart * Created: 1993 * Updated: August 1994 * RCS_ID: $Id: wb_hash.cc,v 1.3 1994/08/14 21:34:01 edz Exp $ * Copyright: (c) 1993, AIAI, University of Edinburgh */ /* static const char sccsid[] = "@(#)wb_hash.cc 1.2 5/9/94"; */ #ifdef __GNUG__ #pragma implementation "wx_hash.h" #endif #include "stdafx.h" #include "wx_list.h" #include "wx_hash.h" #include #include #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif IMPLEMENT_DYNAMIC(wxHashTable, CObject) wxHashTable::wxHashTable (int the_key_type, int size) { n = size; current_position = -1; current_node = NULL; key_type = the_key_type; hash_table = new wxList *[size]; int i; for (i = 0; i < size; i++) hash_table[i] = NULL; } wxHashTable::~wxHashTable (void) { int i; for (i = 0; i < n; i++) if (hash_table[i]) delete hash_table[i]; delete[] hash_table; } BOOL wxHashTable::Create(int the_key_type, int size) { n = size; current_position = -1; current_node = 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; return TRUE; } void wxHashTable::Put (long key, long value, CObject * object) { // Should NEVER be if (key < 0) key = -key; int position = (int) (key % n); if (!hash_table[position]) hash_table[position] = new wxList (wxKEY_INTEGER); hash_table[position]->Append (value, object); } void wxHashTable::Put (long key, char *value, CObject * object) { // Should NEVER be if (key < 0) key = -key; int position = (int) (key % n); if (!hash_table[position]) hash_table[position] = new wxList (wxKEY_INTEGER); hash_table[position]->Append (value, object); } void wxHashTable::Put (long key, CObject * object) { // Should NEVER be if (key < 0) key = -key; int position = (int) (key % n); if (!hash_table[position]) hash_table[position] = new wxList (wxKEY_INTEGER); hash_table[position]->Append (key, object); } void wxHashTable::Put (const char *key, CObject * object) { int position = (int) (MakeKey (key) % n); if (!hash_table[position]) hash_table[position] = new wxList (wxKEY_STRING); hash_table[position]->Append (key, object); } CObject *wxHashTable::Get (long key, long value) { // Should NEVER be if (key < 0) key = -key; int position = (int) (key % n); if (!hash_table[position]) return NULL; else { wxNode *node = hash_table[position]->Find (value); if (node) return node->Data (); else return NULL; } } CObject *wxHashTable::Get (long key, char *value) { // Should NEVER be if (key < 0) key = -key; int position = (int) (key % n); if (!hash_table[position]) return NULL; else { wxNode *node = hash_table[position]->Find (value); if (node) return node->Data (); else return NULL; } } CObject *wxHashTable::Get (long key) { // Should NEVER be if (key < 0) key = -key; int position = (int) (key % n); if (!hash_table[position]) return NULL; else { wxNode *node = hash_table[position]->Find (key); return node ? node->Data () : NULL; } } CObject *wxHashTable::Get (const char *key) { int position = (int) (MakeKey (key) % n); if (!hash_table[position]) return NULL; else { wxNode *node = hash_table[position]->Find (key); return node ? node->Data () : NULL; } } CObject *wxHashTable::Delete (long key) { // Should NEVER be if (key < 0) key = -key; int position = (int) (key % n); if (!hash_table[position]) return NULL; else { wxNode *node = hash_table[position]->Find (key); if (node) { CObject *data = node->Data (); delete node; return data; } else return NULL; } } CObject *wxHashTable::Delete (const char *key) { int position = (int) (MakeKey (key) % n); if (!hash_table[position]) return NULL; else { wxNode *node = hash_table[position]->Find (key); if (node) { CObject *data = node->Data (); delete node; return data; } else return NULL; } } CObject *wxHashTable::Delete (long key, int value) { // Should NEVER be if (key < 0) key = -key; int position = (int) (key % n); if (!hash_table[position]) return NULL; else { wxNode *node = hash_table[position]->Find (value); if (node) { CObject *data = node->Data (); delete node; return data; } else return NULL; } } CObject *wxHashTable::Delete (long key, char *value) { /* // Should NEVER be if (key < 0) key = -key; */ int position = (int) (key % n); if (!hash_table[position]) return NULL; else { wxNode *node = hash_table[position]->Find (value); if (node) { CObject *data = node->Data (); delete node; return data; } else return NULL; } } long wxHashTable::MakeKey (const char *string) { long int_key = 0; while (*string) int_key += (unsigned char) *string++; /* // Don't need this since int_key >= 0) if (int_key < 0) int_key = -int_key; */ return int_key; } void wxHashTable::BeginFind (void) { current_position = -1; current_node = NULL; } wxNode *wxHashTable::Next (void) { wxNode *found = NULL; BOOL end = FALSE; while (!end && !found) { if (!current_node) { current_position++; if (current_position >= n) { current_position = -1; current_node = NULL; end = TRUE; } else { if (hash_table[current_position]) { current_node = hash_table[current_position]->First (); found = current_node; } } } else { current_node = current_node->Next (); found = current_node; } } return found; } void wxHashTable::DeleteContents (BOOL flag) { int i; for (i = 0; i < n; i++) { if (hash_table[i]) hash_table[i]->DeleteContents (flag); } } void wxHashTable::Clear (void) { int i; for (i = 0; i < n; i++) { if (hash_table[i]) hash_table[i]->Clear (); } }