-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.hpp
318 lines (253 loc) · 7.01 KB
/
utils.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: badam <badam@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/09/10 16:50:18 by badam #+# #+# */
/* Updated: 2021/12/20 19:21:14 by badam ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef UTILS_HPP
# define UTILS_HPP
# include <utility>
namespace ft
{
template<class T>
struct remove_const {
typedef T type;
};
template<class T>
struct remove_const<const T> {
typedef typename remove_const<T>::type type;
};
template <class T, T v>
struct integral_constant {
static const T value = v;
typedef T value_type;
typedef integral_constant<T,v> type;
operator const T() { return v; }
};
template <class T>
struct is_integral: integral_constant<bool, false> {};
template <>
struct is_integral<bool>: integral_constant<bool, true> {};
template <>
struct is_integral<char>: integral_constant<bool, true> {};
template <>
struct is_integral<wchar_t>: integral_constant<bool, true> {};
template <>
struct is_integral<signed char>: integral_constant<bool, true> {};
template <>
struct is_integral<short int>: integral_constant<bool, true> {};
template <>
struct is_integral<int>: integral_constant<bool, true> {};
template <>
struct is_integral<long int>: integral_constant<bool, true> {};
template <>
struct is_integral<long long int>: integral_constant<bool, true> {};
template <>
struct is_integral<unsigned char>: integral_constant<bool, true> {};
template <>
struct is_integral<unsigned short int>: integral_constant<bool, true> {};
template <>
struct is_integral<unsigned int>: integral_constant<bool, true> {};
template <>
struct is_integral<unsigned long int>: integral_constant<bool, true> {};
template <>
struct is_integral<unsigned long long int>: integral_constant<bool, true> {};
template<bool Cond, class T = void> struct enable_if {};
template<class T> struct enable_if<true, T> { typedef T type; };
template <class InputIterator1, class InputIterator2>
bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2)
{
while (first1 != last1)
{
if (first2 == last2 || *first2 < *first1)
return false;
else if (*first1 < *first2)
return true;
++first1;
++first2;
}
return (first2 != last2);
}
template <class InputIterator>
InputIterator advance(InputIterator it, typename InputIterator::difference_type n) // may use "+=" and "-="
{
if (n > 0)
{
while (n--)
++it;
}
else
while (n++)
--it;
return (it);
}
template <class InputIterator>
typename InputIterator::difference_type distance(InputIterator first, InputIterator last)
{
typename InputIterator::difference_type i = 0;
while (first++ != last)
++i;
return i;
}
template< class Arg1, class Arg2, class Result >
struct binary_function
{
typedef Result result_type;
typedef Arg1 first_argument_type;
typedef Arg2 second_argument_type;
};
template< class T >
struct less: binary_function< T, T, bool >
{
bool operator()( const T& lhs, const T& rhs ) const
{
return lhs < rhs;
}
};
template <class T1, class T2>
struct pair
{
typedef typename remove_const<T1>::type T1_core;
typedef typename remove_const<T2>::type T2_core;
T1 first;
T2 second;
pair(void)
{
};
pair(const T1 &x, const T2 &y): first(x), second(y)
{
};
pair(const pair<T1_core, T2_core> &p): first(p.first), second(p.second)
{
};
pair &operator=(const pair<T1_core, T2_core> &ref)
{
if (this != &ref)
{
first = ref.first;
second = ref.second;
}
return (*this);
}
};
template< class T1, class T2 >
bool operator==(const ft::pair<T1,T2> &lhs,const ft::pair<T1,T2> &rhs)
{
return (lhs.first == rhs.first && lhs.second == rhs.second);
}
template< class T1, class T2 >
bool operator!=(const ft::pair<T1,T2> &lhs,const ft::pair<T1,T2> &rhs)
{
return (!(lhs == rhs));
}
template<class T1, class T2>
bool operator<(const ft::pair<T1, T2> &lhs, const ft::pair<T1, T2> &rhs)
{
if (lhs.first < rhs.first)
return (true);
else if (lhs.first > rhs.first)
return (false);
else
return (lhs.second < rhs.second);
}
template<class T1, class T2>
bool operator<=(const ft::pair<T1, T2> &lhs, const ft::pair<T1, T2> &rhs)
{
return (!(rhs < lhs));
}
template<class T1, class T2>
bool operator>(const ft::pair<T1, T2> &lhs, const ft::pair<T1, T2> &rhs)
{
return (!(lhs <= rhs));
}
template<class T1, class T2>
bool operator>=(const ft::pair<T1, T2> &lhs, const ft::pair<T1, T2> &rhs)
{
return (!(lhs < rhs));
}
template <class T1, class T2>
pair<T1, T2> make_pair(T1 t, T2 u)
{
pair<T1, T2> elem(t, u);
return (elem);
}
/*
** ft::pair and std::pair mixing
*/
template< class T1, class T2 >
bool operator==(const std::pair<T1,T2> &lhs,const ft::pair<T1,T2> &rhs)
{
return (lhs.first == rhs.first && lhs.second == rhs.second);
}
template< class T1, class T2 >
bool operator!=(const std::pair<T1,T2> &lhs,const ft::pair<T1,T2> &rhs)
{
return (!(lhs == rhs));
}
template<class T1, class T2>
bool operator<(const std::pair<T1, T2> &lhs, const ft::pair<T1, T2> &rhs)
{
if (lhs.first < rhs.first)
return (true);
else if (lhs.first > rhs.first)
return (false);
else
return (lhs.second < rhs.second);
}
template<class T1, class T2>
bool operator<=(const std::pair<T1, T2> &lhs, const ft::pair<T1, T2> &rhs)
{
return (!(rhs < lhs));
}
template<class T1, class T2>
bool operator>(const std::pair<T1, T2> &lhs, const ft::pair<T1, T2> &rhs)
{
return (!(lhs <= rhs));
}
template<class T1, class T2>
bool operator>=(const std::pair<T1, T2> &lhs, const ft::pair<T1, T2> &rhs)
{
return (!(lhs < rhs));
}
template< class T1, class T2 >
bool operator==(const ft::pair<T1,T2> &lhs,const std::pair<T1,T2> &rhs)
{
return (lhs.first == rhs.first && lhs.second == rhs.second);
}
template< class T1, class T2 >
bool operator!=(const ft::pair<T1,T2> &lhs,const std::pair<T1,T2> &rhs)
{
return (!(lhs == rhs));
}
template<class T1, class T2>
bool operator<(const ft::pair<T1, T2> &lhs, const std::pair<T1, T2> &rhs)
{
if (lhs.first < rhs.first)
return (true);
else if (lhs.first > rhs.first)
return (false);
else
return (lhs.second < rhs.second);
}
template<class T1, class T2>
bool operator<=(const ft::pair<T1, T2> &lhs, const std::pair<T1, T2> &rhs)
{
return (!(rhs < lhs));
}
template<class T1, class T2>
bool operator>(const ft::pair<T1, T2> &lhs, const std::pair<T1, T2> &rhs)
{
return (!(lhs <= rhs));
}
template<class T1, class T2>
bool operator>=(const ft::pair<T1, T2> &lhs, const std::pair<T1, T2> &rhs)
{
return (!(lhs < rhs));
}
}
#endif