-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhash_map.h
More file actions
111 lines (104 loc) · 2.58 KB
/
hash_map.h
File metadata and controls
111 lines (104 loc) · 2.58 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
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
#include<algorithm>
#define bucket_num 1543
using namespace std;
/*
hash_map可以用hash_table构建键值对直接实现
重写一遍为了能单独拿来参考
*/
template<class Key, class Value>
class hash_map
{
//冲突以开链法解决
struct node
{
node* next;
pair<Key, Value> value;
node(): next(nullptr) {};
node(pair<Key, Value> val): next(nullptr), value(val) {}
};
private:
node* buckets;
public:
//桶的大小通常根据需求选择某个质数
hash_map(): buckets(new node[bucket_num]) {};
~hash_map()
{
destroy(buckets);
delete[] buckets;
}
//不能有重复值
bool insert(const pair<Key, Value> value)
{
int hashcode = value.first % bucket_num;
node* head = &buckets[hashcode];
while (head->next != nullptr && head->next->value.first != value.first) head = head->next;
if (head->next == nullptr)
head->next = new node(value);
else
return false;
return true;
}
bool remove(const Key key)
{
int hashcode = key % bucket_num;
node* head = &buckets[hashcode];
while (head->next != nullptr && head->next->value.first != key) head = head->next;
if (head->next == nullptr) return false;
else
{
node* temp = head->next;
head->next = head->next->next;
delete temp;
}
return true;
}
pair<bool, Value> find(const Key key)
{
int hashcode = key % bucket_num;
node* head = &buckets[hashcode];
while (head->next != nullptr && head->next->value.first != key) head = head->next;
if (head->next == nullptr) return make_pair(false, head->value.second);
else return make_pair(true, head->next->value.second);
}
private:
void destroy(node* buckets)
{
for (int i = 0; i < bucket_num; ++i)
{
node* head = &buckets[i];
head = head->next;
while (head != nullptr)
{
node* temp = head;
head = head->next;
delete temp;
}
}
}
};
//在cpp文件里测试
// #include"hash_map.h"
// #include<iostream>
// int main(int argc, char const *argv[])
// {
// hash_map<int, string> m;
// m.insert(make_pair(1, "hello"));
// m.insert(make_pair(2, "world"));
// m.insert(make_pair(3, "this"));
// m.insert(make_pair(4, "is"));
// m.insert(make_pair(5, "Shey's"));
// m.insert(make_pair(6, "map"));
// m.remove(2);
// if (m.find(1).first) cout << m.find(1).second << endl;
// if (m.find(2).first) cout << m.find(2).second << endl;
// if (m.find(3).first) cout << m.find(3).second << endl;
// if (m.find(4).first) cout << m.find(4).second << endl;
// if (m.find(5).first) cout << m.find(5).second << endl;
// if (m.find(6).first) cout << m.find(6).second << endl;
// return 0;
// }
// hello
// this
// is
// Shey's
// map