This repository has been archived by the owner on Apr 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.go
204 lines (180 loc) · 4.08 KB
/
utils.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 c3
var (
// An empty Iterable
emptyIterable Iterable = &nilIterable{}
// An empty Iterator.
emptyIterator Iterator = &nilIterator{}
)
func EmptyIterable() Iterable {
return emptyIterable
}
func EmptyIterator() Iterator {
return emptyIterator
}
type nilIterable struct{}
func (x *nilIterable) Iterator() Iterator {
return emptyIterator
}
type nilIterator struct{}
func (x *nilIterator) MoveNext() bool { return false }
func (x *nilIterator) Value() interface{} { return defaultElementValue }
// Sort sorts the list with the given Lesser function
func Sort(l List, lesser Lesser) {
s := &Sorter{l, lesser}
s.Sort()
}
// ToSlice makes a new slice of the items in an Iterable
func ToSlice(c Iterable) []interface{} {
var slice []interface{}
if col, ok := c.(ReadOnlyBag); ok {
slice = make([]interface{}, 0, col.Len())
} else {
slice = make([]interface{}, 0, 4)
}
for i := c.Iterator(); i.MoveNext(); {
slice = append(slice, i.Value())
}
return slice
}
// ToList creates a new List of the items in an Iterable
func ToList(c Iterable) List {
l := NewList()
for i := c.Iterator(); i.MoveNext(); {
l.Add(i.Value())
}
return l
}
// ToReadOnlyList creates a new ReadOnlyList of the items in an Iterable
func ToReadOnlyList(c Iterable) ReadOnlyList {
l := NewList()
for i := c.Iterator(); i.MoveNext(); {
l.Add(i.Value())
}
return l
}
// ToBag creates a new Bag of the items in an Iterable
func ToBag(c Iterable) Bag {
l := NewList()
for i := c.Iterator(); i.MoveNext(); {
l.Add(i.Value())
}
return l
}
// ToReadOnlyBag creates a new ReadOnlyBag of the items in an Iterable
func ToReadOnlyBag(c Iterable) ReadOnlyBag {
l := NewList()
for i := c.Iterator(); i.MoveNext(); {
l.Add(i.Value())
}
return l
}
// ToSet makes a new Set of the unique items in an Iterable
func ToSet(c Iterable) Set {
set := NewSet()
for i := c.Iterator(); i.MoveNext(); {
set.Add(i.Value())
}
return set
}
// ToStack makes a new Stack of the items in an Iterable
func ToStack(c Iterable) Stack {
s := NewStack()
for i := c.Iterator(); i.MoveNext(); {
s.Push(i.Value())
}
return s
}
// ToQueue makes a new Queue of the items in an Iterable
func ToQueue(c Iterable) Queue {
s := NewQueue()
for i := c.Iterator(); i.MoveNext(); {
s.Enqueue(i.Value())
}
return s
}
// For applies the action to each item in the Iterable
func For(c Iterable, action Action) {
for i := c.Iterator(); i.MoveNext(); {
action(i.Value())
}
}
// Go applies the action to each item in the Iterable
// on a separate goroutine using an unbuffered channel
func Go(c Iterable, action Action) {
ch := make(chan interface{})
defer close(ch)
go func() {
for value := range ch {
action(value)
}
}()
For(c, func(value interface{}) { ch <- value })
}
// GoBuffered applies the action to each item in the Iterable
// on a separate goroutine using a buffered channel
func GoBuffered(c Iterable, bufferSize int, action Action) {
ch := make(chan interface{}, bufferSize)
defer close(ch)
go func() {
for value := range ch {
action(value)
}
}()
For(c, func(value interface{}) { ch <- value })
}
// Repeat repeats the item count times.
//
// e.g.:
// Repeat(3,42) // returns [42,42,42]
func Repeat(count int, item interface{}) Iterable {
if count < 0 {
panic("Count parameter invalid")
}
if count == 0 {
return emptyIterable
}
return MakeIterable(func() Generate {
x := 0
return func() (interface{}, bool) {
if x == count {
return defaultElementValue, false
}
x++
return item, true
}
})
}
// Range creates a range iterable that iterates over the ints from start to end inclusive.
//
// e.g.:
// Range(0,9) // returns [0,1,2,3,4,5,6,7,8,9]
// Range(9,0) // returns [9,8,7,6,5,4,3,2,1,0]
func Range(start, end int) Iterable {
inc := 1
if end < start {
inc = -1
}
return MakeIterable(func() Generate {
x := start - inc
y := end
return func() (interface{}, bool) {
if x == y {
return defaultElementValue, false
}
x = x + inc
return x, true
}
})
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func min(a, b int) int {
if a < b {
return a
}
return b
}