-
Notifications
You must be signed in to change notification settings - Fork 12
/
table.go
164 lines (143 loc) · 3.13 KB
/
table.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
package datatable
import (
"fmt"
"strings"
)
// New creates a new datatable
func New(name string) *DataTable {
return &DataTable{name: name}
}
// DataTable is our main struct
type DataTable struct {
name string
cols []*column
nrows int
dirty bool
hasExpr bool
}
// Name returns the datatable's name
func (t *DataTable) Name() string {
return t.name
}
// Rename the datatable
func (t *DataTable) Rename(name string) {
t.name = name
}
// NumRows returns the number of rows in datatable
func (t *DataTable) NumRows() int {
return t.nrows
}
// NumCols returns the number of visible columns in datatable
func (t *DataTable) NumCols() int {
return len(t.Columns())
}
// Columns returns the visible column names in datatable
func (t *DataTable) Columns() []string {
var cols []string
for _, col := range t.cols {
if col.IsVisible() {
cols = append(cols, col.Name())
}
}
return cols
}
// HiddenColumns returns the hidden column names in datatable
func (t *DataTable) HiddenColumns() []string {
var cols []string
for _, col := range t.cols {
if !col.IsVisible() {
cols = append(cols, col.Name())
}
}
return cols
}
// Column gets the column with name
// returns nil if not found
func (t *DataTable) Column(name string) Column {
for _, col := range t.cols {
if col.Name() == name {
return col
}
}
return nil
}
// ColumnIndex gets the index of the column with name
// returns -1 if not found
func (t *DataTable) ColumnIndex(name string) int {
for i, col := range t.cols {
if col.Name() == name {
return i
}
}
return -1
}
// Records returns the rows in datatable as string
// Computes all expressions.
func (t *DataTable) Records() [][]string {
if t == nil {
return nil
}
if err := t.evaluateExpressions(); err != nil {
panic(err)
}
// visible columns
var cols []int
for i, col := range t.cols {
if col.IsVisible() {
cols = append(cols, i)
}
}
rows := make([][]string, 0, t.nrows)
for i := 0; i < t.nrows; i++ {
r := make([]string, 0, len(cols))
for _, pos := range cols {
r = append(r, fmt.Sprintf("%v", t.cols[pos].serie.Get(i)))
}
rows = append(rows, r)
}
return rows
}
// Rows returns the rows in datatable
// Computes all expressions.
func (t *DataTable) Rows(opt ...ExportOption) []Row {
if t == nil {
return nil
}
opts := newExportOptions(opt...)
if err := t.evaluateExpressions(); err != nil {
panic(err)
}
// visible columns
cols := make(map[string]int)
for i, col := range t.cols {
if opts.WithHiddenCols || col.IsVisible() {
cols[col.Name()] = i
}
}
rows := make([]Row, 0, t.nrows)
for i := 0; i < t.nrows; i++ {
r := make(Row, len(cols))
for name, pos := range cols {
r[name] = t.cols[pos].serie.Get(i)
}
rows = append(rows, r)
}
return rows
}
func (t *DataTable) String() string {
var sb strings.Builder
t.Print(&sb)
return sb.String()
}
// Row gets the row at index
func (t *DataTable) Row(at int, opt ...ExportOption) Row {
opts := newExportOptions(opt...)
t.evaluateExpressions()
r := make(Row, len(t.cols))
for _, col := range t.cols {
if opts.WithHiddenCols || col.IsVisible() {
r[col.name] = col.serie.Get(at)
}
}
return r
}