-
Notifications
You must be signed in to change notification settings - Fork 0
/
row.go
208 lines (172 loc) · 4.08 KB
/
row.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package render
import (
"reflect"
"sort"
"strconv"
"strings"
"time"
"github.com/fvbommel/sortorder"
)
// Fields represents a collection of row fields.
type Fields []string
// Customize returns a subset of fields.
func (f Fields) Customize(cols []int, out Fields) {
for i, c := range cols {
if c < 0 {
out[i] = NAValue
continue
}
if c < len(f) {
out[i] = f[c]
}
}
}
// Diff returns true if fields differ or false otherwise.
func (f Fields) Diff(ff Fields, ageCol int) bool {
if ageCol < 0 {
return !reflect.DeepEqual(f[:len(f)-1], ff[:len(ff)-1])
}
if !reflect.DeepEqual(f[:ageCol], ff[:ageCol]) {
return true
}
return !reflect.DeepEqual(f[ageCol+1:], ff[ageCol+1:])
}
// Clone returns a copy of the fields.
func (f Fields) Clone() Fields {
cp := make(Fields, len(f))
copy(cp, f)
return cp
}
// ----------------------------------------------------------------------------
// Row represents a collection of columns.
type Row struct {
ID string
Fields Fields
}
// NewRow returns a new row with initialized fields.
func NewRow(size int) Row {
return Row{Fields: make([]string, size)}
}
// Labelize returns a new row based on labels.
func (r Row) Labelize(cols []int, labelCol int, labels []string) Row {
out := NewRow(len(cols) + len(labels))
for _, col := range cols {
out.Fields = append(out.Fields, r.Fields[col])
}
m := labelize(r.Fields[labelCol])
for _, label := range labels {
out.Fields = append(out.Fields, m[label])
}
return out
}
// Customize returns a row subset based on given col indices.
func (r Row) Customize(cols []int) Row {
out := NewRow(len(cols))
r.Fields.Customize(cols, out.Fields)
out.ID = r.ID
return out
}
// Diff returns true if row differ or false otherwise.
func (r Row) Diff(ro Row, ageCol int) bool {
if r.ID != ro.ID {
return true
}
return r.Fields.Diff(ro.Fields, ageCol)
}
// Clone copies a row.
func (r Row) Clone() Row {
return Row{
ID: r.ID,
Fields: r.Fields.Clone(),
}
}
// Len returns the length of the row.
func (r Row) Len() int {
return len(r.Fields)
}
// ----------------------------------------------------------------------------
// Rows represents a collection of rows.
type Rows []Row
// Delete removes an element by id.
func (rr Rows) Delete(id string) Rows {
idx, ok := rr.Find(id)
if !ok {
return rr
}
if idx == 0 {
return rr[1:]
}
if idx+1 == len(rr) {
return rr[:len(rr)-1]
}
return append(rr[:idx], rr[idx+1:]...)
}
// Upsert adds a new item.
func (rr Rows) Upsert(r Row) Rows {
idx, ok := rr.Find(r.ID)
if !ok {
return append(rr, r)
}
rr[idx] = r
return rr
}
// Find locates a row by id. Returns false is not found.
func (rr Rows) Find(id string) (int, bool) {
for i, r := range rr {
if r.ID == id {
return i, true
}
}
return 0, false
}
// Sort rows based on column index and order.
func (rr Rows) Sort(col int, asc, isNum, isDur bool) {
t := RowSorter{
Rows: rr,
Index: col,
IsNumber: isNum,
IsDuration: isDur,
Asc: asc,
}
sort.Sort(t)
}
// ----------------------------------------------------------------------------
// RowSorter sorts rows.
type RowSorter struct {
Rows Rows
Index int
IsNumber, IsDuration bool
Asc bool
}
func (s RowSorter) Len() int {
return len(s.Rows)
}
func (s RowSorter) Swap(i, j int) {
s.Rows[i], s.Rows[j] = s.Rows[j], s.Rows[i]
}
func (s RowSorter) Less(i, j int) bool {
return Less(s.Asc, s.IsNumber, s.IsDuration, s.Rows[i].Fields[s.Index], s.Rows[j].Fields[s.Index])
}
// ----------------------------------------------------------------------------
// Helpers...
func toAgeDuration(dur string) string {
d, err := time.ParseDuration(dur)
if err != nil {
return durationToSeconds(dur)
}
return strconv.Itoa(int(d.Seconds()))
}
// Less return true if c1 < c2.
func Less(asc, isNumber, isDuration bool, c1, c2 string) bool {
if isNumber {
c1, c2 = strings.Replace(c1, ",", "", -1), strings.Replace(c2, ",", "", -1)
}
if isDuration {
c1, c2 = toAgeDuration(c1), toAgeDuration(c2)
}
b := sortorder.NaturalLess(c1, c2)
if asc {
return b
}
return !b
}