| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- //
- // min.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 & min(T const & l, T const & r, Comp comp = {}, Proj proj = {}) {
- return detail::invoke(comp, l, r, 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 min_element(It it, S end, Comp comp = {}, Proj proj = {}) {
- auto rval = it;
- for (++it; it != end; ++it) {
- if (detail::invoke(comp, *it, *rval, proj, proj)) { rval = it; }
- }
- return rval;
- }
- template <typename Stream, typename Comp = std::less<>,
- typename Proj = detail::identity>
- auto min_element(Stream const & stream, Comp comp = {}, Proj proj = {}) {
- return min_element(stream.begin(), stream.end(), std::ref(comp),
- std::ref(proj));
- }
- template <typename T, typename Comp = std::less<>,
- typename Proj = detail::identity>
- auto min(std::initializer_list<T> stream, Comp comp = {}, Proj proj = {}) {
- return *min_element(stream.begin(), stream.end(), std::ref(comp),
- std::ref(proj));
- }
- template <typename Stream, typename Comp = std::less<>,
- typename Proj = detail::identity>
- auto min(Stream const & stream, Comp comp = {}, Proj proj = {}) {
- return *min_element(stream.begin(), stream.end(), std::ref(comp),
- std::ref(proj));
- }
- }
- #include <stream/detail/undef.h>
|