-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcell.go
202 lines (189 loc) · 3.21 KB
/
cell.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
package data
import (
"fmt"
"math"
"reflect"
"strconv"
"strings"
"go4ml.xyz/errstr"
)
type Cell struct {
Val interface{}
}
func (c Cell) Type() reflect.Type {
return reflect.TypeOf(c.Val)
}
func (c Cell) Na() bool {
return c.Val == nil
}
func (c Cell) Text() string {
switch v := c.Val.(type) {
case string:
return v
case Tensor:
var s []string
dim := v.Dimension()
for i := 0; i < 4; i++ {
if i == 3 || i >= dim.Volume() {
s = append(s, ">")
break
} else if i < dim.Volume() {
s = append(s, fmt.Sprint(v.Index(i)))
}
}
ch, h, w := dim.CHW()
return fmt.Sprintf("(%dx%dx%d){%v}", ch, h, w, strings.Join(s, ","))
default:
return fmt.Sprint(v)
}
}
func (c Cell) String() string { return c.Text() }
func (c Cell) Tensor() Tensor {
switch v := c.Val.(type) {
case Tensor:
return v
default:
panic(errstr.Errorf("can't convert %v to Tensor", reflect.TypeOf(c.Val)))
}
}
func (c Cell) Int() int {
switch v := c.Val.(type) {
case int:
return v
default:
return int(c.Int64())
}
}
func (c Cell) Byte() byte {
switch v := c.Val.(type) {
case byte:
return v
default:
return byte(c.Uint64())
}
}
func (c Cell) float32() float32 {
switch v := c.Val.(type) {
case float32:
return v
default:
return float32(c.Float64())
}
}
func (c Cell) Int64() int64 {
switch v := c.Val.(type) {
case uint8:
return int64(v)
case int8:
return int64(v)
case int:
return int64(v)
case uint:
return int64(v)
case int16:
return int64(v)
case uint16:
return int64(v)
case int32:
return int64(v)
case uint32:
return int64(v)
case int64:
return v
case uint64:
return int64(v)
case float32:
return int64(v)
case float64:
return int64(v)
case string:
x, err := strconv.ParseInt(v, 10, 64)
if err != nil {
panic(err)
}
return x
default:
if c.Na() {
return 0
}
panic(errstr.Errorf("can't convert %v to int", reflect.TypeOf(c.Val)))
}
}
func (c Cell) Uint64() uint64 {
switch v := c.Val.(type) {
case uint8:
return uint64(v)
case int8:
return uint64(v)
case int:
return uint64(v)
case uint:
return uint64(v)
case int16:
return uint64(v)
case uint16:
return uint64(v)
case int32:
return uint64(v)
case uint32:
return uint64(v)
case int64:
return uint64(v)
case uint64:
return v
case float32:
return uint64(v)
case float64:
return uint64(v)
case string:
x, err := strconv.ParseUint(v, 10, 64)
if err != nil {
panic(err)
}
return x
default:
if c.Na() {
return 0
}
panic(errstr.Errorf("can't convert %v to int", reflect.TypeOf(c.Val)))
}
}
func (c Cell) Float64() float64 {
switch v := c.Val.(type) {
case uint8:
return float64(v)
case int8:
return float64(v)
case int:
return float64(v)
case uint:
return float64(v)
case int16:
return float64(v)
case uint16:
return float64(v)
case int32:
return float64(v)
case uint32:
return float64(v)
case int64:
return float64(v)
case uint64:
return float64(v)
case float32:
return float64(v)
case float64:
return v
case string:
x, err := strconv.ParseFloat(v, 64)
if err != nil {
panic(err)
}
return x
default:
if c.Na() {
return math.NaN()
}
panic(errstr.Errorf("can't convert %v to int", reflect.TypeOf(c.Val)))
}
}