zhangzengfei
2022-07-20 c90c3e794bdd95127d0c34ff1d9e8759d18a0445
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
#if !defined(__INT_HASH_H__)
#define __INT_HASH_H__
 
using namespace std;
 
#include <stdio.h>
#include <stdlib.h>
 
/* Hash element class entity */
template <class T>
class CHashElem
{
public:
    CHashElem<T> ()
    {
        key = 0;
        pAtom = 0;
        pNext = 0;
    }
    ~CHashElem<T> ()
    {
        if (pAtom)
        {
            delete pAtom;
            pAtom = 0;
        }
    }
    
    int key;
    T *pAtom;
    CHashElem *pNext;
};
 
/* Hash table and its operaions */
template <class T>
class CIntHash
{
public:
    CIntHash<T>(int iSlots = 100);
    ~CIntHash<T>();
 
    bool Insert(const int key, T *pObj);
    bool Find(const int key, T *&pObj);
    bool Remove(const int key);
    int  Size() {return m_iLength;}
 
     CHashElem<T> **m_ppHashSlots;
    int m_iNumSlots;
    
private:
    int m_iLength;
};
 
template <class T>
CIntHash<T>::CIntHash(int iSlots)
{
    if (iSlots < 5)
    iSlots = 5;
        
    m_ppHashSlots = (CHashElem<T> **)new char[sizeof(void *) * iSlots];
    if (0 == m_ppHashSlots)
    {
        printf("new CHashElem<T> ** failure! \n");
        exit(1);
    }
    for (int i=0; i<iSlots; i++)
        m_ppHashSlots[i] = 0;
    
    m_iNumSlots = iSlots;
    m_iLength = 0;
}
 
template <class T>
CIntHash<T>::~CIntHash() 
{
    if (m_ppHashSlots)
    {
        CHashElem<T> *pHe = 0;
        for (int i=0; i<m_iNumSlots; i++) 
        {
            pHe = m_ppHashSlots[i];
            while (pHe)
            {
                CHashElem<T> *pNext = pHe->pNext;
                delete pHe;
                pHe = pNext;
            }
        }
        delete [] m_ppHashSlots;
        m_ppHashSlots = 0;
    }
}
 
template <class T>
bool CIntHash<T>::Insert(const int key, T *pObj)
{
    unsigned int num = (unsigned int)key % m_iNumSlots;    
    if (num >= 0)
    {
        if (m_ppHashSlots[num]) 
        {
            CHashElem<T> *pIns = m_ppHashSlots[num];
            while (pIns->pNext)
            {
                pIns = pIns->pNext;
            }
            pIns->pNext = new CHashElem<T>;
            pIns->pNext->pNext = 0;
            pIns->pNext->pAtom = pObj;
            pIns->pNext->key = key;
            m_iLength++;
            
            return true;
        } 
        else 
        {
            m_ppHashSlots[num] = new CHashElem<T>;
            m_ppHashSlots[num]->pNext = 0;
            m_ppHashSlots[num]->pAtom = pObj;
            m_ppHashSlots[num]->key = key;
            m_iLength++;
            
            return true;
        }
    }
    
    printf("CIntHash Insert Failure! \n");
    
    return false;
}
 
template <class T>
bool CIntHash<T>::Find(const int key, T *&pObj)
{
    unsigned int num = (unsigned int)key % m_iNumSlots;    
    if (num >= 0)
    {
        CHashElem<T> *pElem = m_ppHashSlots[num];
        while (pElem) 
        {
            if (pElem->key == key) 
            {
                pObj = pElem->pAtom;
                
                return true;
            }
            pElem = pElem->pNext;
        }
    }
    
    printf("CIntHash Find Failure! \n");
    
    return false;
}
 
template <class T>
bool CIntHash<T>::Remove(const int key)
{
    unsigned int num = (unsigned int)key % m_iNumSlots;    
    if (num >= 0) 
    {
        CHashElem<T> *pElem = m_ppHashSlots[num];
        CHashElem<T> *pPrev = 0;
        while (pElem) 
        {
            if (pElem->key == key) 
            {
                if (pPrev) 
                {
                    pPrev->pNext = pElem->pNext;
                }
                else 
                {
                    m_ppHashSlots[num] = pElem->pNext;
                }
                delete pElem;
                m_iLength--;
                
                return true;
            }
            pPrev = pElem;
            pElem = pElem->pNext;
        }
    }
    
    printf("CIntHash Remove Failure! \n");
    
    return false;
}
 
#endif  //#if !defined(__INT_HASH_H__)