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 
14 typedef void* TXpTableItem;
15 
16 template <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 
32  CPtrArray m_Buckets;
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
73  CXpHashTable& operator=(const CXpHashTable& HashTable);
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 
81 template<class KEY>
82 inline 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 
88 inline unsigned long XpHashKey(const char* pszString);
89 inline 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__
INT_PTR m_nCount
Definition: xphashtable.h:33
TXpTableItem FindKey(const KEY &Key) const
CXpArray< SNode * > CPtrArray
Definition: xphashtable.h:30
virtual ~CXpHashTable()
HRESULT Insert(const KEY &Key, const TYPE &Value)
HRESULT Difference(const CXpHashTable &HashTable, CXpHashTable &Result)
const KEY & GetKey(const TXpTableItem Item) const
INT_PTR GetCount() const
bool operator==(const CXpHashTable &HashTable) const
KEY m_Key
Definition: xphashtable.h:26
void GetPair(const TXpTableItem Item, KEY &Key, TYPE &Value) const
TXpTableItem GetNext(const TXpTableItem Item) const
HRESULT Union(const CXpHashTable &HashTable, CXpHashTable &Result)
INT_PTR GetBucket(const KEY &Key) const
CXpHashTable & operator=(const CXpHashTable &HashTable)
INT_PTR GetClosestPrime(const INT_PTR nNbr)
bool IsPrime(const INT_PTR nNbr)
bool FindValue(const KEY &Key, TYPE &Value) const
HRESULT Copy(const CXpHashTable &HashTable)
HRESULT Remove(const KEY &Key)
SNode * m_pNext
Definition: xphashtable.h:25
CPtrArray m_Buckets
Definition: xphashtable.h:32
TYPE m_Value
Definition: xphashtable.h:27
TXpTableItem GetFirst() const
SNode(const KEY &Key, const TYPE &Value)
Definition: xphashtable.h:23
Definition: xphashtable.h:17
Definition: xphashtable.h:21
CXpHashTable(const INT_PTR nHashTableSize, const bool bCalculate=true)
void RemoveAll()
HRESULT Intersection(const CXpHashTable &HashTable, CXpHashTable &Result)
const TYPE & GetValue(const TXpTableItem Item) const
bool HasKey(const KEY &Key) const