-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_set.hpp
executable file
·286 lines (231 loc) · 6.51 KB
/
ft_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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_set.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: areggie <areggie@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/24 19:17:13 by areggie #+# #+# */
/* Updated: 2022/06/12 16:51:39 by areggie ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_SET_HPP
# define FT_SET_HPP
#include "ft_redblacktree.hpp"
#include "ft_utility.hpp"
namespace ft
{
template<
class Key,
class Compare = std::less<Key>,
class Allocator = std::allocator<Key> >
class set
{
public:
typedef Key key_type;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef Compare key_compare;
typedef key_compare value_compare;
typedef key_type value_type;
typedef Allocator 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 RBTree<value_type, key_compare, allocator_type> tree_type;
typedef typename tree_type::iterator iterator;
typedef typename tree_type::const_iterator const_iterator;
typedef typename tree_type::reverse_iterator reverse_iterator;
typedef typename tree_type::const_reverse_iterator const_reverse_iterator;
private:
tree_type rbtree_;
public:
explicit set(const key_compare &comp = key_compare(), const allocator_type &alloc = allocator_type()) : rbtree_(tree_type(comp, alloc)) {}
template< class InputIt >
set(InputIt first, InputIt last, const Compare & comp = Compare(), const Allocator &alloc = Allocator()): rbtree_(tree_type(first, last, comp, alloc)) {}
set(const set& other): rbtree_(tree_type(other.rbtree_)) {}
set operator=(const set & other)
{
this->rbtree_ = other.rbtree_;
return (*this);
}
allocator_type get_allocator(void) const
{
return ( this->rbtree_.get_allocator());
}
size_type size(void) const
{
return (this->rbtree_.size());
}
size_type max_size(void) const
{
return (this->rbtree_.max_size());
}
bool empty(void) const
{
return (this->rbtree_.empty());
}
value_compare value_comp(void) const
{
return (this->rbtree_.value_comp());
}
key_compare key_comp() const
{
return (this->value_comp());
}
iterator begin(void)
{
return (this->rbtree_.begin());
}
const_iterator begin(void) const
{
return (this->rbtree_.begin());
}
iterator end(void)
{
return (this->rbtree_.end());
}
const_iterator end(void) const
{
return (this->rbtree_.end());
}
reverse_iterator rbegin(void)
{
return (this->rbtree_.rbegin());
}
const_reverse_iterator rbegin(void) const
{
return (this->rbtree_.rbegin());
}
reverse_iterator rend(void)
{
return (this->rbtree_.rend());
}
const_reverse_iterator rend(void) const
{
return (this->rbtree_.rend());
}
void clear(void)
{
this->rbtree_.clear();
}
void swap(set & other)
{
this->rbtree_.swap(other.rbtree_);
}
iterator find(const Key& key)
{
return this->rbtree_.find(key);
}
const_iterator find(const Key& key) const
{
return this->rbtree_.find(key);
}
size_type count(const Key &key) const
{
return this->rbtree_.count(key);
}
iterator lower_bound(const key_type& k)
{
return this->rbtree_.lower_bound(k);
}
const_iterator lower_bound(const key_type& k) const
{
return this->rbtree_.lower_bound(k);
}
iterator upper_bound(const key_type& k)
{
return this->rbtree_.lower_bound(k);
}
const_iterator upper_bound(const key_type& k) const
{
return this->rbtree_.lower_bound(k);
}
pair<iterator, iterator> equal_range(const key_type & k)
{
return this->rbtree_.equal_range(k);
}
pair<const_iterator, const_iterator> equal_range(const key_type & k) const
{
return this->rbtree_.equal_range(k);
}
ft::pair<iterator, bool> insert( const value_type& val )
{
return this->rbtree_.insert(val);
}
iterator insert( iterator hint, const value_type& val)
{
return this->rbtree_.insert(hint, val);
}
template<class InputIt>
void insert(typename ft::enable_if< !ft::is_integral<InputIt>::value, InputIt>::type first, InputIt last)
{
this->rbtree_.insert(first, last);
}
void erase(iterator pos)
{
this->rbtree_.erase(pos);
}
size_type erase(const Key & key)
{
return this->rbtree_.erase(key);
}
void erase(iterator first, iterator last)
{
this->rbtree_.erase(first, last);
}
template<class K, class Com, class Al>
friend bool
operator==(const set<K, Com, Al>& lhs, const set<K, Com, Al>& rhs);
template<class K, class Com, class Al>
friend bool
operator<(const set<K, Com, Al>& lhs, const set<K, Com, Al>& rhs);
};
template<class Key, class Compare, class Alloc>
bool operator==(const set<Key, Compare, Alloc>& lhs,
const set<Key, Compare, Alloc>& rhs)
{
return (lhs.rbtree_ == rhs.rbtree_);
}
template<class Key, class Compare, class Alloc>
bool operator!=(const set<Key, Compare, Alloc>& lhs,
const set<Key, Compare, Alloc>& rhs)
{
return !(lhs == rhs);
}
template<class Key, class Compare, class Alloc>
bool operator<(const set<Key, Compare, Alloc>& lhs,
const set<Key, Compare, Alloc>& rhs)
{
return (lhs.rbtree_ < rhs.rbtree_);
}
template<class Key, class Compare, class Alloc>
bool operator>(const set<Key, Compare, Alloc>& lhs,
const set<Key, Compare, Alloc>& rhs)
{
return (rhs < lhs);
}
template<class Key, class Compare, class Alloc>
bool operator<=(const set<Key, Compare, Alloc>& lhs,
const set<Key, Compare, Alloc>& rhs)
{
return !(lhs > rhs);
}
template<class Key, class Compare, class Alloc>
bool operator>=(const set<Key, Compare, Alloc>& lhs,
const set<Key, Compare, Alloc>& rhs)
{
return !(lhs < rhs);
}
};
namespace std
{
template<class Key,class Compare, class Alloc>
void swap(const ft::set<Key, Compare, Alloc>& lhs,
const ft::set<Key, Compare, Alloc>& rhs)
{
lhs.swap(rhs);
}
}
#endif