-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuffer.go
159 lines (141 loc) · 3.74 KB
/
buffer.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
package main
import (
"errors"
"sort"
)
//Buffer struct
type Buffer struct {
sep rune
cont [][]string
colType []int //const colType_str = 0, const colType_int = 0
rowLen int
colLen int
rowFreeze int // true:1, false:0
colFreeze int // true:1, false:0
selectedCell [][]int
}
func createNewBuffer() *Buffer {
return &Buffer{sep: 0, cont: [][]string{}, colType: []int{}, rowLen: 0, colLen: 0, rowFreeze: 1, colFreeze: 1, selectedCell: [][]int{}}
}
func createNewBufferWithData(ss [][]string, strict bool) (*Buffer, error) {
b = createNewBuffer()
for _, s := range ss {
err := b.contAppendSli(s, strict)
if err != nil {
return nil, err
}
}
return b, nil
}
//add []string to buffer
func (b *Buffer) contAppendSli(s []string, strict bool) error {
if b.rowLen == 0 {
b.colLen = len(s)
b.colType = make([]int, b.colLen+1)
}
if strict && len(s) != b.colLen {
return errors.New("Row " + I2S(b.rowLen+b.rowFreeze) + " lack some column")
}
b.cont = append(b.cont, s)
if b.colLen != len(s) {
b.resizeCol(len(s))
}
b.rowLen++
return nil
}
//add empty columns to buffer
func (b *Buffer) resizeCol(n int) {
if n <= 0 {
return
}
lackLen := b.colLen - n
if lackLen < 0 {
lackLen = n - b.colLen
b.colLen = n
}
for ii := range b.cont {
addedValue := "NaN"
for m := 0; m < lackLen; m++ {
b.cont[ii] = append(b.cont[ii], addedValue)
}
}
}
// sort column by string format
func (b *Buffer) sortByStr(colIndex int, rev bool) {
if rev {
if I2B(b.rowFreeze) {
sort.SliceStable(b.cont[1:], func(i, j int) bool { return b.cont[1:][i][colIndex] > b.cont[1:][j][colIndex] })
} else {
sort.SliceStable(b.cont, func(i, j int) bool { return b.cont[i][colIndex] > b.cont[j][colIndex] })
}
} else {
if I2B(b.rowFreeze) {
sort.SliceStable(b.cont[1:], func(i, j int) bool { return b.cont[1:][i][colIndex] < b.cont[1:][j][colIndex] })
} else {
sort.SliceStable(b.cont, func(i, j int) bool { return b.cont[i][colIndex] < b.cont[j][colIndex] })
}
}
}
// sort column by number format
func (b *Buffer) sortByNum(colIndex int, rev bool) {
if rev {
if I2B(b.rowFreeze) {
sort.SliceStable(b.cont[1:], func(i, j int) bool { return S2F(b.cont[1:][i][colIndex]) > S2F(b.cont[1:][j][colIndex]) })
} else {
sort.SliceStable(b.cont, func(i, j int) bool { return S2F(b.cont[i][colIndex]) > S2F(b.cont[j][colIndex]) })
}
} else {
if I2B(b.rowFreeze) {
sort.SliceStable(b.cont[1:], func(i, j int) bool { return S2F(b.cont[1:][i][colIndex]) < S2F(b.cont[1:][j][colIndex]) })
} else {
sort.SliceStable(b.cont, func(i, j int) bool { return S2F(b.cont[i][colIndex]) < S2F(b.cont[j][colIndex]) })
}
}
}
// transpose buffer content
func (b *Buffer) transpose() {
b.rowLen, b.colLen = b.colLen, b.rowLen
b.colType = make([]int, b.colLen+1)
xl := len(b.cont[0])
yl := len(b.cont)
result := make([][]string, xl)
for i := range result {
result[i] = make([]string, yl)
}
for i := 0; i < xl; i++ {
for j := 0; j < yl; j++ {
result[i][j] = b.cont[j][i]
}
}
b.cont = result
}
//get ith column []string data
func (b Buffer) getCol(i int) []string {
result := make([]string, b.rowLen)
for rowI := 0; rowI < b.rowLen; rowI++ {
result[rowI] = b.cont[rowI][i]
}
return result
}
//set ith column data type
func (b *Buffer) setColType(i int, t int) {
b.colType[i] = t
}
//get ith column data type
func (b *Buffer) getColType(i int) int {
return b.colType[i]
}
//clear selectedCell of buffer
//func (b *Buffer) clearSelection() {
// b.selectedCell = [][]int{}
//}
//search string and add result to selectedCell of buffer
func (b *Buffer) selectBySearch(s string) {
for ii, i := range b.cont {
for ji, j := range i {
if s == j {
b.selectedCell = append(b.selectedCell, []int{ii, ji})
}
}
}
}