This repository has been archived by the owner on Mar 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
225 lines (191 loc) · 4.1 KB
/
node.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
213
214
215
216
217
218
219
220
221
222
223
224
225
package AlgebraCalculator
import (
"fmt"
"math"
)
// Flag indexes that are used as index in flagValues.
const (
flagNone = 0
flagRoot = 1
// Data Types no Children
flagData = 10
flagNumber = 11
flagConstant = 12
flagVariable = 13
// Action Typs have Children
flagAction = 20
flagOperator1 = 21
flagOperator2 = 22
flagVector = 23
flagTerm = 24
flagBracketRoot = 30
flagRuleData = 31
flagMax = 40
flagOptional = 30
)
type node struct {
childs []*node
data string
dataNumber float64
flagValues [flagMax]bool
}
func newNode(data string, dataNumber float64, flags ...int) *node {
node := &node{
data: data,
dataNumber: dataNumber,
}
// Set all flagValues that are parsed in.
for _, flag := range flags {
node.setFlag(flag, true)
}
// If no flagValues are set node will ge flag none
if len(flags) == 0 {
node.setFlag(flagNone, true)
}
return node
}
func (n *node) setChilds(childs ...*node) {
n.childs = childs
}
func (n *node) setFlag(flag int, value bool) {
// Remove none when an other flag is set.
if n.flagValues[flagNone] && flag != flagNone && value {
n.flagValues[flagNone] = false
}
n.flagValues[flag] = value
}
func (n *node) hasFlag(flags ...int) bool {
for _, flag := range flags {
if !n.flagValues[flag] {
return false
}
}
return true
}
func (n *node) hasAllFlagsOfNode(reference *node) bool {
for flag, flagValue := range reference.flagValues {
if flag >= flagOptional {
break
}
if flagValue && !n.hasFlag(flag) {
return false
}
}
return true
}
func (n *node) equal(reference *node) bool {
if !n.hasAllFlagsOfNode(reference) {
return false
}
// Number are the same when the number is the same. Else it usually the string.
if n.hasFlag(flagNumber) {
return n.dataNumber == reference.dataNumber
} else {
return n.data == reference.data
}
}
// Recursive equal check.
func (n *node) equalDeep(reference *node) bool {
if len(n.childs) != len(reference.childs) || !n.equal(reference) {
return false
}
for i, child := range n.childs {
if !child.equal(reference.childs[i]) {
return false
}
}
return true
}
// Recursive copy
func (n *node) copyDeep() *node {
copy := newNode(n.data, n.dataNumber)
copy.flagValues = n.flagValues
for _, child := range n.childs {
copy.childs = append(copy.childs, child.copyDeep())
}
return copy
}
// Both data types combined create a clear idenity
func (n *node) getIdentity() string { // TODO improve for sorting
return n.data + fmt.Sprintf("%f", n.dataNumber)
}
// Combined identity of node and childs.
func (n *node) getIdentityDeep() string {
identify := n.getIdentity()
for _, child := range n.childs {
identify += child.getIdentityDeep()
}
return identify
}
func (n *node) print() {
if n.hasFlag(flagBracketRoot) {
Print("(")
}
if n.hasFlag(flagVector) {
Print("(")
for i, child := range n.childs {
if i != 0 {
Print(", ")
}
child.print()
}
Print(")")
} else if n.hasFlag(flagOperator2) {
n.childs[0].print()
Printf(" %s ", n.data)
n.childs[1].print()
} else {
if n.hasFlag(flagNumber) {
if n.dataNumber == math.Trunc(n.dataNumber) {
Printf("%.0f", n.dataNumber)
} else {
Printf("%.4f", n.dataNumber)
}
} else {
Print(n.data)
}
for _, child := range n.childs {
Print(" ")
child.print()
}
}
if n.hasFlag(flagBracketRoot) {
Print(")")
}
}
func (n *node) printTree(indentation int) {
if n.hasFlag(flagOperator2) {
n.childs[0].printTree(indentation + 1)
Print("\n")
printIndentation(indentation)
Printf(" %s ", n.data)
n.childs[1].printTree(indentation + 1)
} else {
Print("\n")
printIndentation(indentation)
if n.hasFlag(flagNumber) {
if n.dataNumber == math.Trunc(n.dataNumber) {
Printf("%.0f", n.dataNumber)
} else {
Printf("%.4f", n.dataNumber)
}
} else {
Print(n.data)
}
for _, child := range n.childs {
Print(" ")
child.printTree(indentation + 1)
}
}
}
func printIndentation(indentation int) {
for i := 0; i < indentation; i++ {
if i == indentation-1 {
Print("|> ")
} else if i == 0 {
Print("| ")
} else {
Print(" ")
}
}
}