|
|
@@ -0,0 +1,89 @@
|
|
|
+//
|
|
|
+// count.h
|
|
|
+// stream
|
|
|
+//
|
|
|
+// Created by Sam Jaffe on 3/30/23.
|
|
|
+//
|
|
|
+
|
|
|
+#pragma once
|
|
|
+
|
|
|
+#include <optional>
|
|
|
+
|
|
|
+#include <stream/detail/identity.h>
|
|
|
+#include <stream/detail/traits.h>
|
|
|
+
|
|
|
+#define FWD(x) std::forward<decltype(x)>(x)
|
|
|
+
|
|
|
+namespace stream::ranges {
|
|
|
+template <typename It, typename S, typename Pred,
|
|
|
+ typename Proj = detail::identity>
|
|
|
+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 <typename Stream, typename Pred, typename Proj = detail::identity>
|
|
|
+bool any_of(Stream const & stream, Pred pred, Proj proj = {}) {
|
|
|
+ return any_of(stream.begin(), stream.end(), std::move(pred), std::move(proj));
|
|
|
+}
|
|
|
+
|
|
|
+template <typename It, typename S, typename Pred,
|
|
|
+ typename Proj = detail::identity>
|
|
|
+bool none_of(It it, S end, Pred pred, Proj proj = {}) {
|
|
|
+ return !any_of(it, end, std::move(pred), std::move(proj));
|
|
|
+}
|
|
|
+
|
|
|
+template <typename Stream, typename Pred, typename Proj = detail::identity>
|
|
|
+bool none_of(Stream const & stream, Pred pred, Proj proj = {}) {
|
|
|
+ return none_of(stream.begin(), stream.end(), std::move(pred),
|
|
|
+ std::move(proj));
|
|
|
+}
|
|
|
+
|
|
|
+template <typename It, typename S, typename Pred,
|
|
|
+ typename Proj = detail::identity>
|
|
|
+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 <typename Stream, typename Pred, typename Proj = detail::identity>
|
|
|
+bool all_of(Stream const & stream, Pred pred, Proj proj = {}) {
|
|
|
+ return all_of(stream.begin(), stream.end(), std::move(pred), std::move(proj));
|
|
|
+}
|
|
|
+
|
|
|
+template <typename It, typename S, typename T, typename Proj = detail::identity>
|
|
|
+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 <typename Stream, typename T, typename Proj = detail::identity>
|
|
|
+bool count(Stream const & stream, T const & value, Proj proj = {}) {
|
|
|
+ return count(stream.begin(), stream.end(), value, std::move(proj));
|
|
|
+}
|
|
|
+
|
|
|
+template <typename It, typename S, typename Pred,
|
|
|
+ typename Proj = detail::identity>
|
|
|
+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 <typename Stream, typename Pred, typename Proj = detail::identity>
|
|
|
+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
|