1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.list;
18
19 import java.util.AbstractList;
20 import java.util.ArrayDeque;
21 import java.util.Collection;
22 import java.util.ConcurrentModificationException;
23 import java.util.Deque;
24 import java.util.Iterator;
25 import java.util.ListIterator;
26 import java.util.NoSuchElementException;
27 import java.util.Objects;
28
29 import org.apache.commons.collections4.CollectionUtils;
30 import org.apache.commons.collections4.OrderedIterator;
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 public class TreeList<E> extends AbstractList<E> {
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 static class AVLNode<E> {
84
85 private AVLNode<E> left;
86
87 private boolean leftIsPrevious;
88
89 private AVLNode<E> right;
90
91 private boolean rightIsNext;
92
93 private int height;
94
95 private int relativePosition;
96
97 private E value;
98
99
100
101
102
103
104
105
106 private AVLNode(final Collection<? extends E> coll) {
107 this(coll.iterator(), 0, coll.size() - 1, 0, null, null);
108 }
109
110
111
112
113
114
115
116
117
118 private AVLNode(final int relativePosition, final E obj,
119 final AVLNode<E> rightFollower, final AVLNode<E> leftFollower) {
120 this.relativePosition = relativePosition;
121 value = obj;
122 rightIsNext = true;
123 leftIsPrevious = true;
124 right = rightFollower;
125 left = leftFollower;
126 }
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149 private AVLNode(final Iterator<? extends E> iterator, final int start, final int end,
150 final int absolutePositionOfParent, final AVLNode<E> prev, final AVLNode<E> next) {
151 final int mid = start + (end - start) / 2;
152 if (start < mid) {
153 left = new AVLNode<>(iterator, start, mid - 1, mid, prev, this);
154 } else {
155 leftIsPrevious = true;
156 left = prev;
157 }
158 value = iterator.next();
159 relativePosition = mid - absolutePositionOfParent;
160 if (mid < end) {
161 right = new AVLNode<>(iterator, mid + 1, end, mid, this, next);
162 } else {
163 rightIsNext = true;
164 right = next;
165 }
166 recalcHeight();
167 }
168
169
170
171
172
173
174
175
176
177
178
179
180 private AVLNode<E> addAll(AVLNode<E> otherTree, final int currentSize) {
181 final AVLNode<E> maxNode = max();
182 final AVLNode<E> otherTreeMin = otherTree.min();
183
184
185
186
187
188
189
190 if (otherTree.height > height) {
191
192
193
194
195 final AVLNode<E> leftSubTree = removeMax();
196
197
198
199
200
201 final Deque<AVLNode<E>> sAncestors = new ArrayDeque<>();
202 AVLNode<E> s = otherTree;
203 int sAbsolutePosition = s.relativePosition + currentSize;
204 int sParentAbsolutePosition = 0;
205 while (s != null && s.height > getHeight(leftSubTree)) {
206 sParentAbsolutePosition = sAbsolutePosition;
207 sAncestors.push(s);
208 s = s.left;
209 if (s != null) {
210 sAbsolutePosition += s.relativePosition;
211 }
212 }
213
214
215
216
217 maxNode.setLeft(leftSubTree, null);
218 maxNode.setRight(s, otherTreeMin);
219 if (leftSubTree != null) {
220 leftSubTree.max().setRight(null, maxNode);
221 leftSubTree.relativePosition -= currentSize - 1;
222 }
223 if (s != null) {
224 s.min().setLeft(null, maxNode);
225 s.relativePosition = sAbsolutePosition - currentSize + 1;
226 }
227 maxNode.relativePosition = currentSize - 1 - sParentAbsolutePosition;
228 otherTree.relativePosition += currentSize;
229
230
231 s = maxNode;
232 while (!sAncestors.isEmpty()) {
233 final AVLNode<E> sAncestor = sAncestors.pop();
234 sAncestor.setLeft(s, null);
235 s = sAncestor.balance();
236 }
237 return s;
238 }
239 otherTree = otherTree.removeMin();
240
241 final Deque<AVLNode<E>> sAncestors = new ArrayDeque<>();
242 AVLNode<E> s = this;
243 int sAbsolutePosition = s.relativePosition;
244 int sParentAbsolutePosition = 0;
245 while (s != null && s.height > getHeight(otherTree)) {
246 sParentAbsolutePosition = sAbsolutePosition;
247 sAncestors.push(s);
248 s = s.right;
249 if (s != null) {
250 sAbsolutePosition += s.relativePosition;
251 }
252 }
253
254 otherTreeMin.setRight(otherTree, null);
255 otherTreeMin.setLeft(s, maxNode);
256 if (otherTree != null) {
257 otherTree.min().setLeft(null, otherTreeMin);
258 otherTree.relativePosition++;
259 }
260 if (s != null) {
261 s.max().setRight(null, otherTreeMin);
262 s.relativePosition = sAbsolutePosition - currentSize;
263 }
264 otherTreeMin.relativePosition = currentSize - sParentAbsolutePosition;
265
266 s = otherTreeMin;
267 while (!sAncestors.isEmpty()) {
268 final AVLNode<E> sAncestor = sAncestors.pop();
269 sAncestor.setRight(s, null);
270 s = sAncestor.balance();
271 }
272 return s;
273 }
274
275
276
277
278 private AVLNode<E> balance() {
279 switch (heightRightMinusLeft()) {
280 case 1:
281 case 0:
282 case -1:
283 return this;
284 case -2:
285 if (left.heightRightMinusLeft() > 0) {
286 setLeft(left.rotateLeft(), null);
287 }
288 return rotateRight();
289 case 2:
290 if (right.heightRightMinusLeft() < 0) {
291 setRight(right.rotateRight(), null);
292 }
293 return rotateLeft();
294 default:
295 throw new IllegalStateException("tree inconsistent!");
296 }
297 }
298
299
300
301
302
303 AVLNode<E> get(final int index) {
304 final int indexRelativeToMe = index - relativePosition;
305
306 if (indexRelativeToMe == 0) {
307 return this;
308 }
309
310 final AVLNode<E> nextNode = indexRelativeToMe < 0 ? getLeftSubTree() : getRightSubTree();
311 if (nextNode == null) {
312 return null;
313 }
314 return nextNode.get(indexRelativeToMe);
315 }
316
317
318
319
320 private int getHeight(final AVLNode<E> node) {
321 return node == null ? -1 : node.height;
322 }
323
324
325
326
327 private AVLNode<E> getLeftSubTree() {
328 return leftIsPrevious ? null : left;
329 }
330
331
332
333
334 private int getOffset(final AVLNode<E> node) {
335 if (node == null) {
336 return 0;
337 }
338 return node.relativePosition;
339 }
340
341
342
343
344 private AVLNode<E> getRightSubTree() {
345 return rightIsNext ? null : right;
346 }
347
348
349
350
351
352
353 E getValue() {
354 return value;
355 }
356
357
358
359
360 private int heightRightMinusLeft() {
361 return getHeight(getRightSubTree()) - getHeight(getLeftSubTree());
362 }
363
364
365
366
367 int indexOf(final Object object, final int index) {
368 if (getLeftSubTree() != null) {
369 final int result = left.indexOf(object, index + left.relativePosition);
370 if (result != -1) {
371 return result;
372 }
373 }
374 if (Objects.equals(value, object)) {
375 return index;
376 }
377 if (getRightSubTree() != null) {
378 return right.indexOf(object, index + right.relativePosition);
379 }
380 return -1;
381 }
382
383
384
385
386
387
388
389
390 AVLNode<E> insert(final int index, final E obj) {
391 final int indexRelativeToMe = index - relativePosition;
392
393 if (indexRelativeToMe <= 0) {
394 return insertOnLeft(indexRelativeToMe, obj);
395 }
396 return insertOnRight(indexRelativeToMe, obj);
397 }
398
399 private AVLNode<E> insertOnLeft(final int indexRelativeToMe, final E obj) {
400 if (getLeftSubTree() == null) {
401 setLeft(new AVLNode<>(-1, obj, this, left), null);
402 } else {
403 setLeft(left.insert(indexRelativeToMe, obj), null);
404 }
405
406 if (relativePosition >= 0) {
407 relativePosition++;
408 }
409 final AVLNode<E> ret = balance();
410 recalcHeight();
411 return ret;
412 }
413
414 private AVLNode<E> insertOnRight(final int indexRelativeToMe, final E obj) {
415 if (getRightSubTree() == null) {
416 setRight(new AVLNode<>(+1, obj, right, this), null);
417 } else {
418 setRight(right.insert(indexRelativeToMe, obj), null);
419 }
420 if (relativePosition < 0) {
421 relativePosition--;
422 }
423 final AVLNode<E> ret = balance();
424 recalcHeight();
425 return ret;
426 }
427
428
429
430
431
432
433 private AVLNode<E> max() {
434 return getRightSubTree() == null ? this : right.max();
435 }
436
437
438
439
440
441
442 private AVLNode<E> min() {
443 return getLeftSubTree() == null ? this : left.min();
444 }
445
446
447
448
449
450
451 AVLNode<E> next() {
452 if (rightIsNext || right == null) {
453 return right;
454 }
455 return right.min();
456 }
457
458
459
460
461
462
463 AVLNode<E> previous() {
464 if (leftIsPrevious || left == null) {
465 return left;
466 }
467 return left.max();
468 }
469
470
471
472
473 private void recalcHeight() {
474 height = Math.max(
475 getLeftSubTree() == null ? -1 : getLeftSubTree().height,
476 getRightSubTree() == null ? -1 : getRightSubTree().height) + 1;
477 }
478
479
480
481
482
483
484
485 AVLNode<E> remove(final int index) {
486 final int indexRelativeToMe = index - relativePosition;
487
488 if (indexRelativeToMe == 0) {
489 return removeSelf();
490 }
491 if (indexRelativeToMe > 0) {
492 setRight(right.remove(indexRelativeToMe), right.right);
493 if (relativePosition < 0) {
494 relativePosition++;
495 }
496 } else {
497 setLeft(left.remove(indexRelativeToMe), left.left);
498 if (relativePosition > 0) {
499 relativePosition--;
500 }
501 }
502 recalcHeight();
503 return balance();
504 }
505
506 private AVLNode<E> removeMax() {
507 if (getRightSubTree() == null) {
508 return removeSelf();
509 }
510 setRight(right.removeMax(), right.right);
511 if (relativePosition < 0) {
512 relativePosition++;
513 }
514 recalcHeight();
515 return balance();
516 }
517
518 private AVLNode<E> removeMin() {
519 if (getLeftSubTree() == null) {
520 return removeSelf();
521 }
522 setLeft(left.removeMin(), left.left);
523 if (relativePosition > 0) {
524 relativePosition--;
525 }
526 recalcHeight();
527 return balance();
528 }
529
530
531
532
533
534
535 private AVLNode<E> removeSelf() {
536 if (getRightSubTree() == null && getLeftSubTree() == null) {
537 return null;
538 }
539 if (getRightSubTree() == null) {
540 if (relativePosition > 0) {
541 left.relativePosition += relativePosition;
542 }
543 left.max().setRight(null, right);
544 return left;
545 }
546 if (getLeftSubTree() == null) {
547 right.relativePosition += relativePosition - (relativePosition < 0 ? 0 : 1);
548 right.min().setLeft(null, left);
549 return right;
550 }
551
552 if (heightRightMinusLeft() > 0) {
553
554 final AVLNode<E> rightMin = right.min();
555 value = rightMin.value;
556 if (leftIsPrevious) {
557 left = rightMin.left;
558 }
559 right = right.removeMin();
560 if (relativePosition < 0) {
561 relativePosition++;
562 }
563 } else {
564
565 final AVLNode<E> leftMax = left.max();
566 value = leftMax.value;
567 if (rightIsNext) {
568 right = leftMax.right;
569 }
570 final AVLNode<E> leftPrevious = left.left;
571 left = left.removeMax();
572 if (left == null) {
573
574
575 left = leftPrevious;
576 leftIsPrevious = true;
577 }
578 if (relativePosition > 0) {
579 relativePosition--;
580 }
581 }
582 recalcHeight();
583 return this;
584 }
585
586 private AVLNode<E> rotateLeft() {
587 final AVLNode<E> newTop = right;
588 final AVLNode<E> movedNode = getRightSubTree().getLeftSubTree();
589
590 final int newTopPosition = relativePosition + getOffset(newTop);
591 final int myNewPosition = -newTop.relativePosition;
592 final int movedPosition = getOffset(newTop) + getOffset(movedNode);
593
594 setRight(movedNode, newTop);
595 newTop.setLeft(this, null);
596
597 setOffset(newTop, newTopPosition);
598 setOffset(this, myNewPosition);
599 setOffset(movedNode, movedPosition);
600 return newTop;
601 }
602
603 private AVLNode<E> rotateRight() {
604 final AVLNode<E> newTop = left;
605 final AVLNode<E> movedNode = getLeftSubTree().getRightSubTree();
606
607 final int newTopPosition = relativePosition + getOffset(newTop);
608 final int myNewPosition = -newTop.relativePosition;
609 final int movedPosition = getOffset(newTop) + getOffset(movedNode);
610
611 setLeft(movedNode, newTop);
612 newTop.setRight(this, null);
613
614 setOffset(newTop, newTopPosition);
615 setOffset(this, myNewPosition);
616 setOffset(movedNode, movedPosition);
617 return newTop;
618 }
619
620
621
622
623
624
625
626 private void setLeft(final AVLNode<E> node, final AVLNode<E> previous) {
627 leftIsPrevious = node == null;
628 left = leftIsPrevious ? previous : node;
629 recalcHeight();
630 }
631
632
633
634
635 private int setOffset(final AVLNode<E> node, final int newOffset) {
636 if (node == null) {
637 return 0;
638 }
639 final int oldOffset = getOffset(node);
640 node.relativePosition = newOffset;
641 return oldOffset;
642 }
643
644
645
646
647
648
649
650 private void setRight(final AVLNode<E> node, final AVLNode<E> next) {
651 rightIsNext = node == null;
652 right = rightIsNext ? next : node;
653 recalcHeight();
654 }
655
656
657
658
659
660
661 void setValue(final E obj) {
662 this.value = obj;
663 }
664
665
666
667
668
669
670
671 void toArray(final Object[] array, final int index) {
672 array[index] = value;
673 if (getLeftSubTree() != null) {
674 left.toArray(array, index + left.relativePosition);
675 }
676 if (getRightSubTree() != null) {
677 right.toArray(array, index + right.relativePosition);
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 @Override
737 public String toString() {
738 return new StringBuilder()
739 .append("AVLNode(")
740 .append(relativePosition)
741 .append(CollectionUtils.COMMA)
742 .append(left != null)
743 .append(CollectionUtils.COMMA)
744 .append(value)
745 .append(CollectionUtils.COMMA)
746 .append(getRightSubTree() != null)
747 .append(rightIsNext)
748 .append(")")
749 .toString();
750 }
751 }
752
753
754
755
756 static class TreeListIterator<E> implements ListIterator<E>, OrderedIterator<E> {
757
758 private final TreeList<E> parent;
759
760
761
762 private AVLNode<E> next;
763
764
765
766 private int nextIndex;
767
768
769
770
771 private AVLNode<E> current;
772
773
774
775 private int currentIndex;
776
777
778
779
780
781
782 private int expectedModCount;
783
784
785
786
787
788
789
790 protected TreeListIterator(final TreeList<E> parent, final int fromIndex) {
791 this.parent = parent;
792 this.expectedModCount = parent.modCount;
793 this.next = parent.root == null ? null : parent.root.get(fromIndex);
794 this.nextIndex = fromIndex;
795 this.currentIndex = -1;
796 }
797
798 @Override
799 public void add(final E obj) {
800 checkModCount();
801 parent.add(nextIndex, obj);
802 current = null;
803 currentIndex = -1;
804 nextIndex++;
805 expectedModCount++;
806 }
807
808
809
810
811
812
813
814
815 protected void checkModCount() {
816 if (parent.modCount != expectedModCount) {
817 throw new ConcurrentModificationException();
818 }
819 }
820
821 @Override
822 public boolean hasNext() {
823 return nextIndex < parent.size();
824 }
825
826 @Override
827 public boolean hasPrevious() {
828 return nextIndex > 0;
829 }
830
831 @Override
832 public E next() {
833 checkModCount();
834 if (!hasNext()) {
835 throw new NoSuchElementException("No element at index " + nextIndex + ".");
836 }
837 if (next == null) {
838 next = parent.root.get(nextIndex);
839 }
840 final E value = next.getValue();
841 current = next;
842 currentIndex = nextIndex++;
843 next = next.next();
844 return value;
845 }
846
847 @Override
848 public int nextIndex() {
849 return nextIndex;
850 }
851
852 @Override
853 public E previous() {
854 checkModCount();
855 if (!hasPrevious()) {
856 throw new NoSuchElementException("Already at start of list.");
857 }
858 if (next == null) {
859 next = parent.root.get(nextIndex - 1);
860 } else {
861 next = next.previous();
862 }
863 final E value = next.getValue();
864 current = next;
865 currentIndex = --nextIndex;
866 return value;
867 }
868
869 @Override
870 public int previousIndex() {
871 return nextIndex() - 1;
872 }
873
874 @Override
875 public void remove() {
876 checkModCount();
877 if (currentIndex == -1) {
878 throw new IllegalStateException();
879 }
880 parent.remove(currentIndex);
881 if (nextIndex != currentIndex) {
882
883 nextIndex--;
884 }
885
886
887 next = null;
888 current = null;
889 currentIndex = -1;
890 expectedModCount++;
891 }
892
893 @Override
894 public void set(final E obj) {
895 checkModCount();
896 if (current == null) {
897 throw new IllegalStateException();
898 }
899 current.setValue(obj);
900 }
901 }
902
903
904 private AVLNode<E> root;
905
906
907 private int size;
908
909
910
911
912 public TreeList() {
913 }
914
915
916
917
918
919
920
921 public TreeList(final Collection<? extends E> coll) {
922 if (!coll.isEmpty()) {
923 root = new AVLNode<>(coll);
924 size = coll.size();
925 }
926 }
927
928
929
930
931
932
933
934 @Override
935 public void add(final int index, final E obj) {
936 modCount++;
937 checkInterval(index, 0, size());
938 if (root == null) {
939 root = new AVLNode<>(index, obj, null, null);
940 } else {
941 root = root.insert(index, obj);
942 }
943 size++;
944 }
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959 @Override
960 public boolean addAll(final Collection<? extends E> c) {
961 if (c.isEmpty()) {
962 return false;
963 }
964 modCount += c.size();
965 final AVLNode<E> cTree = new AVLNode<>(c);
966 root = root == null ? cTree : root.addAll(cTree, size);
967 size += c.size();
968 return true;
969 }
970
971
972
973
974
975
976
977
978
979 private void checkInterval(final int index, final int startIndex, final int endIndex) {
980 if (index < startIndex || index > endIndex) {
981 throw new IndexOutOfBoundsException("Invalid index:" + index + ", size=" + size());
982 }
983 }
984
985
986
987
988 @Override
989 public void clear() {
990 modCount++;
991 root = null;
992 size = 0;
993 }
994
995
996
997
998
999
1000
1001 @Override
1002 public boolean contains(final Object object) {
1003 return indexOf(object) >= 0;
1004 }
1005
1006
1007
1008
1009
1010
1011
1012 @Override
1013 public E get(final int index) {
1014 checkInterval(index, 0, size() - 1);
1015 return root.get(index).getValue();
1016 }
1017
1018
1019
1020
1021
1022
1023
1024 @Override
1025 public int indexOf(final Object object) {
1026
1027 if (root == null) {
1028 return -1;
1029 }
1030 return root.indexOf(object, root.relativePosition);
1031 }
1032
1033
1034
1035
1036
1037
1038 @Override
1039 public Iterator<E> iterator() {
1040
1041 return listIterator(0);
1042 }
1043
1044
1045
1046
1047
1048
1049 @Override
1050 public ListIterator<E> listIterator() {
1051
1052 return listIterator(0);
1053 }
1054
1055
1056
1057
1058
1059
1060
1061 @Override
1062 public ListIterator<E> listIterator(final int fromIndex) {
1063
1064
1065 checkInterval(fromIndex, 0, size());
1066 return new TreeListIterator<>(this, fromIndex);
1067 }
1068
1069
1070
1071
1072
1073
1074
1075 @Override
1076 public E remove(final int index) {
1077 modCount++;
1078 checkInterval(index, 0, size() - 1);
1079 final E result = get(index);
1080 root = root.remove(index);
1081 size--;
1082 return result;
1083 }
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093 @Override
1094 public E set(final int index, final E obj) {
1095 checkInterval(index, 0, size() - 1);
1096 final AVLNode<E> node = root.get(index);
1097 final E result = node.value;
1098 node.setValue(obj);
1099 return result;
1100 }
1101
1102
1103
1104
1105
1106
1107 @Override
1108 public int size() {
1109 return size;
1110 }
1111
1112
1113
1114
1115
1116
1117 @Override
1118 public Object[] toArray() {
1119
1120 final Object[] array = new Object[size()];
1121 if (root != null) {
1122 root.toArray(array, root.relativePosition);
1123 }
1124 return array;
1125 }
1126
1127 }