forked from gorgonia/gorgonia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collections.go
212 lines (186 loc) · 4 KB
/
collections.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
package gorgonia
import (
"fmt"
"sort"
"unsafe"
"github.com/xtgo/set"
)
// Nodes is a slice of nodes, but it also acts as a set of nodes by implementing the Sort interface
type Nodes []*Node
// implement sort.Interface
func (ns Nodes) Len() int { return len(ns) }
func (ns Nodes) Less(i, j int) bool {
return uintptr(unsafe.Pointer(ns[i])) < uintptr(unsafe.Pointer(ns[j]))
// return ns[i].Hashcode() < ns[j].Hashcode()
}
func (ns Nodes) Swap(i, j int) { ns[i], ns[j] = ns[j], ns[i] }
// uses xtgo/set stuff
// Set returns a uniquifies slice. It mutates the slice.
func (ns Nodes) Set() Nodes {
sort.Sort(ns)
size := set.Uniq(ns)
ns = ns[:size]
return ns
}
// Add adds to set
func (ns Nodes) Add(n *Node) Nodes {
for _, node := range ns {
if node == n {
return ns
}
}
ns = append(ns, n)
return ns
}
// Contains checks if the wanted node is in the set
func (ns Nodes) Contains(want *Node) bool {
for _, n := range ns {
if n == want {
return true
}
}
return false
}
// Format implements fmt.Formatter, which allows Nodes to be differently formatted depending on the verbs
func (ns Nodes) Format(s fmt.State, c rune) {
delimiter := ", "
if s.Flag(' ') {
delimiter = " "
}
if s.Flag('+') {
delimiter = ", \n"
}
switch c {
case 'd':
s.Write([]byte("["))
for i, n := range ns {
fmt.Fprintf(s, "%x", n.Hashcode())
if i < len(ns)-1 {
fmt.Fprintf(s, "%s", delimiter)
}
}
s.Write([]byte("]"))
case 'v', 's':
s.Write([]byte("["))
for i, n := range ns {
if s.Flag('#') {
fmt.Fprintf(s, "%s :: %v", n.Name(), n.t)
} else {
fmt.Fprintf(s, "%s", n.Name())
}
if i < len(ns)-1 {
fmt.Fprintf(s, "%s", delimiter)
}
}
s.Write([]byte("]"))
case 'Y':
s.Write([]byte("["))
for i, n := range ns {
fmt.Fprintf(s, "%v", n.t)
if i < len(ns)-1 {
fmt.Fprintf(s, "%s", delimiter)
}
}
s.Write([]byte("]"))
case 'P':
s.Write([]byte("["))
for i, n := range ns {
fmt.Fprintf(s, "%p", n)
if i < len(ns)-1 {
fmt.Fprintf(s, "%s", delimiter)
}
}
s.Write([]byte("]"))
}
}
// Difference is ns - other. Bear in mind it is NOT commutative
func (ns Nodes) Difference(other Nodes) Nodes {
sort.Sort(ns)
sort.Sort(other)
s := append(ns, other...)
count := set.Diff(s, len(ns))
return s[:count]
}
// Intersect performs an intersection with other Nodes
func (ns Nodes) Intersect(other Nodes) Nodes {
sort.Sort(ns)
sort.Sort(other)
s := append(ns, other...)
count := set.Inter(s, len(ns))
return s[:count]
}
// AllSameGraph returns true if all the nodes in the slice belong to the same graph. Note that constants do not have to belong to the same graph.
func (ns Nodes) AllSameGraph() bool {
if len(ns) == 0 {
return false
}
var g *ExprGraph
for _, n := range ns {
if !n.isConstant() {
g = n.g
break
}
}
for _, n := range ns {
if n.g != g && !n.isConstant() {
return false
}
}
return true
}
// Equals returns true if two Nodes are the same
func (ns Nodes) Equals(other Nodes) bool {
if len(ns) != len(other) {
return false
}
for _, n := range ns {
if !other.Contains(n) {
return false
}
}
return true
}
func (ns Nodes) mapSet() NodeSet { return NewNodeSet(ns...) }
func (ns Nodes) index(n *Node) int {
for i, node := range ns {
if node == n {
return i
}
}
return -1
}
func (ns Nodes) reverse() {
l := len(ns)
for i := l/2 - 1; i >= 0; i-- {
o := l - 1 - i
ns[i], ns[o] = ns[o], ns[i]
}
}
func (ns Nodes) replace(what, with *Node) Nodes {
for i, n := range ns {
if n == what {
ns[i] = with
}
}
return ns
}
var removers = make(map[string]int)
func (ns Nodes) remove(what *Node) Nodes {
for i := ns.index(what); i != -1; i = ns.index(what) {
copy(ns[i:], ns[i+1:])
ns[len(ns)-1] = nil // to prevent any unwanted references so things can be GC'd away
ns = ns[:len(ns)-1]
}
return ns
}
func (ns Nodes) dimSizers() []DimSizer {
retVal := borrowDimSizers(len(ns))
for i, n := range ns {
if s, ok := n.op.(sizeOp); ok {
retVal[i] = s
} else {
retVal[i] = n.shape
}
}
return retVal
}