| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- #pragma once
- #include <functional>
- #include <iterator/proxy.h>
- #include <stream/detail/traits.h>
- #include <stream/forward.h>
- #define FWD(x) std::forward<decltype(x)>(x)
- namespace stream::ranges {
- template <typename It, typename Proj>
- class transform_iterator : public proxy<It, transform_iterator<It, Proj>> {
- private:
- using super_t = proxy<It, transform_iterator<It, Proj>>;
- private:
- Proj projection_;
- public:
- transform_iterator() = default;
- transform_iterator(It iter, Proj projection)
- : super_t(std::move(iter)), projection_(projection) {}
- decltype(auto) dereference() const {
- return projection_(super_t::dereference());
- }
- bool at_end() const { return super_t::impl().at_end(); }
- };
- template <typename S, typename Proj> class transform_view {
- private:
- private:
- S stream_;
- Proj projection_;
- public:
- transform_view(S && stream, Proj projection)
- : stream_(FWD(stream)), projection_(projection) {}
- auto begin() const { return transform_iterator(stream_.begin(), invoke()); }
-
- auto end() const {
- if constexpr (traits::is_sentinal_v<S>) {
- return stream_.end();
- } else {
- return transform_iterator(stream_.end(), invoke());
- }
- }
-
- auto empty() const {
- if constexpr (traits::has_empty_v<S>) {
- return stream_.empty();
- }
- }
-
- auto size() {
- if constexpr (traits::has_size_v<S>) {
- return stream_.size();
- }
- }
- private:
- auto invoke() const {
- return std::function([this](traits::cref_t<S> t) -> decltype(auto) {
- return std::invoke(projection_, t);
- });
- }
- };
- template <typename S, typename Proj>
- transform_view(S &&, Proj) -> transform_view<S, Proj>;
- }
- MAKE_ITERATOR_FACADE_TYPEDEFS_T(stream::ranges::transform_iterator);
- namespace stream::ranges::views {
- template <typename Proj> class transform {
- public:
- transform(Proj const & projection) : projection_(projection) {}
- template <typename Stream>
- friend auto operator|(Stream && stream, transform map) {
- // TODO: if constexpr transform_view
- return transform_view(FWD(stream), std::move(map.projection_));
- }
- private:
- Proj projection_;
- };
- }
- #undef FWD
|