-
Notifications
You must be signed in to change notification settings - Fork 1
/
dac.go
1248 lines (1061 loc) · 32.2 KB
/
dac.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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Package dac implements a compressed dictionary for booleans, integers,
// dates, and floats of all sizes. Compression is obtained by application
// of variable byte codes. Direct access to any value is obtained through
// the utilization of Directly Addressable Codes (DACs). The integer data
// can be searched efficiently when sorted.
package dac
import (
"errors"
"math"
"math/bits"
"time"
"unsafe"
)
// nStreams is the maximum number of bytes in the
// variable bytes representation of a data element.
const (
nStreams64 = 8
nStreams32 = 4
nStreams16 = 2
)
// Dict is a dictionary type that stores values in a "Directly Addressable
// Codes" structure. Data is compressed but still provides direct access
// to any value. Moreover, data can be searched efficiently when stored in
// sorted order.
type Dict struct {
chunks [nStreams64][]byte
bitArr [nStreams64 - 1][]uint64
ranks [nStreams64 - 1][]int
}
// TODO: Serdes van en naar file voorzien?!
// New constructs a dictionary with an initial capacity of n values. Setting
// the capacity is optional but recommended for performance reasons. The
// capacity gets automatically updated when needed.
func New(n ...int) (*Dict, error) {
var m int
if len(n) != 0 {
if m = n[0]; m < 0 {
return nil, errors.New("dac: number of elements cannot be negative")
}
}
d := Dict{}
d.chunks[0] = make([]byte, 0, m)
d.bitArr[0] = make([]uint64, 0, (m+63)>>6)
d.ranks[0] = make([]int, 0, (m+63)>>9)
return &d, nil
}
// From constructs a dictionary from the given values.
// From automatically closes the dictionary for writing.
func From(values []uint64) *Dict {
d := Dict{}
d.chunks[0] = make([]byte, 0, len(values))
d.bitArr[0] = make([]uint64, 0, (len(values)+63)>>6)
d.WriteU64List(values)
d.Close()
return &d
}
// Len returns the actual number of entries in the dictionary.
func Len(d *Dict) int {
return len(d.chunks[0])
}
// Close builds support structures that improve the performance of direct reads.
// You should not do any direct reads before calling Close, as the read will
// crash. You can still write to the dictionary after a call to Close, but you
// will have to close the dictionary again before doing any direct reads.
func (d *Dict) Close() { // BuildIndex() noemen???
for i := 0; i < nStreams64-1; i++ {
arr := d.bitArr[i]
if l := (len(arr) + 7) >> 3; len(d.ranks[i]) < l {
d.ranks[i] = make([]int, l) // Is het niet sneller om te verlengen???
}
var prefix int
for j, k := 0, 0; j < len(arr); j++ {
if j&7 == 0 {
d.ranks[i][k] = prefix
k++
}
prefix += bits.OnesCount64(arr[j])
}
}
}
// Reset resets the dictionary without releasing its resources. It allows to
// re-use an existing dictionary.
func (d *Dict) Reset() {
for i := range d.chunks {
d.chunks[i] = d.chunks[i][:0]
}
for i := range d.bitArr {
d.bitArr[i] = d.bitArr[i][:0]
d.ranks[i] = d.ranks[i][:0]
}
}
// WriteBool writes a boolean value to the dictionary.
func (d *Dict) WriteBool(v bool) int {
if v {
return d.WriteU8(1)
}
return d.WriteU8(0)
}
// WriteU8 writes a uint8 value to the dictionary.
func (d *Dict) WriteU8(v uint8) int {
d.chunks[0] = append(d.chunks[0], v)
d.extend(0)
return len(d.chunks[0]) - 1
}
// WriteU16 writes a uint16 value to the dictionary.
func (d *Dict) WriteU16(v uint16) int {
d.chunks[0] = append(d.chunks[0], uint8(v))
v >>= 8
d.extend(0)
if v != 0 {
k := len(d.chunks[0]) - 1
d.bitArr[0][k>>6] |= 1 << (k & 63)
d.chunks[1] = append(d.chunks[1], uint8(v))
d.extend(1)
}
return len(d.chunks[0]) - 1
}
// WriteU32 writes a uint32 value to the dictionary.
func (d *Dict) WriteU32(v uint32) int {
return d.WriteU64(uint64(v))
}
// WriteU64 writes a uint64 value at the end of the dictionary.
// A write index is returned.
func (d *Dict) WriteU64(v uint64) int {
d.chunks[0] = append(d.chunks[0], uint8(v))
v >>= 8
d.extend(0)
for i := uint(0); v != 0 && i < nStreams64-1; i++ {
k := len(d.chunks[i]) - 1
d.bitArr[i][k>>6] |= 1 << (k & 63)
d.chunks[i+1] = append(d.chunks[i+1], uint8(v))
v >>= 8
d.extend(i + 1)
}
return len(d.chunks[0]) - 1
}
// RemoveAt removes the k-th entry from the dictionary.
func (d *Dict) RemoveAt(k int) error {
if k < 0 || len(d.chunks[0]) <= k {
return errors.New("dac: index k is out of bounds")
}
d.chunks[0] = append(d.chunks[0][:k], d.chunks[0][k+1:]...)
l := uint(0)
for l < nStreams64-1 && d.bit(l, k) {
kn := d.rank(l, k)
d.removeIdx(l, k)
k, l = kn, l+1
d.chunks[l] = append(d.chunks[l][:k], d.chunks[l][k+1:]...)
}
if l < nStreams64-1 {
d.removeIdx(l, k)
}
return nil
}
// InsertU64At inserts a uint64 value at index k of the dictionary.
func (d *Dict) InsertU64At(k int, v uint64) error {
if k < 0 || len(d.chunks[0]) <= k {
return errors.New("dac: index k is out of bounds")
}
d.chunks[0] = append(d.chunks[0], 0)
copy(d.chunks[0][k+1:], d.chunks[0][k:])
d.chunks[0][k] = uint8(v)
v >>= 8
l := uint(0)
for v != 0 && l < nStreams64-1 {
d.insertIdx(l, k, 1)
k, l = d.rank(l, k), l+1
d.chunks[l] = append(d.chunks[l], 0)
copy(d.chunks[l][k+1:], d.chunks[l][k:])
d.chunks[l][k] = uint8(v)
v >>= 8
}
if l < nStreams64-1 {
d.insertIdx(l, k, 0)
}
return nil
}
// UpdateU64At updates a uint64 value at index k of the dictionary.
func (d *Dict) UpdateU64At(k int, v uint64) error {
if k < 0 || len(d.chunks[0]) <= k {
return errors.New("dac: index k is out of bounds")
}
d.chunks[0][k] = uint8(v)
v >>= 8
l := uint(0)
// v!=0 and there exist remnants of the original number
for v != 0 && l < nStreams64-1 && d.bit(l, k) { // Op voorhand isPresent definiëren => sneller
k, l = d.rank(l, k), l+1
d.chunks[l][k] = uint8(v)
v >>= 8
}
// v==0, but still remnants of the original number
if l < nStreams64-1 && d.bit(l, k) {
kn := d.rank(l, k)
d.decrementIdx(l, k)
k, l = kn, l+1
d.chunks[l] = append(d.chunks[l][:k], d.chunks[l][k+1:]...)
for l < nStreams64-1 && d.bit(l, k) {
kn = d.rank(l, k)
d.removeIdx(l, k)
k, l = kn, l+1
d.chunks[l] = append(d.chunks[l][:k], d.chunks[l][k+1:]...)
}
if l < nStreams64-1 {
d.removeIdx(l, k)
}
return nil
}
// v!=0 but no more remnants of original number
if v != 0 && l < nStreams64-1 {
d.incrementIdx(l, k)
k, l = d.rank(l, k), l+1
d.chunks[l] = append(d.chunks[l], 0)
copy(d.chunks[l][k+1:], d.chunks[l][k:])
d.chunks[l][k] = uint8(v)
v >>= 8
for v != 0 && l < nStreams64-1 {
d.insertIdx(l, k, 1)
k, l = d.rank(l, k), l+1
d.chunks[l] = append(d.chunks[l], 0)
copy(d.chunks[l][k+1:], d.chunks[l][k:])
d.chunks[l][k] = uint8(v)
v >>= 8
}
if l < nStreams64-1 {
d.insertIdx(l, k, 0)
}
return nil
}
return nil
}
// insertIdx inserts index data for the k-th value at level l. This entails
// updating the length of the index arrays, the insertion of the presence bit
// in bitArr[l] and updates to the ranks[l] array. It is assumed that the
// chunks[l][k] byte is inserted before calling this function.
func (d *Dict) insertIdx(l uint, k int, v uint64) {
// extend d.ranks[l] if needed
if lRanks := len(d.ranks[l]); (len(d.chunks[l])+511)>>9 > lRanks {
d.ranks[l] = append(d.ranks[l], 0)
if lRanks != 0 {
sum := d.ranks[l][lRanks-1]
for i := (lRanks - 1) << 3; i < lRanks<<3; i++ {
sum += bits.OnesCount64(d.bitArr[l][i])
}
d.ranks[l][lRanks] = sum
}
}
// update d.ranks[l]
b := int(v)
for i := k>>9 + 1; i < len(d.ranks[l]); i++ {
bb := int(d.bitArr[l][i<<3-1] >> 63)
d.ranks[l][i] += b - bb
}
// extend d.bitArr[l] if needed
if (len(d.chunks[l])+63)>>6 > len(d.bitArr[l]) {
d.bitArr[l] = append(d.bitArr[l], 0)
}
// update d.bitArr[l]
k64 := k >> 6
k63 := k & 63
mask := uint64(1)<<k63 - 1
overflow := d.bitArr[l][k64] >> 63
d.bitArr[l][k64] = v<<k63 | (d.bitArr[l][k64]&^mask)<<1 | d.bitArr[l][k64]&mask
for i := k64 + 1; i < len(d.bitArr[l]); i++ {
tmp := d.bitArr[l][i] >> 63
d.bitArr[l][i] = overflow | d.bitArr[l][i]<<1
overflow = tmp
}
}
// removeIdx removes index data for the k-th value at level l. This entails
// the removal from the presence bit in bitArr[l], the update of the ranks[l]
// values and checks whether the lengths of the index arrays can be shortened.
// It is assumed that the chunks[l][k] byte has already been removed.
func (d *Dict) removeIdx(l uint, k int) {
// update ranks contents
b := int(d.bitArr[l][k>>6] >> (k & 63) & 1)
for i := k>>9 + 1; i < len(d.ranks[l]); i++ {
bb := int(d.bitArr[l][i<<3] & 1)
d.ranks[l][i] += bb - b
b = bb
}
// shorten ranks length if possible
n := (len(d.chunks[l]) + 511) >> 9
d.ranks[l] = d.ranks[l][:n]
// remove bit from bitArr
var bt uint64
k64 := k >> 6
for i := len(d.bitArr[l]) - 1; i > k64; i-- {
tmp := d.bitArr[l][i] << 63
d.bitArr[l][i] = bt | d.bitArr[l][i]>>1
bt = tmp
}
mask := uint64(1<<(k&63) - 1)
d.bitArr[l][k64] = bt | (d.bitArr[l][k64]>>1)&^mask | d.bitArr[l][k64]&mask
// shorten bitArr length if possible
n = (len(d.chunks[l]) + 63) >> 6
d.bitArr[l] = d.bitArr[l][:n]
}
// incrementIdx ...
func (d *Dict) incrementIdx(l uint, k int) {
// update bitArr
d.bitArr[l][k>>6] |= 1 << (k & 63)
// update d.ranks[l]
for i := k>>9 + 1; i < len(d.ranks[l]); i++ {
d.ranks[l][i]++
}
}
// decrementIdx ...
func (d *Dict) decrementIdx(l uint, k int) {
// update ranks
for i := k>>9 + 1; i < len(d.ranks[l]); i++ {
d.ranks[l][i]--
}
// update bitArr
d.bitArr[l][k>>6] &^= 1 << (k & 63)
}
// WriteBoolList writes a slice of boolean values to the dictionary.
func (d *Dict) WriteBoolList(values []bool) {
l := (len(d.chunks[0])+63)>>6 - len(d.bitArr[0])
d.bitArr[0] = append(d.bitArr[0], make([]uint64, l)...) // Dit kan sneller zonder de tweede make.
for _, v := range values {
if v {
d.chunks[0] = append(d.chunks[0], 1)
} else {
d.chunks[0] = append(d.chunks[0], 0)
}
}
}
// WriteU8List writes a slice of uint8 values to the dictionary.
func (d *Dict) WriteU8List(values []uint8) {
l := (len(d.chunks[0])+len(values)+63)>>6 - len(d.bitArr[0])
d.chunks[0] = append(d.chunks[0], values...)
d.bitArr[0] = append(d.bitArr[0], make([]uint64, l)...) // Dit kan sneller zonder de tweede make.
}
// WriteU16List writes a slice of uint16 values to the dictionary.
func (d *Dict) WriteU16List(values []uint16) {
l := (len(d.chunks[0])+len(values)+63)>>6 - len(d.bitArr[0])
d.bitArr[0] = append(d.bitArr[0], make([]uint64, l)...) // Dit kan sneller zonder de tweede make.
for _, v := range values {
d.chunks[0] = append(d.chunks[0], uint8(v))
v >>= 8
if v != 0 {
k := len(d.chunks[0]) - 1
d.bitArr[0][k>>6] |= 1 << (k & 63)
d.chunks[1] = append(d.chunks[1], uint8(v))
d.extend(1)
}
}
}
// WriteU32List writes a slice of uint32 values to the dictionary.
func (d *Dict) WriteU32List(values []uint32) {
l := (len(d.chunks[0])+len(values)+63)>>6 - len(d.bitArr[0])
d.bitArr[0] = append(d.bitArr[0], make([]uint64, l)...) // Dit kan sneller zonder de tweede make.
for _, v := range values {
d.chunks[0] = append(d.chunks[0], uint8(v))
v >>= 8
for i := uint(0); v != 0 && i < nStreams64-1; i++ {
k := len(d.chunks[i]) - 1
d.bitArr[i][k>>6] |= 1 << (k & 63)
d.chunks[i+1] = append(d.chunks[i+1], uint8(v))
v >>= 8
d.extend(i + 1)
}
}
}
// WriteU64List writes a slice of uint64 values to the dictionary.
func (d *Dict) WriteU64List(values []uint64) {
l := (len(d.chunks[0])+len(values)+63)>>6 - len(d.bitArr[0])
d.bitArr[0] = append(d.bitArr[0], make([]uint64, l)...) // Dit kan sneller zonder de tweede make.
for _, v := range values {
d.chunks[0] = append(d.chunks[0], uint8(v))
v >>= 8
for i := uint(0); v != 0 && i < nStreams64-1; i++ {
k := len(d.chunks[i]) - 1
d.bitArr[i][k>>6] |= 1 << (k & 63)
d.chunks[i+1] = append(d.chunks[i+1], uint8(v))
v >>= 8
d.extend(i + 1)
}
}
}
// WriteI8 writes an int8 value to the dictionary.
func (d *Dict) WriteI8(v int8) int {
uv := uint8((v << 1) ^ (v >> 7))
return d.WriteU8(uv)
}
// WriteI16 writes an int16 value to the dictionary.
func (d *Dict) WriteI16(v int16) int {
uv := uint16((v << 1) ^ (v >> 15))
return d.WriteU16(uv)
}
// WriteI32 writes an int32 value to the dictionary.
func (d *Dict) WriteI32(v int32) int { // TODO: too slow!!!
uv := uint32((v << 1) ^ (v >> 31))
return d.WriteU32(uv)
}
// WriteI64 writes an int64 value to the dictionary.
func (d *Dict) WriteI64(v int64) int {
uv := uint64((v << 1) ^ (v >> 63))
return d.WriteU64(uv)
}
// WriteFloat32 writes a float32 value to the dictionary.
func (d *Dict) WriteFloat32(v float32) int {
x := math.Float32bits(v)
uv := uint64(bits.ReverseBytes32(x))
// return d.WriteU32(uv) // Is d.WriteU64(uv) faster?
return d.WriteU64(uv)
}
// WriteFloat64 writes a float64 value to the dictionary.
func (d *Dict) WriteFloat64(v float64) int {
uv := bits.ReverseBytes64(math.Float64bits(v))
return d.WriteU64(uv)
}
// WriteDateTime writes a time.Time value with nanosecond
// precision to the dictionary. Timezones are not written.
func (d *Dict) WriteDateTime(t time.Time) int {
return d.WriteI64(t.UnixNano())
}
// WriteI8List writes a slice of int8 values to the dictionary.
func (d *Dict) WriteI8List(values []int8) {
l := (len(d.chunks[0])+63)>>6 - len(d.bitArr[0])
d.bitArr[0] = append(d.bitArr[0], make([]uint64, l)...) // Dit kan sneller zonder de tweede make.
for _, v := range values {
uv := uint8((v << 1) ^ (v >> 7))
d.chunks[0] = append(d.chunks[0], uv)
}
}
// WriteI16List writes a slice of int16 values to the dictionary.
func (d *Dict) WriteI16List(values []int16) {
l := (len(d.chunks[0])+len(values)+63)>>6 - len(d.bitArr[0])
d.bitArr[0] = append(d.bitArr[0], make([]uint64, l)...) // Dit kan sneller zonder de tweede make.
for _, v := range values {
uv := uint16((v << 1) ^ (v >> 15))
d.chunks[0] = append(d.chunks[0], uint8(uv))
uv >>= 8
if uv != 0 {
k := len(d.chunks[0]) - 1
d.bitArr[0][k>>6] |= 1 << (k & 63)
d.chunks[1] = append(d.chunks[1], uint8(uv))
d.extend(1)
}
}
}
// WriteI32List writes a slice of int32 values to the dictionary.
func (d *Dict) WriteI32List(values []int32) {
l := (len(d.chunks[0])+len(values)+63)>>6 - len(d.bitArr[0])
d.bitArr[0] = append(d.bitArr[0], make([]uint64, l)...) // Dit kan sneller zonder de tweede make.
for _, v := range values {
uv := uint32((v << 1) ^ (v >> 31))
d.chunks[0] = append(d.chunks[0], uint8(uv))
uv >>= 8
for i := uint(0); uv != 0 && i < nStreams64-1; i++ {
k := len(d.chunks[i]) - 1
d.bitArr[i][k>>6] |= 1 << (k & 63)
d.chunks[i+1] = append(d.chunks[i+1], uint8(uv))
uv >>= 8
d.extend(i + 1)
}
}
}
// WriteI64List writes a slice of int64 values to the dictionary.
func (d *Dict) WriteI64List(values []int64) {
l := (len(d.chunks[0])+len(values)+63)>>6 - len(d.bitArr[0])
d.bitArr[0] = append(d.bitArr[0], make([]uint64, l)...) // Dit kan sneller zonder de tweede make.
for _, v := range values {
uv := uint64((v << 1) ^ (v >> 63))
d.chunks[0] = append(d.chunks[0], uint8(uv))
uv >>= 8
for i := uint(0); uv != 0 && i < nStreams64-1; i++ { // TODO: Deze lus wordt niet uitgevoerd door de testen!!!
k := len(d.chunks[i]) - 1
d.bitArr[i][k>>6] |= 1 << (k & 63)
d.chunks[i+1] = append(d.chunks[i+1], uint8(uv))
uv >>= 8
d.extend(i + 1)
}
}
}
// WriteFloat32List writes a slice of float values to the dictionary.
func (d *Dict) WriteFloat32List(values []float32) {
l := (len(d.chunks[0])+len(values)+63)>>6 - len(d.bitArr[0])
d.bitArr[0] = append(d.bitArr[0], make([]uint64, l)...) // Dit kan sneller zonder de tweede make.
for _, v := range values {
uv := bits.ReverseBytes32(math.Float32bits(v))
d.chunks[0] = append(d.chunks[0], uint8(uv))
uv >>= 8
for i := uint(0); uv != 0 && i < nStreams64-1; i++ {
k := len(d.chunks[i]) - 1
d.bitArr[i][k>>6] |= 1 << (k & 63)
d.chunks[i+1] = append(d.chunks[i+1], uint8(uv))
uv >>= 8
d.extend(i + 1)
}
}
}
// WriteFloat64List writes a slice of float64 values to the dictionary.
func (d *Dict) WriteFloat64List(values []float64) {
l := (len(d.chunks[0])+len(values)+63)>>6 - len(d.bitArr[0])
d.bitArr[0] = append(d.bitArr[0], make([]uint64, l)...) // Dit kan sneller zonder de tweede make.
for _, v := range values {
uv := bits.ReverseBytes64(math.Float64bits(v))
d.chunks[0] = append(d.chunks[0], uint8(uv))
uv >>= 8
for i := uint(0); uv != 0 && i < nStreams64-1; i++ {
k := len(d.chunks[i]) - 1
d.bitArr[i][k>>6] |= 1 << (k & 63)
d.chunks[i+1] = append(d.chunks[i+1], uint8(uv))
uv >>= 8
d.extend(i + 1)
}
}
}
// WriteDateTimeList writes a slice of time.Time values to the dictionary.
func (d *Dict) WriteDateTimeList(dateTimes []time.Time) {
l := (len(d.chunks[0])+len(dateTimes)+63)>>6 - len(d.bitArr[0])
d.bitArr[0] = append(d.bitArr[0], make([]uint64, l)...) // Dit kan sneller zonder de tweede make.
for _, dt := range dateTimes {
v := dt.UnixNano()
uv := uint64((v << 1) ^ (v >> 63))
d.chunks[0] = append(d.chunks[0], uint8(uv))
uv >>= 8
for i := uint(0); uv != 0 && i < nStreams64-1; i++ {
k := len(d.chunks[i]) - 1
d.bitArr[i][k>>6] |= 1 << (k & 63)
d.chunks[i+1] = append(d.chunks[i+1], uint8(uv))
uv >>= 8
d.extend(i + 1)
}
}
}
// ReadBool reads a boolean value at a given index in the dictionary.
func (d *Dict) ReadBool(i int) (bool, error) {
if i < 0 || Len(d) <= i {
return false, errors.New("dac: index k is out of bounds")
}
return d.chunks[0][i] != 0, nil
}
// ReadU8 reads an uint8 value at a given index in the dictionary.
func (d *Dict) ReadU8(i int) (uint8, error) {
if i < 0 || Len(d) <= i {
return 0, errors.New("dac: index k is out of bounds")
}
return d.chunks[0][i], nil
}
// ReadU16 reads an uint16 value at a given index in the dictionary.
func (d *Dict) ReadU16(k int) (v uint16, err error) {
if k < 0 || len(d.chunks[0]) <= k {
return 0, errors.New("dac: index k is out of bounds")
}
buf := (*[nStreams16]byte)(unsafe.Pointer(&v))
buf[0] = d.chunks[0][k]
if d.bit(0, k) {
k = d.rank(0, k)
buf[1] = d.chunks[1][k]
}
return
}
// // ReadU32 reads an uint32 value at a given index in the dictionary.
// func (d *Dict) ReadU32(i int) (uint32, error) { // TODO: too slow!!!
// v, err := d.ReadU64(i)
// return uint32(v), err
// }
// ReadU32 reads an uint32 value at a given index in the dictionary.
func (d *Dict) ReadU32(k int) (v uint32, err error) {
if k < 0 || len(d.chunks[0]) <= k {
return 0, errors.New("dac: index k is out of bounds")
}
buf := (*[nStreams32]byte)(unsafe.Pointer(&v))
buf[0] = d.chunks[0][k]
if d.bit(0, k) {
k = d.rank(0, k)
buf[1] = d.chunks[1][k]
} else {
return
}
if d.bit(1, k) {
k = d.rank(1, k)
buf[2] = d.chunks[2][k]
} else {
return
}
if d.bit(2, k) {
k = d.rank(2, k)
buf[3] = d.chunks[3][k]
}
return
}
// ReadU64 reads an uint64 value at a given index in the dictionary.
func (d *Dict) ReadU64(k int) (v uint64, err error) {
if k < 0 || len(d.chunks[0]) <= k {
return 0, errors.New("dac: index k is out of bounds")
}
buf := (*[nStreams64]byte)(unsafe.Pointer(&v))
buf[0] = d.chunks[0][k]
var l uint
for l < nStreams64-1 && d.bit(l, k) {
k = d.rank(l, k)
l++
buf[l] = d.chunks[l][k]
}
return
}
// ReadBoolList returns all values in the dictionary when they are of boolean
// type. One can avoid the allocation of the return slice in ReadBoolList by
// supplying a slice of a size sufficient to store all values. Supplying a
// slice is optional.
func (d *Dict) ReadBoolList(values []bool) []bool {
m := len(d.chunks[0])
if len(values) < m {
values = make([]bool, m)
} else {
values = values[:m]
}
chunks := d.chunks[0]
for i := 0; i < len(chunks) && i < len(values); i++ {
if chunks[i] == 0 {
values[i] = false
} else {
values[i] = true
}
}
return values
}
// ReadU8List returns all values in the dictionary when they are of uint8
// type. One can avoid the allocation of the return slice in ReadU8List by
// supplying a slice of a size sufficient to store all values. Supplying a
// slice is optional.
func (d *Dict) ReadU8List(values []uint8) []uint8 {
m := len(d.chunks[0])
if len(values) < m {
values = make([]uint8, m)
} else {
values = values[:m]
}
for i := range values {
values[i] = d.chunks[0][i]
}
return values
}
// ReadU16List returns all values in the dictionary when they are of uint16
// type. One can avoid the allocation of the return slice in ReadU16List by
// supplying a slice of a size sufficient to store all values. Supplying a
// slice is optional.
func (d *Dict) ReadU16List(values []uint16) []uint16 {
m := len(d.chunks[0])
if len(values) < m {
values = make([]uint16, m)
} else {
values = values[:m]
}
rank := -1
for i := range values {
buf := (*[nStreams16]byte)(unsafe.Pointer(&values[i]))
buf[0] = d.chunks[0][i]
if d.bit(0, i) {
rank++
buf[1] = d.chunks[1][rank]
}
}
return values
}
// ReadU32List returns all values in the dictionary when they are of uint32
// type. One can avoid the allocation of the return slice in ReadU32List by
// supplying a slice of a size sufficient to store all values. Supplying a
// slice is optional.
func (d *Dict) ReadU32List(values []uint32) []uint32 {
m := len(d.chunks[0])
if len(values) < m {
values = make([]uint32, m)
} else {
values = values[:m]
}
ranks := [nStreams32 - 1]int{-1, -1, -1}
for i := range values {
buf := (*[nStreams32]byte)(unsafe.Pointer(&values[i]))
buf[0] = d.chunks[0][i]
j, k := uint(0), i
for j < nStreams32-1 && d.bit(j, k) {
ranks[j]++
k = ranks[j]
j++
buf[j] = d.chunks[j][k]
}
}
return values
}
// ReadU64List returns all values in the dictionary when they are of uint64
// type. One can avoid the allocation of the return slice in ReadU64List by
// supplying a slice of a size sufficient to store all values. Supplying a
// slice is optional.
func (d *Dict) ReadU64List(values []uint64) []uint64 {
m := len(d.chunks[0])
if len(values) < m {
values = make([]uint64, m)
} else {
values = values[:m]
}
ranks := [nStreams64 - 1]int{-1, -1, -1, -1, -1, -1, -1}
for i := range values {
buf := (*[nStreams64]byte)(unsafe.Pointer(&values[i]))
buf[0] = d.chunks[0][i]
j, k := uint(0), i
for j < nStreams64-1 && d.bit(j, k) {
ranks[j]++
k = ranks[j]
j++
buf[j] = d.chunks[j][k]
}
}
return values
}
// ReadI8 reads an int8 value at a given index in the dictionary.
func (d *Dict) ReadI8(i int) (int8, error) {
uv, err := d.ReadU8(i)
return int8((uv >> 1) ^ -(uv & 1)), err
}
// ReadI16 reads an int16 value at a given index in the dictionary.
func (d *Dict) ReadI16(i int) (int16, error) {
uv, err := d.ReadU64(i) // TODO: Is dit juist? Moeten we niet via uint16 gaan???
return int16((uv >> 1) ^ -(uv & 1)), err
}
// ReadI32 reads an int32 value at a given index in the dictionary.
func (d *Dict) ReadI32(i int) (int32, error) {
uv, err := d.ReadU64(i)
return int32((uv >> 1) ^ -(uv & 1)), err
}
// ReadI64 reads an int64 value at a given index in the dictionary.
func (d *Dict) ReadI64(i int) (int64, error) {
uv, err := d.ReadU64(i)
return int64((uv >> 1) ^ -(uv & 1)), err
}
// ReadFloat32 reads a float32 value at a given index in the dictionary.
func (d *Dict) ReadFloat32(i int) (float32, error) {
if i < 0 || len(d.chunks[0]) <= i {
return 0, errors.New("dac: index k is out of bounds")
}
var uv uint32
buf := (*[nStreams32]byte)(unsafe.Pointer(&uv))
buf[0] = d.chunks[0][i]
l, k := uint(0), i
for l < nStreams32-1 && d.bit(l, k) {
k = d.rank(l, k)
l++
buf[l] = d.chunks[l][k]
}
return math.Float32frombits(bits.ReverseBytes32(uv)), nil
}
// ReadFloat64 reads a float64 value at a given index in the dictionary.
func (d *Dict) ReadFloat64(i int) (float64, error) {
if i < 0 || len(d.chunks[0]) <= i {
return 0, errors.New("dac: index k is out of bounds")
}
var uv uint64
buf := (*[nStreams64]byte)(unsafe.Pointer(&uv))
buf[0] = d.chunks[0][i]
l, k := uint(0), i
for l < nStreams64-1 && d.bit(l, k) {
k = d.rank(l, k)
l++
buf[l] = d.chunks[l][k]
}
return math.Float64frombits(bits.ReverseBytes64(uv)), nil
}
// ReadDateTime reads a time.Time value at a given index in the dictionary.
// No timezone is read.
func (d *Dict) ReadDateTime(i int) (time.Time, error) {
v, err := d.ReadI64(i)
sec := v / 1e9
nsec := v - 1e9*sec
return time.Unix(sec, nsec), err
}
// ReadI8List returns all values in the dictionary when they are of int8
// type. One can avoid the allocation of the return slice in ReadI8List by
// supplying a slice of a size sufficient to store all values. Still,
// this is optional.
func (d *Dict) ReadI8List(values []int8) []int8 {
m := len(d.chunks[0])
if len(values) < m {
values = make([]int8, m)
} else {
values = values[:m]
}
for i := range values {
uv := d.chunks[0][i]
values[i] = int8((uv >> 1) ^ -(uv & 1))
}
return values
}
// ReadI16List returns all values in the dictionary when they are of int16
// type. One can avoid the allocation of the return slice in ReadI16List by
// supplying a slice of a size sufficient to store all values. Still, this
// is optional.
func (d *Dict) ReadI16List(values []int16) []int16 {
m := len(d.chunks[0])
if len(values) < m {
values = make([]int16, m)
} else {
values = values[:m]
}
rank, chunks0, chunks1 := -1, d.chunks[0], d.chunks[1]
for i := range values {
var uv uint16
buf := (*[nStreams16]byte)(unsafe.Pointer(&uv))
buf[0] = chunks0[i]
if d.bit(0, i) {
rank++
buf[1] = chunks1[rank]
}
values[i] = int16((uv >> 1) ^ -(uv & 1))
}
return values
}
// ReadI32List returns all values in the dictionary when they are of int32
// type. One can avoid the allocation of the return slice in ReadI32List by
// supplying a slice of a size sufficient to store all values. Still, this
// is optional.
func (d *Dict) ReadI32List(values []int32) []int32 {
m := len(d.chunks[0])
if len(values) < m {
values = make([]int32, m)
} else {
values = values[:m]
}
ranks := [nStreams32 - 1]int{-1, -1, -1}
for i := range values {
var uv uint32
buf := (*[nStreams32]byte)(unsafe.Pointer(&uv))
buf[0] = d.chunks[0][i]
j, k := uint(0), i
for j < nStreams32-1 && d.bit(j, k) {
ranks[j]++
k = ranks[j]
j++
buf[j] = d.chunks[j][k]
}
values[i] = int32((uv >> 1) ^ -(uv & 1))
}
return values
}
// ReadI64List returns all values in the dictionary when they are of int64
// type. One can avoid the allocation of the return slice in ReadI64List by
// supplying a slice of a size sufficient to store all values. Still,
// this is optional.
func (d *Dict) ReadI64List(values []int64) []int64 {