-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTMap.hpp
175 lines (141 loc) · 3.59 KB
/
TMap.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/******************************************************************************
** (C) Chris Oldwood
**
** MODULE: TMAP.HPP
** COMPONENT: Windows C++ Library
** DESCRIPTION: The TMap class declaration.
**
*******************************************************************************
*/
// Check for previous inclusion
#ifndef WCL_TMAP_HPP
#define WCL_TMAP_HPP
#if _MSC_VER > 1000
#pragma once
#endif
#include <Legacy/Map.hpp>
/******************************************************************************
**
** This is a template version of the CMap class.
**
*******************************************************************************
*/
template<class K, class V> class TMap : public CMap
{
public:
//
// Constructors/Destructor.
//
TMap();
~TMap();
//
// Methods.
//
void Add(K Key, V Value);
void Remove(K Key);
bool Find(K Key, V& Value) const;
V Find(K Key) const;
bool Exists(K Key) const;
};
/******************************************************************************
**
** This is the class used for items stored in the map.
**
*******************************************************************************
*/
template<class K, class V> class TMapItem : public CMapItem
{
public:
//
// Constructors/Destructor.
//
TMapItem(K Key);
TMapItem(K Key, V Value);
virtual ~TMapItem();
//
// Methods.
//
virtual uint Key() const;
virtual bool operator==(const CMapItem& rRHS) const;
//
// Members.
//
K m_Key;
V m_Value;
};
/******************************************************************************
**
** Map hashing functions.
**
*******************************************************************************
*/
template<class K> inline uint HashKey(K Key)
{
return reinterpret_cast<uint>(Key);
}
template<> inline uint HashKey<CString>(CString Key)
{
uint nValue = 0;
for (const tchar* pChar = Key; *pChar != TXT('\0'); ++pChar)
nValue = (nValue * 17) | *pChar;
return nValue;
}
/******************************************************************************
**
** Implementation of inline functions.
**
*******************************************************************************
*/
template<class K, class V> inline TMap<K, V>::TMap()
{
}
template<class K, class V> inline TMap<K, V>::~TMap()
{
}
template<class K, class V> inline void TMap<K, V>::Add(K Key, V Value)
{
CMap::Add(*(new TMapItem<K, V>(Key, Value)));
}
template<class K, class V> inline void TMap<K, V>::Remove(K Key)
{
CMap::Remove(TMapItem<K, V>(Key));
}
template<class K, class V> inline bool TMap<K, V>::Find(K Key, V& Value) const
{
TMapItem<K, V>* pItem = static_cast<TMapItem<K, V>*>(CMap::Find(TMapItem<K, V>(Key)));
if (pItem != NULL)
Value = pItem->m_Value;
return (pItem != NULL);
}
template<class K, class V> inline V TMap<K, V>::Find(K Key) const
{
TMapItem<K, V>* pItem = static_cast<TMapItem<K, V>*>(CMap::Find(TMapItem<K, V>(Key)));
ASSERT(pItem != NULL);
return pItem->m_Value;
}
template<class K, class V> inline bool TMap<K, V>::Exists(K Key) const
{
return CMap::Exists(TMapItem<K, V>(Key));
}
template<class K, class V> inline TMapItem<K, V>::TMapItem(K Key)
: m_Key(Key)
{
}
template<class K, class V> inline TMapItem<K, V>::TMapItem(K Key, V Value)
: m_Key(Key)
, m_Value(Value)
{
}
template<class K, class V> inline TMapItem<K, V>::~TMapItem()
{
}
template<class K, class V> inline uint TMapItem<K, V>::Key() const
{
return HashKey(m_Key);
}
template<class K, class V> inline bool TMapItem<K, V>::operator==(const CMapItem& rRHS) const
{
const TMapItem<K, V>* pRHS = static_cast<const TMapItem<K, V>*>(&rRHS);
return (m_Key == pRHS->m_Key);
}
#endif // WCL_TMAP_HPP