| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- //
- // max.h
- // stream
- //
- // Created by Sam Jaffe on 4/2/23.
- //
- #pragma once
- #include <stream/forward.h>
- #include <stream/detail/identity.h>
- #include <stream/detail/invoke.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>
- T const & max(T const & l, T const & r, Comp comp = {}, Proj proj = {}) {
- return detail::invoke(comp, r, l, proj, proj) ? l : r;
- }
- template <typename It, typename S, typename Comp = std::less<>,
- typename Proj = detail::identity,
- REQUIRES((detail::is_comparable_v<It, S>))>
- auto max_element(It it, S end, Comp comp = {}, Proj proj = {}) {
- auto rval = it;
- for (++it; it != end; ++it) {
- if (detail::invoke(comp, *rval, *it, proj, proj)) { rval = *it; }
- }
- return rval;
- }
- template <typename Stream, typename Comp = std::less<>,
- typename Proj = detail::identity>
- auto max_element(Stream const & stream, Comp comp = {}, Proj proj = {}) {
- return max_element(stream.begin(), stream.end(), std::ref(comp),
- std::ref(proj));
- }
- template <typename T, typename Comp = std::less<>,
- typename Proj = detail::identity>
- auto max(std::initializer_list<T> const & stream, Comp comp = {},
- Proj proj = {}) {
- return *max_element(stream.begin(), stream.end(), std::ref(comp),
- std::ref(proj));
- }
- template <typename Stream, typename Comp = std::less<>,
- typename Proj = detail::identity>
- auto max(Stream const & stream, Comp comp = {}, Proj proj = {}) {
- return *max_element(stream.begin(), stream.end(), std::ref(comp),
- std::ref(proj));
- }
- }
- #include <stream/detail/undef.h>
|