range_walker.h 766 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #pragma once
  2. #include <util/generic/yexception.h>
  3. namespace NYql {
  4. template <typename T>
  5. class TRangeWalker {
  6. private:
  7. const T Start_;
  8. const T Finish_;
  9. T Current_;
  10. public:
  11. TRangeWalker(T start, T finish)
  12. : Start_(start)
  13. , Finish_(finish)
  14. , Current_(start)
  15. {
  16. if (Start_ > Finish_) {
  17. ythrow yexception() << "Invalid range for walker";
  18. }
  19. }
  20. T GetStart() const {
  21. return Start_;
  22. }
  23. T GetFinish() const {
  24. return Finish_;
  25. }
  26. T GetRangeSize() const {
  27. return Finish_ - Start_ + 1;
  28. }
  29. T MoveToNext() {
  30. T result = Current_++;
  31. if (Current_ > Finish_) {
  32. Current_ = Start_;
  33. }
  34. return result;
  35. }
  36. };
  37. }