-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdatatable_filters.go
361 lines (290 loc) · 10.2 KB
/
datatable_filters.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package insyra
import (
"strings"
"time"
)
// ==================== Col Index ====================
// FilterByColIndexGreaterThan filters columns with index greater than the specified column.
func (dt *DataTable) FilterByColIndexGreaterThan(columnIndexLetter string) *DataTable {
dt.mu.Lock()
defer dt.mu.Unlock()
columnIndexLetter = strings.ToUpper(columnIndexLetter)
colIdx, exists := dt.columnIndex[columnIndexLetter]
if !exists {
return &DataTable{}
}
filteredCols := dt.columns[colIdx+1:]
newDt := &DataTable{
columns: filteredCols,
columnIndex: dt.columnIndex,
rowNames: dt.rowNames,
creationTimestamp: dt.creationTimestamp,
}
newDt.lastModifiedTimestamp.Store(dt.lastModifiedTimestamp.Load())
return newDt
}
// FilterByColIndexGreaterThanOrEqualTo filters columns with index greater than or equal to the specified column.
func (dt *DataTable) FilterByColIndexGreaterThanOrEqualTo(columnIndexLetter string) *DataTable {
dt.mu.Lock()
defer dt.mu.Unlock()
columnIndexLetter = strings.ToUpper(columnIndexLetter)
colIdx, exists := dt.columnIndex[columnIndexLetter]
if !exists {
return &DataTable{}
}
filteredCols := dt.columns[colIdx:]
newDt := &DataTable{
columns: filteredCols,
columnIndex: dt.columnIndex,
rowNames: dt.rowNames,
creationTimestamp: dt.creationTimestamp,
}
newDt.lastModifiedTimestamp.Store(dt.lastModifiedTimestamp.Load())
return newDt
}
// FilterByColIndexEqualTo filters to only keep the column with the specified index.
func (dt *DataTable) FilterByColIndexEqualTo(columnIndexLetter string) *DataTable {
dt.mu.Lock()
defer dt.mu.Unlock()
columnIndexLetter = strings.ToUpper(columnIndexLetter)
colIdx, exists := dt.columnIndex[columnIndexLetter]
if !exists {
return &DataTable{}
}
filteredCols := []*DataList{dt.columns[colIdx]}
newDt := &DataTable{
columns: filteredCols,
columnIndex: dt.columnIndex,
rowNames: dt.rowNames,
creationTimestamp: dt.creationTimestamp,
}
newDt.lastModifiedTimestamp.Store(dt.lastModifiedTimestamp.Load())
return newDt
}
// FilterByColIndexLessThan filters columns with index less than the specified column.
func (dt *DataTable) FilterByColIndexLessThan(columnIndexLetter string) *DataTable {
dt.mu.Lock()
defer dt.mu.Unlock()
columnIndexLetter = strings.ToUpper(columnIndexLetter)
colIdx, exists := dt.columnIndex[columnIndexLetter]
if !exists {
return &DataTable{}
}
filteredCols := dt.columns[:colIdx]
newDt := &DataTable{
columns: filteredCols,
columnIndex: dt.columnIndex,
rowNames: dt.rowNames,
creationTimestamp: dt.creationTimestamp,
}
newDt.lastModifiedTimestamp.Store(dt.lastModifiedTimestamp.Load())
return newDt
}
// FilterByColIndexLessThanOrEqualTo filters columns with index less than or equal to the specified column.
func (dt *DataTable) FilterByColIndexLessThanOrEqualTo(columnIndexLetter string) *DataTable {
dt.mu.Lock()
defer dt.mu.Unlock()
columnIndexLetter = strings.ToUpper(columnIndexLetter)
colIdx, exists := dt.columnIndex[columnIndexLetter]
if !exists {
return &DataTable{}
}
filteredCols := dt.columns[:colIdx+1]
newDt := &DataTable{
columns: filteredCols,
columnIndex: dt.columnIndex,
rowNames: dt.rowNames,
creationTimestamp: dt.creationTimestamp,
}
newDt.lastModifiedTimestamp.Store(dt.lastModifiedTimestamp.Load())
return newDt
}
// ==================== Col Name ====================
// FilterByColNameEqualTo filters to only keep the column with the specified name.
func (dt *DataTable) FilterByColNameEqualTo(columnName string) *DataTable {
dt.mu.Lock()
defer dt.mu.Unlock()
colIdx := -1
for i, col := range dt.columns {
if col.name == columnName {
colIdx = i
break
}
}
if colIdx == -1 {
return &DataTable{}
}
filteredCols := []*DataList{dt.columns[colIdx]}
newDt := &DataTable{
columns: filteredCols,
columnIndex: dt.columnIndex,
rowNames: dt.rowNames,
creationTimestamp: dt.creationTimestamp,
}
newDt.lastModifiedTimestamp.Store(dt.lastModifiedTimestamp.Load())
return newDt
}
// FilterByColNameContains filters columns whose name contains the specified substring.
func (dt *DataTable) FilterByColNameContains(substring string) *DataTable {
dt.mu.Lock()
defer dt.mu.Unlock()
var filteredCols []*DataList
for _, col := range dt.columns {
if strings.Contains(col.name, substring) {
filteredCols = append(filteredCols, col)
}
}
newDt := &DataTable{
columns: filteredCols,
columnIndex: dt.columnIndex,
rowNames: dt.rowNames,
creationTimestamp: dt.creationTimestamp,
}
newDt.lastModifiedTimestamp.Store(dt.lastModifiedTimestamp.Load())
return newDt
}
// ==================== Row Index ====================
// FilterByRowIndexGreaterThan filters rows with index greater than the specified threshold.
func (dt *DataTable) FilterByRowIndexGreaterThan(threshold int) *DataTable {
return dt.Filter(func(rowIndex int, columnIndex string, value interface{}) bool {
return rowIndex > threshold
})
}
// FilterByRowIndexGreaterThanOrEqualTo filters rows with index greater than or equal to the specified threshold.
func (dt *DataTable) FilterByRowIndexGreaterThanOrEqualTo(threshold int) *DataTable {
return dt.Filter(func(rowIndex int, columnIndex string, value interface{}) bool {
return rowIndex >= threshold
})
}
// FilterByRowIndexEqualTo filters to only keep the row with the specified index.
func (dt *DataTable) FilterByRowIndexEqualTo(index int) *DataTable {
return dt.Filter(func(rowIndex int, columnIndex string, value interface{}) bool {
return rowIndex == index
})
}
// FilterByRowIndexLessThan filters rows with index less than the specified threshold.
func (dt *DataTable) FilterByRowIndexLessThan(threshold int) *DataTable {
return dt.Filter(func(rowIndex int, columnIndex string, value interface{}) bool {
return rowIndex < threshold
})
}
// FilterByRowIndexLessThanOrEqualTo filters rows with index less than or equal to the specified threshold.
func (dt *DataTable) FilterByRowIndexLessThanOrEqualTo(threshold int) *DataTable {
return dt.Filter(func(rowIndex int, columnIndex string, value interface{}) bool {
return rowIndex <= threshold
})
}
// ==================== Row Name ====================
// FilterByRowNameEqualTo filters to only keep the row with the specified name.
func (dt *DataTable) FilterByRowNameEqualTo(rowName string) *DataTable {
newdt := dt.FilterByRowNameContains(rowName)
for name, index := range newdt.rowNames {
if name == rowName {
return newdt.FilterByRowIndexEqualTo(index)
}
}
return &DataTable{}
}
// FilterByRowNameContains filters rows whose name contains the specified substring.
func (dt *DataTable) FilterByRowNameContains(substring string) *DataTable {
dt.mu.Lock()
defer dt.mu.Unlock()
// 找出符合條件的行索引
var filteredRowIndices []int
for name, rowIndex := range dt.rowNames {
if strings.Contains(name, substring) {
filteredRowIndices = append(filteredRowIndices, rowIndex)
}
}
// 如果沒有符合條件的行,返回空的 DataTable
if len(filteredRowIndices) == 0 {
return &DataTable{}
}
// 構建新的 DataTable,只包含符合條件的行
filteredCols := make([]*DataList, len(dt.columns))
for i := range dt.columns {
filteredCols[i] = &DataList{
data: make([]interface{}, 0, len(filteredRowIndices)),
name: dt.columns[i].name,
creationTimestamp: dt.columns[i].creationTimestamp,
}
filteredCols[i].lastModifiedTimestamp.Store(
dt.columns[i].lastModifiedTimestamp.Load())
for _, rowIndex := range filteredRowIndices {
if rowIndex < len(dt.columns[i].data) {
filteredCols[i].data = append(filteredCols[i].data, dt.columns[i].data[rowIndex])
}
}
}
newDt := &DataTable{
columns: filteredCols,
columnIndex: dt.columnIndex,
rowNames: filterRowNames(dt.rowNames, filteredRowIndices),
creationTimestamp: dt.creationTimestamp,
}
newDt.lastModifiedTimestamp.Store(time.Now().Unix())
return newDt
}
// filterRowNames creates a new map of row names with updated indices after filtering.
func filterRowNames(originalRowNames map[string]int, filteredIndices []int) map[string]int {
newRowNames := make(map[string]int)
for name, originalIndex := range originalRowNames {
for newIndex, filteredIndex := range filteredIndices {
if originalIndex == filteredIndex {
newRowNames[name] = newIndex
}
}
}
return newRowNames
}
// ==================== Custom Element ====================
// FilterByCustomElement filters the table based on a custom function applied to each element.
func (dt *DataTable) FilterByCustomElement(filterFunc func(value interface{}) bool) *DataTable {
return dt.Filter(func(rowIndex int, columnIndex string, value interface{}) bool {
return filterFunc(value)
})
}
// ==================== Custom Filter ====================
// FilterFunc is a custom filter function that takes the row index, column name, and value as input and returns a boolean.
type FilterFunc func(rowIndex int, columnIndex string, value interface{}) bool
// Filter applies a custom filter function to the DataTable and returns the filtered DataTable.
func (dt *DataTable) Filter(filterFunc FilterFunc) *DataTable {
dt.mu.Lock()
defer dt.mu.Unlock()
filteredCols := make([]*DataList, len(dt.columns))
for i := range dt.columns {
filteredCols[i] = &DataList{}
}
for rowIdx := range dt.columns[0].data {
keepRow := false
for colIdx, col := range dt.columns {
value := col.data[rowIdx]
colName := ""
for name, idx := range dt.columnIndex {
if idx == colIdx {
colName = name
break
}
}
if filterFunc(rowIdx, colName, value) {
keepRow = true
filteredCols[colIdx].data = append(filteredCols[colIdx].data, value)
} else {
filteredCols[colIdx].data = append(filteredCols[colIdx].data, nil)
}
}
if !keepRow {
for _, col := range filteredCols {
col.data = col.data[:len(col.data)-1]
}
}
}
newDt := &DataTable{
columns: filteredCols,
columnIndex: dt.columnIndex,
rowNames: dt.rowNames,
creationTimestamp: dt.creationTimestamp,
}
newDt.lastModifiedTimestamp.Store(dt.lastModifiedTimestamp.Load())
return newDt
}