iota_iterator.h 1008 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //
  2. // iota_iterator.h
  3. // stream
  4. //
  5. // Created by Sam Jaffe on 4/6/23.
  6. //
  7. #pragma once
  8. #include <iterator/facade.h>
  9. #include <stream/forward.h>
  10. #include <stream/detail/traits.h>
  11. #include <stream/detail/macro.h>
  12. namespace stream::ranges {
  13. template <typename T, typename Bound>
  14. class iota_iterator : public facade<iota_iterator<T, Bound>,
  15. iterator::category::random_access> {
  16. public:
  17. using sentinal_type =
  18. std::conditional_t<std::is_same_v<T, Bound>, void, Bound>;
  19. iota_iterator() = default;
  20. iota_iterator(T value) : value_(value) {}
  21. T dereference() const { return value_; }
  22. void advance(std::ptrdiff_t off) { value_ += off; }
  23. std::ptrdiff_t distance_to(iota_iterator const & other) const {
  24. return other.value_ - value_;
  25. }
  26. SFINAE((detail::is_sized_sentinel_v<T, Bound>))
  27. friend bool operator-(Bound bound, iota_iterator const & self) {
  28. return bound - self.value_;
  29. }
  30. private:
  31. T value_;
  32. };
  33. }
  34. #include <stream/detail/undef.h>