-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.hpp
267 lines (234 loc) · 8.75 KB
/
set.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* set.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dpoveda- <me@izenynn.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 13:13:37 by dpoveda- #+# #+# */
/* Updated: 2022/07/26 17:33:17 by dpoveda- ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SET_HPP_
# define SET_HPP_
# include <iostream>
# include <memory>
# include "utils/pair.hpp"
# include "utils/rbnode.hpp"
# include "utils/rbtree.hpp"
# include "utils/rbiterator.hpp"
# include "utils/reverse_iterator.hpp"
# include "utils/swap.hpp"
# include "utils/equal.hpp"
# include "utils/lexicographical_compare.hpp"
namespace ft {
template<
class Key,
class Compare = ::std::less<Key>,
class Allocator = ::std::allocator<Key>
> class set {
public:
// typedefs
typedef Key key_type;
typedef key_type value_type;
typedef Compare key_compare;
typedef key_compare value_compare;
typedef Allocator allocator_type;
typedef ft::rbnode<value_type> node_type;
typedef node_type* node_pointer;
typedef node_type& node_reference;
typedef typename allocator_type::size_type size_type;
typedef typename allocator_type::difference_type difference_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;
private:
typedef ft::rbtree<value_type, value_compare, allocator_type> tree_type;
public:
typedef typename tree_type::const_iterator iterator;
typedef typename tree_type::const_iterator const_iterator;
typedef typename ft::reverse_iterator<iterator> reverse_iterator;
typedef typename ft::reverse_iterator<const_iterator> const_reverse_iterator;
// constructors
explicit
set(const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type())
: _tree(), _comp(comp), _alloc(alloc), _value_comp(value_compare()) {}
template<class InputIterator>
set(InputIterator first,
InputIterator last,
const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type()) {
this->_comp = comp;
this->_alloc = alloc;
this->_value_comp = value_compare();
for (InputIterator it = first; it != last; ++it) {
this->_tree.insert(*it);
}
}
set(const set& other) : _tree(other._tree.comp, other._tree.alloc) {
insert(other.begin(), other.end());
}
~set() { this->clear(); }
// assign operator
set& operator=(const set& other) {
this->clear();
this->insert(other.begin(), other.end());
this->_alloc = other._alloc;
this->_comp = other._comp;
this->_value_comp = other._value_comp;
return (*this);
}
// iterators
iterator begin() { return this->_tree.begin(); }
const_iterator begin() const { return this->_tree.begin(); }
iterator end() { return this->_tree.end(); }
const_iterator end() const { return this->_tree.end(); }
reverse_iterator rbegin() { return this->_tree.rbegin(); }
const_reverse_iterator rbegin() const { return this->_tree.rbegin(); }
reverse_iterator rend() { return this->_tree.rend(); }
const_reverse_iterator rend() const { return this->_tree.rend(); }
// capacity
bool empty() const { return this->_tree.empty(); }
size_type size() const { return this->_tree.getSize(); }
size_type max_size() const { return this->_tree.getMaxSize(); }
// operators
// member functions
ft::pair<iterator, bool> insert(const value_type& value) {
return this->_tree.insert(value);
}
iterator insert(iterator hint, const value_type& value) {
(void)hint;
return this->_tree.insert(value).first;
}
template<class InputIt>
void insert(InputIt first, InputIt last) {
for (InputIt it = first; it != last; ++it) {
this->_tree.insert(*it);
}
}
void erase(iterator pos) {
this->_tree.remove(*pos);
}
void erase(iterator first, iterator last) {
for (iterator it = first; it != last;) {
this->_tree.remove(*(it++));
}
}
size_type erase(const key_type& key) {
iterator res = this->find(key);
if (res != this->end()) {
this->_tree.remove(*res);
return 1;
}
return 0;
}
void swap(set& other) {
this->_tree.swap(other._tree);
ft::swap(this->_comp, other._comp);
ft::swap(this->_alloc, other._alloc);
ft::swap(this->_value_comp, other._value_comp);
}
void clear() { this->_tree.clear(); }
key_compare key_comp() const { return this->_comp; }
value_compare value_comp() const { return this->_value_comp; }
iterator find(const key_type& key) {
node_pointer res = this->_tree.find(key);
if (res != ft::nullptr_t) {
return iterator(res, this->_tree.getNodeRoot(), this->_tree.getNodeNil());
}
return this->end();
}
const_iterator find(const key_type& key) const {
node_pointer res = this->_tree.find(key);
if (res != ft::nullptr_t) {
return const_iterator(res, this->_tree.getNodeRoot(), this->_tree.getNodeNil());
}
return this->end();
}
size_type count(const key_type& key) const {
if (this->_tree.find(key) != ft::nullptr_t) {
return 1;
}
return 0;
}
iterator lower_bound(const key_type& key) {
for (iterator it = this->begin(); it != this->end(); ++it) {
if(this->_value_comp(key, it.getCurrent()->data)
|| !(this->_value_comp(it.getCurrent()->data, key)))
return it;
}
return this->end();
}
const_iterator lower_bound(const key_type& key) const {
for (const_iterator it = this->begin(); it != this->end(); ++it) {
if(this->_value_comp(key, it.getCurrent()->data)
|| !(this->_value_comp(it.getCurrent()->data, key)))
return it;
}
return this->end();
}
iterator upper_bound(const key_type& key) {
for (iterator it = this->begin(); it != this->end(); ++it) {
if(this->_value_comp(key, it.getCurrent()->data)) {
return it;
}
}
return this->end();
}
const_iterator upper_bound(const key_type& key) const {
for (const_iterator it = this->begin(); it != this->end(); ++it) {
if(this->_value_comp(key, it.getCurrent()->data)) {
return it;
}
}
return this->end();
}
ft::pair<iterator, iterator> equal_range(const key_type& key) {
return ft::make_pair(this->lower_bound(key), this->upper_bound(key));
}
ft::pair<const_iterator, const_iterator> equal_range(const key_type& key) const {
return ft::make_pair(this->lower_bound(key), this->upper_bound(key));
}
allocator_type get_allocator() const { return this->_alloc; }
private:
tree_type _tree;
key_compare _comp;
allocator_type _alloc;
value_compare _value_comp;
};
// non-member operators
template< class Key, class Compare, class Alloc >
bool operator==( const ft::set<Key, Compare, Alloc>& lhs, const ft::set<Key, Compare, Alloc>& rhs ) {
if (lhs.size() != rhs.size()) {
return false;
}
return ft::equal(lhs.begin(), lhs.end(), rhs.begin());
}
template< class Key, class Compare, class Alloc >
bool operator!=( const ft::set<Key, Compare, Alloc>& lhs, const ft::set<Key, Compare, Alloc>& rhs ) {
return !(lhs == rhs);
}
template< class Key, class Compare, class Alloc >
bool operator<( const ft::set<Key, Compare, Alloc>& lhs, const ft::set<Key, Compare, Alloc>& rhs ) {
return ft::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
template< class Key, class Compare, class Alloc >
bool operator>( const ft::set<Key, Compare, Alloc>& lhs, const ft::set<Key, Compare, Alloc>& rhs ) {
return rhs < lhs;
}
template< class Key, class Compare, class Alloc >
bool operator<=( const ft::set<Key, Compare, Alloc>& lhs, const ft::set<Key, Compare, Alloc>& rhs ) {
return !(rhs < lhs);
}
template< class Key, class Compare, class Alloc >
bool operator>=( const ft::set<Key, Compare, Alloc>& lhs, const ft::set<Key, Compare, Alloc>& rhs ) {
return !(lhs < rhs);
}
template< class Key, class Compare, class Alloc >
void swap(ft::set<Key, Compare, Alloc>& lhs, ft::set<Key, Compare, Alloc>& rhs) {
lhs.swap(rhs);
}
}
#endif