-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmisc.go
149 lines (122 loc) · 2.7 KB
/
misc.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 iter
import (
"fmt"
"io"
"math/rand"
)
type iotaReader[T Numeric] struct {
x T
}
func (r iotaReader[T]) Read() T {
return r.x
}
func (r iotaReader[T]) Next() iotaReader[T] {
return iotaReader[T]{x: r.x + 1}
}
func (r iotaReader[T]) Eq(iotaReader[T]) bool {
return false
}
// IotaReader creates an InputIter that returns [x, x+1, x+2...).
func IotaReader[T Numeric, It iotaReader[T]](x T) It {
return It{x: x}
}
// IotaGenerator creates a Generator that returns [x, x+1, x+2...).
func IotaGenerator[T Numeric](x T) func() T {
r := IotaReader(x)
return func() T {
v := r.Read()
r = r.Next()
return v
}
}
type repeatReader[T any] struct {
x T
}
func (r repeatReader[T]) Read() T { return r.x }
func (r repeatReader[T]) Next() repeatReader[T] { return r }
func (r repeatReader[T]) Eq(repeatReader[T]) bool { return false }
// RepeatReader creates an InputIter that returns [x, x, x...).
func RepeatReader[T any](x T) repeatReader[T] {
return repeatReader[T]{x: x}
}
// RepeatGenerator creates an Generator that returns [x, x, x...).
func RepeatGenerator[T any](x T) func() T {
return func() T { return x }
}
// RandomGenerator creates a generator that returns random item of a slice.
func RandomGenerator[T any](s []T, r *rand.Rand) func() T {
return func() T { return s[r.Intn(len(s))] }
}
type chanReader[T any] struct {
ch chan T
cur T
read1 bool
eof bool
}
func (cr *chanReader[T]) recv() {
v, ok := <-cr.ch
cr.cur, cr.read1, cr.eof = v, true, !ok
}
func (cr *chanReader[T]) Read() T {
if !cr.read1 {
cr.recv()
}
return cr.cur
}
func (cr *chanReader[T]) Next() *chanReader[T] {
if !cr.read1 {
cr.recv()
}
if !cr.eof {
cr.recv()
}
return cr
}
func (cr *chanReader[T]) Eq(x *chanReader[T]) bool {
if !cr.read1 {
cr.recv()
}
return cr.eof && x == nil
}
type chanWriter[T any] struct {
ch chan T
}
func (cr *chanWriter[T]) Write(x T) {
cr.ch <- x
}
// ChanReader returns an InputIter that reads from a channel.
func ChanReader[T any](c chan T) *chanReader[T] {
return &chanReader[T]{
ch: c,
}
}
// ChanWriter returns an OutIter that writes to a channel.
func ChanWriter[T any](c chan T) *chanWriter[T] {
return &chanWriter[T]{
ch: c,
}
}
type ioWriter struct {
w io.Writer
written bool
delimiter []byte
}
func (w *ioWriter) Write(x any) {
if w.written && len(w.delimiter) > 0 {
_, err := w.w.Write(w.delimiter)
if err != nil {
panic(err)
}
} else {
w.written = true
}
_, err := fmt.Fprint(w.w, x)
if err != nil {
panic(err)
}
}
// IOWriter returns an OutputIter that writes values to an io.Writer.
// It panics if meet any error.
func IOWriter(w io.Writer, delimiter string) *ioWriter {
return &ioWriter{w: w, delimiter: []byte(delimiter)}
}