-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_int_seq.go
481 lines (426 loc) · 9.41 KB
/
gen_int_seq.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
// Code generated by cmd/generate. DO NOT EDIT.
// Code generated by cmd/generate. DO NOT EDIT.
// Code generated by cmd/generate. DO NOT EDIT.
package timeseq
import (
"sort"
"sync"
"time"
)
// Int is a time point with int value inside
type Int struct {
Time time.Time
Value int
}
// IsZero returns if time and value are both zero
func (v Int) IsZero() bool {
return v.Value == 0 && v.Time.IsZero()
}
// Equal returns if time and value are both equal
func (v Int) Equal(n Int) bool {
return v.Value == n.Value && v.Time.Equal(n.Time)
}
// Ints is a alias of Int slice
type Ints []Int
// Len implements Interface.Len()
func (s Ints) Len() int {
return len(s)
}
// Swap implements Interface.Swap()
func (s Ints) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// Time implements Interface.Time()
func (s Ints) Time(i int) time.Time {
return s[i].Time
}
// Slice implements Interface.Slice()
func (s Ints) Slice(i, j int) Interface {
return s[i:j]
}
// IntSeq is a wrapper with useful methods of Int slice
type IntSeq struct {
slice Ints
indexOnce sync.Once
timeIndex map[timeKey][]int
valueIndex map[int][]int
valueOrder []int
}
// NewIntSeq returns *IntSeq with copied slice inside
func NewIntSeq(slice Ints) *IntSeq {
temp := make(Ints, len(slice))
copy(temp, slice)
return WrapIntSeq(temp)
}
// WrapIntSeq returns *IntSeq with origin slice inside
func WrapIntSeq(slice Ints) *IntSeq {
if !IsSorted(slice) {
Sort(slice)
}
return newIntSeq(slice)
}
func newIntSeq(slice Ints) *IntSeq {
ret := &IntSeq{
slice: slice,
}
return ret
}
func (s *IntSeq) getSlice() Ints {
if s == nil {
return nil
}
return s.slice
}
func (s *IntSeq) getTimeIndex() map[timeKey][]int {
if s == nil {
return nil
}
return s.timeIndex
}
func (s *IntSeq) getValueIndex() map[int][]int {
if s == nil {
return nil
}
return s.valueIndex
}
func (s *IntSeq) buildIndex() {
if s == nil {
return
}
s.indexOnce.Do(func() {
timeIndex := make(map[timeKey][]int, len(s.slice))
valueIndex := make(map[int][]int, len(s.slice))
valueSlice := s.valueOrder[:0]
for i, v := range s.slice {
k := newTimeKey(v.Time)
timeIndex[k] = append(timeIndex[k], i)
valueIndex[v.Value] = append(valueIndex[v.Value], i)
valueSlice = append(valueSlice, i)
}
sort.SliceStable(valueSlice, func(i, j int) bool {
return s.slice[valueSlice[i]].Value < s.slice[valueSlice[j]].Value
})
s.timeIndex = timeIndex
s.valueIndex = valueIndex
s.valueOrder = valueSlice
})
}
// Ints returns a replica of inside slice
func (s *IntSeq) Ints() Ints {
sslice := s.getSlice()
slice := make(Ints, len(sslice))
copy(slice, sslice)
return slice
}
// Len returns length of inside slice
func (s *IntSeq) Len() int {
sslice := s.getSlice()
return len(sslice)
}
// Index returns element of inside slice, returns zero if index is out of range
func (s *IntSeq) Index(i int) Int {
sslice := s.getSlice()
if i < 0 || i >= len(sslice) {
return Int{}
}
return sslice[i]
}
// Time returns the first element with time t, returns zero if not found
func (s *IntSeq) Time(t time.Time) Int {
got := s.MTime(t)
if len(got) == 0 {
return Int{}
}
return got[0]
}
// MTime returns all elements with time t, returns nil if not found
func (s *IntSeq) MTime(t time.Time) Ints {
s.buildIndex()
sslice := s.getSlice()
index := s.getTimeIndex()[newTimeKey(t)]
if len(index) == 0 {
return nil
}
ret := make(Ints, len(index))
for i, v := range index {
ret[i] = sslice[v]
}
return ret
}
// Value returns the first element with value v, returns zero if not found
func (s *IntSeq) Value(v int) Int {
got := s.MValue(v)
if len(got) == 0 {
return Int{}
}
return got[0]
}
// MValue returns all elements with value v, returns nil if not found
func (s *IntSeq) MValue(v int) Ints {
s.buildIndex()
sslice := s.getSlice()
index := s.getValueIndex()[v]
if len(index) == 0 {
return nil
}
ret := make(Ints, len(index))
for i, v := range index {
ret[i] = sslice[v]
}
return ret
}
// Traverse call fn for every element one by one, break if fn returns true
func (s *IntSeq) Traverse(fn func(i int, v Int) (stop bool)) {
sslice := s.getSlice()
for i, v := range sslice {
if fn != nil && fn(i, v) {
break
}
}
}
// Sum returns sum of all values
func (s *IntSeq) Sum() int {
var ret int
sslice := s.getSlice()
for _, v := range sslice {
ret += v.Value
}
return ret
}
// Max returns the element with max value, returns zero if empty
func (s *IntSeq) Max() Int {
var max Int
found := false
sslice := s.getSlice()
for _, v := range sslice {
if !found {
max = v
found = true
} else if v.Value > max.Value {
max = v
}
}
return max
}
// Min returns the element with min value, returns zero if empty
func (s *IntSeq) Min() Int {
var min Int
found := false
sslice := s.getSlice()
for _, v := range sslice {
if !found {
min = v
found = true
} else if v.Value < min.Value {
min = v
}
}
return min
}
// First returns the first element, returns zero if empty
func (s *IntSeq) First() Int {
sslice := s.getSlice()
if len(sslice) == 0 {
return Int{}
}
return sslice[0]
}
// Last returns the last element, returns zero if empty
func (s *IntSeq) Last() Int {
sslice := s.getSlice()
if len(sslice) == 0 {
return Int{}
}
return sslice[len(sslice)-1]
}
// Percentile returns the element matched with percentile pct, returns zero if empty,
// the pct's valid range is be [0, 1], it will be treated as 1 if greater than 1, as 0 if smaller than 0
func (s *IntSeq) Percentile(pct float64) Int {
s.buildIndex()
sslice := s.getSlice()
if len(sslice) == 0 {
return Int{}
}
if pct > 1 {
pct = 1
}
if pct < 0 {
pct = 0
}
i := int(float64(len(sslice))*pct - 1)
if i < 0 {
i = 0
}
return sslice[s.valueOrder[i]]
}
// Range returns a sub *IntSeq with specified interval
func (s *IntSeq) Range(interval Interval) *IntSeq {
sslice := s.getSlice()
slice := Range(sslice, interval).(Ints)
return newIntSeq(slice)
}
// Slice returns a sub *IntSeq with specified index,
// (1, 2) means [1:2], (-1, 2) means [:2], (-1, -1) means [:]
func (s *IntSeq) Slice(i, j int) *IntSeq {
if i < 0 && j < 0 {
return s
}
sslice := s.getSlice()
if i < 0 {
return newIntSeq(sslice[:j])
}
if j < 0 {
return newIntSeq(sslice[i:])
}
return newIntSeq(sslice[i:j])
}
// Trim returns a *IntSeq without elements which make fn returns true
func (s *IntSeq) Trim(fn func(i int, v Int) bool) *IntSeq {
sslice := s.getSlice()
if fn == nil || len(sslice) == 0 {
return s
}
removeM := map[int]struct{}{}
for i, v := range sslice {
if fn(i, v) {
removeM[i] = struct{}{}
}
}
if len(removeM) == 0 {
return s
}
slice := make(Ints, 0, len(sslice)-len(removeM))
for i, v := range sslice {
if _, ok := removeM[i]; ok {
continue
}
slice = append(slice, v)
}
return newIntSeq(slice)
}
// Merge returns a new *IntSeq with merged data according to the specified rule
func (s *IntSeq) Merge(fn func(t time.Time, v1, v2 *int) *int, seq *IntSeq) *IntSeq {
if fn == nil {
return s
}
var ret Ints
slice1 := s.getSlice()
var slice2 Ints
if seq != nil {
slice2 = seq.getSlice()
}
for i1, i2 := 0, 0; i1 < len(slice1) || i2 < len(slice2); {
var (
t time.Time
v *int
)
switch {
case i1 == len(slice1):
t = slice2[i2].Time
v2 := slice2[i2].Value
v = fn(t, nil, &v2)
i2++
case i2 == len(slice2):
t = slice1[i1].Time
v1 := slice1[i1].Value
v = fn(t, &v1, nil)
i1++
case slice1[i1].Time.Equal(slice2[i2].Time):
t = slice1[i1].Time
v1 := slice1[i1].Value
v2 := slice2[i2].Value
v = fn(t, &v1, &v2)
i1++
i2++
case slice1[i1].Time.Before(slice2[i2].Time):
t = slice1[i1].Time
v1 := slice1[i1].Value
v = fn(t, &v1, nil)
i1++
case slice1[i1].Time.After(slice2[i2].Time):
t = slice2[i2].Time
v2 := slice2[i2].Value
v = fn(t, nil, &v2)
i2++
}
if v != nil {
ret = append(ret, Int{
Time: t,
Value: *v,
})
}
}
return newIntSeq(ret)
}
// Aggregate returns a aggregated *IntSeq according to the specified rule
func (s *IntSeq) Aggregate(fn func(t time.Time, slice Ints) *int, duration time.Duration, interval Interval) *IntSeq {
if fn == nil {
return s
}
ret := Ints{}
temp := Ints{}
sslice := s.getSlice()
if duration <= 0 {
for i := 0; i < s.Len(); {
t := sslice[i].Time
if !interval.Contain(t) {
i++
continue
}
temp = temp[:0]
for i < sslice.Len() && t.Equal(sslice[i].Time) {
temp = append(temp, sslice[i])
i++
}
v := fn(t, temp)
if v != nil {
ret = append(ret, Int{
Time: t,
Value: *v,
})
}
}
return newIntSeq(ret)
}
if len(sslice) == 0 && interval.Duration() < 0 {
return s
}
var begin time.Time
if len(sslice) > 0 {
begin = sslice[0].Time.Truncate(duration)
}
if interval.NotBefore != nil {
begin = (*interval.NotBefore).Truncate(duration)
if begin.Before(*interval.NotBefore) {
begin = begin.Add(duration)
}
}
var end time.Time
if len(sslice) > 0 {
end = sslice[len(sslice)-1].Time.Truncate(duration)
}
if interval.NotAfter != nil {
end = (*interval.NotAfter).Truncate(duration)
}
for t, i := begin, 0; !t.After(end); t = t.Add(duration) {
temp = temp[:0]
itv := BeginAt(t).EndAt(t.Add(duration))
for i < len(sslice) {
if sslice[i].Time.After(*itv.NotAfter) {
break
}
if itv.Contain(sslice[i].Time) {
temp = append(temp, sslice[i])
}
i++
}
v := fn(t, temp)
if v != nil {
ret = append(ret, Int{
Time: t,
Value: *v,
})
}
}
return newIntSeq(ret)
}