| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- //
- // minmax.h
- // stream
- //
- // Created by Sam Jaffe on 3/30/23.
- //
- #pragma once
- #include <stream/forward.h>
- #include <stream/detail/identity.h>
- #include <stream/detail/invoke.h>
- #include <stream/detail/named_pair.h>
- #include <stream/detail/traits.h>
- #include <stream/detail/macro.h>
- namespace stream::ranges {
- template <typename T, typename Comp = std::less<>,
- typename Proj = detail::identity>
- auto minmax(T const & l, T const & r, Comp comp = {}, Proj proj = {})
- -> detail::min_max_result<T const &, T const &> {
- if (detail::invoke(comp, l, r, proj, proj)) { return {l, r}; }
- return {r, l};
- }
- template <typename It, typename S, typename Comp = std::less<>,
- typename Proj = detail::identity,
- REQUIRES((detail::is_comparable_v<It, S>))>
- auto minmax_element(It it, S end, Comp comp = {}, Proj proj = {}) {
- detail::min_max_result rval(it, it);
- for (++it; it != end; ++it) {
- if (detail::invoke(comp, it, rval.min, proj, proj)) {
- rval.min = it;
- } else if (detail::invoke(comp, rval.max, it, proj, proj)) {
- rval.max = it;
- }
- }
- return rval;
- }
- template <typename Stream, typename Comp = std::less<>,
- typename Proj = detail::identity>
- auto minmax_element(Stream const & stream, Comp comp = {}, Proj proj = {}) {
- return minmax_element(stream.begin(), stream.end(), std::ref(comp),
- std::ref(proj));
- }
- template <typename T, typename Comp = std::less<>,
- typename Proj = detail::identity>
- auto minmax(std::initializer_list<T> stream, Comp comp = {}, Proj proj = {}) {
- auto result = minmax_element(stream.begin(), stream.end(), std::ref(comp),
- std::ref(proj));
- return detail::min_max_result(*result.min, *result.max);
- }
- template <typename Stream, typename Comp = std::less<>,
- typename Proj = detail::identity>
- auto minmax(Stream const & stream, Comp comp = {}, Proj proj = {}) {
- auto result = minmax_element(stream.begin(), stream.end(), std::ref(comp),
- std::ref(proj));
- return detail::min_max_result(*result.min, *result.max);
- }
- }
- #include <stream/detail/undef.h>
|