-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathListHash.h
210 lines (191 loc) · 4.63 KB
/
ListHash.h
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Author : XuBenHao
// Version : 1.0.0
// Mail : [email protected]
// Copyright : XuBenHao 2020 - 2030
#ifndef ALLIB_DATASTRUCT_HASH_LISTHASH_H
#define ALLIB_DATASTRUCT_HASH_LISTHASH_H
#include "..\..\stdafx.h"
#include "..\Array\DynArray.h"
#include "..\List\DoubleList.h"
namespace AlLib
{
namespace DataStruct
{
namespace Hash
{
template<typename Key, typename Value>
class ListHash
{
public:
class Pair
{
public:
Pair()
{
}
Pair(const Key& nKey_, const Value& nValue_)
{
m_nKey = nKey_;
m_nValue = nValue_;
}
~Pair()
{
}
public:
Key m_nKey;
Value m_nValue;
};
public:
ListHash(int nSoltNums_,
std::function<int(const Key&, int)> hashFun_ = [](const Key& nKey_, int nSlotsNum_)->int
{
return (nKey_ % nSlotsNum_);
});
~ListHash();
ListHash(const ListHash& hash_);
ListHash& operator=(const ListHash& hash_);
void Insert(const Pair& nPair_);
void Delete(const Key& nKey_);
bool Search(const Key& nKey_, Value& nValue_) const;
private:
Array::DynArray<List::DoubleList<Pair>*> m_arrSlots;
std::function<int(const Key&, int)> m_fHashFun;
};
template<typename Key, typename Value>
ListHash<Key, Value>::ListHash(int nSoltNums_, std::function<int(const Key&, int)> hashFun_)
:m_arrSlots(nSoltNums_, nullptr), m_fHashFun(hashFun_)
{
for (int _i = 0; _i < nSoltNums_; _i++)
{
List::DoubleList<Pair>* _pList = nullptr;
try
{
_pList = new List::DoubleList<Pair>();
}
catch (...)
{
_pList = nullptr;
throw "out of memory";
}
m_arrSlots[_i] = _pList;
}
}
template<typename Key, typename Value>
ListHash<Key, Value>::ListHash(const ListHash& hash_)
: m_arrSlots(hash_.m_arrSlots.GetSize(), nullptr), m_fHashFun(hash_.m_fHashFun)
{
int _nSize = hash_.m_arrSlots.GetSize();
for (int _i = 0; _i < _nSize; _i++)
{
List::DoubleList<Pair>* _pList = nullptr;
try
{
_pList = new List::DoubleList<Pair>(hash_.m_arrSlots[_i]);
}
catch (...)
{
_pList = nullptr;
throw "out of memory";
}
m_arrSlots[_i] = _pList;
}
}
template<typename Key, typename Value>
typename ListHash<Key, Value>& ListHash<Key, Value>::operator=(const ListHash& hash_)
{
if (this == &hash_)
{
return *this;
}
this->~ListHash();
int _nSize = hash_.m_arrSlots.GetSize();
m_arrSlots = Array::DynArray<List::DoubleList<Pair>*>(_nSize, nullptr);
for (int _i = 0; _i < _nSize; _i++)
{
List::DoubleList<Pair>* _pList = nullptr;
try
{
_pList = new List::DoubleList<Pair>(hash_.m_arrSlots[_i]);
}
catch (...)
{
_pList = nullptr;
throw "out of memory";
}
m_arrSlots[_i] = _pList;
}
m_fHashFun = hash_.m_fHashFun;
return *this;
}
template<typename Key, typename Value>
ListHash<Key, Value>::~ListHash()
{
int _nSize = m_arrSlots.GetSize();
for (int _i = 0; _i < _nSize; _i++)
{
delete m_arrSlots[_i];
m_arrSlots[_i] = nullptr;
}
}
template<typename Key, typename Value>
void ListHash<Key, Value>::Insert(const Pair& nPair_)
{
int _nSize = m_arrSlots.GetSize();
int _nIndex = m_fHashFun(nPair_.m_nKey, _nSize);
List::DoubleList<Pair> *_pList = m_arrSlots[_i];
_pList->Add(nPair_);
}
template<typename Key, typename Value>
bool ListHash<Key, Value>::Search(const Key& nKey_, Value& nValue_) const
{
int _nSize = m_arrSlots.GetSize();
int _nIndex = m_fHashFun(nKey_, _nSize);
List::DoubleList<Pair> *_pList = m_arrSlots[_nIndex];
List::DoubleList<Pair>::Node* _pNode = _pList->Find([nKey_](const Pair& nPair_)->bool
{
if (nPair_.m_nKey == nKey_)
{
return true;
}
else
{
return false;
}
});
if (_pNode == nullptr)
{
return false;
}
else
{
Pair _nPair = _pNode->GetValue();
nValue_ = _nPair.m_nValue;
return true;
}
}
template<typename Key, typename Value>
void ListHash<Key, Value>::Delete(const Key& nKey_)
{
int _nSize = m_arrSlots.GetSize();
int _nIndex = m_fHashFun(nKey_, _nSize);
List::DoubleList<Pair> *_pList = m_arrSlots[_nIndex];
List::DoubleList<Pair>::Node* _pNode = _pList->Find([nKey_](const Pair& nPair_)->bool
{
if (nPair_.m_nKey == nKey_)
{
return true;
}
else
{
return false;
}
});
if (_pNode)
{
_pList->Delete(_pNode);
}
}
}
}
}
#endif