forked from scala/scala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NVector.scala
1817 lines (1670 loc) · 72.9 KB
/
NVector.scala
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
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc.
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/
package scala.collection
package immutable
import scala.annotation.switch
import scala.collection.mutable.{ArrayBuilder, Builder, ReusableBuilder}
import scala.collection.generic.DefaultSerializable
import scala.reflect.ClassTag
import java.util.{Arrays, Spliterator}
import java.util.Arrays.{copyOf, copyOfRange}
import java.lang.Math.{abs, max => mmax, min => mmin}
import NVectorStatics.{Arr1, Arr2, Arr3, Arr4, Arr5, Arr6}
import scala.collection.Stepper.EfficientSplit
object NVector extends StrictOptimizedSeqFactory[NVector] {
import NVectorStatics._
private[immutable] val _empty: NVector[Nothing] = new NVector()
def empty[A]: NVector[A] = _empty
def from[E](it: collection.IterableOnce[E]): NVector[E] =
it match {
case v: NVector[E] => v
case _ =>
val knownSize = it.knownSize
if (knownSize == 0) empty[E]
else if (knownSize > 0 && knownSize <= WIDTH) {
val a1 = new Array[Any](knownSize)
it.iterator.copyToArray(a1)
new NVector[E](a1.asInstanceOf[Arr1], knownSize)
} else {
(newBuilder ++= it).result()
}
}
def newBuilder[A]: ReusableBuilder[A, NVector[A]] = new NVectorBuilder[A]
/** Create a Vector with the same element at each index.
*
* Unlike `fill`, which takes a by-name argument for the value and can thereby
* compute different values for each index, this method guarantees that all
* elements are identical. This allows sparse allocation in O(log n) time and space.
*/
def fillSparse[A](n: Int)(elem: A): NVector[A] = {
if(n <= 0) NVector._empty
else {
val b = new NVectorBuilder[A]
b.initSparse(n, elem)
b.result()
}
}
}
/** Vector is a general-purpose, immutable data structure. It provides random access and updates
* in O(log n) time, as well as very fast append/prepend/tail/init (amortized O(1), worst case O(log n)).
* Because vectors strike a good balance between fast random selections and fast random functional updates,
* they are currently the default implementation of immutable indexed sequences.
*
* Vectors are implemented by radix-balanced finger trees of width 32. There is a separate subclass
* for each level (0 to 6, with 0 being the empty vector and 6 a tree with a maximum width of 64 at the
* top level).
*
* Tree balancing:
* - Only the first dimension of an array may have a size < WIDTH
* - In a `data` (central) array the first dimension may be up to WIDTH-2 long, in `prefix1` and `suffix1` up
* to WIDTH, and in other `prefix` and `suffix` arrays up to WIDTH-1
* - `prefix1` and `suffix1` are never empty
* - Balancing does not cross the main data array (i.e. prepending never touches the suffix and appending never touches
* the prefix). The level is increased/decreased when the affected side plus main data is already full/empty
* - All arrays are left-aligned and truncated
*
* In addition to the data slices (`prefix1`, `prefix2`, ..., `dataN`, ..., `suffix2`, `suffix1`) we store a running
* count of elements after each prefix for more efficient indexing without having to dereference all prefix arrays.
*/
final class NVector[+A] private[immutable] (prefix1: Arr1, len1: Int,
prefix2: Arr2, len12: Int,
prefix3: Arr3, len123: Int,
prefix4: Arr4, len1234: Int,
prefix5: Arr5, len12345: Int,
data6: Arr6,
suffix5: Arr5, suffix4: Arr4, suffix3: Arr3, suffix2: Arr2, suffix1: Arr1,
val length: Int, dimension: Int)
extends AbstractSeq[A]
with IndexedSeq[A]
with IndexedSeqOps[A, NVector, NVector[A]]
with StrictOptimizedSeqOps[A, NVector, NVector[A]]
with IterableFactoryDefaults[A, NVector]
with DefaultSerializable {
import NVectorStatics._
@inline private[immutable] def this(prefix1: Arr1, len1: Int,
prefix2: Arr2, len12: Int,
prefix3: Arr3, len123: Int,
prefix4: Arr4, len1234: Int,
prefix5: Arr5, len12345: Int,
data6: Arr6,
suffix5: Arr5, suffix4: Arr4, suffix3: Arr3, suffix2: Arr2, suffix1: Arr1,
length: Int) =
this(prefix1, len1, prefix2, len12, prefix3, len123, prefix4, len1234, prefix5, len12345, data6, suffix5, suffix4, suffix3, suffix2, suffix1, length, 6)
@inline private[immutable] def this(prefix1: Arr1, len1: Int,
prefix2: Arr2, len12: Int,
prefix3: Arr3, len123: Int,
prefix4: Arr4, len1234: Int,
data5: Arr5,
suffix4: Arr4, suffix3: Arr3, suffix2: Arr2, suffix1: Arr1,
length: Int) =
this(prefix1, len1, prefix2, len12, prefix3, len123, prefix4, len1234, data5, 0, null, null, suffix4, suffix3, suffix2, suffix1, length, 5)
@inline private[immutable] def this(prefix1: Arr1, len1: Int,
prefix2: Arr2, len12: Int,
prefix3: Arr3, len123: Int,
data4: Arr4,
suffix3: Arr3, suffix2: Arr2, suffix1: Arr1,
length: Int) =
this(prefix1, len1, prefix2, len12, prefix3, len123, data4, 0, null, 0, null, null, null, suffix3, suffix2, suffix1, length, 4)
@inline private[immutable] def this(prefix1: Arr1, len1: Int,
prefix2: Arr2, len12: Int,
data3: Arr3,
suffix2: Arr2, suffix1: Arr1,
length: Int) =
this(prefix1, len1, prefix2, len12, data3, 0, null, 0, null, 0, null, null, null, null, suffix2, suffix1, length, 3)
@inline private[immutable] def this(prefix1: Arr1, len1: Int,
data2: Arr2,
suffix1: Arr1,
length: Int) =
this(prefix1, len1, data2, 0, null, 0, null, 0, null, 0, null, null, null, null, null, suffix1, length, 2)
@inline private[immutable] def this(data1: Arr1,
length: Int) =
this(data1, 0, null, 0, null, 0, null, 0, null, 0, null, null, null, null, null, data1, length, 1)
@inline private[immutable] def this() =
this(null, 0, null, 0, null, 0, null, 0, null, 0, null, null, null, null, null, null, 0, 0)
@inline private[this] def copy(prefix1: Arr1 = prefix1, len1: Int = len1,
prefix2: Arr2 = prefix2, len12: Int = len12,
prefix3: Arr3 = prefix3, len123: Int = len123,
prefix4: Arr4 = prefix4, len1234: Int = len1234,
prefix5: Arr5 = prefix5, len12345: Int = len12345,
data6: Arr6 = data6,
suffix5: Arr5 = suffix5, suffix4: Arr4 = suffix4, suffix3: Arr3 = suffix3, suffix2: Arr2 = suffix2, suffix1: Arr1 = suffix1,
length: Int = length, dimension: Int = dimension) =
new NVector(prefix1, len1, prefix2, len12, prefix3, len123, prefix4, len1234, prefix5, len12345, data6, suffix5, suffix4, suffix3, suffix2, suffix1, length, dimension)
@inline private[this] def copy6(prefix1: Arr1 = prefix1, len1: Int = len1,
prefix2: Arr2 = prefix2, len12: Int = len12,
prefix3: Arr3 = prefix3, len123: Int = len123,
prefix4: Arr4 = prefix4, len1234: Int = len1234,
prefix5: Arr5 = prefix5, len12345: Int = len12345,
data6: Arr6 = data6,
suffix5: Arr5 = suffix5, suffix4: Arr4 = suffix4, suffix3: Arr3 = suffix3, suffix2: Arr2 = suffix2, suffix1: Arr1 = suffix1,
length: Int = length) =
new NVector(prefix1, len1, prefix2, len12, prefix3, len123, prefix4, len1234, prefix5, len12345, data6, suffix5, suffix4, suffix3, suffix2, suffix1, length, 6)
@inline private[this] def copy5(prefix1: Arr1 = prefix1, len1: Int = len1,
prefix2: Arr2 = prefix2, len12: Int = len12,
prefix3: Arr3 = prefix3, len123: Int = len123,
prefix4: Arr4 = prefix4, len1234: Int = len1234,
prefix5: Arr5 = prefix5,
suffix4: Arr4 = suffix4, suffix3: Arr3 = suffix3, suffix2: Arr2 = suffix2, suffix1: Arr1 = suffix1,
length: Int = length) =
new NVector(prefix1, len1, prefix2, len12, prefix3, len123, prefix4, len1234, prefix5, 0, null, null, suffix4, suffix3, suffix2, suffix1, length, 5)
@inline private[this] def copy4(prefix1: Arr1 = prefix1, len1: Int = len1,
prefix2: Arr2 = prefix2, len12: Int = len12,
prefix3: Arr3 = prefix3, len123: Int = len123,
prefix4: Arr4 = prefix4,
suffix3: Arr3 = suffix3, suffix2: Arr2 = suffix2, suffix1: Arr1 = suffix1,
length: Int = length) =
new NVector(prefix1, len1, prefix2, len12, prefix3, len123, prefix4, 0, null, 0, null, null, null, suffix3, suffix2, suffix1, length, 4)
@inline private[this] def copy3(prefix1: Arr1 = prefix1, len1: Int = len1,
prefix2: Arr2 = prefix2, len12: Int = len12,
prefix3: Arr3 = prefix3,
suffix2: Arr2 = suffix2, suffix1: Arr1 = suffix1,
length: Int = length) =
new NVector(prefix1, len1, prefix2, len12, prefix3, 0, null, 0, null, 0, null, null, null, null, suffix2, suffix1, length, 3)
@inline private[this] def copy2(prefix1: Arr1 = prefix1, len1: Int = len1,
prefix2: Arr2 = prefix2,
suffix1: Arr1 = suffix1,
length: Int = length) =
new NVector(prefix1, len1, prefix2, 0, null, 0, null, 0, null, 0, null, null, null, null, null, suffix1, length, 2)
@inline private[this] def copy1(prefix1: Arr1 = prefix1, length: Int = length) =
new NVector(prefix1, 0, null, 0, null, 0, null, 0, null, 0, null, null, null, null, null, prefix1, length, 1)
if(dimension == 1)
assert(prefix1.length == length)
override def iterableFactory: SeqFactory[NVector] = NVector
protected[this] def tinyAppendLimit: Int = 4 + vectorSliceCount
override final def appendedAll[B >: A](suffix: collection.IterableOnce[B]): NVector[B] = {
val k = suffix.knownSize
if(k == 0) this
else appendedAll0(suffix, k)
}
override def className = "NVector"
@inline override final def take(n: Int): NVector[A] = slice(0, n)
@inline override final def drop(n: Int): NVector[A] = slice(n, length)
@inline override final def takeRight(n: Int): NVector[A] = slice(length - mmax(n, 0), length)
@inline override final def dropRight(n: Int): NVector[A] = slice(0, length - mmax(n, 0))
override final def slice(from: Int, until: Int): NVector[A] = {
val lo = mmax(from, 0)
val hi = mmin(until, length)
val newlen = hi - lo
if(newlen == length) this
else if(newlen <= 0) NVector._empty
else slice0(lo, hi)
}
/** Dimension of the slice at index */
protected[immutable] final def vectorSliceDim(idx: Int): Int = {
val c = vectorSliceCount/2
c+1-abs(idx-c)
}
override def iterator: Iterator[A] = (dimension: @switch) match {
case 0 => Iterator.empty
case 1 => new ArrayOps.ArrayIterator(prefix1).asInstanceOf[Iterator[A]]
case _ => new NVectorIterator(this)
}
override def copyToArray[B >: A](xs: Array[B], start: Int, len: Int): Int = iterator.copyToArray(xs, start, len)
//override def toVector: Vector[A] = this
//override protected def applyPreferredMaxLength: Int = Vector.defaultApplyPreferredMaxLength
override def stepper[S <: Stepper[_]](implicit shape: StepperShape[A, S]): S with EfficientSplit = (dimension: @switch) match {
case 0 =>
empty1.stepper(shape.asInstanceOf[StepperShape[AnyRef, S]])
case 1 =>
prefix1.stepper(shape.asInstanceOf[StepperShape[AnyRef, S]])
case _ =>
import convert.impl._
val s = shape.shape match {
case StepperShape.IntShape => new IntNVectorStepper(new NVectorIterator(this).asInstanceOf[NVectorIterator[Int]])
case StepperShape.LongShape => new LongNVectorStepper(new NVectorIterator(this).asInstanceOf[NVectorIterator[Long]])
case StepperShape.DoubleShape => new DoubleNVectorStepper(new NVectorIterator(this).asInstanceOf[NVectorIterator[Double]])
case _ => shape.parUnbox(new AnyNVectorStepper[A](new NVectorIterator(this)))
}
s.asInstanceOf[S with EfficientSplit]
}
protected[this] def ioob(index: Int): IndexOutOfBoundsException =
if(length == 0) new IndexOutOfBoundsException(s"$index is out of bounds (empty vector)")
else new IndexOutOfBoundsException(s"$index is out of bounds (min 0, max ${length-1})")
override final def head: A =
try prefix1(0).asInstanceOf[A]
catch { case _: NullPointerException => throw ioob(0) }
override def equals(o: Any): Boolean = {
if(this eq o.asInstanceOf[AnyRef]) true
else if(length == 0 && o.isInstanceOf[NVector[_]]) false
else super.equals(o)
}
@inline def apply(index: Int): A = {
if(index < 0 || index >= length) throw ioob(index)
(dimension: @switch) match {
case 1 =>
prefix1(index)
case 2 =>
if(index >= len1) {
val io = index - len1
val i2 = io >>> BITS
val i1 = io & MASK
if(i2 < prefix2.length) prefix2(i2)(i1)
else suffix1(i1)
} else prefix1(index)
case 3 =>
if(index >= len12) {
val io = index - len12
val i3 = io >>> BITS2
val i2 = (io >>> BITS) & MASK
val i1 = io & MASK
if(i3 < prefix3.length) prefix3(i3)(i2)(i1)
else if(i2 < suffix2.length) suffix2(i2)(i1)
else suffix1(i1)
} else if(index >= len1) {
val io = index - len1
prefix2(io >>> BITS)(io & MASK)
} else prefix1(index)
case 4 =>
if(index >= len123) {
val io = index - len123
val i4 = io >>> BITS3
val i3 = (io >>> BITS2) & MASK
val i2 = (io >>> BITS) & MASK
val i1 = io & MASK
if(i4 < prefix4.length) prefix4(i4)(i3)(i2)(i1)
else if(i3 < suffix3.length) suffix3(i3)(i2)(i1)
else if(i2 < suffix2.length) suffix2(i2)(i1)
else suffix1(i1)
} else if(index >= len12) {
val io = index - len12
prefix3(io >>> BITS2)((io >>> BITS) & MASK)(io & MASK)
} else if(index >= len1) {
val io = index - len1
prefix2(io >>> BITS)(io & MASK)
} else prefix1(index)
case 5 =>
if(index >= len1234) {
val io = index - len1234
val i5 = io >>> BITS4
val i4 = (io >>> BITS3) & MASK
val i3 = (io >>> BITS2) & MASK
val i2 = (io >>> BITS) & MASK
val i1 = io & MASK
if(i5 < prefix5.length) prefix5(i5)(i4)(i3)(i2)(i1)
else if(i4 < suffix4.length) suffix4(i4)(i3)(i2)(i1)
else if(i3 < suffix3.length) suffix3(i3)(i2)(i1)
else if(i2 < suffix2.length) suffix2(i2)(i1)
else suffix1(i1)
} else if(index >= len123) {
val io = index - len123
prefix4(io >>> BITS3)((io >>> BITS2) & MASK)((io >>> BITS) & MASK)(io & MASK)
} else if(index >= len12) {
val io = index - len12
prefix3(io >>> BITS2)((io >>> BITS) & MASK)(io & MASK)
} else if(index >= len1) {
val io = index - len1
prefix2(io >>> BITS)(io & MASK)
} else prefix1(index)
case 6 =>
if(index >= len12345) {
val io = index - len12345
val i6 = io >>> BITS5
val i5 = (io >>> BITS4) & MASK
val i4 = (io >>> BITS3) & MASK
val i3 = (io >>> BITS2) & MASK
val i2 = (io >>> BITS) & MASK
val i1 = io & MASK
if(i6 < data6.length) data6(i6)(i5)(i4)(i3)(i2)(i1)
else if(i5 < suffix5.length) suffix5(i5)(i4)(i3)(i2)(i1)
else if(i4 < suffix4.length) suffix4(i4)(i3)(i2)(i1)
else if(i3 < suffix3.length) suffix3(i3)(i2)(i1)
else if(i2 < suffix2.length) suffix2(i2)(i1)
else suffix1(i1)
} else if(index >= len1234) {
val io = index - len1234
prefix5(io >>> BITS4)((io >>> BITS3) & MASK)((io >>> BITS2) & MASK)((io >>> BITS) & MASK)(io & MASK)
} else if(index >= len123) {
val io = index - len123
prefix4(io >>> BITS3)((io >>> BITS2) & MASK)((io >>> BITS) & MASK)(io & MASK)
} else if(index >= len12) {
val io = index - len12
prefix3(io >>> BITS2)((io >>> BITS) & MASK)(io & MASK)
} else if(index >= len1) {
val io = index - len1
prefix2(io >>> BITS)(io & MASK)
} else prefix1(index)
}
}.asInstanceOf[A]
override def updated[B >: A](index: Int, elem: B): NVector[B] = {
if(index < 0 || index >= length) throw ioob(index)
(dimension: @switch) match {
case 1 =>
copy1(prefix1 = copyUpdate(prefix1, index, elem))
case 2 =>
if(index >= len1) {
val io = index - len1
val i2 = io >>> BITS
val i1 = io & MASK
if(i2 < prefix2.length) copy2(prefix2 = copyUpdate(prefix2, i2, i1, elem))
else copy2(suffix1 = copyUpdate(suffix1, i1, elem))
} else {
copy2(prefix1 = copyUpdate(prefix1, index, elem))
}
case 3 =>
if(index >= len12) {
val io = index - len12
val i3 = io >>> BITS2
val i2 = (io >>> BITS) & MASK
val i1 = io & MASK
if (i3 < prefix3.length ) copy3(prefix3 = copyUpdate(prefix3, i3, i2, i1, elem))
else if(i2 < suffix2.length) copy3(suffix2 = copyUpdate(suffix2, i2, i1, elem))
else copy3(suffix1 = copyUpdate(suffix1, i1, elem))
} else if(index >= len1) {
val io = index - len1
copy3(prefix2 = copyUpdate(prefix2, io >>> BITS, io & MASK, elem))
} else {
copy3(prefix1 = copyUpdate(prefix1, index, elem))
}
case 4 =>
if(index >= len123) {
val io = index - len123
val i4 = io >>> BITS3
val i3 = (io >>> BITS2) & MASK
val i2 = (io >>> BITS) & MASK
val i1 = io & MASK
if (i4 < prefix4.length ) copy4(prefix4 = copyUpdate(prefix4, i4, i3, i2, i1, elem))
else if(i3 < suffix3.length) copy4(suffix3 = copyUpdate(suffix3, i3, i2, i1, elem))
else if(i2 < suffix2.length) copy4(suffix2 = copyUpdate(suffix2, i2, i1, elem))
else copy4(suffix1 = copyUpdate(suffix1, i1, elem))
} else if(index >= len12) {
val io = index - len12
copy4(prefix3 = copyUpdate(prefix3, io >>> BITS2, (io >>> BITS) & MASK, io & MASK, elem))
} else if(index >= len1) {
val io = index - len1
copy4(prefix2 = copyUpdate(prefix2, io >>> BITS, io & MASK, elem))
} else {
copy4(prefix1 = copyUpdate(prefix1, index, elem))
}
case 5 =>
if(index >= len1234) {
val io = index - len1234
val i5 = io >>> BITS4
val i4 = (io >>> BITS3) & MASK
val i3 = (io >>> BITS2) & MASK
val i2 = (io >>> BITS) & MASK
val i1 = io & MASK
if (i5 < prefix5.length ) copy5(prefix5 = copyUpdate(prefix5, i5, i4, i3, i2, i1, elem))
else if(i4 < suffix4.length) copy5(suffix4 = copyUpdate(suffix4, i4, i3, i2, i1, elem))
else if(i3 < suffix3.length) copy5(suffix3 = copyUpdate(suffix3, i3, i2, i1, elem))
else if(i2 < suffix2.length) copy5(suffix2 = copyUpdate(suffix2, i2, i1, elem))
else copy5(suffix1 = copyUpdate(suffix1, i1, elem))
} else if(index >= len123) {
val io = index - len123
copy5(prefix4 = copyUpdate(prefix4, io >>> BITS3, (io >>> BITS2) & MASK, (io >>> BITS) & MASK, io & MASK, elem))
} else if(index >= len12) {
val io = index - len12
copy5(prefix3 = copyUpdate(prefix3, io >>> BITS2, (io >>> BITS) & MASK, io & MASK, elem))
} else if(index >= len1) {
val io = index - len1
copy5(prefix2 = copyUpdate(prefix2, io >>> BITS, io & MASK, elem))
} else {
copy5(prefix1 = copyUpdate(prefix1, index, elem))
}
case 6 =>
if(index >= len12345) {
val io = index - len12345
val i6 = io >>> BITS5
val i5 = (io >>> BITS4) & MASK
val i4 = (io >>> BITS3) & MASK
val i3 = (io >>> BITS2) & MASK
val i2 = (io >>> BITS) & MASK
val i1 = io & MASK
if (i6 < data6.length ) copy6(data6 = copyUpdate(data6, i6, i5, i4, i3, i2, i1, elem))
else if(i5 < suffix5.length) copy6(suffix5 = copyUpdate(suffix5, i5, i4, i3, i2, i1, elem))
else if(i4 < suffix4.length) copy6(suffix4 = copyUpdate(suffix4, i4, i3, i2, i1, elem))
else if(i3 < suffix3.length) copy6(suffix3 = copyUpdate(suffix3, i3, i2, i1, elem))
else if(i2 < suffix2.length) copy6(suffix2 = copyUpdate(suffix2, i2, i1, elem))
else copy6(suffix1 = copyUpdate(suffix1, i1, elem))
} else if(index >= len1234) {
val io = index - len1234
copy6(prefix5 = copyUpdate(prefix5, io >>> BITS4, (io >>> BITS3) & MASK, (io >>> BITS2) & MASK, (io >>> BITS) & MASK, io & MASK, elem))
} else if(index >= len123) {
val io = index - len123
copy6(prefix4 = copyUpdate(prefix4, io >>> BITS3, (io >>> BITS2) & MASK, (io >>> BITS) & MASK, io & MASK, elem))
} else if(index >= len12) {
val io = index - len12
copy6(prefix3 = copyUpdate(prefix3, io >>> BITS2, (io >>> BITS) & MASK, io & MASK, elem))
} else if(index >= len1) {
val io = index - len1
copy6(prefix2 = copyUpdate(prefix2, io >>> BITS, io & MASK, elem))
} else {
copy6(prefix1 = copyUpdate(prefix1, index, elem))
}
}
}
override def appended[B >: A](elem: B): NVector[B] = {
(dimension: @switch) match {
case 0 =>
new NVector(wrap1(elem), 1)
case 1 =>
if(length < WIDTH) {
val a = copyOf(prefix1, length+1)
a(length) = elem.asInstanceOf[AnyRef]
new NVector(a, length+1)
} else new NVector(prefix1, WIDTH, empty2, wrap1(elem), WIDTH+1)
case 2 =>
if (suffix1.length < WIDTH ) copy2(suffix1 = copyAppend(suffix1, elem.asInstanceOf[AnyRef]), length = length+1)
else if(prefix2.length < WIDTH-2) copy2(prefix2 = copyAppend(prefix2, suffix1), suffix1 = wrap1(elem), length = length+1)
else new NVector(prefix1, len1, prefix2, WIDTH*(WIDTH-2) + len1, empty3, wrap2(suffix1), wrap1(elem), length+1)
case 3 =>
if (suffix1.length < WIDTH ) copy3(suffix1 = copyAppend(suffix1, elem.asInstanceOf[AnyRef]), length = length+1)
else if(suffix2.length < WIDTH-1) copy3(suffix2 = copyAppend(suffix2, suffix1), suffix1 = wrap1(elem), length = length+1)
else if(prefix3.length < WIDTH-2) copy3(prefix3 = copyAppend(prefix3, copyAppend(suffix2, suffix1)), suffix2 = empty2, suffix1 = wrap1(elem), length = length+1)
else new NVector(prefix1, len1, prefix2, len12, prefix3, (WIDTH-2)*WIDTH2 + len12, empty4, wrap3(copyAppend(suffix2, suffix1)), empty2, wrap1(elem), length+1)
case 4 =>
if (suffix1.length < WIDTH ) copy4(suffix1 = copyAppend(suffix1, elem.asInstanceOf[AnyRef]), length = length+1)
else if(suffix2.length < WIDTH-1) copy4(suffix2 = copyAppend(suffix2, suffix1), suffix1 = wrap1(elem), length = length+1)
else if(suffix3.length < WIDTH-1) copy4(suffix3 = copyAppend(suffix3, copyAppend(suffix2, suffix1)), suffix2 = empty2, suffix1 = wrap1(elem), length = length+1)
else if(prefix4.length < WIDTH-2) copy4(prefix4 = copyAppend(prefix4, copyAppend(suffix3, copyAppend(suffix2, suffix1))), suffix3 = empty3, suffix2 = empty2, suffix1 = wrap1(elem), length = length+1)
else new NVector(prefix1, len1, prefix2, len12, prefix3, len123, prefix4, (WIDTH-2)*WIDTH3 + len123, empty5, wrap4(copyAppend(suffix3, copyAppend(suffix2, suffix1))), empty3, empty2, wrap1(elem), length+1)
case 5 =>
if (suffix1.length < WIDTH ) copy5(suffix1 = copyAppend(suffix1, elem.asInstanceOf[AnyRef]), length = length+1)
else if(suffix2.length < WIDTH-1) copy5(suffix2 = copyAppend(suffix2, suffix1), suffix1 = wrap1(elem), length = length+1)
else if(suffix3.length < WIDTH-1) copy5(suffix3 = copyAppend(suffix3, copyAppend(suffix2, suffix1)), suffix2 = empty2, suffix1 = wrap1(elem), length = length+1)
else if(suffix4.length < WIDTH-1) copy5(suffix4 = copyAppend(suffix4, copyAppend(suffix3, copyAppend(suffix2, suffix1))), suffix3 = empty3, suffix2 = empty2, suffix1 = wrap1(elem), length = length+1)
else if(prefix5.length < WIDTH-2) copy5(prefix5 = copyAppend(prefix5, copyAppend(suffix4, copyAppend(suffix3, copyAppend(suffix2, suffix1)))), suffix4 = empty4, suffix3 = empty3, suffix2 = empty2, suffix1 = wrap1(elem), length = length+1)
else new NVector(prefix1, len1, prefix2, len12, prefix3, len123, prefix4, len1234, prefix5, (WIDTH-2)*WIDTH4 + len1234, empty6, wrap5(copyAppend(suffix4, copyAppend(suffix3, copyAppend(suffix2, suffix1)))), empty4, empty3, empty2, wrap1(elem), length+1)
case 6 =>
if (suffix1.length < WIDTH ) copy6(suffix1 = copyAppend(suffix1, elem.asInstanceOf[AnyRef]), length = length+1)
else if(suffix2.length < WIDTH-1 ) copy6(suffix2 = copyAppend(suffix2, suffix1), suffix1 = wrap1(elem), length = length+1)
else if(suffix3.length < WIDTH-1 ) copy6(suffix3 = copyAppend(suffix3, copyAppend(suffix2, suffix1)), suffix2 = empty2, suffix1 = wrap1(elem), length = length+1)
else if(suffix4.length < WIDTH-1 ) copy6(suffix4 = copyAppend(suffix4, copyAppend(suffix3, copyAppend(suffix2, suffix1))), suffix3 = empty3, suffix2 = empty2, suffix1 = wrap1(elem), length = length+1)
else if(suffix5.length < WIDTH-1 ) copy6(suffix5 = copyAppend(suffix5, copyAppend(suffix4, copyAppend(suffix3, copyAppend(suffix2, suffix1)))), suffix4 = empty4, suffix3 = empty3, suffix2 = empty2, suffix1 = wrap1(elem), length = length+1)
else if(data6.length < LASTWIDTH-2) copy6(data6 = copyAppend(data6, copyAppend(suffix5, copyAppend(suffix4, copyAppend(suffix3, copyAppend(suffix2, suffix1))))), suffix5 = empty5, suffix4 = empty4, suffix3 = empty3, suffix2 = empty2, suffix1 = wrap1(elem), length = length+1)
else throw new IllegalArgumentException
}
}
override def prepended[B >: A](elem: B): NVector[B] = {
(dimension: @switch) match {
case 0 =>
new NVector(wrap1(elem), 1)
case 1 =>
if(length < WIDTH) new NVector(copyPrepend1(elem, prefix1), length+1)
else new NVector(wrap1(elem), 1, empty2, prefix1, length+1)
case 2 =>
if (len1 < WIDTH ) copy2(copyPrepend1(elem, prefix1), len1+1, length = length+1)
else if(prefix2.length < WIDTH-2) copy2(wrap1(elem), 1, copyPrepend(prefix1, prefix2), length = length+1)
else new NVector(wrap1(elem), 1, wrap2(prefix1), len1+1, empty3, prefix2, suffix1, length+1)
case 3 =>
if (len1 < WIDTH ) copy3(prefix1 = copyPrepend1(elem, prefix1), len1 = len1+1, len12 = len12+1, length = length+1)
else if(len12 < WIDTH2 ) copy3(prefix1 = wrap1(elem), len1 = 1, prefix2 = copyPrepend(prefix1, prefix2), len12 = len12+1, length = length+1)
else if(prefix3.length < WIDTH-2) copy3(prefix1 = wrap1(elem), len1 = 1, prefix2 = empty2, len12 = 1, prefix3 = copyPrepend(copyPrepend(prefix1, prefix2), prefix3), length = length+1)
else new NVector(wrap1(elem), 1, empty2, 1, wrap3(copyPrepend(prefix1, prefix2)), len12+1, empty4, prefix3, suffix2, suffix1, length+1)
case 4 =>
if (len1 < WIDTH ) copy4(copyPrepend1(elem, prefix1), len1+1, len12 = len12+1, len123 = len123+1, length = length+1)
else if(len12 < WIDTH2 ) copy4(wrap1(elem), 1, copyPrepend(prefix1, prefix2), len12+1, len123 = len123+1, length = length+1)
else if(len123 < WIDTH3 ) copy4(wrap1(elem), 1, empty2, 1, copyPrepend(copyPrepend(prefix1, prefix2), prefix3), len123+1, length = length+1)
else if(prefix4.length < WIDTH-2) copy4(wrap1(elem), 1, empty2, 1, empty3, 1, copyPrepend(copyPrepend(copyPrepend(prefix1, prefix2), prefix3), prefix4), length = length+1)
else new NVector(wrap1(elem), 1, empty2, 1, empty3, 1, wrap4(copyPrepend(copyPrepend(prefix1, prefix2), prefix3)), len123+1, empty5, prefix4, suffix3, suffix2, suffix1, length+1)
case 5 =>
if (len1 < WIDTH ) copy5(copyPrepend1(elem, prefix1), len1+1, len12 = len12+1, len123 = len123+1, len1234 = len1234+1, length = length+1)
else if(len12 < WIDTH2 ) copy5(wrap1(elem), 1, copyPrepend(prefix1, prefix2), len12+1, len123 = len123+1, len1234 = len1234+1, length = length+1)
else if(len123 < WIDTH3 ) copy5(wrap1(elem), 1, empty2, 1, copyPrepend(copyPrepend(prefix1, prefix2), prefix3), len123+1, len1234 = len1234+1, length = length+1)
else if(len1234 < WIDTH4 ) copy5(wrap1(elem), 1, empty2, 1, empty3, 1, copyPrepend(copyPrepend(copyPrepend(prefix1, prefix2), prefix3), prefix4), len1234+1, length = length+1)
else if(prefix5.length < WIDTH-2) copy5(wrap1(elem), 1, empty2, 1, empty3, 1, empty4, 1, copyPrepend(copyPrepend(copyPrepend(copyPrepend(prefix1, prefix2), prefix3), prefix4), prefix5), length = length+1)
else new NVector(wrap1(elem), 1, empty2, 1, empty3, 1, empty4, 1, wrap5(copyPrepend(copyPrepend(copyPrepend(prefix1, prefix2), prefix3), prefix4)), len1234+1, empty6, prefix5, suffix4, suffix3, suffix2, suffix1, length+1)
case 6 =>
if (len1 < WIDTH ) copy6(copyPrepend1(elem, prefix1), len1+1, len12 = len12+1, len123 = len123+1, len1234 = len1234+1, len12345 = len12345+1, length = length+1)
else if(len12 < WIDTH2 ) copy6(wrap1(elem), 1, copyPrepend(prefix1, prefix2), len12+1, len123 = len123+1, len1234 = len1234+1, len12345 = len12345+1, length = length+1)
else if(len123 < WIDTH3 ) copy6(wrap1(elem), 1, empty2, 1, copyPrepend(copyPrepend(prefix1, prefix2), prefix3), len123+1, len1234 = len1234+1, len12345 = len12345+1, length = length+1)
else if(len1234 < WIDTH4 ) copy6(wrap1(elem), 1, empty2, 1, empty3, 1, copyPrepend(copyPrepend(copyPrepend(prefix1, prefix2), prefix3), prefix4), len1234+1, len12345 = len12345+1, length = length+1)
else if(len12345 < WIDTH5 ) copy6(wrap1(elem), 1, empty2, 1, empty3, 1, empty4, 1, copyPrepend(copyPrepend(copyPrepend(copyPrepend(prefix1, prefix2), prefix3), prefix4), prefix5), len12345+1, length = length+1)
else if(data6.length < LASTWIDTH-2) copy6(wrap1(elem), 1, empty2, 1, empty3, 1, empty4, 1, empty5, 1, copyPrepend(copyPrepend(copyPrepend(copyPrepend(copyPrepend(prefix1, prefix2), prefix3), prefix4), prefix5), data6), length = length+1)
else throw new IllegalArgumentException
}
}
override def foreach[U](f: A => U): Unit = {
if(dimension > 0) {
foreachElem(prefix1, f)
if(dimension > 1) {
foreachElem(prefix2, f)
if(dimension > 2) {
foreachElem(prefix3, f)
if(dimension > 3) {
foreachElem(prefix4, f)
if(dimension > 4) {
foreachElem(prefix5, f)
if(dimension > 5) {
foreachElem(data6, f)
foreachElem(suffix5, f)
}
foreachElem(suffix4, f)
}
foreachElem(suffix3, f)
}
foreachElem(suffix2, f)
}
foreachElem(suffix1, f)
}
}
}
override def map[B](f: A => B): NVector[B] = {
if(length == 0)
NVector._empty
else {
val p1 = mapElems1(prefix1, f)
copy(
prefix1 = mapElems1(prefix1, f),
prefix2 = if(prefix2 eq null) null else mapElems(2, prefix2, f),
prefix3 = if(prefix3 eq null) null else mapElems(3, prefix3, f),
prefix4 = if(prefix4 eq null) null else mapElems(4, prefix4, f),
prefix5 = if(prefix5 eq null) null else mapElems(5, prefix5, f),
data6 = if(data6 eq null) null else mapElems(6, data6, f),
suffix5 = if(suffix5 eq null) null else mapElems(5, suffix5, f),
suffix4 = if(suffix4 eq null) null else mapElems(4, suffix4, f),
suffix3 = if(suffix3 eq null) null else mapElems(3, suffix3, f),
suffix2 = if(suffix2 eq null) null else mapElems(2, suffix2, f),
suffix1 = if(prefix1 eq suffix1) p1 else mapElems1(suffix1, f)
)
}
}
override def last: A = suffix1(suffix1.length-1).asInstanceOf[A]
protected[this] def slice0(lo: Int, hi: Int): NVector[A] = {
if(dimension == 1)
copy1(copyOfRange(prefix1, lo, hi), hi-lo)
else {
val b = new NVectorSliceBuilder(lo, hi)
b.consider(1, prefix1)
b.consider(2, prefix2)
if(dimension > 2) {
b.consider(3, prefix3)
if(dimension > 3) {
b.consider(4, prefix4)
if(dimension > 4) {
b.consider(5, prefix5)
if(dimension > 5) {
b.consider(6, data6)
b.consider(5, suffix5)
}
b.consider(4, suffix4)
}
b.consider(3, suffix3)
}
b.consider(2, suffix2)
}
b.consider(1, suffix1)
b.result()
}
}
override def tail: NVector[A] = {
if(length == 1) NVector._empty
else if(length == 0) throw new UnsupportedOperationException("empty.tail")
else if(len1 > 1) copy(copyTail(prefix1), len1-1, len12 = len12-1, len123 = len123-1, len1234 = len1234-1, len12345 = len12345-1, length = length-1)
else slice0(1, length)
}
override def init: NVector[A] = {
if(length == 1) NVector._empty
else if(length == 0) throw new UnsupportedOperationException("empty.init")
else if(suffix1.length > 1) copy(suffix1 = copyInit(suffix1), length = length-1)
else slice0(0, length-1)
}
/** Number of slices */
protected[immutable] def vectorSliceCount: Int = mmax(dimension*2 - 1, 0)
/** Slice at index */
protected[immutable] def vectorSlice(idx: Int): Array[_ <: AnyRef] = {
(dimension: @switch) match {
case 1 =>
prefix1
case 2 =>
(idx: @switch) match {
case 0 => prefix1
case 1 => prefix2
case 2 => suffix1
}
case 3 =>
(idx: @switch) match {
case 0 => prefix1
case 1 => prefix2
case 2 => prefix3
case 3 => suffix2
case 4 => suffix1
}
case 4 =>
(idx: @switch) match {
case 0 => prefix1
case 1 => prefix2
case 2 => prefix3
case 3 => prefix4
case 4 => suffix3
case 5 => suffix2
case 6 => suffix1
}
case 5 =>
(idx: @switch) match {
case 0 => prefix1
case 1 => prefix2
case 2 => prefix3
case 3 => prefix4
case 4 => prefix5
case 5 => suffix4
case 6 => suffix3
case 7 => suffix2
case 8 => suffix1
}
case 6 =>
(idx: @switch) match {
case 0 => prefix1
case 1 => prefix2
case 2 => prefix3
case 3 => prefix4
case 4 => prefix5
case 5 => data6
case 6 => suffix5
case 7 => suffix4
case 8 => suffix3
case 9 => suffix2
case 10 => suffix1
}
}
}
/** Length of all slices up to and including index */
protected[immutable] def vectorSlicePrefixLength(idx: Int): Int = {
(dimension: @switch) match {
case 0 =>
0
case 1 =>
prefix1.length
case 2 =>
(idx: @switch) match {
case 0 => len1
case 1 => length - suffix1.length
case 2 => length
}
case 3 =>
(idx: @switch) match {
case 0 => len1
case 1 => len12
case 2 => len12 + prefix3.length*WIDTH2
case 3 => length - suffix1.length
case 4 => length
}
case 4 =>
(idx: @switch) match {
case 0 => len1
case 1 => len12
case 2 => len123
case 3 => len123 + prefix4.length*WIDTH3
case 4 => len123 + prefix4.length*WIDTH3 + suffix3.length*WIDTH2
case 5 => length - suffix1.length
case 6 => length
}
case 5 =>
(idx: @switch) match {
case 0 => len1
case 1 => len12
case 2 => len123
case 3 => len1234
case 4 => len1234 + prefix5.length*WIDTH4
case 5 => len1234 + prefix5.length*WIDTH4 + suffix4.length*WIDTH3
case 6 => len1234 + prefix5.length*WIDTH4 + suffix4.length*WIDTH3 + suffix3.length*WIDTH2
case 7 => length - suffix1.length
case 8 => length
}
case 6 =>
(idx: @switch) match {
case 0 => len1
case 1 => len12
case 2 => len123
case 3 => len1234
case 4 => len12345
case 5 => len12345 + data6.length*WIDTH5
case 6 => len12345 + data6.length*WIDTH5 + suffix5.length*WIDTH4
case 7 => len12345 + data6.length*WIDTH5 + suffix5.length*WIDTH4 + suffix4.length*WIDTH3
case 8 => len12345 + data6.length*WIDTH5 + suffix5.length*WIDTH4 + suffix4.length*WIDTH3 + suffix3.length*WIDTH2
case 9 => length - suffix1.length
case 10 => length
}
}
}
private[this] def appendedAll0[B >: A](suffix: collection.IterableOnce[B], k: Int): NVector[B] = {
if(length == 0)
NVector.from(suffix)
else {
val suffix1b = append1IfSpace(suffix1, suffix)
if(suffix1b ne null) {
if(dimension == 1)
copy(prefix1 = suffix1b, length = length-suffix1.length+suffix1b.length)
else
copy(suffix1 = suffix1b, length = length-suffix1.length+suffix1b.length)
}
else {
if(k > 0 && k < tinyAppendLimit) {
var v: NVector[B] = this
suffix match {
case it: Iterable[_] => it.foreach(x => v = v.appended(x))
case _ => suffix.iterator.foreach(x => v = v.appended(x))
}
v
} else new NVectorBuilder[B].initFrom(this).addAll(suffix).result()
}
}
}
}
/** Helper class for vector slicing. It is initialized with the validated start and end index,
* then the vector slices are added in succession with `consider`. No matter what the dimension
* of the originating vector is or where the cut is performed, this always results in a
* structure with the highest-dimensional data in the middle and fingers of decreasing dimension
* at both ends, which can be turned into a new vector with very little rebalancing.
*/
private[immutable] final class NVectorSliceBuilder(lo: Int, hi: Int) {
//println(s"***** NVectorSliceBuilder($lo, $hi)")
import NVectorStatics._
private[this] val slices = new Array[Array[AnyRef]](11)
private[this] var len, pos, maxDim = 0
@inline private[this] def prefixIdx(n: Int) = n-1
@inline private[this] def suffixIdx(n: Int) = 11-n
def consider[T <: AnyRef](n: Int, a: Array[T]): Unit = {
//println(s"***** consider($n, /${a.length})")
val count = a.length * (1 << (BITS*(n-1)))
val lo0 = mmax(lo-pos, 0)
val hi0 = mmin(hi-pos, count)
if(hi0 > lo0) addSlice(n, a, lo0, hi0)
pos += count
}
private[this] def addSlice[T <: AnyRef](n: Int, a: Array[T], lo: Int, hi: Int): Unit = {
//println(s"***** addSlice($n, /${a.length}, $lo, $hi)")
if(n == 1) {
add(1, copyOrUse(a, lo, hi))
} else {
val bitsN = BITS * (n-1)
var loN = lo >>> bitsN
var hiN = hi >>> bitsN
if(n != 6) {
loN &= MASK
hiN &= MASK
}
val loRest = lo & ((1 << bitsN) - 1)
val hiRest = hi & ((1 << bitsN) - 1)
//println(s"***** bitsN=$bitsN, loN=$loN, hiN=$hiN, loRest=$loRest, hiRest=$hiRest")
if(loRest == 0) {
if(hiRest == 0) {
add(n, copyOrUse(a, loN, hiN))
} else {
if(hiN > loN) add(n, copyOrUse(a, loN, hiN))
addSlice(n-1, a(hiN).asInstanceOf[Array[AnyRef]], 0, hiRest)
}
} else {
addSlice(n-1, a(loN).asInstanceOf[Array[AnyRef]], loRest, 1 << bitsN)
if(hiRest == 0) {
if(hiN > loN+1) add(n, copyOrUse(a, loN+1, hiN))
} else {
if(hiN-1 > loN+1) add(n, copyOrUse(a, loN+1, hiN-1))
addSlice(n-1, a(hiN-1).asInstanceOf[Array[AnyRef]], 0, hiRest)
}
}
}
}
private[this] def add[T <: AnyRef](n: Int, a: Array[T]): Unit = {
val idx =
if(n <= maxDim) suffixIdx(n)
else { maxDim = n; prefixIdx(n) }
slices(idx) = a.asInstanceOf[Array[AnyRef]]
len += (a.length * (1 << (BITS*(n-1))))
}
def result[A](): NVector[A] = {
if(len <= 32) {
if(len == 0) NVector._empty
else {
val prefix1 = slices(prefixIdx(1))
val suffix1 = slices(suffixIdx(1))
val a: Arr1 =
if(prefix1 ne null) {
if(suffix1 ne null) concatArrays(prefix1, suffix1)
else prefix1
} else if(suffix1 ne null) suffix1
else {
val prefix2 = slices(prefixIdx(2)).asInstanceOf[Arr2]
if(prefix2 ne null) prefix2(0)
else {
val suffix2 = slices(suffixIdx(2)).asInstanceOf[Arr2]
suffix2(0)
}
}
new NVector(a, len)
}
} else {
balancePrefix(1)
balanceSuffix(1)
var resultDim = maxDim
if(resultDim < 6) {
val pre = slices(prefixIdx(maxDim))
val suf = slices(suffixIdx(maxDim))
if((pre ne null) && (suf ne null)) {
// The highest-dimensional data consists of two slices: concatenate if they fit into the main data array,
// otherwise increase the dimension
if(pre.length + suf.length <= WIDTH-2) {
slices(prefixIdx(maxDim)) = concatArrays(pre, suf)
slices(suffixIdx(maxDim)) = null
} else resultDim += 1
} else {
// A single highest-dimensional slice could have length WIDTH-1 if it came from a prefix or suffix but we
// only allow WIDTH-2 for the main data, so increase the dimension in this case
val one = if(pre ne null) pre else suf
if(one.length > WIDTH-2) resultDim += 1
}
}
val prefix1 = slices(prefixIdx(1))
val suffix1 = slices(suffixIdx(1))
val len1 = prefix1.length
val res = (resultDim: @switch) match {
case 2 =>
val data2 = dataOr(2, empty2)
new NVector[A](prefix1, len1, data2, suffix1, len)
case 3 =>
val prefix2 = prefixOr(2, empty2)
val data3 = dataOr(3, empty3)
val suffix2 = suffixOr(2, empty2)
val len12 = len1 + (prefix2.length * WIDTH)
new NVector[A](prefix1, len1, prefix2, len12, data3, suffix2, suffix1, len)
case 4 =>
val prefix2 = prefixOr(2, empty2)
val prefix3 = prefixOr(3, empty3)
val data4 = dataOr(4, empty4)
val suffix3 = suffixOr(3, empty3)
val suffix2 = suffixOr(2, empty2)
val len12 = len1 + (prefix2.length * WIDTH)
val len123 = len12 + (prefix3.length * WIDTH2)
new NVector[A](prefix1, len1, prefix2, len12, prefix3, len123, data4, suffix3, suffix2, suffix1, len)
case 5 =>
val prefix2 = prefixOr(2, empty2)
val prefix3 = prefixOr(3, empty3)
val prefix4 = prefixOr(4, empty4)
val data5 = dataOr(5, empty5)
val suffix4 = suffixOr(4, empty4)
val suffix3 = suffixOr(3, empty3)
val suffix2 = suffixOr(2, empty2)
val len12 = len1 + (prefix2.length * WIDTH)
val len123 = len12 + (prefix3.length * WIDTH2)
val len1234 = len123 + (prefix4.length * WIDTH3)
new NVector[A](prefix1, len1, prefix2, len12, prefix3, len123, prefix4, len1234, data5, suffix4, suffix3, suffix2, suffix1, len)
case 6 =>
val prefix2 = prefixOr(2, empty2)
val prefix3 = prefixOr(3, empty3)
val prefix4 = prefixOr(4, empty4)
val prefix5 = prefixOr(5, empty5)
val data6 = dataOr(6, empty6)
val suffix5 = suffixOr(5, empty5)
val suffix4 = suffixOr(4, empty4)
val suffix3 = suffixOr(3, empty3)
val suffix2 = suffixOr(2, empty2)
val len12 = len1 + (prefix2.length * WIDTH)
val len123 = len12 + (prefix3.length * WIDTH2)
val len1234 = len123 + (prefix4.length * WIDTH3)
val len12345 = len1234 + (prefix5.length * WIDTH4)
new NVector[A](prefix1, len1, prefix2, len12, prefix3, len123, prefix4, len1234, prefix5, len12345, data6, suffix5, suffix4, suffix3, suffix2, suffix1, len)
}
res
}
}
@inline private[this] def prefixOr[T <: AnyRef](n: Int, a: Array[T]): Array[T] = {
val p = slices(prefixIdx(n))
if(p ne null) p.asInstanceOf[Array[T]] else a
}
@inline private[this] def suffixOr[T <: AnyRef](n: Int, a: Array[T]): Array[T] = {
val s = slices(suffixIdx(n))
if(s ne null) s.asInstanceOf[Array[T]] else a
}
@inline private[this] def dataOr[T <: AnyRef](n: Int, a: Array[T]): Array[T] = {
val p = slices(prefixIdx(n))
if(p ne null) p.asInstanceOf[Array[T]]
else {
val s = slices(suffixIdx(n))
if(s ne null) s.asInstanceOf[Array[T]] else a
}
}
/** Ensure prefix is not empty */
private[this] def balancePrefix(n: Int): Unit = {
if(slices(prefixIdx(n)) eq null) {
if(n == maxDim) {
slices(prefixIdx(n)) = slices(suffixIdx(n))
slices(suffixIdx(n)) = null
} else {
balancePrefix(n+1)
val preN1 = slices(prefixIdx(n+1)).asInstanceOf[Array[Array[AnyRef]]]
//assert(preN1 ne null)