-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash_map.hpp
223 lines (180 loc) · 3.78 KB
/
hash_map.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
#ifndef __HASH_MAP_HPP__
#define __HASH_MAP_HPP__
#include "nano/hashtable.hpp"
#include "functional.hpp"
#include "nano/hash_function.hpp"
namespace nano {
template< class Key,
class Value,
class KeyEqual = equal_to<Key>,
class Hash = hash<Key>,
class Allocator = allocator<pair<const Key, Value> > >
class hash_map
{
public:
typedef Key key_type;
typedef Value mapped_type;
typedef pair<const Key, Value> value_type;
typedef size_t size_type;
typedef Hash hasher;
typedef KeyEqual key_equal;
struct pair_equal
{
bool operator()(const value_type& lhs, const value_type& rhs) const
{
return key_equal()(lhs.first, rhs.first);
}
};
struct key_hash
{
size_type operator()(const value_type& value) const
{
return hasher()(value.first);
}
};
typedef hashtable<value_type, pair_equal, key_hash, Allocator> table;
typedef typename table::reference reference;
typedef typename table::const_reference const_reference;
typedef typename table::pointer pointer;
typedef typename table::const_pointer const_pointer;
typedef typename table::iterator iterator;
typedef typename table::const_iterator const_iterator;
typedef hash_map<Key, Value, KeyEqual, Hash, Allocator> my_type;
public:
explicit hash_map(size_type buckets_count = 7,
const key_hash& hash = key_hash(),
const pair_equal& equal = pair_equal())
: ht(buckets_count, hash, equal) {}
hash_map(const my_type& other)
: ht(other.ht) {}
my_type& operator=(const my_type& other)
{
ht = other.ht;
}
public:
iterator begin()
{
return ht.begin();
}
const_iterator begin() const
{
return ht.begin();
}
iterator end()
{
return ht.end();
}
const_iterator end() const
{
return ht.end();
}
bool empty() const
{
return elements_count == 0;
}
size_type size() const
{
return ht.size();
}
size_type max_size() const
{
return ht.max_size();
}
size_type buckets_count() const
{
return ht.buckets_count();
}
size_type max_buckets_count() const
{
return ht.max_buckets_count();
}
void clear()
{
ht.clear();
}
pair<iterator, bool> insert(const value_type& value)
{
return ht.insert_unique(value);
}
template< class InputIt >
void insert(InputIt first, InputIt last)
{
ht.insert_unique(first, last);
}
iterator erase(iterator pos)
{
return ht.erase(pos);
}
iterator erase(iterator first, iterator last)
{
return ht.erase(first, last);
}
size_type erase(const key_type& key)
{
return ht.erase(value_type(key, mapped_type()));
}
void swap(const my_type& other)
{
ht.swap(other.ht);
}
mapped_type& at(const key_type& key)
{
return ht.at(value_type(key, mapped_type()));
}
mapped_type& operator[](const key_type& key)
{
return (*(ht.insert_unique(value_type(key, mapped_type())).first)).second;
}
pair<iterator, iterator> equal_range(const key_type& key)
{
return ht.equal_range(value_type(key, mapped_type()));
}
pair<const_iterator, const_iterator> equal_range(const key_type& key) const
{
return ht.equal_range(value_type(key, mapped_type()));
}
iterator find(const key_type& key)
{
return ht.find(value_type(key, value_type()));
}
const_iterator find(const key_type& key) const
{
return ht.find(value_type(key, value_type()));
}
size_type count(const key_type& key) const
{
return ht.count(value_type(key, value_type()));
}
float load_factor() const
{
return ht.load_factor();
}
float max_load_factor() const
{
return ht.max_load_factor();
}
void max_load_factor(float factor)
{
ht.max_load_factor(factor);
}
void rehash(size_type count)
{
ht.rehash(count);
}
void reserve(size_type count)
{
ht.reserve(count);
}
key_equal key_eq() const
{
return ht.key_eq();
}
hasher hash_function() const
{
return ht.hash_function();
}
private:
table ht;
};
}
#endif