// // count.h // stream // // Created by Sam Jaffe on 3/30/23. // #pragma once #include #include #include #define FWD(x) std::forward(x) namespace stream::ranges { template bool any_of(It it, S end, Pred pred, Proj proj = {}) { for (; it != end; ++it) { if (std::invoke(pred, std::invoke(proj, *it))) { return true; } } return false; } template bool any_of(Stream const & stream, Pred pred, Proj proj = {}) { return any_of(stream.begin(), stream.end(), std::move(pred), std::move(proj)); } template bool none_of(It it, S end, Pred pred, Proj proj = {}) { return !any_of(it, end, std::move(pred), std::move(proj)); } template bool none_of(Stream const & stream, Pred pred, Proj proj = {}) { return none_of(stream.begin(), stream.end(), std::move(pred), std::move(proj)); } template bool all_of(It it, S end, Pred pred, Proj proj = {}) { for (; it != end; ++it) { if (!std::invoke(pred, std::invoke(proj, *it))) { return false; } } return true; } template bool all_of(Stream const & stream, Pred pred, Proj proj = {}) { return all_of(stream.begin(), stream.end(), std::move(pred), std::move(proj)); } template bool count(It it, S end, T const & value, Proj proj = {}) { size_t result = 0; for (; it != end; ++it) { if (value == std::invoke(proj, *it)) { ++result; } } return result; } template bool count(Stream const & stream, T const & value, Proj proj = {}) { return count(stream.begin(), stream.end(), value, std::move(proj)); } template auto count_if(It it, S end, Pred pred, Proj proj = {}) { size_t result = 0; for (; it != end; ++it) { if (std::invoke(pred, std::invoke(proj, *it))) { ++result; } } return result; } template auto count_if(Stream const & stream, Pred pred, Proj proj = {}) { return count_if(stream.begin(), stream.end(), std::move(pred), std::move(proj)); } } #undef FWD