|
| 1 | +// -*- C++ -*- |
| 2 | +//===----------------------------------------------------------------------===// |
| 3 | +// |
| 4 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | +// See https://llvm.org/LICENSE.txt for license information. |
| 6 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | + |
| 10 | +#ifndef _EZCXX_ALGORITHM |
| 11 | +#define _EZCXX_ALGORITHM |
| 12 | + |
| 13 | +#include <__config> |
| 14 | + |
| 15 | +// currently unused, but included in the standard |
| 16 | +#include <initializer_list> |
| 17 | + |
| 18 | +#pragma clang system_header |
| 19 | + |
| 20 | +// very limited implementation of <algorithm> |
| 21 | +// only supports std:max, std::min, and std::clamp |
| 22 | +// these functions can be replaced when <algorithm> is properly implemented |
| 23 | + |
| 24 | +namespace std { |
| 25 | + |
| 26 | +template <class _Tp, class _Compare> _EZCXX_NODISCARD_EXT inline constexpr |
| 27 | +const _Tp& max(const _Tp& __a, const _Tp& __b, _Compare __comp) |
| 28 | +{ |
| 29 | + return __comp(__a, __b) ? __b : __a; |
| 30 | +} |
| 31 | + |
| 32 | +template <class _Tp> _EZCXX_NODISCARD_EXT inline constexpr |
| 33 | +const _Tp& max(const _Tp& __a, const _Tp& __b) |
| 34 | +{ |
| 35 | + return (__a < __b) ? __b : __a; |
| 36 | +} |
| 37 | + |
| 38 | +template <class _Tp, class _Compare> _EZCXX_NODISCARD_EXT inline constexpr |
| 39 | +const _Tp& min(const _Tp& __a, const _Tp& __b, _Compare __comp) |
| 40 | +{ |
| 41 | + return __comp(__a, __b) ? __a : __b; |
| 42 | +} |
| 43 | + |
| 44 | +template <class _Tp> _EZCXX_NODISCARD_EXT inline constexpr |
| 45 | +const _Tp& min(const _Tp& __a, const _Tp& __b) |
| 46 | +{ |
| 47 | + return (__a < __b) ? __a : __b; |
| 48 | +} |
| 49 | + |
| 50 | +template <class _Tp, class _Compare> _EZCXX_NODISCARD_EXT inline constexpr |
| 51 | +const _Tp& clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp) |
| 52 | +{ |
| 53 | + return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v; |
| 54 | +} |
| 55 | + |
| 56 | +template <class _Tp> _EZCXX_NODISCARD_EXT inline constexpr |
| 57 | +const _Tp& clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi) |
| 58 | +{ |
| 59 | + return (__v < __lo) ? __lo : (__hi < __v) ? __hi : __v; |
| 60 | +} |
| 61 | + |
| 62 | +} // namespace std |
| 63 | + |
| 64 | +#endif // _EZCXX_ALGORITHM |
0 commit comments