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
/
queries.go
344 lines (289 loc) · 8.49 KB
/
queries.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package c3
import "math/rand"
import "time"
// The c3 query representation
type Q struct {
result Iterable
}
// Action is invoked for every item in the query result.
type Action func(item interface{})
// Predicate if a function that returns true if the predicate holds for the item.
type Predicate func(item interface{}) bool
// Aggregator converts an item and an aggregate into an aggregate result
type Aggregator func(item interface{}, aggregate interface{}) (aggregateResult interface{})
// Selector converts an item into another item
type Selector func(item interface{}) interface{}
// Lesser compares 2 items, such that a<b:true, false otherwise
type Lesser func(a, b interface{}) bool
// ManySelector converts 1 item into zero or more items
type ManySelector func(interface{}) Iterable
// Iterator provides an iterator for the query results.
func (q *Q) Iterator() Iterator {
return q.result.Iterator()
}
// Filters the items using the filter function.
// If filter returns true, the item is included
// in the result, otherwise it is skipped.
func (q *Q) Where(filter Predicate) *Q {
return &Q{&whereIterable{q.result, filter}}
}
// Select uses the selector to create a new result for each item.
func (q *Q) Select(selector Selector) *Q {
return &Q{&selectIterable{q.result, selector}}
}
// SelectMany uses the selector to create an Iteratable containing zero or more
// items for each item, and concatenates all the results.
func (q *Q) SelectMany(selector ManySelector) *Q {
return &Q{&selectManyIterable{q.result, selector}}
}
// For applies the action to every item in the query result.
func (q *Q) For(action Action) {
For(q, action)
}
// Run runs the query and discards the results.
func (q *Q) Run() {
for i := q.Iterator(); i.MoveNext(); {
// do nothing.
}
}
// Go applies the action to every item in the query result on a
// seperate goroutine using an unbuffered channel.
func (q *Q) Go(action Action) {
Go(q, action)
}
// Go applies the action to every item in the query result on a
// seperate goroutine using a buffered channel of the supplied size.
func (q *Q) GoBuffered(bufferSize int, action Action) {
GoBuffered(q, bufferSize, action)
}
// ToSlice puts the query results in a new slice
func (q *Q) ToSlice() []interface{} {
return ToSlice(q)
}
// ToList puts the query results in a new List
func (q *Q) ToList() List {
result, ok := q.result.(List)
if ok {
return result
}
return ToList(q)
}
// ToReadOnlyList puts the query results in a new ReadOnlyList
func (q *Q) ToReadOnlyList() ReadOnlyList {
return ToList(q)
}
// ToReadOnlyList puts the query results in a new ReadOnlyList
func (q *Q) ToReadOnlyBag() ReadOnlyBag {
return ToList(q)
}
// ToBag puts the query results in a new Bag
func (q *Q) ToBag() Bag {
return ToList(q)
}
// ToSet puts the unique query results in a new Set
func (q *Q) ToSet() Set {
return ToSet(q)
}
// ToQueue puts the unique query results in a new Queue
func (q *Q) ToQueue() Queue {
return ToQueue(q)
}
// ToStack puts the unique query results in a new Stack
func (q *Q) ToStack() Stack {
return ToStack(q)
}
// Aggregate applies the action to every item in the query result
// and combines them in a single result.
func (q *Q) Aggregate(aggregate interface{}, action Aggregator) interface{} {
for i := q.Iterator(); i.MoveNext(); {
aggregate = action(i.Value(), aggregate)
}
return aggregate
}
// First returns the first query result and true,
// or nil and false if there are no results.
func (q *Q) First() (interface{}, bool) {
for i := q.Iterator(); i.MoveNext(); {
return i.Value(), true
}
return defaultElementValue, false
}
// Last returns the last query result and true,
// or nil and false if there are no results.
func (q *Q) Last() (interface{}, bool) {
value, ok := defaultElementValue, false
for i := q.Iterator(); i.MoveNext(); {
value, ok = i.Value(), true
}
return value, ok
}
// Any returns true if there are results, false if there are not any results.
func (q *Q) Any() bool {
for i := q.Iterator(); i.MoveNext(); {
return true
}
return false
}
// All returns true if the predicate holds for all results, false otherwise.
func (q *Q) All(predicate Predicate) bool {
for i := q.Iterator(); i.MoveNext(); {
if !predicate(i.Value()) {
return false
}
}
return true
}
// Contains returns true if the query results contain the item, false otherwise.
func (q *Q) Contains(item interface{}) bool {
return q.Where(func(x interface{}) bool {
return x == item
}).Any()
}
// Count counts the number of results.
func (q *Q) Count() int {
count := 0
for i := q.Iterator(); i.MoveNext(); {
count++
}
return count
}
// Take truncates the results after count results have been computed.
// If there are less results Take returns only the available results.
func (q *Q) Take(count int) *Q {
taken := 0
return q.Where(func(v interface{}) bool {
if taken >= count {
return false
}
taken++
return taken <= count
})
}
// Prepend prepends the items to the query result.
func (q *Q) Prepend(items ...interface{}) *Q {
return NewQuery(ListOf(items...)).Concat(q)
}
// Appens appends the items to the query result.
func (q *Q) Append(items ...interface{}) *Q {
return q.Concat(ListOf(items...))
}
// Concat appends the items to the query result.
func (q *Q) Concat(items Iterable) *Q {
return NewQuery(&concatIterable{q, items})
}
// Distinct filters non-unique items from the query result.
func (q *Q) Distinct() *Q {
set := make(map[interface{}]bool)
return q.Where(func(v interface{}) bool {
if !set[v] {
set[v] = true
return true
}
return false
})
}
type concatIterable struct {
a, b Iterable
}
func (i *concatIterable) Iterator() Iterator {
return &concatIterator{i.a.Iterator(), i.b.Iterator(), defaultElementValue}
}
type concatIterator struct {
a, b Iterator
value interface{}
}
func (i *concatIterator) Value() interface{} {
return i.value
}
func (i *concatIterator) MoveNext() bool {
if i.a.MoveNext() {
i.value = i.a.Value()
return true
}
if i.b.MoveNext() {
i.value = i.b.Value()
return true
}
i.value = defaultElementValue
return false
}
// Tee applies the action to every item in the query result
// and passes every result on to the next query operator.
func (q *Q) Tee(action Action) *Q {
return q.Where(func(v interface{}) bool {
action(v)
return true
})
}
// Skip skips the first count results and returns all results after that.
// If there are less results Skip returns an empty result set.
func (q *Q) Skip(count int) *Q {
skipped := 0
return q.Where(func(v interface{}) bool {
if skipped >= count {
return true
}
skipped++
return skipped > count
})
}
// Sort sorts the result set using the lesser function.
func (q *Q) Sort(lesser Lesser) *Q {
l := q.ToList()
Sort(l, lesser)
return &Q{l}
}
// Shuffle randomizes the order of the result set.
func (q *Q) Shuffle() *Q {
return &Q{MakeIterable(func() Generate {
// make and fill the shuffle buffer
bufCap := 32
buf := make([]interface{}, 0, bufCap)
i := q.Iterator()
for len(buf) < bufCap && i.MoveNext() {
buf = append(buf, i.Value())
}
// setup the iterator state
shuffleDone := len(buf) == 0
sourceDone := len(buf) < bufCap
if shuffleDone {
// just return
return func() (interface{}, bool) {
return defaultElementValue, false
}
}
// setup the randomizer
rndSeed := time.Now().UnixNano()
rndSource := rand.NewSource(rndSeed)
rnd := rand.New(rndSource)
// return the Generate function.
return func() (interface{}, bool) {
if shuffleDone {
return defaultElementValue, false
}
// get the value from the buffer
bufLen := len(buf)
index := rnd.Intn(bufLen)
value := buf[index]
// get the next value from the source
if !sourceDone {
if i.MoveNext() {
buf[index] = i.Value()
return value, true
} else {
sourceDone = true
}
}
// move the last buffer value into the current value's location
lastIndex := bufLen - 1
// if index==lastIndex this is an expensive NOP
buf[index] = buf[lastIndex]
// clear the last value's location to help the
// garbage collector and then shorten the buffer
buf[lastIndex] = defaultElementValue
buf = buf[:lastIndex]
shuffleDone = len(buf) == 0
return value, true
}
})}
}