-
Notifications
You must be signed in to change notification settings - Fork 0
/
series_bool_base.go
667 lines (576 loc) · 14.5 KB
/
series_bool_base.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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
package gandalff
import (
"fmt"
"time"
"github.com/caerbannogwhite/preludiometa"
)
func (s SeriesBool) printInfo() {
fmt.Println("SeriesBool")
fmt.Println("==========")
fmt.Println("IsNullable:", s.isNullable)
fmt.Println("Sorted: ", s.sorted)
fmt.Println("Data: ", s.data)
fmt.Println("NullMask: ", s.nullMask)
fmt.Println("Partition: ", s.partition)
fmt.Println("Context: ", s.ctx)
}
//////////////////////// BASIC ACCESSORS
// Return the context of the series.
func (s SeriesBool) GetContext() *Context {
return s.ctx
}
// Return the number of elements in the series.
func (s SeriesBool) Len() int {
return len(s.data)
}
// Return the type of the series.
func (s SeriesBool) Type() preludiometa.BaseType {
return preludiometa.BoolType
}
// Return the type and cardinality of the series.
func (s SeriesBool) TypeCard() preludiometa.BaseTypeCard {
return preludiometa.BaseTypeCard{Base: preludiometa.BoolType, Card: s.Len()}
}
// Return if the series is grouped.
func (s SeriesBool) IsGrouped() bool {
return s.partition != nil
}
// Return if the series admits null values.
func (s SeriesBool) IsNullable() bool {
return s.isNullable
}
// Return if the series is sorted.
func (s SeriesBool) IsSorted() SeriesSortOrder {
return s.sorted
}
// Return if the series is error.
func (s SeriesBool) IsError() bool {
return false
}
// Return the error message of the series.
func (s SeriesBool) GetError() string {
return ""
}
// Return the partition of the series.
func (s SeriesBool) GetPartition() SeriesPartition {
return s.partition
}
// Return if the series has null values.
func (s SeriesBool) HasNull() bool {
for _, v := range s.nullMask {
if v != 0 {
return true
}
}
return false
}
// Return the number of null values in the series.
func (s SeriesBool) NullCount() int {
count := 0
for _, x := range s.nullMask {
for ; x != 0; x >>= 1 {
count += int(x & 1)
}
}
return count
}
// Return if the element at index i is null.
func (s SeriesBool) IsNull(i int) bool {
if s.isNullable {
return s.nullMask[i>>3]&(1<<uint(i%8)) != 0
}
return false
}
// Return the null mask of the series.
func (s SeriesBool) GetNullMask() []bool {
mask := make([]bool, len(s.data))
idx := 0
for _, v := range s.nullMask {
for i := 0; i < 8 && idx < len(s.data); i++ {
mask[idx] = v&(1<<uint(i)) != 0
idx++
}
}
return mask
}
// Set the null mask of the series.
func (s SeriesBool) SetNullMask(mask []bool) Series {
if s.partition != nil {
return SeriesError{"SeriesBool.SetNullMask: cannot set values on a grouped series"}
}
if s.isNullable {
for k, v := range mask {
if v {
s.nullMask[k>>3] |= 1 << uint(k%8)
} else {
s.nullMask[k>>3] &= ^(1 << uint(k%8))
}
}
return s
} else {
nullMask := __binVecInit(len(s.data), false)
for k, v := range mask {
if v {
nullMask[k>>3] |= 1 << uint(k%8)
} else {
nullMask[k>>3] &= ^(1 << uint(k%8))
}
}
s.isNullable = true
s.nullMask = nullMask
return s
}
}
// Make the series nullable.
func (s SeriesBool) MakeNullable() Series {
if !s.isNullable {
s.isNullable = true
s.nullMask = __binVecInit(len(s.data), false)
}
return s
}
// Make the series non-nullable.
func (s SeriesBool) MakeNonNullable() Series {
if s.isNullable {
s.isNullable = false
s.nullMask = make([]uint8, 0)
}
return s
}
// Get the element at index i.
func (s SeriesBool) Get(i int) any {
return s.data[i]
}
// Append appends a value or a slice of values to the series.
func (s SeriesBool) Append(v any) Series {
if s.partition != nil {
return SeriesError{"SeriesBool.Append: cannot append values to a grouped series"}
}
switch v := v.(type) {
case nil:
s.data = append(s.data, false)
s = s.MakeNullable().(SeriesBool)
if len(s.data) > len(s.nullMask)<<3 {
s.nullMask = append(s.nullMask, 0)
}
s.nullMask[(len(s.data)-1)>>3] |= 1 << uint8((len(s.data)-1)%8)
case SeriesNA:
s.isNullable, s.nullMask = __mergeNullMasks(len(s.data), s.isNullable, s.nullMask, v.Len(), true, __binVecInit(v.Len(), true))
s.data = append(s.data, make([]bool, v.Len())...)
case bool:
s.data = append(s.data, v)
if s.isNullable && len(s.data) > len(s.nullMask)<<3 {
s.nullMask = append(s.nullMask, 0)
}
case []bool:
s.data = append(s.data, v...)
if s.isNullable && len(s.data) > len(s.nullMask)<<3 {
s.nullMask = append(s.nullMask, make([]uint8, (len(s.data)>>3)-len(s.nullMask))...)
}
case NullableBool:
s.data = append(s.data, v.Value)
s = s.MakeNullable().(SeriesBool)
if len(s.data) > len(s.nullMask)<<3 {
s.nullMask = append(s.nullMask, 0)
}
if !v.Valid {
s.nullMask[(len(s.data)-1)>>3] |= 1 << uint8((len(s.data)-1)%8)
}
case []NullableBool:
ssize := len(s.data)
s.data = append(s.data, make([]bool, len(v))...)
s = s.MakeNullable().(SeriesBool)
if len(s.data) > len(s.nullMask)<<3 {
s.nullMask = append(s.nullMask, make([]uint8, (len(s.data)>>3)-len(s.nullMask)+1)...)
}
for i, b := range v {
s.data[ssize+i] = b.Value
if !b.Valid {
s.nullMask[(ssize+i)>>3] |= 1 << uint8((ssize+i)%8)
}
}
case SeriesBool:
if s.ctx != v.ctx {
return SeriesError{"SeriesBool.Append: cannot append SeriesBool from different contexts"}
}
s.isNullable, s.nullMask = __mergeNullMasks(len(s.data), s.isNullable, s.nullMask, len(v.data), v.isNullable, v.nullMask)
s.data = append(s.data, v.data...)
default:
return SeriesError{fmt.Sprintf("SeriesBool.Append: invalid type %T", v)}
}
s.sorted = SORTED_NONE
return s
}
// Take the elements according to the given interval.
func (s SeriesBool) Take(params ...int) Series {
indeces, err := seriesTakePreprocess("SeriesBool", s.Len(), params...)
if err != nil {
return SeriesError{err.Error()}
}
return s.filterIntSlice(indeces, false)
}
// Return the elements of the series as a slice.
func (s SeriesBool) Data() any {
return s.data
}
// Copy the series.
func (s SeriesBool) Copy() Series {
data := make([]bool, len(s.data))
copy(data, s.data)
nullMask := make([]uint8, len(s.nullMask))
copy(nullMask, s.nullMask)
return SeriesBool{
isNullable: s.isNullable,
sorted: s.sorted,
data: data,
nullMask: nullMask,
partition: s.partition,
ctx: s.ctx,
}
}
func (s SeriesBool) getData() []bool {
return s.data
}
// Ungroup the series.
func (s SeriesBool) UnGroup() Series {
s.partition = nil
return s
}
//////////////////////// FILTER OPERATIONS
// Filters out the elements by the given mask.
// Mask can be SeriesBool, SeriesInt, bool slice or a int slice.
func (s SeriesBool) Filter(mask any) Series {
switch mask := mask.(type) {
case SeriesBool:
return s.filterBoolSlice(mask.data)
case SeriesInt:
return s.filterIntSlice(mask.data, true)
case []bool:
return s.filterBoolSlice(mask)
case []int:
return s.filterIntSlice(mask, true)
default:
return SeriesError{fmt.Sprintf("SeriesBool.Filter: invalid type %T", mask)}
}
}
func (s SeriesBool) filterBoolSlice(mask []bool) Series {
if len(mask) != len(s.data) {
return SeriesError{fmt.Sprintf("SeriesBool.Filter: mask length (%d) does not match series length (%d)", len(mask), len(s.data))}
}
elementCount := 0
for _, v := range mask {
if v {
elementCount++
}
}
var data []bool
var nullMask []uint8
data = make([]bool, elementCount)
if s.isNullable {
nullMask = __binVecInit(elementCount, false)
dstIdx := 0
for srcIdx, v := range mask {
if v {
data[dstIdx] = s.data[srcIdx]
if srcIdx%8 > dstIdx%8 {
nullMask[dstIdx>>3] |= ((s.nullMask[srcIdx>>3] & (1 << uint(srcIdx%8))) >> uint(srcIdx%8-dstIdx%8))
} else {
nullMask[dstIdx>>3] |= ((s.nullMask[srcIdx>>3] & (1 << uint(srcIdx%8))) << uint(dstIdx%8-srcIdx%8))
}
dstIdx++
}
}
} else {
nullMask = make([]uint8, 0)
dstIdx := 0
for srcIdx, v := range mask {
if v {
data[dstIdx] = s.data[srcIdx]
dstIdx++
}
}
}
s.data = data
s.nullMask = nullMask
return s
}
func (s SeriesBool) filterIntSlice(indexes []int, check bool) Series {
if len(indexes) == 0 {
s.data = make([]bool, 0)
s.nullMask = make([]uint8, 0)
return s
}
// check if indexes are in range
if check {
for _, v := range indexes {
if v < 0 || v >= len(s.data) {
return SeriesError{fmt.Sprintf("SeriesBool.Filter: index %d is out of range", v)}
}
}
}
var data []bool
var nullMask []uint8
size := len(indexes)
data = make([]bool, size)
if s.isNullable {
nullMask = __binVecInit(size, false)
for dstIdx, srcIdx := range indexes {
data[dstIdx] = s.data[srcIdx]
if srcIdx%8 > dstIdx%8 {
nullMask[dstIdx>>3] |= ((s.nullMask[srcIdx>>3] & (1 << uint(srcIdx%8))) >> uint(srcIdx%8-dstIdx%8))
} else {
nullMask[dstIdx>>3] |= ((s.nullMask[srcIdx>>3] & (1 << uint(srcIdx%8))) << uint(dstIdx%8-srcIdx%8))
}
}
} else {
nullMask = make([]uint8, 0)
for dstIdx, srcIdx := range indexes {
data[dstIdx] = s.data[srcIdx]
}
}
s.data = data
s.nullMask = nullMask
return s
}
// Apply the given function to each element of the series.
func (s SeriesBool) Map(f MapFunc) Series {
if len(s.data) == 0 {
return s
}
v := f(s.Get(0))
switch v.(type) {
case bool:
data := make([]bool, len(s.data))
for i := 0; i < len(s.data); i++ {
data[i] = f(s.data[i]).(bool)
}
return SeriesBool{
isNullable: s.isNullable,
sorted: SORTED_NONE,
data: data,
nullMask: s.nullMask,
partition: nil,
ctx: s.ctx,
}
case int:
data := make([]int, len(s.data))
for i := 0; i < len(s.data); i++ {
data[i] = f(s.data[i]).(int)
}
return SeriesInt{
isNullable: s.isNullable,
sorted: SORTED_NONE,
data: data,
nullMask: s.nullMask,
partition: nil,
ctx: s.ctx,
}
case int64:
data := make([]int64, len(s.data))
for i := 0; i < len(s.data); i++ {
data[i] = f(s.data[i]).(int64)
}
return SeriesInt64{
isNullable: s.isNullable,
sorted: SORTED_NONE,
data: data,
nullMask: s.nullMask,
partition: nil,
ctx: s.ctx,
}
case float64:
data := make([]float64, len(s.data))
for i := 0; i < len(s.data); i++ {
data[i] = f(s.data[i]).(float64)
}
return SeriesFloat64{
isNullable: s.isNullable,
sorted: SORTED_NONE,
data: data,
nullMask: s.nullMask,
partition: nil,
ctx: s.ctx,
}
case string:
data := make([]*string, len(s.data))
for i := 0; i < len(s.data); i++ {
data[i] = s.ctx.stringPool.Put(f(s.data[i]).(string))
}
return SeriesString{
isNullable: s.isNullable,
sorted: SORTED_NONE,
data: data,
nullMask: s.nullMask,
partition: nil,
ctx: s.ctx,
}
case time.Time:
data := make([]time.Time, len(s.data))
for i := 0; i < len(s.data); i++ {
data[i] = f(s.data[i]).(time.Time)
}
return SeriesTime{
isNullable: s.isNullable,
sorted: SORTED_NONE,
data: data,
nullMask: s.nullMask,
partition: nil,
ctx: s.ctx,
}
case time.Duration:
data := make([]time.Duration, len(s.data))
for i := 0; i < len(s.data); i++ {
data[i] = f(s.data[i]).(time.Duration)
}
return SeriesDuration{
isNullable: s.isNullable,
sorted: SORTED_NONE,
data: data,
nullMask: s.nullMask,
partition: nil,
ctx: s.ctx,
}
default:
return SeriesError{fmt.Sprintf("SeriesBool.Map: Unsupported type %T", v)}
}
}
// Apply the given function to each element of the series.
func (s SeriesBool) MapNull(f MapFuncNull) Series {
if len(s.data) == 0 {
return s
}
if !s.isNullable {
return SeriesError{"SeriesBool.MapNull: series is not nullable"}
}
v, isNull := f(s.Get(0), s.IsNull(0))
switch v.(type) {
case bool:
data := make([]bool, len(s.data))
nullMask := make([]uint8, len(s.nullMask))
for i := 0; i < len(s.data); i++ {
v, isNull = f(s.data[i], s.IsNull(i))
data[i] = v.(bool)
if isNull {
nullMask[i>>3] |= 1 << uint(i%8)
}
}
return SeriesBool{
isNullable: true,
sorted: SORTED_NONE,
data: data,
nullMask: nullMask,
partition: nil,
ctx: s.ctx,
}
case int:
data := make([]int, len(s.data))
nullMask := make([]uint8, len(s.nullMask))
for i := 0; i < len(s.data); i++ {
v, isNull = f(s.data[i], s.IsNull(i))
data[i] = v.(int)
if isNull {
nullMask[i>>3] |= 1 << uint(i%8)
}
}
return SeriesInt{
isNullable: true,
sorted: SORTED_NONE,
data: data,
nullMask: nullMask,
partition: nil,
ctx: s.ctx,
}
case int64:
data := make([]int64, len(s.data))
nullMask := make([]uint8, len(s.nullMask))
for i := 0; i < len(s.data); i++ {
v, isNull = f(s.data[i], s.IsNull(i))
data[i] = v.(int64)
if isNull {
nullMask[i>>3] |= 1 << uint(i%8)
}
}
return SeriesInt64{
isNullable: true,
sorted: SORTED_NONE,
data: data,
nullMask: nullMask,
partition: nil,
ctx: s.ctx,
}
case float64:
data := make([]float64, len(s.data))
nullMask := make([]uint8, len(s.nullMask))
for i := 0; i < len(s.data); i++ {
v, isNull = f(s.data[i], s.IsNull(i))
data[i] = v.(float64)
if isNull {
nullMask[i>>3] |= 1 << uint(i%8)
}
}
return SeriesFloat64{
isNullable: true,
sorted: SORTED_NONE,
data: data,
nullMask: nullMask,
partition: nil,
ctx: s.ctx,
}
case string:
data := make([]*string, len(s.data))
nullMask := make([]uint8, len(s.nullMask))
for i := 0; i < len(s.data); i++ {
v, isNull = f(s.data[i], s.IsNull(i))
data[i] = s.ctx.stringPool.Put(v.(string))
if isNull {
nullMask[i>>3] |= 1 << uint(i%8)
}
}
return SeriesString{
isNullable: true,
sorted: SORTED_NONE,
data: data,
nullMask: nullMask,
partition: nil,
ctx: s.ctx,
}
case time.Time:
data := make([]time.Time, len(s.data))
nullMask := make([]uint8, len(s.nullMask))
for i := 0; i < len(s.data); i++ {
v, isNull = f(s.data[i], s.IsNull(i))
data[i] = v.(time.Time)
if isNull {
nullMask[i>>3] |= 1 << uint(i%8)
}
}
return SeriesTime{
isNullable: true,
sorted: SORTED_NONE,
data: data,
nullMask: nullMask,
partition: nil,
ctx: s.ctx,
}
case time.Duration:
data := make([]time.Duration, len(s.data))
nullMask := make([]uint8, len(s.nullMask))
for i := 0; i < len(s.data); i++ {
v, isNull = f(s.data[i], s.IsNull(i))
data[i] = v.(time.Duration)
if isNull {
nullMask[i>>3] |= 1 << uint(i%8)
}
}
return SeriesDuration{
isNullable: true,
sorted: SORTED_NONE,
data: data,
nullMask: nullMask,
partition: nil,
ctx: s.ctx,
}
default:
return SeriesError{fmt.Sprintf("SeriesBool.MapNull: Unsupported type %T", v)}
}
}