| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- //
- // stream_helpers.h
- // stream
- //
- // Created by Sam Jaffe on 4/4/23.
- //
- #pragma once
- #include <iterator/end_aware_iterator.h>
- #include <iterator/proxy.h>
- #include <stream/detail/traits.h>
- namespace ranges = stream::ranges;
- namespace views = stream::ranges::views;
- #include <iterator/detail/macro.h>
- #define SELF() (*static_cast<CRTP const *>(this))
- /**
- * There are the following types of iterators/containers that we are interested
- * in:
- * - Does the object use common iterators or sentinel iterators
- * - Does the object have a size/empty
- */
- template <typename It>
- class remaining_iterator : public iterator::end_aware_iterator<It> {
- public:
- using super_t = iterator::end_aware_iterator<It>;
- public:
- using super_t::super_t;
- friend auto operator-(remaining_iterator const & it, iterator::sentinel_t) {
- return std::distance(super_t::impl(), super_t::end());
- }
- };
- MAKE_ITERATOR_FACADE_TYPEDEFS_T(remaining_iterator);
- template <typename CRTP> struct Sized {
- size_t size() const { return SELF().impl().size(); }
- bool empty() const { return SELF().impl().size() == 0; }
- };
- template <typename CRTP> struct Common {
- auto begin() const { return SELF().impl().begin(); }
- auto end() const { return SELF().impl().end(); }
- };
- template <typename CRTP> struct Sentinel {
- auto begin() const { return iterator::end_aware_iterator(SELF().impl()); }
- auto end() const { return iterator::sentinel; }
- };
- template <typename CRTP> struct SizedSentinel {
- auto begin() const { return remaining_iterator(SELF().impl()); }
- auto end() const { return iterator::sentinel; }
- };
- template <typename C, template <typename> class... Traits>
- class Range : public Traits<Range<C, Traits...>>... {
- private:
- C impl_;
- public:
- Range() = default;
- Range(C impl) : impl_(std::move(impl)) {}
- template <typename T> Range(std::initializer_list<T> init) : impl_(init) {}
- auto & impl() const { return impl_; }
- };
- #include <iterator/detail/undef.h>
|