-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolumn.go
152 lines (131 loc) · 2.64 KB
/
column.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
package catle
import (
"strconv"
"sort"
)
type ColumnInfo interface {
SetHeader(string)
GetHeader() string
SetWidth(int)
GetWidth() int
SetStatus(status)
GetStatus() status
}
type Column interface {
ColumnInfo
Append(string) error
String(int) string
SortedOrder(bool) []int
Size() int
}
type DefaultColumnInfo struct {
Header string
Width int
Status status
}
func (ci *DefaultColumnInfo) SetHeader(h string) {
ci.Header = h
}
func (ci *DefaultColumnInfo) GetHeader() string {
return ci.Header
}
func (ci *DefaultColumnInfo) SetWidth(w int) {
ci.Width = w
}
func (ci *DefaultColumnInfo) GetWidth() int {
return ci.Width
}
func (ci *DefaultColumnInfo) SetStatus(s status) {
ci.Status = s
}
func (ci *DefaultColumnInfo) GetStatus() status {
return ci.Status
}
type ColumnStr struct {
DefaultColumnInfo
Values []string
}
func (cs *ColumnStr) String(i int) string {
return cs.Values[i]
}
func (cs *ColumnStr) Append(val string) error {
cs.Values = append(cs.Values, val)
return nil
}
func (cs *ColumnStr) Size() int {
return len(cs.Values)
}
func (cs *ColumnStr) SortedOrder(ascending bool) []int{
ix := make([]int, len(cs.Values))
for i := range ix {
ix[i] = i
}
if ascending {
sort.Slice(ix, func(i, j int) bool {
return cs.Values[ix[i]] < cs.Values[ix[j]]
})
}else{
sort.Slice(ix, func(i, j int) bool {
return cs.Values[ix[i]] > cs.Values[ix[j]]
})
}
return ix
}
type ColumnInt struct {
DefaultColumnInfo
Values []int
}
func (ci *ColumnInt) String(i int) string {
return strconv.Itoa(ci.Values[i])
}
func (ci *ColumnInt) Append(val string) error {
num, err := strconv.Atoi(val)
if err != nil {
return err
}
ci.Values = append(ci.Values, num)
return nil
}
func (ci *ColumnInt) Size() int {
return len(ci.Values)
}
func (ci *ColumnInt) SortedOrder(ascending bool) []int {
ix := make([]int, len(ci.Values))
for i := range ix {
ix[i] = i
}
if ascending {
sort.Slice(ix, func(i, j int) bool {
return ci.Values[ix[i]] < ci.Values[ix[j]]
})
}else{
sort.Slice(ix, func(i, j int) bool {
return ci.Values[ix[i]] > ci.Values[ix[j]]
})
}
return ix
}
func NewColumnStr(c Column) (*ColumnStr, error) {
ci := &ColumnStr{}
ci.SetHeader(c.GetHeader())
ci.SetStatus(c.GetStatus())
ci.SetWidth(c.GetWidth())
for i := 0; i < c.Size(); i++ {
if err := ci.Append(c.String(i)); err != nil {
return nil, err
}
}
return ci, nil
}
func NewColumnInt(c Column) (*ColumnInt, error) {
ci := &ColumnInt{}
ci.SetHeader(c.GetHeader())
ci.SetStatus(c.GetStatus())
ci.SetWidth(c.GetWidth())
for i := 0; i < c.Size(); i++ {
if err := ci.Append(c.String(i)); err != nil {
return nil, err
}
}
return ci, nil
}