-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathutil.go
86 lines (75 loc) · 1.16 KB
/
util.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
// Copyright 2009 The GoMatrix Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package matrix
import "runtime"
func max(x, y float64) float64 {
if x > y {
return x
}
return y
}
func maxInt(x, y int) int {
if x > y {
return x
}
return y
}
func min(x, y float64) float64 {
if x < y {
return x
}
return y
}
func minInt(x, y int) int {
if x < y {
return x
}
return y
}
func sum(a []float64) (s float64) {
for _, v := range a {
s += v
}
return
}
func product(a []float64) float64 {
p := float64(1)
for _, v := range a {
p *= v
}
return p
}
type box interface{}
func countBoxes(start, cap int) chan box {
ints := make(chan box)
go func() {
for i := start; i < cap; i++ {
ints <- i
}
close(ints)
}()
return ints
}
func parFor(inputs <-chan box, foo func(i box)) (wait func()) {
n := runtime.GOMAXPROCS(0)
block := make(chan bool, n)
for j := 0; j < n; j++ {
go func() {
for {
i, ok := <-inputs
if !ok {
break
}
foo(i)
}
block <- true
}()
}
wait = func() {
for i := 0; i < n; i++ {
<-block
}
}
return
}