-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.go
234 lines (213 loc) · 6.77 KB
/
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
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
package timetable
import (
"slices"
"sort"
"time"
)
type Compact[Value any] struct {
times []time.Time
values [][]Value
}
func New[Value any](columns ...List[Value]) *Compact[Value] {
table := new(Compact[Value])
for _, column := range columns {
table = table.AddColumnFillMissingWithZero(column)
}
return table
}
type sorter struct {
less func(a, b int) bool
swap func(a, b int)
len int
}
func (s sorter) Len() int { return s.len }
func (s sorter) Less(i, j int) bool { return s.less(i, j) }
func (s sorter) Swap(i, j int) { s.swap(i, j) }
func (table *Compact[Value]) sort() {
sort.Sort(sorter{
less: func(a, b int) bool { return table.times[a].Before(table.times[b]) },
swap: func(a, b int) {
table.times[a], table.times[b] = table.times[b], table.times[a]
for i := range table.values {
table.values[i][a], table.values[i][b] = table.values[i][b], table.values[i][a]
}
},
len: len(table.times),
})
}
func (table *Compact[Value]) Times() []time.Time { return slices.Clone(table.times) }
func (table *Compact[Value]) Values() [][]Value {
result := make([][]Value, len(table.values))
for i, values := range table.values {
result[i] = slices.Clone(values)
}
return result
}
func (table *Compact[Value]) UnderlyingValues() [][]Value { return table.values }
func (table *Compact[Value]) UnderlyingTimes() []time.Time { return table.times }
func (table *Compact[Value]) NumberOfColumns() int { return len(table.values) }
func (table *Compact[Value]) NumberOfRows() int { return len(table.times) }
func (table *Compact[Value]) NumberOfCells() int { return len(table.values) * len(table.times) }
func (table *Compact[Value]) FirstTime() time.Time { return table.times[0] }
func (table *Compact[Value]) LastTime() time.Time { return table.times[len(table.times)-1] }
func (table *Compact[Value]) isEmpty() bool { return table == nil || table.times == nil }
func (table *Compact[Value]) Column(column int) (List[Value], bool) {
if column < 0 || column >= len(table.values) {
var zero List[Value]
return zero[:], false
}
list := make(List[Value], len(table.times))
for row := range table.times {
list[row].time = table.times[row]
list[row].value = table.values[column][row]
}
return list, true
}
func (table *Compact[Value]) Row(t time.Time) ([]Value, bool) {
index, found := slices.BinarySearchFunc(table.times, t, time.Time.Compare)
if !found {
var zero [0]Value
return zero[:], false
}
list := make([]Value, len(table.values))
for column := range table.values {
list[column] = table.values[column][index]
}
return list, true
}
func (table *Compact[Value]) AddColumnFillMissingWithZero(list List[Value]) *Compact[Value] {
return table.AddColumn(list, zeroValue[Value])
}
func zeroValue[Value any](time.Time, int) Value {
var zero Value
return zero
}
func zeroTable[Value any](n int) *Compact[Value] {
values := make([][]Value, n)
var zeroValues [0]Value
for i := range values {
values[i] = zeroValues[:0:0]
}
var zeroTimes [0]time.Time
return &Compact[Value]{
times: zeroTimes[:0:0],
values: values,
}
}
func (table *Compact[Value]) AddColumn(list List[Value], missing func(time.Time, int) Value) *Compact[Value] {
if table.isEmpty() {
return addInitialColumn(list)
}
if table.NumberOfRows() == 0 {
return zeroTable[Value](len(table.values) + 1)
}
list = slices.Clone(list)
slices.SortFunc(list, Cell[Value].compareTimes)
t0, t1 := table.FirstTime(), table.LastTime()
list = list.Between(t0, t1)
if len(list) == 0 {
return zeroTable[Value](len(table.values) + 1)
}
t0, t1 = list.FirstTime(), list.LastTime()
updated := table.Between(t0, t1)
if updated.NumberOfRows() == 0 {
return zeroTable[Value](len(table.values) + 1)
}
return updated.addAdditionalColumn(list, missing)
}
func addInitialColumn[Value any](list List[Value]) *Compact[Value] {
table := new(Compact[Value])
slices.SortFunc(list, Cell[Value].compareTimes)
newValues := make([]Value, 0, max(len(list), len(table.times)))
table.times = make([]time.Time, 0, len(list))
for _, element := range list {
table.times = append(table.times, element.time)
newValues = append(newValues, element.value)
}
table.values = append(table.values, newValues)
return table
}
func (table *Compact[Value]) addAdditionalColumn(list List[Value], missing func(time.Time, int) Value) *Compact[Value] {
var missingTimes []time.Time
for _, cell := range list {
_, found := slices.BinarySearchFunc(table.times, cell.time, time.Time.Compare)
if !found {
missingTimes = append(missingTimes, cell.time)
}
}
times := slices.Grow(missingTimes, len(table.times)+len(missingTimes))
for _, t := range table.times {
times = append(times, t)
}
slices.SortFunc(times, time.Time.Compare)
times = slices.Compact(times)
values := make([][]Value, len(table.values)+1)
for column := range table.values {
values[column] = slices.Grow(table.values[column], len(times))
values[column] = values[column][:0]
for _, t := range times {
index, found := slices.BinarySearchFunc(table.times, t, time.Time.Compare)
var value Value
if found {
value = table.values[column][index]
} else {
value = missing(t, column)
}
values[column] = append(values[column], value)
}
}
for _, t := range times {
index, found := slices.BinarySearchFunc(list, Cell[Value]{time: t}, Cell[Value].compareTimes)
var value Value
if found {
value = list[index].value
} else {
value = missing(t, len(table.values))
}
values[len(values)-1] = append(values[len(values)-1], value)
}
times = slices.Clip(times)
for i := range values {
values[i] = slices.Clip(values[i])
}
return &Compact[Value]{times: times, values: values}
}
func (table *Compact[Value]) Between(t0, t1 time.Time) *Compact[Value] {
if table.isEmpty() || len(table.times) == 0 {
return &Compact[Value]{
times: nil,
values: make([][]Value, len(table.values)),
}
}
if t1.Before(t0) {
t0, t1 = t1, t0
}
var firstIndex, lastIndex int
list := table.times
if last := list[len(list)-1]; t0.After(last) {
firstIndex = len(list)
} else if first := list[0]; t0.Before(first) {
lastIndex = 0
} else if i, ok := slices.BinarySearchFunc(list, t0, time.Time.Compare); ok {
firstIndex = i
} else if i >= 0 && i < len(list) {
firstIndex = i
}
if last := list[len(list)-1]; t1.After(last) {
lastIndex = len(list)
} else if first := list[0]; t1.Before(first) {
lastIndex = 0
} else if i, ok := slices.BinarySearchFunc(list, t1, time.Time.Compare); ok {
lastIndex = i + 1
} else if i >= 0 && i < len(list) {
lastIndex = i
}
values := make([][]Value, len(table.values))
for i := range table.values {
values[i] = table.values[i][firstIndex:lastIndex:lastIndex]
}
return &Compact[Value]{
times: table.times[firstIndex:lastIndex:lastIndex],
values: values,
}
}