class_name RangeIterator extends Iterator

var index: int
var range: IntRange
var step: int
var descending: bool
var _done: bool = false

func _init(range: IntRange, start_index: int = range.min, step: int = 1) -> void:
    assert(step != 0, "step cannot be zero")

    self.range = range
    self.index = start_index
    self.step = step
    self.descending = step < 0

func from_values(from: int, to: int, start_index: int = range.min, step: int = 1) -> RangeIterator:
    return RangeIterator.new(IntRange.new(from, to), start_index, step)

func length() -> int:
    return range.length()

func clone() -> RangeIterator:
    return RangeIterator.new(range, index)

func next() -> Option:
    if _done: return Option.none
    if index == range.max or not range.contains(index):
        _done = true
        if range.inclusive: return Option.some(index)
        else: return Option.none

    var current_value = Option.some(index)
    index += step
    return current_value
