transform.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #pragma once
  2. #include <functional>
  3. #include <stream/forward.h>
  4. #include <stream/detail/traits.h>
  5. #include <stream/iterator/sentinel.h>
  6. #include <stream/iterator/transform_iterator.h>
  7. #include <stream/view/interface.h>
  8. #include <stream/detail/macro.h>
  9. namespace stream::ranges {
  10. template <typename S, typename Proj>
  11. class transform_view : public proxy_view_interface<transform_view<S, Proj>, S,
  12. transform_iterator, Proj> {
  13. public:
  14. using iterator = transform_iterator<detail::begin_t<S>, Proj>;
  15. private:
  16. S stream_;
  17. Proj projection_;
  18. public:
  19. transform_view(S && stream, Proj projection)
  20. : stream_(FWD(stream)), projection_(projection) {}
  21. auto begin() const { return iterator(stream_.begin(), projection_); }
  22. auto end() const { return sentinel_end(this, stream_, projection_); }
  23. };
  24. template <typename S, typename Proj>
  25. transform_view(S &&, Proj) -> transform_view<S, Proj>;
  26. }
  27. namespace stream::ranges::views {
  28. template <typename Proj> class transform {
  29. public:
  30. transform(Proj const & projection) : projection_(projection) {}
  31. template <typename Stream>
  32. friend auto operator|(Stream && stream, transform map) {
  33. // TODO: if constexpr transform_view
  34. return transform_view(FWD(stream), std::move(map.projection_));
  35. }
  36. private:
  37. Proj projection_;
  38. };
  39. }
  40. #include <stream/detail/undef.h>