-
Notifications
You must be signed in to change notification settings - Fork 18
/
matrix.go
126 lines (95 loc) · 2.31 KB
/
matrix.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
package node
type Predicate func(nodeName string) bool
func (node *Node) FindParent(predicate Predicate) *Node {
current := node.Parent
for current != nil {
if predicate(current.Name) {
return current
}
current = current.Parent
}
return nil
}
func ReversePath(path []int) []int {
var result []int
if len(path) == 0 {
return []int{}
}
for i := len(path) - 1; i >= 0; i-- {
result = append(result, path[i])
}
return result
}
func (node *Node) PathUpwardsUpto(upto *Node) []int {
var result []int
parent := node.Parent
lookingFor := node
for parent != nil {
for i, parentChild := range parent.Children {
if parentChild == lookingFor {
result = append(result, i)
lookingFor = parent
break
}
}
if parent == upto {
break
}
parent = parent.Parent
}
return result
}
func (node *Node) GetPath(path []int) *Node {
found := node
for _, index := range path {
if len(found.Children) < index+1 {
return nil
}
found = found.Children[index]
}
return found
}
func (node *Node) DeepCopy() *Node {
return node.CopyWithParent(nil)
}
func (node *Node) DeepCopyWithReplacements(target *Node, replacements []*Node) *Node {
// Remember the path to the target node so we'll be able
// to find it in the deep copy
pathToNode := ReversePath(target.PathUpwardsUpto(node))
nodeCopy := node.DeepCopy()
targetCopy := nodeCopy.GetPath(pathToNode)
targetCopy.ReplaceWith(replacements)
return nodeCopy
}
func (node *Node) ReplaceWith(with []*Node) {
var newChildren []*Node
// Link replacements to the node's parent
for _, withItem := range with {
withItem.Parent = node.Parent
}
for _, thisLevelChild := range node.Parent.Children {
if thisLevelChild == node {
newChildren = append(newChildren, with...)
} else {
newChildren = append(newChildren, thisLevelChild)
}
}
node.Parent.Children = newChildren
}
func (node *Node) MergeMapsOrOverwrite(with *Node) {
_, nodeIsMap := node.Value.(*MapValue)
_, withIsMap := with.Value.(*MapValue)
if nodeIsMap && withIsMap {
for _, child := range with.Children {
child.Parent = node
node.Children = append(node.Children, child)
}
return
}
node.Value = with.Value
node.Children = node.Children[:0]
for _, child := range with.Children {
child.Parent = node
node.Children = append(node.Children, child)
}
}