WX_DECLARE_HASH() macro for type safe hashes

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@6292 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2000-02-25 19:23:11 +00:00
parent 794bcc2dea
commit bcaa23de09
5 changed files with 354 additions and 148 deletions

View File

@@ -1,24 +1,26 @@
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
// Name: hash.h // Name: wx/hash.h
// Purpose: wxHashTable class // Purpose: wxHashTable class
// Author: Julian Smart // Author: Julian Smart
// Modified by: // Modified by: VZ at 25.02.00: type safe hashes with WX_DECLARE_HASH()
// Created: 01/02/97 // Created: 01/02/97
// RCS-ID: $Id$ // RCS-ID: $Id$
// Copyright: (c) // Copyright: (c)
// Licence: wxWindows licence // Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
#ifndef _WX_WXHASHH__ #ifndef _WX_HASH_H__
#define _WX_WXHASHH__ #define _WX_HASH_H__
#ifdef __GNUG__ #ifdef __GNUG__
#pragma interface "hash.h" #pragma interface "hash.h"
#endif #endif
#include "wx/object.h"
#include "wx/list.h" #include "wx/list.h"
// the default size of the hash
#define wxHASH_SIZE_DEFAULT (1000)
/* /*
* A hash table is an array of user-definable size with lists * A hash table is an array of user-definable size with lists
* of data items hanging off the array positions. Usually there'll * of data items hanging off the array positions. Usually there'll
@@ -26,11 +28,56 @@
* the list to find the desired item. * the list to find the desired item.
*/ */
class WXDLLEXPORT wxHashTable: public wxObject // ----------------------------------------------------------------------------
{ // this is the base class for object hashes: hash tables which contain
DECLARE_DYNAMIC_CLASS(wxHashTable) // pointers to objects
// ----------------------------------------------------------------------------
public: class WXDLLEXPORT wxHashTableBase : public wxObject
{
public:
wxHashTableBase();
void Create(wxKeyType keyType = wxKEY_INTEGER,
size_t size = wxHASH_SIZE_DEFAULT);
void Destroy();
size_t GetSize() const { return m_hashSize; }
size_t GetCount() const { return m_count; }
void DeleteContents(bool flag);
protected:
// find the node for (key, value)
wxNodeBase *GetNode(long key, long value) const;
// the array of lists in which we store the values for given key hash
wxListBase **m_hashTable;
// the size of m_lists array
size_t m_hashSize;
// the type of indexing we use
wxKeyType m_keyType;
// the total number of elements in the hash
size_t m_count;
// should we delete our data?
bool m_deleteContents;
private:
// no copy ctor/assignment operator (yet)
DECLARE_NO_COPY_CLASS(wxHashTableBase);
};
// ----------------------------------------------------------------------------
// for compatibility only
// ----------------------------------------------------------------------------
class WXDLLEXPORT wxHashTable : public wxObject
{
public:
int n; int n;
int current_position; int current_position;
wxNode *current_node; wxNode *current_node;
@@ -38,17 +85,21 @@ class WXDLLEXPORT wxHashTable: public wxObject
unsigned int key_type; unsigned int key_type;
wxList **hash_table; wxList **hash_table;
wxHashTable(int the_key_type = wxKEY_INTEGER, int size = 1000); wxHashTable(int the_key_type = wxKEY_INTEGER,
~wxHashTable(void); int size = wxHASH_SIZE_DEFAULT);
~wxHashTable();
// copy ctor and assignment operator // copy ctor and assignment operator
wxHashTable(const wxHashTable& table) { DoCopy(table); } wxHashTable(const wxHashTable& table) { DoCopy(table); }
wxHashTable& operator=(const wxHashTable& table) { Clear(); DoCopy(table); return *this; } wxHashTable& operator=(const wxHashTable& table)
{ Clear(); DoCopy(table); return *this; }
void DoCopy(const wxHashTable& table); void DoCopy(const wxHashTable& table);
void Destroy(void); // Robert Roebling void Destroy();
bool Create(int the_key_type = wxKEY_INTEGER, int size = 1000); bool Create(int the_key_type = wxKEY_INTEGER,
int size = wxHASH_SIZE_DEFAULT);
// Note that there are 2 forms of Put, Get. // Note that there are 2 forms of Put, Get.
// With a key and a value, the *value* will be checked // With a key and a value, the *value* will be checked
@@ -93,19 +144,81 @@ class WXDLLEXPORT wxHashTable: public wxObject
// Not necessary, of course, if you're only storing pointers to // Not necessary, of course, if you're only storing pointers to
// objects maintained separately // objects maintained separately
void BeginFind(void); void BeginFind();
wxNode *Next(void); wxNode *Next();
void DeleteContents(bool flag); void DeleteContents(bool flag);
void Clear(void); void Clear();
// Returns number of nodes // Returns number of nodes
size_t GetCount() const { return m_count; } size_t GetCount() const { return m_count; }
private: private:
size_t m_count; // number of elements in the hashtable size_t m_count; // number of elements in the hashtable
bool m_deleteContents; bool m_deleteContents;
DECLARE_DYNAMIC_CLASS(wxHashTable)
}; };
// defines a new type safe hash table which stores the elements of type eltype
// in lists of class listclass
#define WX_DECLARE_HASH(eltype, listclass, hashclass) \
class WXDLLEXPORT hashclass : public wxHashTableBase \
{ \
public: \
hashclass(wxKeyType keyType = wxKEY_INTEGER, \
size_t size = wxHASH_SIZE_DEFAULT) \
{ Create(keyType, size); } \
\
~hashclass() { Destroy(); } \
\
void Put(long key, long val, eltype *data) { DoPut(key, val, data); } \
void Put(long key, eltype *data) { DoPut(key, key, data); } \
\
eltype *Get(long key, long value) const \
{ \
wxNodeBase *node = GetNode(key, value); \
return node ? ((listclass::Node *)node)->GetData() : (eltype *)0; \
} \
eltype *Get(long key) const { return Get(key, key); } \
\
eltype *Delete(long key, long value) \
{ \
eltype *data; \
\
wxNodeBase *node = GetNode(key, value); \
if ( node ) \
{ \
data = ((listclass::Node *)node)->GetData(); \
\
delete node; \
m_count--; \
} \
else \
{ \
data = (eltype *)0; \
} \
\
return data; \
} \
eltype *Delete(long key) { return Delete(key, key); } \
\
protected: \
void DoPut(long key, long value, eltype *data) \
{ \
size_t slot = (size_t)abs(key % m_hashSize); \
\
if ( !m_hashTable[slot] ) \
{ \
m_hashTable[slot] = new listclass(m_keyType); \
if ( m_deleteContents ) \
m_hashTable[slot]->DeleteContents(TRUE); \
} \
\
((listclass *)m_hashTable[slot])->Append(value, data); \
m_count++; \
} \
}
#endif #endif
// _WX_WXHASHH__ // _WX_HASH_H__

View File

@@ -187,6 +187,7 @@ private:
class WXDLLEXPORT wxListBase : public wxObject class WXDLLEXPORT wxListBase : public wxObject
{ {
friend class wxNodeBase; // should be able to call DetachNode() friend class wxNodeBase; // should be able to call DetachNode()
friend class wxHashTableBase; // should be able to call untyped Find()
public: public:
// default ctor & dtor // default ctor & dtor
wxListBase(wxKeyType keyType = wxKEY_NONE) { Init(keyType); } wxListBase(wxKeyType keyType = wxKEY_NONE) { Init(keyType); }

View File

@@ -2,13 +2,21 @@
// Name: hash.cpp // Name: hash.cpp
// Purpose: wxHashTable implementation // Purpose: wxHashTable implementation
// Author: Julian Smart // Author: Julian Smart
// Modified by: // Modified by: VZ at 25.02.00: type safe hashes with WX_DECLARE_HASH()
// Created: 01/02/97 // Created: 01/02/97
// RCS-ID: $Id$ // RCS-ID: $Id$
// Copyright: (c) Julian Smart and Markus Holzem // Copyright: (c) Julian Smart and Markus Holzem
// Licence: wxWindows licence // Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#ifdef __GNUG__ #ifdef __GNUG__
#pragma implementation "hash.h" #pragma implementation "hash.h"
#endif #endif
@@ -29,8 +37,92 @@
#include <string.h> #include <string.h>
#include <stdarg.h> #include <stdarg.h>
// ----------------------------------------------------------------------------
// wxWin macros
// ----------------------------------------------------------------------------
IMPLEMENT_DYNAMIC_CLASS(wxHashTable, wxObject) IMPLEMENT_DYNAMIC_CLASS(wxHashTable, wxObject)
// ============================================================================
// implementation
// ============================================================================
// ----------------------------------------------------------------------------
// wxHashTablleBase for working with "void *" data
// ----------------------------------------------------------------------------
wxHashTableBase::wxHashTableBase()
{
m_deleteContents = FALSE;
m_hashTable = (wxListBase **)NULL;
m_hashSize = 0;
m_count = 0;
m_keyType = wxKEY_NONE;
}
void wxHashTableBase::Create(wxKeyType keyType, size_t size)
{
Destroy();
m_hashSize = size;
m_keyType = keyType;
m_hashTable = new wxListBase *[size];
for ( size_t n = 0; n < m_hashSize; n++ )
{
m_hashTable[n] = (wxListBase *) NULL;
}
}
void wxHashTableBase::Destroy()
{
if ( m_hashTable )
{
for ( size_t n = 0; n < m_hashSize; n++ )
{
delete m_hashTable[n];
}
delete [] m_hashTable;
m_hashTable = (wxListBase **)NULL;
m_count = 0;
}
}
void wxHashTableBase::DeleteContents(bool flag)
{
m_deleteContents = flag;
for ( size_t n = 0; n < m_hashSize; n++ )
{
if ( m_hashTable[n] )
{
m_hashTable[n]->DeleteContents(flag);
}
}
}
wxNodeBase *wxHashTableBase::GetNode(long key, long value) const
{
size_t slot = (size_t)abs(key % m_hashSize);
wxNodeBase *node;
if ( m_hashTable[slot] )
{
node = m_hashTable[slot]->Find(wxListKey(value));
}
else
{
node = (wxNodeBase *)NULL;
}
return node;
}
// ----------------------------------------------------------------------------
// old not type safe wxHashTable
// ----------------------------------------------------------------------------
wxHashTable::wxHashTable (int the_key_type, int size) wxHashTable::wxHashTable (int the_key_type, int size)
{ {
n = 0; n = 0;
@@ -51,12 +143,12 @@ wxHashTable::wxHashTable (int the_key_type, int size)
*/ */
} }
wxHashTable::~wxHashTable (void) wxHashTable::~wxHashTable ()
{ {
Destroy(); Destroy();
} }
void wxHashTable::Destroy(void) void wxHashTable::Destroy()
{ {
if (!hash_table) return; if (!hash_table) return;
int i; int i;
@@ -346,13 +438,13 @@ long wxHashTable::MakeKey (const wxChar *string) const
return int_key; return int_key;
} }
void wxHashTable::BeginFind (void) void wxHashTable::BeginFind ()
{ {
current_position = -1; current_position = -1;
current_node = (wxNode *) NULL; current_node = (wxNode *) NULL;
} }
wxNode *wxHashTable::Next (void) wxNode *wxHashTable::Next ()
{ {
wxNode *found = (wxNode *) NULL; wxNode *found = (wxNode *) NULL;
bool end = FALSE; bool end = FALSE;
@@ -396,7 +488,7 @@ void wxHashTable::DeleteContents (bool flag)
} }
} }
void wxHashTable::Clear (void) void wxHashTable::Clear ()
{ {
int i; int i;
for (i = 0; i < n; i++) for (i = 0; i < n; i++)