-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrange.go
52 lines (45 loc) · 1.04 KB
/
range.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package enu
type RangeValuer[T1, T2 any] interface {
Compare(T1) int
Value() T1
Next(step T2) RangeValuer[T1, T2]
}
func FromRange[T1, T2 any](min, max RangeValuer[T1, T2], step T2) *Enumerable[T1] {
return New[T1](NewRange(min, max, step))
}
func NewRange[T1, T2 any](min, max RangeValuer[T1, T2], step T2) *RangeEnumerator[T1, T2] {
return &RangeEnumerator[T1, T2]{
min: min,
max: max,
step: step,
}
}
type RangeEnumerator[T1, T2 any] struct {
min RangeValuer[T1, T2]
max RangeValuer[T1, T2]
current RangeValuer[T1, T2]
step T2
}
func empty[T any]() T {
var empty T
return empty
}
func (e *RangeEnumerator[T1, T2]) Dispose() {
e.current = nil
}
func (e *RangeEnumerator[T1, T2]) Next() (T1, bool) {
if e.min.Compare(e.max.Value()) == 1 {
return empty[T1](), false
}
if e.current == nil {
val := e.min.Value()
e.current = e.min.Next(e.step)
return val, true
}
if e.current.Compare(e.max.Value()) == 1 {
return empty[T1](), false
}
val := e.current.Value()
e.current = e.current.Next(e.step)
return val, true
}