-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_table.go
83 lines (63 loc) · 1.99 KB
/
feature_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
package feature
import (
"fmt"
)
type Table struct {
features []Feature
featureMap map[TypeKey]int
items []*Instance
numColumns int
numRows int
}
func CreateTable(features []Feature) *Table {
table := Table{}
featureMap := map[TypeKey]int{}
for index, feature := range features {
featureMap[feature.TypeKey()] = index
}
table.features = features
table.featureMap = featureMap
table.items = []*Instance{}
table.numColumns = len(features)
return &table
}
func (t *Table) AddStringRow(records []string) {
if len(records) != t.numColumns {
panic(fmt.Sprintf("Number of records added does not equal the number of columns. Expected: %d; Got: %d", t.numColumns, len(records)))
}
row := make([]*Instance, len(t.features))
for index, feature := range t.features {
row[index] = feature.Create(records[index])
}
t.items = append(t.items, row...)
t.numRows += 1
}
func (t *Table) AddColumn(feature Feature, column []*Instance) {
if len(column) != t.NumRows() {
panic(fmt.Sprintf("Number of rows in added column does not equal the number of rows. Expected: %d; Got: %s", t.NumRows(), len(column)))
}
t.features = append(t.features, feature)
t.featureMap[feature.TypeKey()] = len(t.features) - 1
items := make([]*Instance, len(t.items)+len(column))
for rowIndex := 0; rowIndex < t.NumRows(); rowIndex++ {
copy(items[rowIndex*t.numColumns+rowIndex:rowIndex*t.numColumns+t.numColumns+rowIndex], t.items[rowIndex*t.numColumns:rowIndex*t.numColumns+t.numColumns])
items[rowIndex*t.numColumns+t.numColumns+rowIndex] = column[rowIndex]
}
t.items = items
t.numColumns += 1
}
func (t *Table) At(rowIndex, columnIndex int) *Instance {
return t.items[rowIndex*t.numColumns+columnIndex]
}
func (t *Table) LabelFromColumnIndex(columnIndex int) *Feature {
return &t.features[columnIndex]
}
func (t *Table) ColumnIndexFromLabel(typeKey TypeKey) int {
return t.featureMap[typeKey]
}
func (t *Table) NumColumns() int {
return t.numColumns
}
func (t *Table) NumRows() int {
return t.numRows
}