-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathset.hpp
More file actions
239 lines (185 loc) · 5.33 KB
/
set.hpp
File metadata and controls
239 lines (185 loc) · 5.33 KB
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
#ifndef __SET_HPP__
# define __SET_HPP__
#include "Tree.hpp"
namespace ft {
template <class T, class Compare = std::less<T>,
class Alloc = std::allocator<T> >
class set
{
public:
typedef typename delConst<T>::type key_type;
typedef typename delConst<T>::type value_type;
typedef Compare key_compare;
typedef Compare value_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 typename allocator_type::size_type size_type;
typedef typename allocator_type::difference_type difference_type;
typedef Tree<value_type, value_compare, allocator_type> Tree_type;
public:
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;
set (const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type()):_tree(new Tree_type(comp, alloc)),
_comp(comp), _alloc(alloc)
{
}
template <class InputIterator>
set (InputIterator first, InputIterator last,
const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type()): _tree (new Tree_type(first, last, comp, alloc)),
_comp(comp), _alloc(alloc){
}
set (const set& x): _tree (new Tree_type(*x._tree)), _comp(x._comp), _alloc(x._alloc) {
}
set& operator= (const set& x) {
*_tree = *x._tree;
_comp = x.key_comp();
_alloc = x.get_allocator();
return *this;
}
~set() {
delete _tree;
}
iterator begin() {
return _tree->begin();
}
const_iterator begin() const {
return _tree->begin();
}
iterator end() {
return _tree->end();
}
const_iterator end() const {
return _tree->end();
}
reverse_iterator rbegin() {
return _tree->rbegin();
}
const_reverse_iterator rbegin() const {
return _tree->rbegin();
}
reverse_iterator rend() {
return _tree->rend();
}
const_reverse_iterator rend() const {
return _tree->rend();
}
bool empty() const {
return _tree->empty();
}
size_type size() const {
return _tree->size();
}
size_type max_size() const {
return _tree->max_size();
}
// mapped_type& operator[] (const key_type& k) {
// iterator it = _tree->find(k);
// if (it != end())
// return it->second;
// else
// return (*((this->insert(ft::make_pair(k, mapped_type()))).first)).second;
// }
pair<iterator,bool> insert (const value_type& val) {
return _tree->insert(val);
}
iterator insert (iterator position, const value_type& val) {
return _tree->insert(position, val);
}
template <class InputIterator>
void insert (InputIterator first, InputIterator last,
typename ft::enable_if<!ft::is_integral<InputIterator>::value>::type* = 0) {
_tree->insert(first, last);
}
void erase (iterator position) {
_tree->erase(position);
}
size_type erase (const key_type& k) {
return _tree->erase(k);
}
void erase (iterator first, iterator last) {
for (; first != last;) {
_tree->erase(first++);
}
}
void swap (set& x) {
std::swap(this->_tree, x._tree);
}
void clear() {
_tree->clear();
}
key_compare key_comp() const {
return _comp;
}
value_compare value_comp() const {
return value_compare(_comp);
}
iterator find (const key_type& k) {
return _tree->find(k);
}
const_iterator find (const key_type& k) const {
return _tree->find(k);
}
size_type count (const key_type& k) const {
return _tree->count(k);
}
iterator lower_bound (const key_type& k) {
return _tree->lower_bound(k);
}
const_iterator lower_bound (const key_type& k) const {
return _tree->lower_bound(k);
}
iterator upper_bound (const key_type& k) {
return _tree->upper_bound(k);
}
const_iterator upper_bound (const key_type& k) const {
return _tree->upper_bound(k);
}
pair<const_iterator,const_iterator> equal_range (const key_type& k) const {
return pair<const_iterator, const_iterator>(lower_bound(k), upper_bound(k));
}
pair<iterator,iterator> equal_range (const key_type& k) {
return pair<iterator, iterator>(lower_bound(k), upper_bound(k));
}
allocator_type get_allocator() const {
return _alloc;
}
friend bool operator==( const set<T,Compare,Alloc>& lhs,
const set<T,Compare,Alloc>& rhs ) {
if (lhs.size() != rhs.size())
return false;
return ft::equal(lhs.begin(), lhs.end(), rhs.begin());
}
friend bool operator!=( const set<T,Compare,Alloc>& lhs,
const set<T,Compare,Alloc>& rhs ) {
return !(lhs == rhs);
}
friend bool operator<( const set<T,Compare,Alloc>& lhs,
const set<T,Compare,Alloc>& rhs ) {
return ft::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
friend bool operator<=( const set<T,Compare,Alloc>& lhs,
const set<T,Compare,Alloc>& rhs ) {
return !(rhs < lhs);
}
friend bool operator>( const set<T,Compare,Alloc>& lhs,
const set<T,Compare,Alloc>& rhs ) {
return (rhs < lhs);
}
friend bool operator>=( const set<T,Compare,Alloc>& lhs,
const set<T,Compare,Alloc>& rhs ) {
return !(lhs < rhs);
}
private:
Tree_type *_tree;
key_compare _comp;
allocator_type _alloc;
};
}
#endif