Files
LNXSDK/lib/aura/Sources/aura/utils/StepIterator.hx
2025-01-22 16:18:30 +01:00

37 lines
892 B
Haxe

// =============================================================================
// Adapted from
// https://code.haxe.org/category/data-structures/step-iterator.html
// =============================================================================
package aura.utils;
/**
Use this as a static extension:
```haxe
using aura.utils.StepIterator;
for (i in (0...10).step(2)) {
// Do something...
}
```
**/
inline function step(iter: IntIterator, step: Int) {
return @:privateAccess new StepIterator(iter.min, iter.max, step);
}
private class StepIterator {
var currentIndex: Int;
final end: Int;
final step: Int;
public inline function new(start: Int, end: Int, step: Int) {
this.currentIndex = start;
this.end = end;
this.step = step;
}
public inline function hasNext() return currentIndex < end;
public inline function next() return (currentIndex += step) - step;
}