-
Notifications
You must be signed in to change notification settings - Fork 1
/
Vector.scala
1214 lines (1107 loc) · 36.4 KB
/
Vector.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
/**
* Copyright © 2011 Phil Bagwell and Tiark Rompf. This is a prototype implementation, use at your own risk.
*/
package Vector
// Benchmark Version 09.09.2011
import Math._
class vec[T:ClassManifest]{
var root:AnyRef=null
val Width=8
val Invar=1 // sets min standard size for a slot ie w-invar
val Extras=2 // sets number of extra slots allowed, ie linear search limit
var t=1==0 // set to 1 for debug printing
var vSize=0 // size of vector
var vHw=0 // height of vector w, w^2, w^3, ...
var cost=0 // used for cost accounting not needed in final version
type Ara=Array[AnyRef]
type GTa=Array[T]
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
// Slices
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
def slice(left:Int,right:Int):vec[T]={
val s=sliceR(right)
//s.printTrie
s.sliceL(left)
}
def sliceR(right:Int):vec[T]={
if(t)println("SliceR")
if((right<vSize)&&(right>=0)&&(root!=null)){
val (n,hw)=rSliceDown2(root,right-1,vHw,false)
val nv = new vec[T]
nv.vSize=right
nv.root=n
nv.vHw=hw
nv
}
else this
}
def rSliceDown2(n:AnyRef,right:Int,hw:Int,hasLeft:Boolean):(AnyRef,Int)={
// Works but can be simplified
val sw=hw/Width
var is=right/sw
n match {
case in:Ara=>{
val len=in.length-1
if(in(0)==null){
// Aligned vector
val (rhn:AnyRef,hwr:Int)=rSliceDown2(in(is+1),right-is*sw,hw/Width,(is!=0)||hasLeft)
if(is==0){
if(hasLeft){
// has left above so return add level and return right node/height
val rcnodes=new Array[AnyRef](2)
cost+=2
rcnodes(1)=rhn
rcnodes(0)=null
(rcnodes,hw)
}
else (rhn,hwr) // nothing on left above so just return right node and height
}
else{
// Make copy of remaining left node
val cnodes=new Array[AnyRef](is+2)
cost+=is+2
for(i<-0 until is)cnodes(i+1)=in(i+1)
cnodes(is+1)=rhn
cnodes(0)=null
(cnodes,hw)
}
}
else{
val szs=in(0).asInstanceOf[Array[Int]]
var ix=right
while(szs(is)<=ix)is+=1
ix=ix-(if(is==0)0 else szs(is-1))
val nn=in.asInstanceOf[Ara](is+1)
val (rhn,hwr)=rSliceDown2(nn,ix,hw/Width,(is!=0)||hasLeft)
if(is==0){
if(hasLeft){
val rcnodes=new Array[AnyRef](2)
val rsizes=new Array[Int](1)
cost+=3
rcnodes(1)=rhn
rsizes(0)=right+1 //++++
rcnodes(0)=rsizes
(rcnodes,hw)
}
else (rhn,hwr) // nothing on left so return right node and height
}
else{
val cnodes=new Array[AnyRef](is+2)
val sizes=new Array[Int](is+1)
cost+=2*is+1
for(i<-0 until is){
cnodes(i+1)=in(i+1)
sizes(i)=szs(i)
}
cnodes(0)=sizes
sizes(is)=right+1
cnodes(is+1)=rhn
(cnodes,hw)
}
}
}
case vn:GTa=>{
// copy up to is
var lvals=new Array[T](is+1)
cost+=is+1
for(i<-0 to is)lvals(i)=vn(i)
(lvals,hw)
}
}
}
def sliceL(left:Int):vec[T]={
if(left>=vSize)new vec[T]
else if((left>0)&&(root!=null)){
val (n,hw)=lSliceDown2(root,left,vHw,false)
val nv = new vec[T]
nv.vSize=vSize-left
nv.root=n
nv.vHw=hw
nv
}
else this
}
// hasRight flags there are more slots to the right of this one
def lSliceDown2(n:AnyRef,left:Int,hw:Int,hasRight:Boolean):(AnyRef,Int)={
val sw=hw/Width
var is=left/sw
n match {
case in:Ara=>{
val len=in.length-1
val (inl,ist,ix)=if(in(0)!=null){
// is a sized node so find index position
if(t)println("A")
val szs=in(0).asInstanceOf[Array[Int]]
var ix=left
var it=is
while(szs(it)<=ix)it+=1
ix=ix-(if(it==0)0 else szs(it-1))
val nn=in.asInstanceOf[Ara](it+1)
(nn,it,ix)
}
else(in(is+1),is,left-is*sw)
if(t)println("B ist,ix",ist,ix)
val lastslt=len-1
val (lhn,hwr)=lSliceDown2(inl,ix,hw/Width,(ist!=lastslt)||hasRight)
if(ist==lastslt){ // no more slots to left
if(hasRight){
if(t)println("C")
val rcnodes=new Array[AnyRef](2)
cost+=2
rcnodes(1)=lhn
rcnodes(0)=null
(rcnodes,hw)
}
else (lhn,hwr) // nothing on left so return right node and height
}
else{
// has slots on left so copy them across
if(t)println("D len,ist",len,ist)
val cnodes=new Array[AnyRef](len-ist+1)
for(i<-0 until len-ist-1)cnodes(i+2)=in(ist+2+i)
val szs=in(0).asInstanceOf[Array[Int]]
val rsizes=new Array[Int](len-ist)
cost+=2*(len-ist)+1
for(i<-0 until len-ist){
val sz=if(in(0)!=null)szs(ist+i) else sw*(ist+1+i)
rsizes(i)=sz-left
}
cnodes(0)=rsizes
cnodes(1)=lhn
(cnodes,hw)
}
}
case vn:GTa=>{
// copy from is to end
val lenv=vn.length
if(t)println("G lenv,is",lenv,is)
var lvals=new Array[T](lenv-is)
cost+=lenv-is
for(i<-is until lenv)lvals(i-is)=vn(i)
(lvals,hw)
}
}
}
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
// Concatenation
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
def conca (b:vec[T]):vec[T]={
cost=0
if(t)println("conca")
if(vSize==0)b
else if(b.vSize==0)this
else{
/* not needed for debug only
val hwl=findhw(root)
val hwr=findhw(b.root)
if((hwl!=vHw)||(hwr!=b.vHw))println("HW Err ************",hwl,vHw,hwr,b.vHw)
*/
if(t)println("CST hwl,hwr",vHw,vSize,b.vHw,b.vSize)
val tnca=concatSubTree(root,vHw,b.root,b.vHw,true)
// create new vector
val nvec=new vec[T]
nvec.root=if((vHw==Width)&&(b.vHw==Width)&&(vSize+b.vSize<=Width))
tnca.asInstanceOf[Ara](1)
else setSizes(tnca,max(vHw,b.vHw))
nvec.vSize=vSize+b.vSize
nvec.vHw=findhw(nvec.root) // should be able to avoid this extra tree traversal
if(t)println("Conca - E vHw",nvec.vHw)
nvec.cost=cost
nvec
}
}
// IsTop
def concatSubTree(til:AnyRef,hwl:Int,tir:AnyRef,hwr:Int,isTop:Boolean):Ara={
if(t)println("CST hwl,hwr",hwl,hwr)
if(hwl>hwr){
// left vector higher than right
if(t)println("CST hwl>hwr")
val tnla=til.asInstanceOf[Ara]
val tnca=concatSubTree(tnla(tnla.length-1),hwl/Width,tir,hwr,false)
if(t)println("CST RB hwl>hwr")
rebalance(tnla,tnca,null,hwl,isTop)
}
else if(hwl<hwr){
// right vector higher than left
if(t)println("CST hwl<hwr")
val tnra=tir.asInstanceOf[Ara]
val tnca=concatSubTree(til,hwl,tnra(1),hwr/Width,false)
if(t)println("CST RB hwl<hwr")
rebalance(null,tnca,tnra,hwr,isTop)
}
else if(hwl==Width){
// height = w so at bottom
val gnla=til.asInstanceOf[GTa]
val gnra=tir.asInstanceOf[GTa]
val lenl=gnla.length
val lenr=gnra.length
if(t)println("CST G hwl==Width lenl,lenr",lenl,lenr)
if(isTop&&(lenr+lenl<=Width)){
// sum of two less than w so copy into one if at top of vectors
val gal=new GTa(lenr+lenl)
for(i<-0 until lenl)gal(i)=gnla(i)
for(i<-0 until lenr)gal(i+lenl)=gnra(i)
val na=new Ara(2)
na(0)=null;na(1)=gal
cost+=lenl+lenr+2
na
}
else{
if(t)println("CST G B")
// else simply return the two subtrees as they will be balance
// at the next level up
val na=new Ara(3);na(0)=null
cost+=3
na(1)=til
na(2)=tir
na
}
}
else{
// two heights the same so move down both
if(t)println("CST hwl==hwr ")
val tnla=til.asInstanceOf[Ara]
val tnra=tir.asInstanceOf[Ara]
val tnca=concatSubTree(tnla(tnla.length-1),hwl/Width,tnra(1),hwr/Width,false)
if(t)println("CST hwl==hwr -------------------------")
rebalance(tnla,tnca,tnra,hwl,isTop)
}
}
def rebalance(al:Ara,ac:Ara,ar:Ara,hw:Int,IsTop:Boolean):Ara={
// Put all the slots at this level in one array ++Note: This can be avoided by indexing the sub arrays as one
var allx=0
// remember Ara(0) is Size
val lenl=if(al!=null)al.length-2 else 0
val lenc=if(ac!=null)ac.length-1 else 0
val lenr=if(ar!=null)ar.length-2 else 0
val all=new Ara(lenl+lenc+lenr)
if(t)println("RB A lenl,lenc,lenr",lenl,lenc,lenr)
if(lenl>0){for(i<-0 until lenl)all(i)=al(i+1);allx+=lenl}
for(i<-0 until lenc)all(i+allx)=ac(i+1);allx+=lenc
if(lenr>0){for(i<-0 until lenr)all(i+allx)=ar(i+2)}
// shuffle slot sizes to fit invariant
val (szs,slen)=shuffle(all)
// Now copy across according to model sizes in szs
val nall=copyAcross(all,szs,slen,hw)
// split across two nodes if greater than Width
// This splitting/copying can be avoided by moving this logic into the copyAcross
// and only creating one or two arrays as needed.
val ra=if(slen<=Width){
if(t)println("E IsTop",IsTop)
if(!IsTop){
if(t)println("H")
val na=new Ara(2)
cost+=2
na(0)=null
na(1)=setSizes(nall,hw)
na
}
else nall
}
else{
if(t)println("F")
val nal=new Ara(Width+1)
val nar=new Ara(slen-Width+1)
for(i<-0 until Width)nal(i+1)=nall(i+1)
for(i<-0 until slen-Width)nar(i+1)=nall(i+Width+1)
val na=new Ara(3)
cost+=3
na(0)=null
na(1)=setSizes(nal,hw)
na(2)= setSizes(nar,hw)
na
}
cost+=slen
if(t)println("EX RB")
ra
}
// returns an array with the desired slot sizes.
// This version allows an Extra number of slots however many slots.
// if the slots are less than w then as many as Extra+1 could be small
// while if the total number of slots at the level are as great as 2w
// then still only Extra can be small
def shuffle(all:Ara):(Array[Int],Int)={
val alen=all.length
val szs=new Array[Int](alen)
cost+=alen
var tcnt=0
// find total slots in the two levels.
for(i<- 0 until alen){
val sz=sizeSlot(all(i))
szs(i)=sz
tcnt+=sz
}
if(t)for(i<- 0 until alen)println("SHFS",i,szs(i))
// Calculate the ideal or effective number slots
// used to limit number of extra slots.
val effslt=tcnt/Width + 1
var nalen=alen
// note - this makes multiple passes, can be done in one.
// redistribute the smallest slots until only the allowed extras remain
while(Extras<nalen-effslt){
var ix=0
// skip over any already greater than w-Invar
while(szs(ix)>Width-Invar)ix+=1
var el=szs(ix)
if(t)println("SHTB el,ix",el,ix)
// Found a short one so redistribute over following ones
do{
val msz=if(szs(ix+1)+el>Width)Width else el+szs(ix+1)
szs(ix)=msz
el=el+szs(ix+1)-msz
if(t)println("SHTC el,ix,msz",el,ix,msz)
ix+=1
}while(el>0)
if(t)println("SHTC el,ix",el,ix)
// shuffle up remaining slot sizes
for(i<- ix+1 until nalen)szs(i-1)=szs(i)
nalen-=1
}
if(t)for(i<- 0 until nalen-1)println("SHFD",i,szs(i))
(szs,nalen)
}
// Takes the slot size model and copies across slots to match it.
def copyAcross(all:Ara,szs:Array[Int],slen:Int,hw:Int):Ara={
val nall=new Ara(slen+1)
var ix=0 // index into the all input array
var offset=0 // offset into an individual slot array.
// It points to the next sub tree in the array to be copied
for(i<- 0 until slen){
if(t)println("RB CA ix,offset,szs(i)",ix,offset,szs(i))
val nsize = szs(i)
if(t)println("CA Beg ix,offset,nsize",ix,offset,nsize)
val (ae,asIs)=all(ix) match{
case a:Ara=>if((offset==0)&&(nsize==a.length-1))(a,true) else (a,false)
case g:GTa=>if((offset==0)&&(nsize==g.length))(g,true) else (g,false)
}
val ra=if(asIs){ix+=1;ae}
else{
var rta:AnyRef=null
var fillcnt=0
var offs=offset
var nix=ix
var ga:GTa=null
var aa:Ara=null
// collect enough slots together to match the size needed
while((fillcnt<nsize)&&(nix<all.length)){
if(t)println("CA fillcnt,nix,offset,nsize",fillcnt,nix,offs,nsize)
all(nix) match{
case aaa:Ara=>{
aa=if(fillcnt==0) new Ara(nsize+1) else aa
val lena=aaa.length-1
if(t)println("CA Ara lena,fillcnt,offset",lena,fillcnt,offs)
if(nsize-fillcnt>=lena-offs){
for(i<-0 until lena-offs)aa(i+fillcnt+1)=aaa(i+offs+1)
if(t)println("YY")
nix+=1
fillcnt+=lena-offs
offs=0
}
else{
for(i<-0 until nsize-fillcnt)aa(i+fillcnt+1)=aaa(i+offs+1)
if(t)println("YZ")
offs+=nsize-fillcnt
fillcnt=nsize
}
rta=aa
}
case gaa:GTa=>{
ga=if(fillcnt==0) new GTa(nsize) else ga
val lena=gaa.length
if(t)println("CA G lena,fillcnt,offset",lena,fillcnt,offs)
if(nsize-fillcnt>=lena-offs){
if(t)println("CA ZA lena,nsize,fillcnt,offs,galen",lena,nsize,fillcnt,offs,ga.length)
for(i<-0 until lena-offs)ga(i+fillcnt)=gaa(i+offs)
if(t)println("ZY")
fillcnt+=lena-offs
nix+=1
offs=0
}
else{
if(t)println("CA ZB lena,nsize,fillcnt,offs,galen",lena,nsize,fillcnt,offs,ga.length)
for(i<-0 until nsize-fillcnt)ga(i+fillcnt)=gaa(i+offs)
if(t)println("ZZ")
offs+=nsize-fillcnt
fillcnt=nsize
}
rta=ga
}
}
}
cost+=nsize
rta=rta match{
case a:Ara=>setSizes(a,hw/Width)
case g:GTa=>g
}
ix=nix
offset=offs
rta
}
nall(i+1)=ra
}
nall
}
// Finds the sizes of all the sub trees
// Note ** sizes of subtrie should be passed up the tree rather than calculated as an extra traversal
def setSizes(a:Ara,hw:Int)={
var sigma=0
val lena=a.length-1
val szs=new Array[Int](lena)
cost+=lena
for(i<- 0 until lena){
sigma+=sizeSubTrie(a(i+1),hw/Width)
if(t)println("i,lena,sigma",i,lena,sigma)
szs(i)=sigma
}
a(0)=szs
a
}
// Find the size of one array
def sizeSlot(a:AnyRef)={
if(a==null){println("sizeSlot NULL");0}
else{
a match {
case aa:Ara=>aa.length-1 // allow for size in aa(0)
case at:GTa=>at.length
}
}
}
// Finds number of extra slots above those normally needed
// to represent size
def findExtras(inl:Ara,slotsz:Int):(Int)={
if(inl(0)!=null){
val inli=inl(0).asInstanceOf[Array[Int]]
val lenl=inli.length
val bndry=inli(lenl-1)
lenl-((bndry-1)/slotsz)-1
}
else 0
}
/*
def equalizeHeight(nl:AnyRef,hwl:Int,hwr:Int,sz:Int):AnyRef={
var thwl=hwl
var n=nl
val hasSizes=nl match{
case in:Ara=>in(0)!=null
case _ => false
}
while(thwl<hwr){
var cnodes=new Ara(2)
var sizes=if(hasSizes){
val ns=new Array[Int](1)
cost+=3
ns(0)=sz
ns
}
else null
cnodes(0)=sizes
cnodes(1)=n
n=cnodes
thwl*=Width
}
n
}
*/
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
// Sizes and heights
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
def sizeTrie:Int=vSize // Returns size (length) of vector
//def sizeTrie(tn:AnyRef):Int={sizeSubTrie(tn,heightWeight(tn))}
def sizeSubTrie(tn:AnyRef,hw:Int):Int={
tn match {
case in:Ara=>{
if(in(0)==null){
val len=in.length
val sltsz=hw/Width
sltsz*(len-2)+sizeSubTrie(in(len-1),sltsz)
}
else {
val sn=in(0).asInstanceOf[Array[Int]]
sn(sn.length-1)
}
}
case vn:GTa=>vn.length
}
}
/*
// Finds height as a weight ie Width^h
def heightWeight(n:AnyRef):Int={
var hw=Width
var ni=n
while(ni match{
case in:Ara=>{ni=in(1);true}
case vn:GTa=>false
}
)hw*=Width
hw
}
*/
def findhw(n:AnyRef):Int={
n match{
case a:Ara=>findhw(a(1))*Width
case g:GTa=>Width
}
}
// ************************************************
// All methods before this point needed for concat and slice functionality.
// Index is also needed which is at the end of the file.
// ************************************************
// The following methods implement utilities for constructing test vectors
// They are not the for final implementation
// ************************************************
// Find the height of a Tree
// ************************************************
/*
def XheightCheck(){
println("hCkc")
hChk(root,vHw)
}
def XhChk(n:AnyRef,hw:Int){
n match{
case a:Ara=>for(i<- 0 until a.length-2)hChk(a(i+1),hw/Width)
case g:GTa=>if(hw!=Width)println("HW Err")
}
}
*/
// returns the top weight for a given size
def sztohw(sz:Int)={
var hw=Width
while(sz>hw)hw*=Width
hw
}
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
// Add Item
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
def +(value:T)={
val nvec=new vec[T]
val hw=sztohw(vSize)
nvec.vSize=vSize+1
nvec.root=if (root==null){
var a=new Array[T](1)
a(0)=value
nvec.vHw=Width
a
}
else {
val (n,replace)=addVal(root,value,vHw)
if(replace){nvec.vHw=vHw;n}
else{
val a=new Array[AnyRef](3)
a(0)=null
a(1)=root
a(2)=n
nvec.vHw=vHw*Width
a
}
}
nvec
}
def addVal(n:AnyRef,value:T,hw:Int):(AnyRef,Boolean)={
n match {
case na:Array[AnyRef]=>{
val lenn=na.length
val (n,replace)=addVal(na(lenn-1),value,hw/Width)
if(replace){
val ta=new Array[AnyRef](lenn)
for(i<- 0 until lenn-1)ta(i)=na(i)
na(lenn-1)=n
(na,true)
}
else if(lenn<=Width){
val ta=new Array[AnyRef](lenn+1)
for(i<- 0 until lenn)ta(i)=na(i)
ta(lenn)=n
(ta,true)
}
else{
val ta=new Array[AnyRef](2)
ta(0)=null
ta(1)=n
(ta,false)
}
}
case nt:Array[T]=>{
if(nt.length<Width){
val la=nt.length
var a=new Array[T](la+1)
for(i<-0 until la)a(i)=nt(i)
a(la)=value
(a,true)
}
else {
val b=new Array[T](1)
b(0)=value
(b,false)
}
}
}
}
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
// Display Trie - For Testing and Debugging
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
def printTrie{
if(t)println("PrintTrie")
// var hw=Width
// while(vSize>hw)hw*=Width
// println(vSize,hw,vSize)
printSubTrie(root,0,vHw,vSize)
}
def printSubTrie(n:AnyRef,lev:Int,hw:Int,rhs:Int){
//if(t)println("PrintSubTrie")
if(n==null)println("null")
else{
var sp=lev.toString + " "
for(s<- 0 to lev)sp= sp + " "
n match {
case an:Array[AnyRef]=>{
val ilen=an.length
if(an(0)==null)sp+="***"
for(i<- 1 until ilen){
val bdry=i*hw/Width
val sz=if(an(0)==null)min(bdry,rhs)
else {
val sza=an(0)match {case as:Array[Int]=>as}
sza(i-1)
}
sp= sp + " s " + sz.toString
}
println(sp)
for(i<- 1 until ilen){
val bdry=i*hw/Width
val nrhs=if(bdry<rhs)bdry else rhs-bdry
printSubTrie(an(i),lev+1,hw/Width,nrhs)
}
}
case vn:Array[T]=>{
val vlen=vn.length
for(i<- 0 until vlen){
sp=sp + " v " + vn(i).toString
}
println(sp)
}
}
}
}
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
// These two method used for tests only not required in final version
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
// Testing & Debugging support
def slotCount():(Int,Int,Int,Int,Int,Int,Int)={
if(root==null)(0,0,0,0,0,0,0)
else subcnt(root,1,sztohw(vSize))
}
// Testing & Debugging support
def subcnt(n:AnyRef,lev:Int,hw:Int):(Int,Int,Int,Int,Int,Int,Int)={
var maxslot=0
var totnodes=0
var totslots=0
var totAslots=0
var totxtr=0
var maxxtr=0
var maxl=0
n match {
case in:Ara=>{
val len=in.length-1
totxtr=findExtras(in,hw/Width)
maxxtr=totxtr
totAslots=1
for(i<- 1 to len){
val (mxsl,totn,tots,totAs,mxl,totx,maxx)=subcnt(in(i),lev+1,hw/Width)
maxslot=max(maxslot,mxsl)
maxslot=max(maxslot,len)
maxl=max(maxl,mxl)
maxxtr=max(maxxtr,maxx)
totnodes+=totn
totslots+=tots
totxtr+=totx
totAslots+=totAs
}
(maxslot,totnodes+1,totslots+len,totAslots,maxl,totxtr,maxxtr)
}
case vn:GTa=>{
(vn.length,1,vn.length,0,lev,0,0)
}
}
}
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
// Update to an Item
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
def update (index:Int,value:T):vec[T]={
if((index<0)||(index>=vSize)||(root==null))this
else {
val hw=sztohw(vSize)
val nvec=new vec[T]
nvec.root=updateTrie(root,index,value,vHw)
nvec.vSize=vSize
nvec.vHw=vHw
nvec
}
}
def updateTrie(n:AnyRef,ix:Int,value:T,hw:Int):AnyRef={
val sw=hw/Width
var is=ix/sw
n match {
case in:Ara=>{
val subn=if(in(0)==null)updateTrie(in(is),ix-is*sw,value,hw/Width)
else{
val szs=in(0).asInstanceOf[Array[Int]]
while(szs(is)<=ix)is+=1
val nix=ix-(if(is==0)0 else szs(is-1))
updateTrie(in(is),nix,value,hw/Width)
}
val len=in.length-1
val cnodes=new Array[AnyRef](len+1)
for(i<- 0 to len)cnodes(i)=in(i)
cnodes(is+1)=subn
cnodes
}
case vn:GTa=>{
val len=vn.length
val lvals=new Array[T](len)
for(i<- 0 until len)lvals(i)=vn(i)
lvals(is)=value
lvals
}
}
}
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
// Index to an Item
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
val S5=1<<5;val S10=1<<10;val S15=1<<15;val S20=1<<20;val S25=1<<25;val S30=1<<30
// Index method optimised for regular vectors
def index(index:Int):T={
if(Width!=32)indexAll(index) // Only used for test purposes with vectors not based on 32.
else{ // Can be removed for vector implementation
var ix=index
def sized(ia:AnyRef,sp:Int):AnyRef={
val szs=ia.asInstanceOf[Ara](0).asInstanceOf[Array[Int]]
var is=ix>>sp
while(szs(is)<=ix)is+=1
ix=ix-(if(is==0)0 else szs(is-1))
ia.asInstanceOf[Ara](is+1)
/*
// To eveluate bin or linear search use commented code below here
var l = 0
var r=szs.length-1
var m=r/2
while(r-l>1){
if(szs(m)>ix)r=m
else l=m
m=(r+l)/2
}
val ps=if(ix<szs(l))l else r
ix=ix-(if(ps==0)0 else szs(ps-1))
ia.asInstanceOf[Ara](ps+1)
*/
}
if((ix<0)||(ix>=vSize))throw new IndexOutOfBoundsException(ix.toString)
else{
vHw match {
case S5 => root.asInstanceOf[GTa](ix)
case S10 =>{
val n1=if(root.asInstanceOf[Ara](0)==null)root.asInstanceOf[Ara]((ix>>5)+1) else sized(root,5)
n1.asInstanceOf[GTa](ix&31)
}
case S15 =>{
val n1=if(root.asInstanceOf[Ara](0)==null)root.asInstanceOf[Ara]((ix>>10)+1) else sized(root,10)
val n2=if(n1.asInstanceOf[Ara](0)==null)n1.asInstanceOf[Ara](((ix>>5)&31)+1) else sized(n1,5)
n2.asInstanceOf[GTa](ix&31)
}
case S20 =>{
val n1=if(root.asInstanceOf[Ara](0)==null)root.asInstanceOf[Ara]((ix>>15)+1) else sized(root,15)
val n2=if(n1.asInstanceOf[Ara](0)==null)n1.asInstanceOf[Ara](((ix>>10)&31)+1) else sized(n1,10)
val n3=if(n2.asInstanceOf[Ara](0)==null)n2.asInstanceOf[Ara](((ix>>5)&31)+1) else sized(n2,5)
n3.asInstanceOf[GTa](ix&31)
}
case S25 =>{
val n1=if(root.asInstanceOf[Ara](0)==null)root.asInstanceOf[Ara]((ix>>20)+1) else sized(root,20)
val n2=if(n1.asInstanceOf[Ara](0)==null)n1.asInstanceOf[Ara](((ix>>15)&31)+1) else sized(n1,15)
val n3=if(n2.asInstanceOf[Ara](0)==null)n2.asInstanceOf[Ara](((ix>>10)&31)+1) else sized(n2,10)
val n4=if(n3.asInstanceOf[Ara](0)==null)n3.asInstanceOf[Ara](((ix>>5)&31)+1) else sized(n3,5)
n4.asInstanceOf[GTa](ix&31)
}
case S30 =>{
val n1=if(root.asInstanceOf[Ara](0)==null)root.asInstanceOf[Ara]((ix>>25)+1) else sized(root,25)
val n2=if(n1.asInstanceOf[Ara](0)==null)n1.asInstanceOf[Ara](((ix>>20)&31)+1) else sized(n1,20)
val n3=if(n2.asInstanceOf[Ara](0)==null)n2.asInstanceOf[Ara](((ix>>15)&31)+1) else sized(n2,15)
val n4=if(n3.asInstanceOf[Ara](0)==null)n3.asInstanceOf[Ara](((ix>>10)&31)+1) else sized(n3,10)
val n5=if(n4.asInstanceOf[Ara](0)==null)n4.asInstanceOf[Ara](((ix>>5)&31)+1) else sized(n4,5)
n5.asInstanceOf[GTa](ix&31)
}
case _ => throw new IllegalArgumentException()
}
}
}
}
// This method Only used to provide indexing for all Widths not just 32. Not needed in the final version
// Used here for test purposes and to support display with testing for other values of Width.
def indexAll(index:Int):T={
if((index<0)||(index>=vSize))throw new IndexOutOfBoundsException(index.toString)
var n=root
var hw=vHw
var is=0
//println("IX",index,hw)
var ix=index
var vnv:GTa=null
while(n!=null){
val sw=hw/Width
is=ix/sw
n match {
case in:Ara=>{
val len=in.length-1
if(len<=is)n=null
else{
if(in(0)==null){n=in(is+1);ix=ix-is*sw}
else{
val szs=in(0).asInstanceOf[Array[Int]]
// To eveluate bin or linear search use commented code below here
/*
var l=0
var r=len-1
var m=r/2
while(r-l>1){
if(szs(m)>ix)r=m
else l=m
m=(r+l)/2
}
val ps=if(ix<szs(l))l else r
n=in(ps+1);ix=ix-(if(ps==0)0 else szs(ps-1))
*/
// Radix search of level
while(szs(is)<=ix)is+=1
ix=ix-(if(is==0)0 else szs(is-1))
n=in(is+1)
// End Direct index Search
}
}
}
case vn:GTa=>{
val len=vn.length
if(len>is)vnv=vn
n=null
}
}
hw/=Width
}
vnv(is)
}
}
/*
Test code for alternative indexing implementations
// Pure vector no test for RRM-Trees
vHw match {
case S5 => root.asInstanceOf[GTa](ix)
case S10 =>{
val n1=root.asInstanceOf[Ara]((ix>>5)+1)
n1.asInstanceOf[GTa](ix&31)
}
case S15 =>{
val n1=root.asInstanceOf[Ara]((ix>>10)+1)
val n2=n1.asInstanceOf[Ara](((ix>>5)&31)+1)
n2.asInstanceOf[GTa](ix&31)
}
case S20 =>{
val n1=root.asInstanceOf[Ara]((ix>>15)+1)
val n2=n1.asInstanceOf[Ara](((ix>>10)&31)+1)
val n3=n2.asInstanceOf[Ara](((ix>>5)&31)+1)
n3.asInstanceOf[GTa](ix&31)