-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataframe.go
88 lines (78 loc) · 2.12 KB
/
dataframe.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
package elefas
import (
"fmt"
)
type SizedNumber interface {
int8 | int16 | int32 | int64 |
uint8 | uint16 | uint32 | uint64 |
float32 | float64
}
type DataFrame[T SizedNumber] struct {
Dims []int
Data []T
}
func MakeDataFrame[T SizedNumber](dims []int) DataFrame[T] {
if len(dims) == 0 {
return DataFrame[T]{}
}
totalSize := 1
for i := 0; i < len(dims); i++ {
totalSize *= dims[i]
}
return DataFrame[T]{
Dims: dims,
Data: make([]T, totalSize),
}
}
func (df DataFrame[T]) DimCount() int { return len(df.Dims) }
func (df DataFrame[T]) Dim(i int) int { return df.Dims[i] }
func (df DataFrame[T]) TotalSize() int { return len(df.Data) }
func (df DataFrame[T]) FlatAt(i int) T { return df.Data[i] }
func (df DataFrame[T]) SetFlatAt(v T, i int) { df.Data[i] = v }
func (df DataFrame[T]) Index(indices ...int) int {
if len(indices) != len(df.Dims) {
panic("number of indices does not match number of dims")
}
for i := 0; i < len(df.Dims); i++ {
if indices[i] >= df.Dims[i] {
panic(fmt.Sprintf("index %d is too big for dimension %d which is %d", indices[i], i, df.Dims[i]))
}
}
idx := indices[0]
for i := 1; i < len(df.Dims); i++ {
idx = df.Dims[i]*idx + indices[i]
}
return idx
}
func (df DataFrame[T]) At(indices ...int) T { return df.Data[df.Index(indices...)] }
func (df DataFrame[T]) SetAt(v T, indices ...int) { df.Data[df.Index(indices...)] = v }
func (df DataFrame[T]) Sub(index int) DataFrame[T] {
size := df.TotalSize() / df.Dims[0]
return DataFrame[T]{
Dims: df.Dims[1:],
Data: df.Data[size*index : size*(index+1)],
}
}
func (df DataFrame[T]) Slice(start, end int) DataFrame[T] {
if start > end {
panic("start must be less than end")
}
ndims := make([]int, len(df.Dims))
copy(ndims, df.Dims)
ndims[0] = end - start
subSize := df.TotalSize() / df.Dims[0]
return DataFrame[T]{
Dims: ndims,
Data: df.Data[subSize*start : subSize*end],
}
}
func CastDf[T, U SizedNumber](df DataFrame[T]) DataFrame[U] {
res := DataFrame[U]{
Dims: df.Dims,
Data: make([]U, len(df.Data)),
}
for i := 0; i < len(df.Data); i++ {
res.Data[i] = U(df.Data[i])
}
return res
}