-
Notifications
You must be signed in to change notification settings - Fork 9
/
vector.go
61 lines (49 loc) · 1.03 KB
/
vector.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
package varis
import (
"sync"
)
// Vector implement array of float64
type Vector []float64
// sum all element of vector
func (v Vector) sum() (result float64) {
for _, i := range v {
result += i
}
return result
}
// broadcast send all values to each of channels of channels array
func (v Vector) broadcast(channels []chan float64) {
if len(v) != len(channels) {
panic("Length not equal")
}
for i, c := range channels {
c <- v[i]
}
}
// collectVector get all values from each of channels of channels array
func collectVector(channels []chan float64) (vector Vector) {
count := len(channels)
vector = make(Vector, count)
wg := sync.WaitGroup{}
wg.Add(count)
for i, c := range channels {
go func(index int, channel chan float64) {
vector[index] = <-channel
wg.Done()
}(i, c)
}
wg.Wait()
return vector
}
// is compare two Vectors
func (v Vector) is(other Vector) bool {
if len(v) != len(other) {
return false
}
for index, value := range v {
if value != other[index] {
return false
}
}
return true
}