iota.h 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. #include <stream/detail/macro.h>
  11. namespace stream::ranges {
  12. template <typename T, typename Bound> struct iota_view {
  13. public:
  14. constexpr static inline bool has_size_v = std::is_constructible_v<T, Bound>;
  15. private:
  16. T value_;
  17. Bound bound_;
  18. public:
  19. iota_view(T value, Bound bound) : value_(value), bound_(bound) {}
  20. auto begin() const { return iota_iterator(value_, bound_); }
  21. auto end() const {
  22. if constexpr (has_size_v) {
  23. return iota_iterator(bound_, bound_);
  24. } else {
  25. return bound_;
  26. }
  27. }
  28. bool empty() const { return begin().at_end(); }
  29. SFINAE(has_size_v, size_t) size() const { return begin().distance_to(end()); }
  30. };
  31. }
  32. namespace stream::ranges::views {
  33. template <typename T, typename Bound> auto iota(T value, Bound bound) {
  34. return iota_view(value, bound);
  35. }
  36. }
  37. MAKE_ITERATOR_FACADE_TYPEDEFS_T(stream::ranges::iota_iterator);
  38. #include <stream/detail/undef.h>