-
Notifications
You must be signed in to change notification settings - Fork 1
/
joiner.go
149 lines (119 loc) · 3.11 KB
/
joiner.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
package ranges
type joinerResult[T any] struct {
r InputRange[InputRange[T]]
separator ForwardRange[T]
savedSeparator ForwardRange[T]
isPrimed bool
}
func (jr *joinerResult[T]) prime() {
if !jr.isPrimed {
for !jr.r.Empty() && jr.r.Front().Empty() {
jr.r.PopFront()
if !jr.r.Empty() && !jr.separator.Empty() {
jr.savedSeparator = jr.separator.Save()
break
}
}
jr.isPrimed = true
}
}
func (jr *joinerResult[T]) Empty() bool {
jr.prime()
return jr.r.Empty()
}
func (jr *joinerResult[T]) Front() T {
jr.prime()
if !jr.savedSeparator.Empty() {
return jr.savedSeparator.Front()
}
return jr.r.Front().Front()
}
func (jr *joinerResult[T]) PopFront() {
jr.prime()
if !jr.savedSeparator.Empty() {
jr.savedSeparator.PopFront()
if jr.savedSeparator.Empty() {
jr.isPrimed = false
}
} else {
jr.r.Front().PopFront()
jr.isPrimed = false
}
}
type joinerForwardResult[T any] struct {
r ForwardRange[ForwardRange[T]]
separator ForwardRange[T]
savedSeparator ForwardRange[T]
isPrimed bool
}
func (jr *joinerForwardResult[T]) prime() {
if !jr.isPrimed {
for !jr.r.Empty() && jr.r.Front().Empty() {
jr.r.PopFront()
if !jr.r.Empty() && !jr.separator.Empty() {
jr.savedSeparator = jr.separator.Save()
break
}
}
jr.isPrimed = true
}
}
func (jr *joinerForwardResult[T]) Empty() bool {
jr.prime()
return jr.r.Empty()
}
func (jr *joinerForwardResult[T]) Front() T {
jr.prime()
if !jr.savedSeparator.Empty() {
return jr.savedSeparator.Front()
}
return jr.r.Front().Front()
}
func (jr *joinerForwardResult[T]) PopFront() {
jr.prime()
if !jr.savedSeparator.Empty() {
jr.savedSeparator.PopFront()
if jr.savedSeparator.Empty() {
jr.isPrimed = false
}
} else {
jr.r.Front().PopFront()
jr.isPrimed = false
}
}
func (jfr *joinerForwardResult[T]) Save() ForwardRange[T] {
return &joinerForwardResult[T]{
jfr.r.Save(),
jfr.separator,
jfr.savedSeparator.Save(),
jfr.isPrimed,
}
}
// Joiner joins ranges together using a given ForwardRange as a separator.
func Joiner[T any](r InputRange[InputRange[T]], separator ForwardRange[T]) InputRange[T] {
return &joinerResult[T]{r, separator, Null[T](), false}
}
// JoinerF is `Joiner`, where the position can be saved.
func JoinerF[T any](r ForwardRange[ForwardRange[T]], separator ForwardRange[T]) ForwardRange[T] {
return &joinerForwardResult[T]{r, separator, Null[T](), false}
}
// JoinerS is `JoinerF` accepting a slice of ranges.
func JoinerS[T any](r []ForwardRange[T], separator ForwardRange[T]) ForwardRange[T] {
return JoinerF(SliceRange(r), separator)
}
// JoinerSS is `JoinerF` accepting a slice of lisces.
func JoinerSS[T any](r [][]T, separator []T) ForwardRange[T] {
return JoinerF(
CacheF(MapS(r, func(item []T) ForwardRange[T] { return SliceRange(item) })),
SliceRange(separator),
)
}
// JoinStrings joins a range of strings together as one string.
func JoinStrings(r ForwardRange[string], separator string) string {
return string(SliceF(
JoinerF(
CacheF(MapF(r, func(x string) ForwardRange[byte] { return Bytes(x) })),
Bytes(separator),
),
))
}