-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathHashMap.cpp
More file actions
executable file
·71 lines (62 loc) · 1.56 KB
/
HashMap.cpp
File metadata and controls
executable file
·71 lines (62 loc) · 1.56 KB
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
#include "HashMap.h"
//HashNode
HashNode::HashNode(UINT64 nKey0, UINT64 nKey1, int nIndex)
{
m_nKey[0] = nKey0;
m_nKey[1] = nKey1;
m_nIndex = nIndex;
m_pNext = NULL;
}
HashNode::~HashNode()
{
if (m_pNext != NULL)
delete m_pNext;
}
//HashMap
HashMap::HashMap(Constant * pConstant, int nSize)
{
m_pConstant = pConstant;
int i;
for (i = 0; i < 40; i++) {
if (nSize * 2 < pConstant->m_pHashSize[i])
break;
}
m_nNum = pConstant->m_pHashSize[i];
m_pData = new (HashNode *[m_nNum]);
for (i = 0; i < m_nNum; i++)
m_pData[i] = NULL;
m_nHashKey = (((UINT64)1) << 60) % m_nNum;
}
HashMap::~HashMap()
{
for (int i = 0; i < m_nNum; i++)
if (m_pData[i] != NULL)
delete m_pData[i];
delete []m_pData;
}
void HashMap::Set(UINT64 nKey0, UINT64 nKey1, int nIndex)
{
int i = Hash(nKey0, nKey1);
HashNode ** ppNode = &m_pData[i];
while (*ppNode != NULL && ((*ppNode)->m_nKey[0] != nKey0 || (*ppNode)->m_nKey[1] != nKey1))
ppNode = &((*ppNode)->m_pNext);
if (*ppNode == NULL)
*ppNode = new HashNode(nKey0, nKey1, nIndex);
else
(*ppNode)->m_nIndex = nIndex;
}
int HashMap::Get(UINT64 nKey0, UINT64 nKey1)
{
int i = Hash(nKey0, nKey1);
HashNode * pNode = m_pData[i];
while (pNode != NULL && (pNode->m_nKey[0] != nKey0 || pNode->m_nKey[1] != nKey1))
pNode = pNode->m_pNext;
return (pNode == NULL ? -1 : pNode->m_nIndex);
}
int HashMap::Hash(UINT64 nKey0, UINT64 nKey1)
{
UINT64 k0 = nKey0 % m_nNum;
UINT64 k1 = nKey1 % m_nNum;
UINT64 k = k0 * m_nHashKey + k1;
return (int)(k % m_nNum);
}