-
Notifications
You must be signed in to change notification settings - Fork 0
/
itertools.go
124 lines (107 loc) · 2.7 KB
/
itertools.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package itertools
import (
"errors"
"reflect"
)
func slice(v any) bool {
return reflect.TypeOf(v).Kind() == reflect.Slice
}
type Iterator struct {
items any
index int
cycle bool
}
// Iter creates a new Iterator from any slice
func Iter(items any) (*Iterator, error) {
if !slice(items) {
return nil, errors.New("Iter() requires a slice")
}
return &Iterator{items, -1, false}, nil
}
// Cycle creates an infinite Iterator from any slice
func Cycle(items any) (*Iterator, error) {
if !slice(items) {
return nil, errors.New("Cycle() requires a slice")
}
return &Iterator{items, -1, true}, nil
}
// HasNext returns true if the Iterator has a next item (always true if cycle mode is enabled)
func (it *Iterator) HasNext() bool {
if it.cycle {
return true
}
return it.index+1 < reflect.ValueOf(it.items).Len()
}
// Next returns the next item (nil if not set)
func (it *Iterator) Next() any {
val := reflect.ValueOf(it.items)
if it.cycle {
it.index = (it.index + 1) % val.Len()
return val.Index(it.index).Interface()
}
if it.HasNext() {
it.index++
return val.Index(it.index).Interface()
}
return nil
}
// HasPrev returns true if the Iterator has a previous item (always true if cycle mode is enabled)
func (it *Iterator) HasPrev() bool {
if it.cycle {
return true
}
return it.index-1 >= 0
}
// Prev returns the previous item (nil if not set)
func (it *Iterator) Prev() any {
val := reflect.ValueOf(it.items)
if it.index-1 >= 0 {
it.index--
return val.Index(it.index).Interface()
}
if it.cycle && val.Len() > 0 {
it.index = val.Len() - 1
return val.Index(it.index).Interface()
}
return nil
}
// Current returns the current item (nil if not set)
func (it *Iterator) Current() any {
val := reflect.ValueOf(it.items)
if it.index >= 0 && it.index < val.Len() {
return val.Index(it.index).Interface()
}
return nil
}
// Len returns the length of the Iterator
func (it *Iterator) Len() int {
return reflect.ValueOf(it.items).Len()
}
// Index returns the current index (0-based)
func (it *Iterator) Index() int {
return it.index
}
// IsCycle returns true if the Iterator is in cycle mode
func (it *Iterator) IsCycle() bool {
return it.cycle
}
// Reset resets the Iterator to the beginning (nil item)
func (it *Iterator) Reset() {
it.index = -1
}
// SetIndex sets the Iterator to the given index (0-based)
func (it *Iterator) SetIndex(index int) error {
if it.cycle {
it.index = index % reflect.ValueOf(it.items).Len()
return nil
}
if index >= 0 && index < reflect.ValueOf(it.items).Len() {
it.index = index
return nil
}
return errors.New("Index out of range")
}
// SetCycle sets the Iterator to cycle mode (true) or not (false)
func (it *Iterator) SetCycle(cycle bool) {
it.cycle = cycle
}