| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- #pragma once
- #include <functional>
- #include <stream/forward.h>
- #include <stream/detail/traits.h>
- #include <stream/iterator/transform_iterator.h>
- #include <stream/detail/macro.h>
- namespace stream::ranges {
- 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 (detail::is_sentinal_v<S>) {
- return stream_.end();
- } else {
- return transform_iterator(stream_.end(), invoke());
- }
- }
- SFINAE(detail::has_empty_v<S>, bool) empty() const { return stream_.empty(); }
- SFINAE(detail::has_size_v<S>, size_t) size() { return stream_.size(); }
- private:
- auto invoke() const {
- return std::function([this](detail::cref_t<S> t) -> decltype(auto) {
- return std::invoke(projection_, t);
- });
- }
- };
- template <typename S, typename Proj>
- transform_view(S &&, Proj) -> transform_view<S, Proj>;
- }
- 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_;
- };
- }
- #include <stream/detail/undef.h>
|