-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.hpp
344 lines (281 loc) · 8.02 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: graja <graja@student.42wolfsburg.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/07 17:28:46 by graja #+# #+# */
/* Updated: 2022/05/10 11:01:14 by graja ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MAP_H
# define MAP_H
# include <iostream>
# include <stdexcept>
# include <memory>
# include "iterator.hpp"
# include "vector.hpp"
# include "utility.hpp"
# include "RBtree.hpp"
# include "RBT_iterator.hpp"
namespace ft
{
template <typename Key, typename T, typename Compare = std::less<Key>,
typename Alloc = std::allocator<ft::pair<const Key, T > > >
class map
{
public:
typedef Key key_type;
typedef T mapped_type;
typedef ft::pair<const key_type, mapped_type> value_type;
typedef Compare key_compare;
typedef Alloc allocator_type;
typedef typename allocator_type::reference reference;
typedef typename allocator_type::const_reference const_reference;
typedef typename allocator_type::pointer pointer;
typedef typename allocator_type::const_pointer const_pointer;
typedef ptrdiff_t difference_type;
typedef size_t size_type;
typedef RBT_iterator<Key, T, false> iterator;
typedef RBT_iterator<Key, T, true> const_iterator;
typedef ft::reverse_iterator<iterator> reverse_iterator;
typedef ft::reverse_iterator<const_iterator> const_reverse_iterator;
private:
RBtree<key_type, mapped_type> _tree;
allocator_type _alloc;
key_compare _comp;
public:
explicit map(const key_compare& comp = key_compare(), const allocator_type& alloc =
allocator_type()): _tree()
{
_alloc = alloc;
_comp = comp;
}
template <class InputIterator>
map(InputIterator first, InputIterator last, const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type()): _tree()
{
_alloc = alloc;
_comp = comp;
while (first != last)
{
this->_tree.insert(*first);
first++;
}
}
map(map const & cpy)
{
const_iterator in = cpy.begin();
this->_tree.clear();
while (in != cpy.end())
{
this->_tree.insert(*in);
in++;
}
}
map & operator=(map const & right)
{
const_iterator in = right.begin();
while (in != right.end())
{
this->_tree.insert(*in);
in++;
}
return (*this);
}
~map(void) {}
class value_compare
{
friend class map;
protected:
key_compare comp;
value_compare(Compare c) : comp(c) {}
public:
typedef bool result_type;
typedef value_type first_argument_type;
typedef value_type second_argument_type;
bool operator()(const value_type & x, const value_type & y) const
{
return comp(x.first, y.first);
}
};
//Iterators
//points to smallest key !
iterator begin(void)
{
return (iterator(this->_tree.begin(), this->_tree.begin(), this->_tree.rbegin()));
}
const_iterator begin(void) const
{
return (const_iterator(this->_tree.begin(), this->_tree.begin(), this->_tree.rbegin()));
}
iterator end(void)
{
return (iterator(NULL, this->_tree.begin(), this->_tree.rbegin()));
}
const_iterator end(void) const
{
return (const_iterator(NULL, this->_tree.begin(), this->_tree.rbegin()));
}
//points to largest key !!
reverse_iterator rbegin(void)
{
return (reverse_iterator(
iterator(this->_tree.rbegin(), this->_tree.begin(), this->_tree.rbegin())));
}
const_reverse_iterator rbegin(void) const
{
return (const_reverse_iterator(
const_iterator(this->_tree.rbegin(), this->_tree.begin(), this->_tree.rbegin())));
}
reverse_iterator rend(void)
{
return (reverse_iterator(iterator(NULL, this->_tree.begin(), this->_tree.rbegin())));
}
const_reverse_iterator rend(void) const
{
return (const_reverse_iterator(
const_iterator(NULL, this->_tree.begin(), this->_tree.rbegin())));
}
//Modifiers
ft::pair<iterator, bool> insert(value_type const & val)
{
iterator in(find(val.first));
if (in != iterator())
return (ft::make_pair(in, false));
in = iterator(this->_tree.insert(val));
return (ft::make_pair(in, true));
}
iterator insert(iterator pos, const value_type& val)
{
if (pos != iterator())
return (this->_tree.insert(val));
return (iterator());
}
template <class InputIterator>
void insert (InputIterator first, InputIterator last)
{
while (first != last)
{
this->_tree.insert(*first);
first++;
}
}
size_type erase(key_type const & k)
{
return (this->_tree.erase(k));
}
void erase(iterator pos)
{
this->_tree.erase((*pos).first);
}
void erase(iterator first, iterator last)
{
ft::vector<key_type> tmp;
typename ft::vector<key_type>::iterator in;
while (first != last)
{
tmp.push_back((*first).first);
first++;
}
in = tmp.begin();
while (in != tmp.end())
{
erase(*in);
in++;
}
}
void swap(map & scd)
{
map<key_type, mapped_type, key_compare, allocator_type> tmp(scd);
scd.clear();
scd = *this;
this->clear();
*this = tmp;
}
void clear(void)
{
this->_tree.clear();
}
//Capacity
bool empty(void) const {return (this->_tree.empty());}
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[](const key_type & k)
{
return ((*((this->insert(ft::make_pair(k,mapped_type()))).first)).second);
}
//Observers
key_compare key_comp() const { return _comp;}
value_compare value_comp() const {return value_compare(_comp);}
//operations
iterator find(key_type const & key)
{
return (iterator(this->_tree.find(key)));
}
size_type count(key_type const & k)
{
if (this->_tree.find(k) != NULL)
return (1);
return (0);
}
iterator lower_bound(key_type const & key)
{
iterator it = begin();
while (it != end() && _comp((*it).first, key))
it++;
return (it);
}
iterator upper_bound(key_type const & key)
{
iterator it = begin();
while (it != end() && !_comp(key, (*it).first))
it++;
return (it);
}
ft::pair<iterator, iterator> equal_range(key_type const & key)
{
return (ft::make_pair(lower_bound(key), upper_bound(key)));
}
allocator_type get_allocator() const
{
return (_alloc);
}
};
//nonmember relational operators
template <class U, class V, class C, class A>
bool operator== ( const map<U,V,C,A>& lhs, const map<U,V,C,A>& rhs )
{
if (lhs.size() != rhs.size())
return (false);
return (ft::equal(lhs.begin(), lhs.end(), rhs.begin()));
}
template <class U, class V, class C, class A>
bool operator!= ( const map<U,V,C,A>& lhs, const map<U,V,C,A>& rhs )
{
return (!(lhs == rhs));
}
template <class U, class V, class C, class A>
bool operator< ( const map<U,V,C,A>& lhs, const map<U,V,C,A>& rhs )
{
return (ft::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end()));
}
template <class U, class V, class C, class A>
bool operator> ( const map<U,V,C,A>& lhs, const map<U,V,C,A>& rhs )
{
return ((rhs < lhs));
}
template <class U, class V, class C, class A>
bool operator>=( const map<U,V,C,A>& lhs, const map<U,V,C,A>& rhs )
{
return (rhs < lhs);
}
template <class U, class V, class C, class A>
bool operator<=( const map<U,V,C,A>& lhs, const map<U,V,C,A>& rhs )
{
return (!(rhs < lhs));
}
} //end namespace
#endif