iota.h 1018 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. //
  2. // iota.h
  3. // stream
  4. //
  5. // Created by Sam Jaffe on 3/29/23.
  6. //
  7. #pragma once
  8. #include <stream/forward.h>
  9. #include <stream/iterator/iota_iterator.h>
  10. namespace stream::ranges {
  11. template <typename T, typename Bound> struct iota_view {
  12. public:
  13. constexpr static inline bool has_size_v = std::is_constructible_v<T, Bound>;
  14. private:
  15. T value_;
  16. Bound bound_;
  17. public:
  18. iota_view(T value, Bound bound) : value_(value), bound_(bound) {}
  19. auto begin() const { return iota_iterator(value_, bound_); }
  20. auto end() const {
  21. if constexpr (has_size_v) {
  22. return iota_iterator(bound_, bound_);
  23. } else {
  24. return bound_;
  25. }
  26. }
  27. bool empty() const { return begin().at_end(); }
  28. auto size() const -> std::enable_if_t<has_size_v, size_t> {
  29. return begin().distance_to(end());
  30. }
  31. };
  32. }
  33. namespace stream::ranges::views {
  34. template <typename T, typename Bound> auto iota(T value, Bound bound) {
  35. return iota_view(value, bound);
  36. }
  37. }
  38. MAKE_ITERATOR_FACADE_TYPEDEFS_T(stream::ranges::iota_iterator);