xphashtable.h
1/* Distributed under the Apache License, Version 2.0.
2 See accompanying NOTICE file for details.*/
3
4// This file is part of the ObjectFusion cross platform (XP)
5// class library.
6// Copyright (C) 1998 ObjectFusion, L.L.C.
7// All rights reserved.
8
9#ifndef __XPHASHTABLE_H__
10#define __XPHASHTABLE_H__
11
12#include "xparray.h"
13
14typedef void* TXpTableItem;
15
16template <class KEY, class TYPE>
18{
19 protected:
20
21 struct SNode
22 {
23 SNode(const KEY& Key, const TYPE& Value) : m_pNext(NULL), m_Key(Key), m_Value(Value) {}
24
26 KEY m_Key;
27 TYPE m_Value;
28 };
29
31
33 INT_PTR m_nCount;
34 public:
35
36 CXpHashTable(const INT_PTR nHashTableSize, const bool bCalculate = true);
37 virtual ~CXpHashTable();
38
39 inline HRESULT Insert(const KEY& Key, const TYPE& Value);
40
41 inline HRESULT Remove(const KEY& Key);
42 inline void RemoveAll();
43
44 inline INT_PTR GetCount() const;
45
46 inline bool HasKey(const KEY& Key) const;
47 inline TXpTableItem FindKey(const KEY& Key) const;
48 inline bool FindValue(const KEY& Key, TYPE& Value) const;
49
50 inline const KEY& GetKey(const TXpTableItem Item) const;
51
52
53 inline const TYPE& GetValue(const TXpTableItem Item) const;
54 inline TYPE& GetValue(const TXpTableItem Item);
55
56 inline void GetPair(const TXpTableItem Item, KEY& Key, TYPE& Value) const;
57
58 inline TXpTableItem GetFirst() const;
59 inline TXpTableItem GetNext(const TXpTableItem Item) const;
60
61 inline HRESULT Copy(const CXpHashTable& HashTable);
62
63 // Result always gets the value from this table, not tHashTable.
64 inline HRESULT Intersection(const CXpHashTable& HashTable, CXpHashTable& Result);
65 inline HRESULT Union(const CXpHashTable& HashTable, CXpHashTable& Result);
66 // Result contains elements from this table not in HashTable.
67 inline HRESULT Difference(const CXpHashTable& HashTable, CXpHashTable& Result);
68
69 inline bool operator==(const CXpHashTable& HashTable) const;
70
71 protected:
72 // Not implemented, not copyable
74
75 inline INT_PTR GetBucket(const KEY& Key) const;
76
77 bool IsPrime(const INT_PTR nNbr);
78 INT_PTR GetClosestPrime(const INT_PTR nNbr);
79};
80
81template<class KEY>
82inline unsigned long XpHashKey(const KEY& Key)
83{
84 // This works for most integral types (INT_PTR, char, float, etc).
85 return (unsigned long)Key >> 4;
86}
87
88inline unsigned long XpHashKey(const char* pszString);
89inline unsigned long XpHashKey(const wchar_t* pszString);
90
91#ifdef __AFX_H__
92 inline unsigned long XpHashKey(const CString& csString);
93#endif // __AFX_H__
94
95#include "xphashtable.inl"
96
97#endif //__XPHASHTABLE_H__
Definition: xphashtable.h:18
HRESULT Copy(const CXpHashTable &HashTable)
HRESULT Intersection(const CXpHashTable &HashTable, CXpHashTable &Result)
INT_PTR GetCount() const
void RemoveAll()
virtual ~CXpHashTable()
INT_PTR m_nCount
Definition: xphashtable.h:33
TXpTableItem GetFirst() const
bool operator==(const CXpHashTable &HashTable) const
TXpTableItem GetNext(const TXpTableItem Item) const
bool FindValue(const KEY &Key, TYPE &Value) const
const TYPE & GetValue(const TXpTableItem Item) const
HRESULT Insert(const KEY &Key, const TYPE &Value)
bool IsPrime(const INT_PTR nNbr)
const KEY & GetKey(const TXpTableItem Item) const
INT_PTR GetBucket(const KEY &Key) const
bool HasKey(const KEY &Key) const
void GetPair(const TXpTableItem Item, KEY &Key, TYPE &Value) const
CXpHashTable(const INT_PTR nHashTableSize, const bool bCalculate=true)
HRESULT Difference(const CXpHashTable &HashTable, CXpHashTable &Result)
INT_PTR GetClosestPrime(const INT_PTR nNbr)
HRESULT Remove(const KEY &Key)
HRESULT Union(const CXpHashTable &HashTable, CXpHashTable &Result)
CXpHashTable & operator=(const CXpHashTable &HashTable)
CXpArray< SNode * > CPtrArray
Definition: xphashtable.h:30
CPtrArray m_Buckets
Definition: xphashtable.h:32
TYPE & GetValue(const TXpTableItem Item)
TXpTableItem FindKey(const KEY &Key) const
Definition: xphashtable.h:22
SNode(const KEY &Key, const TYPE &Value)
Definition: xphashtable.h:23
TYPE m_Value
Definition: xphashtable.h:27
SNode * m_pNext
Definition: xphashtable.h:25
KEY m_Key
Definition: xphashtable.h:26

Distributed under the Apache License, Version 2.0.

See accompanying NOTICE file for details.