HASH.H

//////////////////////////////////////////////////////////////////////////////// 
// hash.h
//
// Copyright (C) 1987-1997 By Microsoft Corp. All rights reserved.
// Copyright (C) 1997 Metawise Computing, Inc. All rights reserved.
//
////////////////////////////////////////////////////////////////////////////////


#ifndef _HASH_H_
#define _HASH_H_

class CHashItem
{
public:
CHashItem (void* pData);

void* m_pData;
CHashItem* m_pNext;
};

typedef CHashItem* PCHashItem;

// A hash table which uses a DWORD as its key.
// A generic pointer can be associated with the key.
class CDWordHashItem : public CHashItem
{
public:
CDWordHashItem (DWORD dwKey, void* pData);

DWORD m_dwKey;
};

class CStringHashItem : public CHashItem
{
public:
CStringHashItem (LPCTSTR lpszKey, void* pData);

CString m_strKey;
};

typedef struct
{
int nIndex;
CHashItem* pItem;

} HashEnumContext;


class CHashTable
{
public:
CHashTable (int nSize);
~CHashTable ();

// Operations
BOOL LookUp (DWORD dwKey, void** ppData);
BOOL Insert (DWORD dwKey, void* pData);
void* Delete (DWORD dwKey);

BOOL LookUp (LPCTSTR lpszKey, void** ppData);
BOOL Insert (LPCTSTR lpszKey, void* pData);
void* Delete (LPCTSTR lpszKey);

// methods to enumerate all items in hash table;
// if items are deleted in the middle of enumeration,
// these functions may cause crash!!!
DWORD FindFirst (LPTSTR* ppszKey, void** ppData);
BOOL FindNext (DWORD dwHandle, LPTSTR* ppszKey, void** ppData);
void FindClose (DWORD dwHandle);

protected:
// Implementations
void InsertItem (CHashItem* pItem, CHashItem** ppItems);

int Hash (DWORD dwKey);
CHashItem* FindItem (DWORD dwKey, CDWordHashItem** ppItems);
CHashItem* DeleteItem (DWORD dwKey, CDWordHashItem** ppItems);

int Hash (LPCTSTR lpszKey);
CHashItem* FindItem (LPCTSTR lpszKey, CStringHashItem** ppItems);
CHashItem* DeleteItem (LPCTSTR lpszKey, CStringHashItem** ppItems);

int m_nSize;// size of array m_ppItems[]
int m_nCount;// #items in hash table
CHashItem** m_ppItems;// array of item pointers
};

#endif // _HASH_H_