count.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // count.h
  3. // stream
  4. //
  5. // Created by Sam Jaffe on 3/30/23.
  6. //
  7. #pragma once
  8. #include <optional>
  9. #include <stream/detail/identity.h>
  10. #include <stream/detail/traits.h>
  11. #define FWD(x) std::forward<decltype(x)>(x)
  12. namespace stream::ranges {
  13. template <typename It, typename S, typename Pred,
  14. typename Proj = detail::identity>
  15. bool any_of(It it, S end, Pred pred, Proj proj = {}) {
  16. for (; it != end; ++it) {
  17. if (std::invoke(pred, std::invoke(proj, *it))) { return true; }
  18. }
  19. return false;
  20. }
  21. template <typename Stream, typename Pred, typename Proj = detail::identity>
  22. bool any_of(Stream const & stream, Pred pred, Proj proj = {}) {
  23. return any_of(stream.begin(), stream.end(), std::move(pred), std::move(proj));
  24. }
  25. template <typename It, typename S, typename Pred,
  26. typename Proj = detail::identity>
  27. bool none_of(It it, S end, Pred pred, Proj proj = {}) {
  28. return !any_of(it, end, std::move(pred), std::move(proj));
  29. }
  30. template <typename Stream, typename Pred, typename Proj = detail::identity>
  31. bool none_of(Stream const & stream, Pred pred, Proj proj = {}) {
  32. return none_of(stream.begin(), stream.end(), std::move(pred),
  33. std::move(proj));
  34. }
  35. template <typename It, typename S, typename Pred,
  36. typename Proj = detail::identity>
  37. bool all_of(It it, S end, Pred pred, Proj proj = {}) {
  38. for (; it != end; ++it) {
  39. if (!std::invoke(pred, std::invoke(proj, *it))) { return false; }
  40. }
  41. return true;
  42. }
  43. template <typename Stream, typename Pred, typename Proj = detail::identity>
  44. bool all_of(Stream const & stream, Pred pred, Proj proj = {}) {
  45. return all_of(stream.begin(), stream.end(), std::move(pred), std::move(proj));
  46. }
  47. template <typename It, typename S, typename T, typename Proj = detail::identity>
  48. bool count(It it, S end, T const & value, Proj proj = {}) {
  49. size_t result = 0;
  50. for (; it != end; ++it) {
  51. if (value == std::invoke(proj, *it)) { ++result; }
  52. }
  53. return result;
  54. }
  55. template <typename Stream, typename T, typename Proj = detail::identity>
  56. bool count(Stream const & stream, T const & value, Proj proj = {}) {
  57. return count(stream.begin(), stream.end(), value, std::move(proj));
  58. }
  59. template <typename It, typename S, typename Pred,
  60. typename Proj = detail::identity>
  61. auto count_if(It it, S end, Pred pred, Proj proj = {}) {
  62. size_t result = 0;
  63. for (; it != end; ++it) {
  64. if (std::invoke(pred, std::invoke(proj, *it))) { ++result; }
  65. }
  66. return result;
  67. }
  68. template <typename Stream, typename Pred, typename Proj = detail::identity>
  69. auto count_if(Stream const & stream, Pred pred, Proj proj = {}) {
  70. return count_if(stream.begin(), stream.end(), std::move(pred),
  71. std::move(proj));
  72. }
  73. }
  74. #undef FWD