-
Notifications
You must be signed in to change notification settings - Fork 3
/
sort.go
112 lines (92 loc) · 2.02 KB
/
sort.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
package gollection
import (
"reflect"
"sync"
)
func (g *gollection) SortBy(f /* func(v1, v2 <T>) bool */ interface{}) *gollection {
if g.err != nil {
return &gollection{err: g.err}
}
if g.ch != nil {
return g.sortByStream(f)
}
return g.sortBy(f)
}
func (g *gollection) sortBy(f interface{}) *gollection {
sv, err := g.validateSlice("SortBy")
if err != nil {
return &gollection{err: err}
}
ret := reflect.MakeSlice(sv.Type(), sv.Len(), sv.Cap())
reflect.Copy(ret, sv)
funcValue, _, err := g.validateSortByFunc(f)
if err != nil {
return &gollection{err: err}
}
processSort(funcValue, ret)
return &gollection{
slice: ret.Interface(),
err: nil,
}
}
func (g *gollection) sortByStream(f interface{}) *gollection {
next := &gollection{
ch: make(chan interface{}),
}
funcValue, _, err := g.validateSortByFunc(f)
if err != nil {
return &gollection{err: err}
}
var ret reflect.Value
var initialized bool
var skippedFirst bool
var currentType reflect.Type
wg := sync.WaitGroup{}
wg.Add(1)
go func(wg *sync.WaitGroup, currentType *reflect.Type) {
for {
select {
case v, ok := <-g.ch:
if ok {
if !skippedFirst {
skippedFirst = true
*currentType = v.(reflect.Type)
continue
}
// initialze next stream type
if !initialized {
ret = reflect.MakeSlice(reflect.SliceOf(reflect.ValueOf(v).Type()), 0, 0)
initialized = true
}
ret = reflect.Append(ret, reflect.ValueOf(v))
} else {
wg.Done()
return
}
default:
continue
}
}
}(&wg, ¤tType)
wg.Wait()
processSort(funcValue, ret)
go func() {
// initialze next stream type
next.ch <- currentType
for i := 0; i < ret.Len(); i++ {
next.ch <- ret.Index(i).Interface()
}
close(next.ch)
}()
return next
}
type funcs struct {
length int
less func(i, j int) bool
swap func(i, j int)
}
func (f *funcs) Len() int { return f.length }
func (f *funcs) Less(i, j int) bool { return f.less(i, j) }
func (f *funcs) Swap(i, j int) {
f.swap(i, j)
}