equal.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //
  2. // equal.h
  3. // stream
  4. //
  5. // Created by Sam Jaffe on 4/2/23.
  6. //
  7. #pragma once
  8. #include <stream/forward.h>
  9. #include <stream/detail/invoke.h>
  10. #include <stream/detail/traits.h>
  11. #include <stream/detail/macro.h>
  12. namespace stream::range {
  13. template <typename It1, typename S1, typename It2, typename S2,
  14. typename Cmp = std::equal_to<>, typename Proj1 = detail::identity,
  15. typename Proj2 = detail::identity,
  16. REQUIRES((detail::is_comparable_v<It1, S1> &&
  17. detail::is_comparable_v<It2, S2>))>
  18. bool equal(It1 it1, S1 end1, It2 it2, S2 end2, Cmp cmp = {}, Proj1 proj1 = {},
  19. Proj2 proj2 = {}) {
  20. if constexpr (detail::is_sized_sentinel_v<It1, S1> &&
  21. detail::is_sized_sentinel_v<It2, S2>) {
  22. if ((end1 - it1) != (end2 - it2)) { return false; }
  23. }
  24. for (; it1 != end1 && it2 != end2; ++it1, ++it2) {
  25. if (!detail::invoke(cmp, *it1, *it2, proj1, proj2)) { return false; }
  26. }
  27. return (it1 == end1) && (it2 == end2);
  28. }
  29. template <typename R1, typename R2, typename Cmp = std::equal_to<>,
  30. typename Proj1 = detail::identity, typename Proj2 = detail::identity>
  31. bool equal(R1 const r1, R2 const & r2, Cmp cmp = {}, Proj1 proj1 = {},
  32. Proj2 proj2 = {}) {
  33. return equal(r1.begin(), r1.end(), r2.begin(), r2.end(), std::ref(cmp),
  34. std::ref(proj1), std::ref(proj2));
  35. }
  36. }
  37. #include <stream/detail/undef.h>