-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashTable.hpp
163 lines (118 loc) · 4.95 KB
/
HashTable.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
/*
Copyright (c) 2017 waynezxcv <liuweiself@126.com>
https://github.com/waynezxcv/Playground
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef HashTable_hpp
#define HashTable_hpp
#include <stdio.h>
#include "Dictionary.hpp"
#include "Hash.hpp"
#include "IllegalParameterValue.hpp"
namespace LWTL {
//哈希表的每一个位置叫一个桶bucket
//对关键字为k的数对,f(k)是起始桶。
//桶的数量等于散列表的长度或大小。
//因为散列函数可以将多个关键字映射到同一个桶,所以桶要能容纳多个数对。
//除法散列函数: f(k) = k % D
//用除法散列函数: f(k) = k % D 实现的字典
template <typename K,typename E>
class HashTable : public Dictionary<K, E> {
public:
//构造函数
HashTable(int theDivisor) {
divisor = theDivisor;
dSize = 0;
//分配和初始化哈希表数组
table = new pair<const K, E>* [divisor];
for (int i = 0; i < divisor; i ++) {
table[i] = NULL;
}
}
~HashTable () {
for (int i = 0; i < divisor; i ++) {
if (table[i] != NULL) {
delete table[i];
}
}
delete [] table;
}
//搜索一个关键字为theKey的数对,如果匹配的数对存在返回它的地址,否则如果散列不满,则返回theKey可以插入的位置
int search(const K& theKey) const {
int i = (int)hash(theKey) % divisor;
int j = i;
do {
if (table[i] == NULL || table[j] -> first == theKey) {
return j;
}
j = (j + 1) % divisor;
} while (j != i);
return j;
}
pair<const K, E>* find (const K& theKey) const override {
//搜索哈希表
int b = search(theKey);
//判断table[b]是否是匹配数对
if (table[b] == NULL || table[b] -> first != theKey) {
return NULL;
}
return table[b];
}
void insert (const pair<const K,E>& thePair) override {
//把数对插入字典,如果存在关键字相同的数对,则覆盖
int b = search(thePair.first);//搜索哈希表
//如果数对不存在
if (table[b] == NULL) {
//插入这个数对
table[b] = new pair<const K, E>(thePair);
dSize ++;
} else {
//数对存在
//检查是否重复
if (table[b] -> first == thePair.first) {
table[b] -> second = thePair.second;
}else {
//溢出
throw IllegalParameterValue("the hash table is overflow");
}
}
}
void erase (const K& theKey) override {
int b = search(theKey);//搜索哈希表
if (table[b] != NULL) {
if (table[b] -> first == theKey) {
delete table[b];
dSize --;
}
} else {
throw IllegalParameterValue("the pair is not exsit");
}
}
bool empty () const override {
return dSize == 0;
}
int size () const override {
return dSize;
}
private:
pair<const K, E>** table;//哈希表
Hash<K> hash;//把类型K映射到一个非负整数
int dSize;//字典中的数对个数
int divisor;//散列函数 f(k) = k%D 的除数D,即桶的个数
};
}
#endif /* HashTable_hpp */