Embedded Template Library 1.0
functional.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2014 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_FUNCTIONAL_INCLUDED
32#define ETL_FUNCTIONAL_INCLUDED
33
34#include "platform.h"
35
38
41
42namespace etl
43{
44 //***************************************************************************
47 //***************************************************************************
48 template <typename T>
50 {
51 public:
52
53 typedef T type;
54
55 ETL_CONSTEXPR20 explicit reference_wrapper(T& t_) ETL_NOEXCEPT
56 : t(&t_)
57 {
58 }
59
60 ETL_CONSTEXPR20 reference_wrapper(const reference_wrapper& rhs) ETL_NOEXCEPT
61 : t(rhs.t)
62 {
63 }
64
65 ETL_CONSTEXPR20 reference_wrapper<T>& operator = (const reference_wrapper& rhs) ETL_NOEXCEPT
66 {
67 t = rhs.t;
68 return *this;
69 }
70
71 ETL_CONSTEXPR20 T& get() const ETL_NOEXCEPT
72 {
73 return *t;
74 }
75
76 ETL_CONSTEXPR20 operator T&() const ETL_NOEXCEPT
77 {
78 return *t;
79 }
80
81 private:
82
83 T* t;
84 };
85
86 //***************************************************************************
87 template <typename T>
88 reference_wrapper<T> ref(T& t)
89 {
90 return reference_wrapper<T>(t);
91 }
92
93 //***************************************************************************
94 template <typename T>
95 reference_wrapper<T> ref(reference_wrapper<T> t)
96 {
97 return reference_wrapper<T>(t.get());
98 }
99
100 //***************************************************************************
101 template <typename T>
102 reference_wrapper<const T> cref(const T& t)
103 {
104 return reference_wrapper<const T>(t);
105 }
106
107 //***************************************************************************
108 template <typename T>
109 reference_wrapper<const T> cref(reference_wrapper<T> t)
110 {
111 return reference_wrapper<const T>(t.get());
112 }
113
114 //***************************************************************************
115 template <typename TArgumentType, typename TResultType>
117 {
118 typedef TArgumentType argument_type;
119 typedef TResultType result_type;
120 };
121
122 //***************************************************************************
123 template <typename TFirstArgumentType, typename TSecondArgumentType, typename TResultType>
125 {
126 typedef TFirstArgumentType first_argument_type;
127 typedef TSecondArgumentType second_argument_type;
128 typedef TResultType result_type;
129 };
130
131 //***************************************************************************
132 template <typename T = void>
133 struct less : public etl::binary_function<T, T, bool>
134 {
135 typedef T value_type;
136
137 ETL_CONSTEXPR bool operator()(const T &lhs, const T &rhs) const
138 {
139 return (lhs < rhs);
140 }
141 };
142
143#if ETL_USING_CPP11
144 template <>
145 struct less<void> : public etl::binary_function<void, void, bool>
146 {
147 typedef int is_transparent;
148
149 template <typename T1, typename T2>
150 constexpr auto operator()(T1&& lhs, T2&& rhs) const -> decltype(static_cast<T1&&>(lhs) < static_cast<T2&&>(rhs))
151 {
152 return static_cast<T1&&>(lhs) < static_cast<T2&&>(rhs);
153 }
154 };
155#endif
156
157 //***************************************************************************
158 template <typename T = void>
159 struct less_equal : public etl::binary_function<T, T, bool>
160 {
161 typedef T value_type;
162
163 ETL_CONSTEXPR bool operator()(const T& lhs, const T& rhs) const
164 {
165 return !(rhs < lhs);
166 }
167 };
168
169#if ETL_USING_CPP11
170 template <>
171 struct less_equal<void> : public etl::binary_function<void, void, bool>
172 {
173 typedef int is_transparent;
174
175 template <typename T1, typename T2>
176 constexpr auto operator()(T1&& lhs, T2&& rhs) const -> decltype(static_cast<T1&&>(lhs) < static_cast<T2&&>(rhs))
177 {
178 return !(static_cast<T1&&>(lhs) < static_cast<T2&&>(rhs));
179 }
180 };
181#endif
182
183 //***************************************************************************
184 template <typename T = void>
185 struct greater : public etl::binary_function<T, T, bool>
186 {
187 typedef T value_type;
188
189 ETL_CONSTEXPR bool operator()(const T &lhs, const T &rhs) const
190 {
191 return (rhs < lhs);
192 }
193 };
194
195#if ETL_USING_CPP11
196 template <>
197 struct greater<void> : public etl::binary_function<void, void, bool>
198 {
199 typedef int is_transparent;
200
201 template <typename T1, typename T2>
202 constexpr auto operator()(T1&& lhs, T2&& rhs) const -> decltype(static_cast<T1&&>(lhs) < static_cast<T2&&>(rhs))
203 {
204 return static_cast<T1&&>(rhs) < static_cast<T2&&>(lhs);
205 }
206 };
207#endif
208
209 //***************************************************************************
210 template <typename T = void>
211 struct greater_equal : public etl::binary_function<T, T, bool>
212 {
213 typedef T value_type;
214
215 ETL_CONSTEXPR bool operator()(const T& lhs, const T& rhs) const
216 {
217 return !(lhs < rhs);
218 }
219 };
220
221#if ETL_USING_CPP11
222 template <>
223 struct greater_equal<void> : public etl::binary_function<void, void, bool>
224 {
225 typedef int is_transparent;
226
227 template <typename T1, typename T2>
228 constexpr auto operator()(T1&& lhs, T2&& rhs) const -> decltype(static_cast<T1&&>(lhs) < static_cast<T2&&>(rhs))
229 {
230 return static_cast<T1&&>(rhs) < static_cast<T2&&>(lhs);
231 }
232 };
233#endif
234
235 //***************************************************************************
236 template <typename T = void>
237 struct equal_to : public etl::binary_function<T, T, bool>
238 {
239 typedef T value_type;
240
241 ETL_CONSTEXPR bool operator()(const T &lhs, const T &rhs) const
242 {
243 return lhs == rhs;
244 }
245 };
246
247#if ETL_USING_CPP11
248 template <>
249 struct equal_to<void> : public etl::binary_function<void, void, bool>
250 {
251 typedef void value_type;
252 typedef int is_transparent;
253
254 template <typename T1, typename T2>
255 constexpr auto operator()(T1&& lhs, T2&& rhs) const -> decltype(static_cast<T1&&>(lhs) < static_cast<T2&&>(rhs))
256 {
257 return static_cast<T1&&>(lhs) == static_cast<T2&&>(rhs);
258 }
259 };
260#endif
261
262 //***************************************************************************
263 template <typename T = void>
264 struct not_equal_to : public etl::binary_function<T, T, bool>
265 {
266 typedef T value_type;
267
268 ETL_CONSTEXPR bool operator()(const T &lhs, const T &rhs) const
269 {
270 return !(lhs == rhs);
271 }
272 };
273
274#if ETL_USING_CPP11
275 template <>
276 struct not_equal_to<void> : public etl::binary_function<void, void, bool>
277 {
278 typedef int is_transparent;
279
280 template <typename T1, typename T2>
281 constexpr auto operator()(T1&& lhs, T2&& rhs) const -> decltype(static_cast<T1&&>(lhs) < static_cast<T2&&>(rhs))
282 {
283 return !(static_cast<T1&&>(lhs) == static_cast<T2&&>(rhs));
284 }
285 };
286#endif
287
288 //***************************************************************************
289 template <typename TFunction>
290 class binder1st : public etl::unary_function<typename TFunction::second_argument_type, typename TFunction::result_type>
291 {
292 protected:
293
294 TFunction operation;
295 typename TFunction::first_argument_type value;
296
297 public:
298
299 binder1st(const TFunction& f, const typename TFunction::first_argument_type& v)
300 : operation(f), value(v)
301 {
302 }
303
304 typename TFunction::result_type operator()(typename TFunction::second_argument_type& x) const
305 {
306 return operation(value, x);
307 }
308
309 typename TFunction::result_type operator()(const typename TFunction::second_argument_type& x) const
310 {
311 return operation(value, x);
312 }
313 };
314
315 template <typename F, typename T>
316 binder1st<F> bind1st(const F& f, const T& x)
317 {
318 return binder1st<F>(f, x);
319 }
320
321 //***************************************************************************
322 template <typename TFunction >
323 class binder2nd : public etl::unary_function<typename TFunction::first_argument_type, typename TFunction::result_type>
324 {
325 protected:
326 TFunction operation;
327 typename TFunction::second_argument_type value;
328 public:
329 binder2nd(const TFunction& f, const typename TFunction::second_argument_type& v)
330 : operation(f), value(v)
331 {
332 }
333
334 typename TFunction::result_type operator()(typename TFunction::first_argument_type& x) const
335 {
336 return operation(x, value);
337 }
338
339 typename TFunction::result_type operator()(const typename TFunction::first_argument_type& x) const
340 {
341 return operation(x, value);
342 }
343 };
344
345 template <typename F, typename T>
346 binder2nd<F> bind2nd(const F& f, const T& x)
347 {
348 return binder2nd<F>(f, x);
349 }
350
351 //***************************************************************************
352 template <typename T = void>
353 struct plus
354 {
355 typedef T first_argument_type;
356 typedef T second_argument_type;
357 typedef T result_type;
358
359 ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const
360 {
361 return lhs + rhs;
362 }
363 };
364
365 //***************************************************************************
366 template <typename T = void>
367 struct minus
368 {
369 typedef T first_argument_type;
370 typedef T second_argument_type;
371 typedef T result_type;
372
373 ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const
374 {
375 return lhs - rhs;
376 }
377 };
378
379 //***************************************************************************
380 template <typename T = void>
381 struct negate
382 {
383 typedef T argument_type;
384 typedef T result_type;
385
386 ETL_CONSTEXPR T operator()(const T& lhs) const
387 {
388 return -lhs;
389 }
390 };
391
392 //***************************************************************************
393 template <typename T = void>
395 {
396 typedef T first_argument_type;
397 typedef T second_argument_type;
398 typedef T result_type;
399
400 ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const
401 {
402 return lhs * rhs;
403 }
404 };
405
406 //***************************************************************************
407 template <typename T = void>
408 struct divides
409 {
410 typedef T first_argument_type;
411 typedef T second_argument_type;
412 typedef T result_type;
413
414 ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const
415 {
416 return lhs / rhs;
417 }
418 };
419
420 //***************************************************************************
421 template <typename T = void>
422 struct modulus
423 {
424 typedef T first_argument_type;
425 typedef T second_argument_type;
426 typedef T result_type;
427
428 ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const
429 {
430 return lhs % rhs;
431 }
432 };
433
434 //***************************************************************************
435 template <typename T = void>
437 {
438 typedef T first_argument_type;
439 typedef T second_argument_type;
440 typedef T result_type;
441
442 ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const
443 {
444 return lhs && rhs;
445 }
446 };
447
448 //***************************************************************************
449 template <typename T = void>
451 {
452 typedef T first_argument_type;
453 typedef T second_argument_type;
454 typedef T result_type;
455
456 ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const
457 {
458 return lhs || rhs;
459 }
460 };
461
462 //***************************************************************************
463 template <typename T = void>
465 {
466 typedef T first_argument_type;
467 typedef T second_argument_type;
468 typedef T result_type;
469
470 ETL_CONSTEXPR T operator()(const T& lhs) const
471 {
472 return !lhs;
473 }
474 };
475
476 //***************************************************************************
477 template <typename T = void>
478 struct bit_and
479 {
480 typedef T first_argument_type;
481 typedef T second_argument_type;
482 typedef T result_type;
483
484 ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const
485 {
486 return lhs & rhs;
487 }
488 };
489
490 //***************************************************************************
491 template <typename T = void>
492 struct bit_or
493 {
494 typedef T first_argument_type;
495 typedef T second_argument_type;
496 typedef T result_type;
497
498 ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const
499 {
500 return lhs | rhs;
501 }
502 };
503
504 //***************************************************************************
505 template <typename T = void>
506 struct bit_xor
507 {
508 typedef T first_argument_type;
509 typedef T second_argument_type;
510 typedef T result_type;
511
512 ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const
513 {
514 return lhs ^ rhs;
515 }
516 };
517
518 //***************************************************************************
519 template <typename T = void>
520 struct bit_not
521 {
522 typedef T first_argument_type;
523 typedef T second_argument_type;
524 typedef T result_type;
525
526 ETL_CONSTEXPR T operator()(const T& lhs) const
527 {
528 return ~lhs;
529 }
530 };
531}
532
533#endif
534
Definition: functional.h:291
Definition: functional.h:324
Definition: functional.h:50
bitset_ext
Definition: absolute.h:38
Definition: functional.h:125
Definition: functional.h:479
Definition: functional.h:521
Definition: functional.h:493
Definition: functional.h:507
Definition: functional.h:409
Definition: functional.h:238
Definition: functional.h:212
Definition: functional.h:186
Definition: functional.h:160
Definition: functional.h:134
Definition: functional.h:437
Definition: functional.h:465
Definition: functional.h:451
Definition: functional.h:368
Definition: functional.h:423
Definition: functional.h:395
Definition: functional.h:382
Definition: functional.h:265
Definition: functional.h:354
Definition: functional.h:117