-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.hpp
286 lines (228 loc) · 7.66 KB
/
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#ifndef MAP_HPP
# define MAP_HPP
# include "RBT.hpp"
# include "utils.hpp"
namespace ft {
template <typename Key, typename T, typename Compare = std::less<Key>,
typename Allocator = std::allocator<ft::pair<Key const, T> > >
class map {
public:
typedef Key key_type;
typedef T mapped_type;
typedef pair<Key const, T> value_type;
typedef Compare key_compare;
typedef Allocator allocator_type;
class value_compare: public std::binary_function<value_type, value_type, bool> {
private:
friend class map;
protected:
key_compare comp;
value_compare(key_compare c): comp(c) {
}
public:
bool operator()(value_type const &lhs, value_type const &rhs) const {
return (comp(lhs.first, rhs.first));
}
};
private:
typedef rb_tree<value_type, allocator_type, value_compare> tree_type;
public:
typedef typename tree_type::iterator iterator;
typedef typename tree_type::const_iterator const_iterator;
typedef typename Allocator::pointer pointer;
typedef typename Allocator::const_pointer const_pointer;
typedef typename Allocator::reference reference;
typedef typename Allocator::const_reference const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef ft::reverse_iterator<iterator> reverse_iterator;
typedef ft::reverse_iterator<const_iterator> const_reverse_iterator;
private:
allocator_type _alloc;
key_compare _key_comp;
value_compare _value_comp;
tree_type _tree;
public:
explicit map(key_compare const &comp = key_compare(), allocator_type const alloc = allocator_type())
: _alloc(alloc), _key_comp(comp), _value_comp(comp), _tree(_alloc, _value_comp) {
}
template <typename InputIterator>
map(InputIterator first, InputIterator last,
key_compare const &comp = key_compare(), allocator_type const alloc = allocator_type())
: _alloc(alloc), _key_comp(comp), _value_comp(comp), _tree(_alloc, _value_comp) {
this->insert(first, last);
}
map(map<key_type, value_type, key_compare, allocator_type> const &m)
: _alloc(m.alloc), _key_comp(m.comp), _value_comp(m.comp), _tree(_alloc, _value_comp) {
this->insert(m.begin(), m.end());
}
~map() {
}
map<key_type, value_type, key_compare, allocator_type>
&operator=(map<key_type, value_type, key_compare, allocator_type> const &rhs) {
if (this != &rhs) {
this->_tree = rhs._tree;
this->_alloc = rhs._alloc;
this->_key_comp = rhs._key_comp;
this->_value_comp = rhs._value_comp;
}
return (*this);
}
// iterators
iterator begin(void) {
return (this->_tree.begin());
}
const_iterator begin(void) const {
return (this->_tree.begin());
}
iterator end(void) {
return (this->_tree.end());
}
const_iterator end() const {
return (this->_tree.end());
}
reverse_iterator rbegin(void) {
return (this->_tree.rbegin());
}
const_reverse_iterator rbegin(void) const {
return (this->_tree.rbegin());
}
reverse_iterator rend(void) {
return (this->_tree.rend());
}
const_reverse_iterator rend(void) const {
return (this->_tree.rend());
}
// capacity
bool empty(void) const {
return (this->size() == 0);
}
size_type size(void) const {
return (this->_tree.size());
}
size_type max_size(void) const {
return (this->_tree.max_size());
}
// element access
mapped_type &operator[](key_type const &key) {
iterator it = this->find(key);
if (it == this->end())
it = this->insert(ft::make_pair(key, mapped_type())).first;
return (it->second);
}
// modifiers
ft::pair<iterator, bool> insert(value_type const &p) {
return (this->_tree.insert(this->end(), p));
}
iterator insert(iterator position, value_type const &p) {
return (this->_tree.insert(position, p).first);
}
template <typename InputIterator>
void insert(InputIterator first, InputIterator last) {
for (; first != last; ++first)
this->_tree.insert(this->end(), *first);
}
void erase(iterator position) {
this->_tree.erase(position);
}
size_type erase(key_type const &key) {
size_type ret = this->count(key);
this->_tree.erase(ft::make_pair(key, mapped_type()));
return (ret);
}
void erase(iterator first, iterator last) {
iterator it;
while (first != last) {
it = first;
++first;
this->_tree.erase(it);
}
}
void swap(map<Key, T, Compare, Allocator> &m) {
this->_tree.swap(m._tree);
}
void clear(void) {
this->_tree.clear();
}
// observers
key_compare key_comp(void) const {
return (this->_key_comp);
}
value_compare value_comp(void) const {
return (this->_value_comp);
}
// map operations
iterator find(key_type const &key) {
return (this->_tree.find(ft::make_pair(key, mapped_type())));
}
const_iterator find(key_type const &key) const {
return (this->_tree.find(ft::make_pair(key, mapped_type())));
}
size_type count(key_type const &key) const {
return (this->find(key) != this->end());
}
iterator lower_bound(key_type const &key) {
return (this->_tree.lower_bound(ft::make_pair(key, mapped_type())));
}
const_iterator lower_bound(key_type const &key) const {
return (this->_tree.lower_bound(ft::make_pair(key, mapped_type())));
}
iterator upper_bound(key_type const &key) {
return (this->_tree.upper_bound(ft::make_pair(key, mapped_type())));
}
const_iterator upper_bound(key_type const &key) const {
return (this->_tree.upper_bound(ft::make_pair(key, mapped_type())));
}
pair<iterator, iterator> equal_range(key_type const &key) {
iterator it = upper_bound(key);
iterator ite = lower_bound(key);
return (ft::make_pair<iterator, iterator>(ite, it));
}
pair<const_iterator, const_iterator> equal_range(key_type const &key) const {
const_iterator it = upper_bound(key);
const_iterator ite = lower_bound(key);
return (ft::make_pair<const_iterator, const_iterator>(ite, it));
}
void print(void) {
this->_tree.print();
}
};//End map class
template <typename Key, typename T, typename Compare, typename Allocator>
bool operator==(map<Key, T, Compare, Allocator> const &lhs,
map<Key, T, Compare, Allocator> const &rhs) {
if (lhs.size() != rhs.size())
return (false);
return (ft::equal(lhs.begin(), lhs.end(), rhs.begin()));
}
template <typename Key, typename T, typename Compare, typename Allocator>
bool operator<(map<Key, T, Compare, Allocator> const &lhs,
map<Key, T, Compare, Allocator> const &rhs) {
return (ft::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end()));
}
template <typename Key, typename T, typename Compare, typename Allocator>
bool operator!=(map<Key, T, Compare, Allocator> const &lhs,
map<Key, T, Compare, Allocator> const &rhs) {
return (!(lhs == rhs));
}
template <typename Key, typename T, typename Compare, typename Allocator>
bool operator>(map<Key, T, Compare, Allocator> const &lhs,
map<Key, T, Compare, Allocator> const &rhs) {
return (rhs < lhs);
}
template <typename Key, typename T, typename Compare, typename Allocator>
bool operator<=(map<Key, T, Compare, Allocator> const &lhs,
map<Key, T, Compare, Allocator> const &rhs) {
return (!(rhs < lhs));
}
template <typename Key, typename T, typename Compare, typename Allocator>
bool operator>=(map<Key, T, Compare, Allocator> const &lhs,
map<Key, T, Compare, Allocator> const &rhs) {
return (!(lhs < rhs));
}
// specialized algorithm
template <typename Key, typename T, typename Compare, typename Allocator>
void swap(map<Key, T, Compare, Allocator> &lhs, map<Key, T, Compare, Allocator> &rhs) {
lhs.swap(rhs);
}
}// End ft namespace
#endif