range.hpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #pragma once
  2. namespace nall {
  3. struct range_t {
  4. struct iterator {
  5. iterator(int64_t position, int64_t step = 0) : position(position), step(step) {}
  6. auto operator*() const -> int64_t { return position; }
  7. auto operator!=(const iterator& source) const -> bool { return step > 0 ? position < source.position : position > source.position; }
  8. auto operator++() -> iterator& { position += step; return *this; }
  9. private:
  10. int64_t position;
  11. const int64_t step;
  12. };
  13. struct reverse_iterator {
  14. reverse_iterator(int64_t position, int64_t step = 0) : position(position), step(step) {}
  15. auto operator*() const -> int64_t { return position; }
  16. auto operator!=(const reverse_iterator& source) const -> bool { return step > 0 ? position > source.position : position < source.position; }
  17. auto operator++() -> reverse_iterator& { position -= step; return *this; }
  18. private:
  19. int64_t position;
  20. const int64_t step;
  21. };
  22. auto begin() const -> iterator { return {origin, stride}; }
  23. auto end() const -> iterator { return {target}; }
  24. auto rbegin() const -> reverse_iterator { return {target - stride, stride}; }
  25. auto rend() const -> reverse_iterator { return {origin - stride}; }
  26. int64_t origin;
  27. int64_t target;
  28. int64_t stride;
  29. };
  30. inline auto range(int64_t size) {
  31. return range_t{0, size, 1};
  32. }
  33. inline auto range(int64_t offset, int64_t size) {
  34. return range_t{offset, size, 1};
  35. }
  36. inline auto range(int64_t offset, int64_t size, int64_t step) {
  37. return range_t{offset, size, step};
  38. }
  39. }