/
TC_numbers.go
166 lines (146 loc) · 3.57 KB
/
TC_numbers.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
package ThreadComputation
import (
"fmt"
"math"
conv "github.com/cstockton/go-conv"
"github.com/gammazero/deque"
)
type TCNumbers struct {
P float64
N []float64
}
func tcRotateFloat64(slice []float64, n int) (rotateSlice []float64) {
var begin []float64
var end []float64
size := len(slice)
rotateSlice = make([]float64, size)
nAbs := math.Abs(float64(n))
if int(nAbs) > size {
remainder, _ := tcQuotientAndRemainderF64(float64(n), float64(size))
n = int(remainder)
}
if n != 0 {
if n > 0 {
index := size - n
begin = slice[index:]
end = slice[0:index]
copy(rotateSlice, begin)
copy(rotateSlice[n:], end)
} else {
n = int(nAbs)
index := size - n
begin = slice[n:]
end = slice[0:n]
copy(rotateSlice, begin)
copy(rotateSlice[index:], end)
}
} else {
copy(rotateSlice, slice)
}
return rotateSlice
}
func tcQuotientAndRemainderF64(x, y float64) (Remainder, Quotient float64) {
Quotient = float64(math.Floor(float64(x / y)))
Remainder = x - y*Quotient
return Remainder, Quotient
}
func MakeNumbers() *TCNumbers {
res := new(TCNumbers)
res.P = 0.0
return res
}
func (tc *TCstate) Numbers() *TCNumbers {
return MakeNumbers()
}
func (n *TCNumbers) Add(v interface{}) bool {
switch v.(type) {
case float64:
n.P += 100.0
n.N = append(n.N, v.(float64))
case int64:
n.P += 100.0
n.N = append(n.N, float64(v.(int64)))
case string:
n.P += 100.0
num, err := conv.Float64(v.(string))
if err != nil {
return false
}
n.N = append(n.N, num)
case *TCList:
n.P += 100.0
for i := 0; i < v.(*TCList).Len(); i++ {
n.Add(v.(*TCList).Q.At(i))
}
case *TCValue:
switch v.(*TCValue).Value.(type) {
case int64:
n.P += float64(v.(*TCValue).P)
n.N = append(n.N, float64(v.(*TCValue).Value.(int64)))
case float64:
n.P += float64(v.(*TCValue).P)
n.N = append(n.N, v.(*TCValue).Value.(float64))
case string:
n.P += float64(v.(*TCValue).P)
num, err := conv.Float64(v.(*TCValue).Value.(string))
if err != nil {
return false
}
n.N = append(n.N, num)
}
default:
return false
}
return true
}
func (n *TCNumbers) Set(v []float64) {
n.N = v
}
func (n *TCNumbers) Len() int {
return len(n.N)
}
func (n *TCNumbers) RotateLeft() {
n.N = tcRotateFloat64(n.N, -1)
}
func (n *TCNumbers) RotateRight() {
n.N = tcRotateFloat64(n.N, 1)
}
func (n *TCNumbers) String() string {
out := "numbers[ "
for x := 0; x < len(n.N); x++ {
out += fmt.Sprintf("%v ", n.N[x])
}
out += " ]"
return out
}
func (n *TCNumbers) List() *TCList {
out := MakeList()
for x := 0; x < len(n.N); x++ {
out.Q.PushBack(n.N[x])
}
return out
}
func TCNumbersFunction(l *TCExecListener, name string, q *deque.Deque) (interface{}, error) {
n := l.TC.Numbers()
for q.Len() > 0 {
e := q.PopFront()
if ! n.Add(e) {
return nil, l.TC.MakeError("Error creating numbers[]")
}
}
return n, nil
}
func TCNumbersValueFunction(l *TCExecListener, name string, q *deque.Deque) (interface{}, error) {
n := l.TC.Numbers()
for q.Len() > 0 {
e := q.PopFront()
if ! n.Add(e) {
return nil, l.TC.MakeError("Error creating Numbers[]")
}
}
return MakeValue(n, float32(n.P/float64(n.Len())), 0), nil
}
func init() {
SetCommand("numbers", TCNumbersFunction)
SetCommand("JustNumbers", TCNumbersValueFunction)
}