-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
TList.java
3565 lines (3147 loc) · 85.8 KB
/
TList.java
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
/**
* Created on Jul 24, 2005
*/
package me.nallar.tickthreading.collections;
import java.util.AbstractList;
import java.util.Arrays;
import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import java.util.Random;
import java.util.RandomAccess;
/**
* This List implementation offers an alternative to the standard Java
* LinkedList and ArrayList implementations. A full binary tree structure is
* used to support the list with list elements being stored in the leaves of the
* tree in inorder. Both sequential(iterator) and indexed access to elements is
* supported and all indexed accesses whether read-only (get(int)) or update
* (add(int,Object), remove(int)) are achieved in O(lg(n)) time provided the
* underlying tree structure remains balanced. A public method, rebuild(), is
* provided to allow the programmer to re-balance the data structure whenever
* desired. Automatic rebalancing may be enabled by coding setMode(NORMAL). When
* NORMAL is set a heuristic is employed to automatically check if a rebuild is
* needed after each add/remove operation and, if needed, to launch a rebuild.
* Since rebuilding is an O(n) operation the heuristic avoids rebuilding too
* frequently. At times if a series of add/remove operations only infrequently
* updates the same or adjacent locations then rebuilding is counter-productive
* because the underlying data structure tends to remain well balanced. In this
* case setMode(RANDOM) can be coded. In cases where RANDOM mode is appropriate
* it is dramatically faster than either LinkedList or ArrayList except for very
* small list sizes. In NORMAL mode this List implementation outperforms both
* LinkedList and ArrayList for large list sizes. Enhancements were added in May
* 2009 which boost TList performance to about 8 times that of LinkList and
* ArrayList. TLists are not particularly sensitive to where elements are added
* except that if a large number of elements are added to the same position or
* to adjacent positions then a rebuild my be required to achieve maximal
* efficiency. TLists are recommended for large lists where access is random or
* non-random. Be assured that iteration through a TList is very efficient. Two
* additional methods, split and splice, are also of note. The former splits a
* given TList into 2 TLists while the latter splices one TList into another.
* Splice is similar to addALL but has been optimized for the case when the
* input collection is a TList and is much faster than addAll.
* <p/>
* **Note** The rebuild() method in this implementation rebuilds the underlying
* full binary tree in such a way that at each node in the tree the number of
* leaves in the left subtree differs from the number of leaves in the right
* subtree by at most +1. Thus no part of the tree will degenerate into what is,
* essentially, a linked list (a performance killer!). Concentrated adding of
* elements to a single or small group of adjacent locations causes just such a
* degeneration of the tree into a linked list. In May 2009 we enhanced the add,
* addFirst and addLast methods, when the mode is NORMAL, to employ what we call
* local randomization which adds an element to a randomly selected location
* near the location specified when the method was invoked. Then using the leaf
* chain we then are able to migrate the element the correct leaf. Locally
* randomized additions keeps the tree shape from degenerating. Combining local
* randomization with periodic rebuilding greatly improves performance and
* testing shows that with large lists TLists strongly outperform ArrayLists.
* Indeed TLists are about 8 times faster!
* <p/>
* **Note** We have marked a number of methods in TList as 'final'. This was
* done because these methods are, in their implementation details and usage,
* highly critical to the correct operation of the TList class and many of its
* non-final methods. Programmers with access to the source code can, of course,
* remove these 'final' access modifiers thus enabling users of TList to
* override these 'final' methods. However, those who wish to pursue such a
* course should do so with great caution and resolve to test! test! test! any
* such changes.
* @author BOB JACKSON (re-jackson@consolidated.net).
*/
public class TList extends AbstractList implements List, RandomAccess,
Cloneable, java.io.Serializable {
/**
* setMode(NORMAL) causes a heuristic to be used to decide whether to
* rebuild the TList following an add or remove operation. This mode setting
* is appropriate if many add/remove operations are performed to only a few
* locations or to adjacent locations. NORMAL is the default.
*/
public static final int NORMAL = 0;
/**
* setMode(RANDOM) specifies that no rebuild of the TList is to be performed
* after subsequent add/remove operations. This mode is appropriate when
* subsequent add/remove operations are expected to only infrequently update
* the same or adjacent locations. This mode avoids the overhead of rebuild.
*/
public static final int RANDOM = 1;
/**
* reference to tree root
*/
private transient Object root; // reference to root
/**
* reference to first tree leaf
*/
private transient Leaf head; // first leaf in inorder
/**
* reference to last tree leaf
*/
transient Leaf tail; // last leaf in inorder
/**
* number of updates since last rebuild
*/
private transient int uprb = 0; // count of updates since last rebuild
/**
* mode of operation (NORMAL or RANDOM). Default NORMAL implies that a
* heuristic will be used to decide when to rebuild. RANDOM bypasses
* automatic rebuilds.
*/
private int mode = NORMAL; // use heuristic to decide on rebuild
/**
* Randomizer. Used during add/remove operations in NORMAL mode to implement
* local randomization. This keeps the tree shape manageble especially when
* coupled with periodic rebuild.
*/
private static final Random rndx = new Random();
/**
* used to delimit local randomization
*/
private static final int LIMIT = 256;
/**
* Constructs an empty TList.
*/
public TList() {
root = null;
head = null;
tail = null;
}
/**
* Ensure compatibility with ArrayList.
*/
public TList(int initialCapacity) {
root = null;
head = null;
tail = null;
}
/**
* Constructs a TList initialized with the elements of c.
*/
public TList(Collection c) {
root = null;
head = null;
tail = null;
TList.this.addAll(c);
}
/**
* Constructs a TList initialized with the elements of a.
*/
public TList(Object[] a) {
root = null;
head = null;
tail = null;
TList.this.addAll(Arrays.asList(a));
/*
* int len = a.length; for (int i = 0; i < len; i++) {
* TList.this.add(a[i]); }
*/
}
/**
* A dummy ensureCapacity method for compatibility
* with ArrayList
*/
public final void ensureCapacity(int minCapacity) {
}
/**
* A dummy trimToSize method for compatibility with
* ArrayList
*/
public final void trimToSize() {
}
/**
* Returns true if there are no elements, else false
* @return true if there are no elements, else false
*/
@Override
public boolean isEmpty() {
return size() == 0;
}
/**
* Returns the number of elements in the List.
* @return The number of elements in the List.
*/
@Override
public final int size() {
return (root == null) ? 0 : (root instanceof Leaf) ? 1 : ((Node) root)
.getWeight();
}
/**
* Remove all elements from the TList.
*/
@Override
public final void clear() {
if (root == null) {
return;
}
modCount++;
tail = null;
head = null;
root = null;
uprb = 0;
}
/**
* Returns a shallow copy of this TList instance. (The elements themselves
* are not copied.)
* @return A clone of this TList instance
*/
@Override
public final Object clone() {
class Q { // local work class
Node val = null;
Q next = null;
} // end Q
TList clone;
int sz = size();
try {
clone = (TList) super.clone();
clone.modCount = 0;
clone.uprb = 0;
clone.mode = mode;
clone.root = null;
clone.tail = null;
clone.head = null;
if (sz == 0) {
return clone;
}
if (sz == 1) {
clone.root = new Leaf((((Leaf) root)).getValue());
clone.head = (Leaf) (clone.root);
clone.tail = (Leaf) (clone.root);
return clone;
}
Q front = new Q();
front.val = new Node(sz);
Q back = front;
Q curr;
Q q;
clone.root = front.val;
for (int i = 0; i < sz - 2; i++) {
q = new Q();
q.val = new Node(2); // pre-set to min wt.
back.next = q;
back = q;
}
curr = front;
Q posn = curr;
int wt = sz, wl, wr;
set_weights:
while (wt > 3) {
wl = wt / 2;
wr = wt - wl;
posn = posn.next;
(posn.val).setWeight(wl);
posn = posn.next;
(posn.val).setWeight(wr);
curr = curr.next;
wt = (curr.val).getWeight();
} // end set_weights
curr = front;
posn = front;
Node g;
set_children:
while (curr != null) {
g = curr.val; // never null!
wt = g.getWeight();
wl = wt / 2;
wr = wt - wl;
if (wl > 1) {
posn = posn.next;
g.setLeft(posn.val);
}
if (wr > 1) {
posn = posn.next;
g.setRight(posn.val);
}
curr = curr.next;
} // end set_children
Object node, xnode;
Leaf nxlf = new Leaf();
Leaf pv, v = null;
nxlf.setRight(head);
int index = 0, first, last, pivot, wn;
set_leaves:
do {
node = clone.root;
wn = sz;
first = 0;
last = sz;
while (wn > 4) {
pivot = first;
xnode = ((Node) node).getLeft();
wn = ((Node) xnode).getWeight();
pivot += wn;
if (index < pivot) {
last = pivot;
node = xnode;
} else {
first = pivot;
node = ((Node) node).getRight();
wn = ((Node) node).getWeight();
}
} // end while (wn>4)
if (wn == 4) {
xnode = ((Node) node).getLeft();
nxlf = nxlf.getRight();
pv = v;
v = new Leaf((((Leaf) nxlf)).getValue());
if (pv == null) {
clone.head = v;
} else {
v.setLeft(pv);
pv.setRight(v);
}
((Node) xnode).setLeft(v);
index++;
nxlf = nxlf.getRight();
pv = v;
v = new Leaf((((Leaf) nxlf)).getValue());
if (pv == null) {
clone.head = v;
} else {
v.setLeft(pv);
pv.setRight(v);
}
((Node) xnode).setRight(v);
index++;
node = ((Node) node).getRight();
}
if (wn == 3) {
nxlf = nxlf.getRight();
pv = v;
v = new Leaf((((Leaf) nxlf)).getValue());
if (pv == null) {
clone.head = v;
} else {
v.setLeft(pv);
pv.setRight(v);
}
((Node) node).setLeft(v);
index++;
node = ((Node) node).getRight();
}
nxlf = nxlf.getRight();
pv = v;
v = new Leaf((((Leaf) nxlf)).getValue());
if (pv == null) {
clone.head = v;
} else {
v.setLeft(pv);
pv.setRight(v);
}
((Node) node).setLeft(v);
index++;
nxlf = nxlf.getRight();
pv = v;
v = new Leaf((((Leaf) nxlf)).getValue());
if (pv == null) {
clone.head = v;
} else {
v.setLeft(pv);
pv.setRight(v);
}
((Node) node).setRight(v);
index++;
} while (index < sz);
clone.tail = v;
return clone;
} catch (CloneNotSupportedException e) {
throw new InternalError();
}
}
/**
* Performs a function similar to addAll(index,collection) but optimized for
* the case when the input collection is a TList. When the splice operation
* is completed the TList specified by the parameter 'other' will have an
* empty state, i.e. other.isEmpty() will return 'true'. Note, however, that
* the elements previously contained in the 'other' TList will nevertheless
* be contained in this TList just before the element which had index
* 'whereIndex' before the splice operation began.
* @param whereIndex the index where the other TList will be spliced.
* @param other the TList to be spliced into this TList at whereIndex.
* @throws IndexOutOfBoundsException
* @throws NullPointerException
* @throws IllegalArgumentException
*/
public final void splice(int whereIndex, TList other)
throws IndexOutOfBoundsException, NullPointerException,
IllegalArgumentException {
if (whereIndex < 0 || whereIndex > this.size()) {
throw new IndexOutOfBoundsException();
}
if (other == null) {
throw new NullPointerException();
}
if (other == this) {
throw new IllegalArgumentException();
}
this.rebuildTest();
other.rebuildTest();
if (other.size() == 0) {
return;
}
if (this.size() == 0) {
this.root = other.root;
this.head = other.head;
this.tail = other.tail;
other.clear();
this.modCount++;
return;
}
if (whereIndex == 0) {
other.splice(other.size(), this);
this.root = other.root;
this.head = other.head;
this.tail = other.tail;
other.clear();
this.rebuildTest();
this.modCount++;
return;
}
if (whereIndex == this.size()) {
int sz = this.size() + other.size();
Node z = new Node(sz);
z.setLeft(this.root);
z.setRight(other.root);
this.root = z;
z = null;
(this.tail).setRight(other.head);
(other.head).setLeft(this.tail);
this.tail = other.tail;
other.clear();
this.rebuildTest();
this.modCount++;
return;
}
TList tlist2 = this.split(whereIndex);
this.splice(this.size(), other);
this.splice(this.size(), tlist2);
}
/**
* Splits this TList into 2 TLists. Upon return this TList will contain only
* those elements with index less than whereIndex. Those elements with index
* equal to or greater than whereIndex will be moved to a new TList which is
* returned to the caller.
* @param whereIndex the position where this TList will be split
* @return a TList containing the elements of this TList which had an index
* equal to or greater than whereIndex in this TList.
* @throws IndexOutOfBoundsException
*/
public final TList split(int whereIndex) throws IndexOutOfBoundsException {
if (whereIndex < 0 || whereIndex > this.size()) {
throw new IndexOutOfBoundsException();
}
TList retTList = new TList();
retTList.mode = this.mode;
if (whereIndex == this.size()) {
this.rebuildTest();
return retTList;
}
if (whereIndex == 0) {
retTList.root = this.root;
retTList.head = this.head;
retTList.tail = this.tail;
retTList.uprb = this.uprb;
this.clear();
retTList.rebuildTest();
return retTList;
}
retTList = getTList(this.size() - whereIndex, this.getLeaf(whereIndex));
this.removeRange(whereIndex, this.size());
return retTList;
}
/**
* Constructs a sublist or view over this list beginning at fromindex and
* including all indexes greater than fromIndex which are less than toIndex.
* If fromIndex=toIndex the list is empty. A sub-sublist may also be
* constructed on top of a sublist.
* @param fromIndex Index into the base list where this sublist begins.
* @param toIndex The index into the base list which is 1 greater than the
* largest base index in the sublist.
* @return a sublist of this list from fromIndex up to but not including
* toIndex.
*/
@Override
public final List subList(int fromIndex, int toIndex) {
return new SubTList(this, fromIndex, toIndex);
}
/**
* Returns an array whose elements are equal to the elements of this list in
* the same order as would be returned by an iterator over this list.
* @return an array whose elements are the elements of this list in the same
* order as would returned by an iterator over this list.
*/
@Override
public Object[] toArray() {
int s = size();
Object[] o = new Object[s];
Iterator iter = iterator();
int i = 0;
while (iter.hasNext()) {
o[i++] = iter.next();
}
return o;
}
/**
* Returns an array whose elements are equal to the elements of this list in
* the same order as would be returned by an iterator over this list.
* @param o The input array which is to be filled (if possible) with
* elements of this list and returned. If the input array is too
* small a new array is allocated with the same runtime type as
* o, filled, and returned. If o is too large o[size()] is set to
* null.
* @return an array whose elements are the elements of this list in the same
* order as would returned by an iterator over this list. The
* runtime type of the returned array is the same as the type as the
* input array o.
* @throws NullPointerException if the input o is null.
*/
@Override
public Object[] toArray(Object[] o) throws NullPointerException {
if (o == null) {
throw new NullPointerException();
}
int s = size();
if (o.length < s) {
o = (Object[]) java.lang.reflect.Array.newInstance(o.getClass()
.getComponentType(), s);
}
Iterator iter = iterator();
int i = 0;
while (iter.hasNext()) {
o[i++] = iter.next();
}
if (o.length > s) {
o[s] = null;
}
return o;
}
/**
* Returns true if the list contains the specified element.
* @param o element whose presence in the list is to be tested.
* @return true if the list contains the specified element.
*/
@Override
public boolean contains(Object o) {
Iterator iter = iterator();
while (iter.hasNext()) {
Object o2 = iter.next();
if (o == null) {
if (o2 == null) {
return true;
}
} else { // o != null
if (o2 != null) {
if (o2.equals(o)) {
return true;
}
}
}
}
return false;
} // end contains
/**
* Returns the index of the first occurance of an element in this list which
* matches the input. If no match is found returns -1
* @param o Element whose first index in the list is to be returned.
* @return The first index of the input element in the list.
*/
@Override
public int indexOf(Object o) {
ListIterator lter = listIterator();
if (o == null) {
while (lter.hasNext()) {
if (lter.next() == null) {
return lter.previousIndex();
}
}
} else {
while (lter.hasNext()) {
if (o.equals(lter.next())) {
return lter.previousIndex();
}
}
}
return -1;
}
/**
* Returns the index of the last occurance of an element in this list which
* matches the input. If no match is found returns -1
* @param o Element whose last index in the list is to be returned.
* @return The last index of the input element in the list.
*/
@Override
public int lastIndexOf(Object o) {
ListIterator lter = listIterator(size());
if (o == null) {
while (lter.hasPrevious()) {
if (lter.previous() == null) {
return lter.nextIndex();
}
}
} else {
while (lter.hasPrevious()) {
if (o.equals(lter.previous())) {
return lter.nextIndex();
}
}
}
return -1;
}
/**
* Returns true if the list contains all elements in specified collection.
* @param c collection whose elements are to be tested for presence in
* this list.
* @return true if the list contains all the elements in the specified
* collection.
* @throws NullPointerException if the input collection is null.
*/
@Override
public boolean containsAll(Collection c) throws NullPointerException {
if (c == null) {
throw new NullPointerException();
}
for (Object aC : c) {
if (!this.contains(aC)) {
return false;
}
}
return true;
}
/**
* Add a new element to the end of the List. Size() increases by 1.
* @param o element to be added to the end of the List.
* @return true.
*/
@Override
public boolean add(Object o) {
addLast(o); // increments modCount
return true;
}
/**
* Add a collection of elements to the end of the List. size() increases by
* c.size().
* @param c Collection of elements to be added to the end of the list.
* @return true.
* @throws NullPointerException if the input collection is null.
*/
@Override
public boolean addAll(Collection c) throws NullPointerException {
if (c == null) {
throw new NullPointerException();
}
if (c.isEmpty()) {
return false;
}
/*
* if (c instanceof TList) { splice(this.size(), (TList) c); return
* true; }
*/
for (Object aC : c) {
this.addLast(aC); // increments modCount
}
return true;
}
/**
* Add a collection of elements to the List at the specified position.
* size() increases by c.size().
* @param index position in list in front of which the elements of the
* Collection c are to be added.
* @param c Collection of elements to be added to the list at the
* indicated position.
* @return true if any elements are added to the list.
* @throws NullPointerException if the input collection is null.
* @throws IndexOutOfBoundsException if index is less than 0 or greater than size().
*/
@Override
public boolean addAll(int index, Collection c) throws NullPointerException,
IndexOutOfBoundsException {
if (index < 0 || index > size()) {
throw new IndexOutOfBoundsException();
}
if (c == null) {
throw new NullPointerException();
}
if (c.isEmpty()) {
return false;
}
/*
* if (c instanceof TList) { splice(index, (TList) c); return true; }
*/
Iterator iter = c.iterator();
int i = index;
// ListIterator lter = listIterator(index);
while (iter.hasNext()) {
// lter.add(iter.next());
this.add(i++, iter.next()); // increments modCount
}
return true;
}
/**
* Add a new element to the end of the List. Size() increases by 1.
* @param o element to be added to the end of the List.
*/
public final void addLast(Object o) {
if (root == null) {
root = new Leaf(o);
head = (Leaf) root;
tail = head;
modCount++;
return;
} // end if
if (root instanceof Leaf) {
Object r = new Node(2); // make a new root
((Node) r).setLeft(root); // make old root its left child
Leaf rl = new Leaf(o); // create new leaf
((Node) r).setRight(rl); // make it right child of r
head = (Leaf) root;
tail = rl;
head.setRight(tail);
tail.setLeft(head);
root = r;
modCount++;
return;
} // end if
if (mode == NORMAL) {
int index = size();
int ix0 = index - LIMIT, ix1 = index;
if (ix0 < 0) {
ix0 = 0;
}
int ixx = ix0 + rndx.nextInt(1 + (ix1 - ix0));
if (ixx != index) {
Leaf lf, lg;
if (ixx == 0) {
lf = xaddFirst(null); // modCount++
} else {
lf = xadd(ixx, null); // modCount++
}
// ixx < index
for (int k = ixx; k < index; k++, lf = lg) {
lg = lf.getRight();
lf.setValue(lg.getValue());
}
lf.setValue(o);
return;
}
}
modCount++;
uprb += 1;
Object p = root; // save reference
Object node = root; // begin at root
while (node instanceof Node) {
((Node) node).setWeight(((Node) node).getWeight() + 1); // increment
p = node; // save reference to parent
node = ((Node) node).getRight();
} // endwhile
/*
* node is now the last leaf; we make a new leaf to the right of node
* which will be the new last leaf.
*/
Object q = new Node(2); // make a new parent
((Node) q).setLeft(node); // make node the left child
Leaf rl = new Leaf(o); // create new leaf
((Node) q).setRight(rl); // make it the right child of q
tail = rl; // tail is new leaf
tail.setLeft((Leaf) node);
((Leaf) node).setRight(tail);
((Node) p).setRight(q); // make q the parent's right child
rebuildTest();
} // end addLast
/**
* Called in NORMAL mode during local randomization.
* @param o new object to be added last in list.
* @return Leaf containing o.
*/
private Leaf xaddLast(Object o) {
modCount++;
uprb += 1;
Object p = root; // save reference
Object node = root; // begin at root
while (node instanceof Node) {
((Node) node).setWeight(((Node) node).getWeight() + 1); // increment
p = node; // save reference to parent
node = ((Node) node).getRight();
} // endwhile
/*
* node is now the last leaf; we make a new leaf to the right of node
* which will be the new last leaf.
*/
Object q = new Node(2); // make a new parent
((Node) q).setLeft(node); // make node the left child
Leaf rl = new Leaf(o); // create new leaf
((Node) q).setRight(rl); // make it the right child of q
tail = rl; // tail is new leaf
tail.setLeft((Leaf) node);
((Leaf) node).setRight(tail);
((Node) p).setRight(q); // make q the parent's right child
rebuildTest();
return rl;
} // end xaddLast
/**
* Add a new element to the front of the List. All original elements are
* shifted over by 1 and thus their indices are increased by 1. Size()
* increases by 1.
* @param o element to be added to the front of the List.
*/
public final void addFirst(Object o) {
if (root == null) {
root = new Leaf(o);
head = (Leaf) root;
tail = head;
modCount++;
return;
} // end if
if (root instanceof Leaf) {
Object r = new Node(2); // make a new root
((Node) r).setRight(root); // make root its right child
Leaf rl = new Leaf(o); // create a new leaf
((Node) r).setLeft(rl); // make it left child of q
head = rl;
tail = (Leaf) root;
head.setRight(tail);
tail.setLeft(head);
root = r;
modCount++;
return;
} // end if
if (mode == NORMAL) {
int index = 0;
int ix0 = 0, ix1 = LIMIT;
if (ix1 > size()) {
ix1 = size();
}
int ixx = ix0 + rndx.nextInt(1 + (ix1 - ix0));
if (ixx != index) {
Leaf lf, lg;
if (ixx == size()) {
lf = xaddLast(null); // modCount++
} else {
lf = xadd(ixx, null); // modCount++
}
// ixx > index
for (int k = ixx; k > index; k--, lf = lg) {
lg = lf.getLeft();
lf.setValue(lg.getValue());
}
lf.setValue(o);
return;
}
}
modCount++;
uprb += 1;
Object p = root; // save reference
Object node = root; // begin at root
while (node instanceof Node) {
((Node) node).setWeight(((Node) node).getWeight() + 1); // increment
p = node; // save reference to parent
node = ((Node) node).getLeft();
} // endwhile
/*
* node is now the first leaf; we make a new leaf to the left of node
* which will be the new first leaf.
*/
Object q = new Node(2); // make a new parent
((Node) q).setRight(node); // make node the right child
Leaf rl = new Leaf(o); // create a new leaf
((Node) q).setLeft(rl); // make a new left child of q
head = rl; // head is new leaf
head.setRight((Leaf) node);
((Leaf) node).setLeft(head);
((Node) p).setLeft(q); // make q the parent's left child
rebuildTest();
} // end addFirst
/**
* Called in NORMAL mode during local randomization.
* @param o new object to be added first in list.
* @return Leaf containing o.
*/
private Leaf xaddFirst(Object o) {
modCount++;
uprb += 1;
Object p = root; // save reference
Object node = root; // begin at root
while (node instanceof Node) {
((Node) node).setWeight(((Node) node).getWeight() + 1); // increment
p = node; // save reference to parent
node = ((Node) node).getLeft();
} // endwhile
/*
* node is now the first leaf; we make a new leaf to the left of node
* which will be the new first leaf.
*/
Object q = new Node(2); // make a new parent
((Node) q).setRight(node); // make node the right child
Leaf rl = new Leaf(o); // create a new leaf
((Node) q).setLeft(rl); // make a new left child of q
head = rl; // head is new leaf
head.setRight((Leaf) node);
((Leaf) node).setLeft(head);
((Node) p).setLeft(q); // make q the parent's left child
rebuildTest();
return rl;
} // end xaddFirst
/**
* Add a new element to the List at the specified position. The element
* originally at the specified position and all following elements are
* shifted over by 1 position and thus their indices are increased by 1.
* Size() increases by 1.
* @param index position where the new element is to be inserted. If index =
* size() calls addLast(Object o).
* @param o new element to be inserted in the List.