-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.hpp
424 lines (383 loc) · 13.6 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: smia <smia@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/21 20:34:14 by smia #+# #+# */
/* Updated: 2022/12/30 05:56:43 by smia ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <iostream>
#include "iterator.hpp"
#include "pair.hpp"
#include "lexicographical_compare.hpp"
#include "vector.hpp"
namespace ft
{
template<class Key,class T,class Compare = std::less<Key>,class alloc = std::allocator<ft::pair< const Key, T> > >
class map
{
public:
typedef Key Key_type;
typedef T mapped_key;
typedef ft::pair< const Key_type, mapped_key> value_type;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef Compare key_compare;
typedef alloc allocator_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef typename allocator_type::pointer pointer;
typedef typename allocator_type::const_pointer const_pointer;
typedef typename ft::bidirectional_iterator<value_type, key_compare, allocator_type> iterator;
typedef typename ft::bidirectional_iterator<const value_type, key_compare, allocator_type> const_iterator;
typedef typename ft::reverse_iterator<iterator> reverse_iterator;
typedef typename ft::reverse_iterator<const_iterator> const_reverse_iterator;
class value_compare : public std::binary_function<value_type, value_type, bool>
{
friend class map;
protected:
Compare _cmp;
value_compare(Compare cmp)
{
_cmp = cmp;
}
public:
typedef bool result_type;
typedef value_type first;
typedef value_type secound;
bool operator()( const value_type& lhs, const value_type& rhs ) const
{
return _cmp(lhs.first, rhs.first);
}
};
///// constructors
explicit map (const key_compare& comp = key_compare(), const allocator_type& Alloc = allocator_type()) : _alloc(Alloc), _compare(comp)
{
_size = 0;
}
template <class InputIterator>
map (InputIterator first, InputIterator last,const key_compare& comp = key_compare(), const allocator_type& Alloc = allocator_type()) : _alloc(Alloc), _compare(comp)
{
while(first != last)
{
insert(*first);
++first;
}
_size = _avl.get_size();
}
map( const map& other )
{
_avl = other._avl;
_compare = other._compare;
_size = other._size;
}
allocator_type get_allocator() const
{
return (_alloc);
}
map& operator=( const map& other )
{
_compare = other._compare;
_avl = other._avl;
_size = other._size;
return (*this);
}
~map()
{
clear();
}
// iterators
iterator begin()
{
node<value_type, alloc>* ptr = _avl.min_node(_avl.get_root());
if (ptr == NULL)
return (iterator(NULL, &_avl));
return (iterator(ptr->_value, &_avl));
}
const_iterator begin() const
{
node<value_type, alloc>* ptr = _avl.min_node(_avl.get_root());
if (ptr == NULL)
return (iterator(NULL, &_avl));
return (iterator(ptr->_value, &_avl));
}
iterator end()
{
return (iterator(NULL, &_avl));
}
const_iterator end() const
{
return (iterator(NULL, &_avl));
}
reverse_iterator rbegin()
{
return (reverse_iterator(end()));
}
const_reverse_iterator rbegin() const
{
return (const_reverse_iterator(end()));
}
reverse_iterator rend()
{
return (reverse_iterator(begin()));
}
const_reverse_iterator rend() const
{
return(const_reverse_iterator(begin()));
}
// modifiers
void clear()
{
_avl.Clear();
_size = 0;
}
ft::pair<iterator, bool> insert( const value_type& value )
{
iterator it;
node<value_type, alloc>* Node = _avl.helper_search(_avl.get_root(), value);
if(Node != NULL)
{
it = iterator(Node->_value, &_avl);
return (ft::make_pair(it,false));
}
else
{
node<value_type, alloc>* ptr = _avl.insert(value);
_size = _avl.get_size();
it = iterator(ptr->_value, &_avl);
return (ft::make_pair(it,true));
}
}
iterator insert( iterator pos, const value_type& value )
{
(void)pos;
pair<iterator, bool> p = insert(value);
return p.first;
}
template< class InputIt >
void insert( InputIt first, InputIt last )
{
while(first != NULL && first != last)
{
_avl.insert(*first);
++first;
}
_size = _avl.get_size();
}
void erase( iterator pos )
{
_avl.Delete(*pos);
_size = _avl.get_size();
}
void erase( iterator first, iterator last )
{
ft::vector<Key> keys;
while (first != last)
{
keys.push_back(first->first);
++first;
}
for(size_t i = 0; i < keys.size(); i++)
erase(keys[i]);
}
size_type erase( const Key& key )
{
size_type size = _size;
value_type p = ft::make_pair<const Key, T>(key, T());
_avl.Delete(p);
if (size == _size)
return 0;
_size = _avl.get_size();
return 1;
}
void swap( map& other )
{
size_t sizeTmp = _size;
_size = other._size;
other._size = sizeTmp;
_avl.swap(other._avl);
std::swap(_compare, other._compare);
}
// element access
T& at( const Key& key )
{
node<value_type, alloc>* ptr = _avl.helper_search(_avl.get_root(), ft::make_pair<Key_type, mapped_key>(key, mapped_key()));
if (ptr != NULL)
return ptr->_value->second;
throw std::out_of_range("out of range\n");
}
const T& at( const Key& key ) const
{
node<value_type, alloc>* ptr = _avl.helper_search(_avl.get_root(), ft::make_pair<Key_type, mapped_key>(key, mapped_key()));
if (ptr != NULL)
return ptr->_value->second;
throw std::out_of_range("out of range\n");
}
T& operator[]( const Key& k )
{
value_type p = ft::make_pair<const Key, T>(k, T());
node<value_type, alloc>* Node = _avl.helper_search(_avl.get_root(), p);
if (!Node) {
Node = _avl.insert(p);
_size = _avl.get_size();
return (Node->_value->second);
}
return Node->_value->second;
}
// capacity
bool empty() const
{
return (_size == 0);
}
size_type size() const
{
return _size;
}
size_type max_size() const
{
return (_alloc.max_size());
}
// Lookup
size_type count( const Key& key ) const
{
node<value_type, alloc>* ptr = _avl.helper_search(_avl.get_root(), ft::make_pair<Key_type, mapped_key>(key, mapped_key()));
if (ptr != NULL)
return 1;
return 0;
}
iterator find( const Key& key )
{
node<value_type, alloc>* ptr = _avl.helper_search(_avl.get_root(), ft::make_pair<Key_type, mapped_key>(key, mapped_key()));
if (ptr == NULL)
return (end());
return (iterator(ptr->_value, &_avl));
}
const_iterator find( const Key& key ) const
{
node<value_type, alloc>* ptr = _avl.helper_search(_avl.get_root(), ft::make_pair<Key_type, mapped_key>(key, mapped_key()));
if (ptr == NULL)
return (end());
return (const_iterator(ptr->_value, &_avl));
}
iterator lower_bound( const Key& key )
{
iterator it = this->begin();
while(it != this->end())
{
if (!_compare((*it).first, key))
break ;
++it;
}
return it;
}
const_iterator lower_bound( const Key& key ) const
{
const_iterator it = this->begin();
while(it != this->end())
{
if (!_compare((*it).first, key))
break ;
++it;
}
return it;
}
iterator upper_bound( const Key& key )
{
iterator it = this->begin();
while(it != this->end())
{
if (_compare(key, (*it).first))
break ;
++it;
}
return it;
}
const_iterator upper_bound( const Key& key ) const
{
const_iterator it = this->begin();
while(it != this->end())
{
if (_compare(key, (*it).first))
break ;
++it;
}
return it;
}
ft::pair<iterator,iterator> equal_range( const Key& key )
{
return (ft::make_pair(lower_bound(key), upper_bound(key)));
}
ft::pair<const_iterator,const_iterator> equal_range( const Key& key ) const
{
return (make_pair(lower_bound(key), upper_bound(key)));
}
// Observers
key_compare key_comp() const
{
return _compare;
}
value_compare value_comp() const
{
return (value_compare(_compare));
}
void print()
{
_avl.printTree(_avl.get_root());
}
private:
AvlTree<value_type, Compare, alloc> _avl;
size_type _size;
allocator_type _alloc;
key_compare _compare;
};
template< class Key, class T, class Compare, class alloc >
void swap( ft::map<Key,T,Compare,alloc>& lhs, ft::map<Key,T,Compare,alloc>& rhs )
{
lhs.swap(rhs);
}
template< class Key, class T, class Compare, class alloc >
bool operator==( const typename ft::map<Key,T,Compare,alloc>& lhs,const typename ft::map<Key,T,Compare,alloc>& rhs )
{
typename ft::map<Key, T, Compare, alloc>::const_iterator it = lhs.begin();
typename ft::map<Key, T, Compare, alloc>::const_iterator itt = rhs.begin();
while(it != lhs.end() && itt != rhs.end())
{
if (it != itt)
return false;
++it;
++itt;
}
if (it == itt)
return true;
return false;
}
template< class Key, class T, class cmp, class alloc >
bool operator!=( const ft::map<Key,T, cmp,alloc>& lhs,const ft::map<Key,T, cmp,alloc>& rhs )
{
return (!(lhs == rhs));
}
template< class Key, class T, class cmp, class alloc >
bool operator>( const ft::map<Key,T, cmp,alloc>& lhs,const ft::map<Key,T, cmp,alloc>& rhs )
{
return (ft::lexicographical_compare(rhs.begin(), rhs.end(), lhs.begin(), lhs.end()));
}
template< class Key, class T, class cmp, class alloc >
bool operator<( const ft::map<Key,T,cmp,alloc>& lhs,const ft::map<Key,T,cmp,alloc>& rhs )
{
return (ft::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end()));
}
template< class Key, class T, class cmp, class alloc >
bool operator<=( const ft::map<Key,T,cmp,alloc>& lhs,const ft::map<Key,T,cmp,alloc>& rhs )
{
return ((lhs == rhs) || (lhs < rhs));
}
template< class Key, class T, class cmp, class alloc >
bool operator>=( const ft::map<Key,T,cmp,alloc>& lhs,const ft::map<Key,T,cmp,alloc>& rhs )
{
return ((lhs == rhs) || (lhs > rhs));
}
};