Debian Codegraph @

net/sf/saxon/ma/parray/ImmList2.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.ma.parray;
9
10 import net.sf.saxon.tree.jiter.ConcatenatingIterator;
11
12 import java.util.Iterator;
13
14 /**
15 * Implementation of an immutable list of arbitrary length, implemented as a binary tree
16 * @param <E> the type of the elements of the list
17 */
18 public class ImmList2<E> extends ImmList<E> {
19
20 private ImmList<E> left;
21 private ImmList<E> right;
22 private int size;
23
24 protected ImmList2(ImmList<E> left, ImmList<E> right) {
25 this.left = left;
26 this.right = right;
27 this.size = left.size() + right.size();
28 }
29
30 @Override
31 public E get(int index) {
32 if (index < 0) {
33 throw outOfBounds(index, size);
34 } else if (index < left.size()) {
35 return left.get(index);
36 } else if (index < size) {
37 return right.get(index - left.size());
38 } else {
39 throw outOfBounds(index, size);
40 }
41 }
42
43 @Override
44 public int size() {
45 return size;
46 }
47
48 @Override
49 public boolean isEmpty() {
50 return false;
51 }
52
53 @Override
54 public ImmList<E> replace(int index, E member) {
55 if (index < 0) {
56 throw outOfBounds(index, size);
57 } else if (index < left.size()) {
58 return new ImmList2<>(left.replace(index, member), right);
59 } else if (index < size) {
60 return new ImmList2<>(left, right.replace(index - left.size(), member));
61 } else {
62 throw outOfBounds(index, size);
63 }
64 }
65
66 @Override
67 public ImmList<E> insert(int index, E member) {
68 if (index < 0) {
69 throw outOfBounds(index, size);
70 } else if (index <= left.size()) {
71 return new ImmList2<>(left.insert(index, member), right).rebalance();
72 } else if (index <= size) {
73 return new ImmList2<>(left, right.insert(index - left.size(), member)).rebalance();
74 } else {
75 throw outOfBounds(index, size);
76 }
77 }
78
79 @Override
80 public ImmList<E> append(E member) {
81 return new ImmList2<>(this, new ImmList1<>(member)).rebalance();
82 }
83
84 @Override
85 public ImmList<E> appendList(ImmList<E> members) {
86 return new ImmList2<>(this, members).rebalance();
87 }
88
89 @Override
90 public ImmList<E> remove(int index) {
91 if (index < 0) {
92 throw outOfBounds(index, size);
93 } else if (index < left.size()) {
94 return new ImmList2<>(left.remove(index), right).rebalance();
95 } else if (index < size) {
96 return new ImmList2<>(left, right.remove(index - left.size())).rebalance();
97 } else {
98 throw outOfBounds(index, size);
99 }
100 }
101
102 @Override
103 public ImmList<E> subList(int start, int end) {
104 if (start < 0 || start >= size) {
105 throw outOfBounds(start, size);
106 } else if (end < start || end > size) {
107 throw outOfBounds(end, size);
108 }
109 if (start < left.size() && end <= left.size()) {
110 return left.subList(start, end);
111 } else if (start >= left.size() && end >= left.size()) {
112 return right.subList(start - left.size(), end - left.size());
113 } else {
114 return new ImmList2<>(left.subList(start, left.size()), right.subList(0, end - start)).rebalance();
115 }
116 }
117
118 @Override
119 public Iterator<E> iterator() {
120 return new ConcatenatingIterator<>(left.iterator(), () -> right.iterator());
121 }
122
123 private static final int THRESHOLD = 10;
124
125 @Override
126 protected ImmList<E> rebalance() {
127 if (left.isEmpty()) {
128 return right;
129 }
130 if (right.isEmpty()) {
131 return left;
132 }
133 ImmList<E> l2 = left;//.rebalance();
134 ImmList<E> r2 = right;//.rebalance();
135 System.err.println("Balance l=" + left.size() + " r=" + right.size());
136 if (size() > THRESHOLD) {
137 if (l2 instanceof ImmList2 && l2.size() > THRESHOLD * r2.size()) {
138 return new ImmList2<>(((ImmList2<E>) l2).left, new ImmList2<>(((ImmList2<E>) l2).right, r2));
139 } else if (r2 instanceof ImmList2 && r2.size() > THRESHOLD * l2.size()) {
140 return new ImmList2<>(new ImmList2<>(l2, ((ImmList2<E>) r2).left), ((ImmList2<E>) r2).right);
141 } else {
142 return this;
143 }
144 } else if (left == l2 && right == r2) {
145 return this;
146 } else {
147 return new ImmList2<>(l2, r2);
148 }
149 }
150 }
151