-
Notifications
You must be signed in to change notification settings - Fork 1
/
splitter.go
204 lines (157 loc) · 4.76 KB
/
splitter.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package ranges
type splitterState uint8
const (
splitterInitialState splitterState = 0
splitterIsPrimed splitterState = 1 << iota
splitterIsEmpty
splitterHasFinalSplit
)
type splitterResult[T any] struct {
r ForwardRange[T]
separator ForwardRange[T]
current ForwardRange[T]
cb func(a, b T) bool
state splitterState
}
func (sr *splitterResult[T]) Empty() bool {
if (sr.state & splitterHasFinalSplit) != 0 {
return false
}
if (sr.state & splitterIsEmpty) != 0 {
return true
}
if (sr.state & splitterIsPrimed) == 0 {
sr.current = TakeWhileF(sr.r, func(a T) bool {
saved := sr.r.Save()
if StartsWith[T](saved, sr.separator.Save(), sr.cb) {
sr.r = saved
if saved.Empty() {
sr.state |= splitterHasFinalSplit
}
return false
}
return true
})
sr.state |= splitterIsPrimed
}
return false
}
func (sr *splitterResult[T]) Front() ForwardRange[T] {
if sr.Empty() {
panic("Front() called on an empty Splitter() range")
}
return sr.current
}
func (sr *splitterResult[T]) PopFront() {
if (sr.state & splitterHasFinalSplit) != 0 {
sr.state ^= splitterHasFinalSplit
return
}
if sr.Empty() {
panic("PopFront() called on an empty Splitter() range")
}
for !sr.current.Empty() {
sr.current.PopFront()
}
sr.state ^= splitterIsPrimed
if sr.r.Empty() {
sr.state ^= splitterIsEmpty
}
}
func (sr *splitterResult[T]) Save() ForwardRange[ForwardRange[T]] {
return &splitterResult[T]{sr.r.Save(), sr.separator, sr.current.Save(), sr.cb, sr.state}
}
type splitWhenResult[T any] struct {
r ForwardRange[T]
current ForwardRange[T]
cb func(a, b T) bool
state splitterState
}
func (swr *splitWhenResult[T]) Empty() bool {
if (swr.state & splitterHasFinalSplit) != 0 {
return false
}
if (swr.state & splitterIsEmpty) != 0 {
return true
}
if (swr.state & splitterIsPrimed) == 0 {
swr.current = TakeWhileF(swr.r, func(a T) bool {
/*
if StartsWith[T](saved, I(swr.separator.Save()), sr.cb) {
swr.r = saved
if saved.Empty() {
swr.state |= splitterHasFinalSplit
}
return false
}
*/
return true
})
swr.state |= splitterIsPrimed
}
return false
}
func (swr *splitWhenResult[T]) Front() ForwardRange[T] {
if swr.Empty() {
panic("Front() called on an empty SplitWhen() range")
}
return swr.current
}
func (swr *splitWhenResult[T]) PopFront() {
if (swr.state & splitterHasFinalSplit) != 0 {
swr.state ^= splitterHasFinalSplit
return
}
if swr.Empty() {
panic("PopFront() called on an empty SplitWhen() range")
}
for !swr.current.Empty() {
swr.current.PopFront()
}
swr.state ^= splitterIsPrimed
if swr.r.Empty() {
swr.state ^= splitterIsEmpty
}
}
func (swr *splitWhenResult[T]) Save() ForwardRange[ForwardRange[T]] {
return &splitWhenResult[T]{swr.r.Save(), swr.current.Save(), swr.cb, swr.state}
}
// Splitter splits a range using a ForwardRange as a separator
// where `cb(a, b) == true` on each element.
func Splitter[T any](r ForwardRange[T], cb func(a, b T) bool, separator ForwardRange[T]) ForwardRange[ForwardRange[T]] {
return &splitterResult[T]{r, separator, Null[T](), cb, splitterInitialState}
}
// SplitterS is `Splitter` accepting a slice.
func SplitterS[T any](r []T, cb func(a, b T) bool, separator ForwardRange[T]) ForwardRange[ForwardRange[T]] {
return Splitter(SliceRange(r), cb, separator)
}
// SplitterSS is `SplitterS` accepting a slice for both ranges.
func SplitterSS[T any](r []T, cb func(a, b T) bool, separator []T) ForwardRange[ForwardRange[T]] {
return SplitterS(r, cb, SliceRange(separator))
}
// SplitterComparable splits ranges using a ForwardRange as a separator where
// each element is equal.
func SplitterComparable[T comparable](r ForwardRange[T], separator ForwardRange[T]) ForwardRange[ForwardRange[T]] {
return Splitter(r, Eq[T], separator)
}
// SplitterComparableS is `SplitterComparable` accepting a slice.
func SplitterComparableS[T comparable](r []T, separator ForwardRange[T]) ForwardRange[ForwardRange[T]] {
return SplitterComparable(SliceRange(r), separator)
}
// SplitterComparableSS is `SplitterComparable` accepting a slice for both ranges
func SplitterComparableSS[T comparable](r []T, separator []T) ForwardRange[ForwardRange[T]] {
return SplitterComparableS(r, SliceRange(separator))
}
// SplitString splits a string by a `separator` into a range of strings.
func SplitString(r string, separator string) ForwardRange[string] {
return CacheF(
MapF(
SplitterComparable(Bytes(r), Bytes(separator)),
Pipe2(SliceF[byte], func(arr []byte) string { return string(arr) }),
),
)
}
// SplitWhen splits a range where `cb(a, b) == true` for adjacent elements.
func SplitWhen[T any](r InputRange[T], cb func(a, b T) bool) InputRange[InputRange[T]] {
return ChunkBy(r, func(a, b T) bool { return !cb(a, b) })
}