Debian Codegraph @

net/sf/saxon/ma/arrays/ImmutableArrayItem.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.arrays;
9
10 import net.sf.saxon.ma.parray.ImmList;
11 import net.sf.saxon.om.GroundedValue;
12 import net.sf.saxon.trans.XPathException;
13 import net.sf.saxon.z.IntIterator;
14 import net.sf.saxon.z.IntSet;
15
16 import java.util.Arrays;
17
18 /**
19 * Implementation of ArrayItem backed by a persistent immutable array, so that operations
20 * that "update" the array do not have to copy the whole array
21 */
22
23 public class ImmutableArrayItem extends AbstractArrayItem {
24
25 private ImmList<GroundedValue<?>> vector;
26
27 public ImmutableArrayItem(SimpleArrayItem other) {
28 this.vector = ImmList.fromList(other.getMembers());
29 }
30
31 private ImmutableArrayItem(ImmList<GroundedValue<?>> vector) {
32 this.vector = vector;
33 }
34
35 /**
36 * Get a member of the array
37 *
38 * @param index the position of the member to retrieve (zero-based)
39 * @return the value at the given position.
40 * @throws XPathException if the index is out of range
41 */
42 @Override
43 public GroundedValue<?> get(int index) throws XPathException {
44 try {
45 return vector.get(index);
46 } catch (IndexOutOfBoundsException e) {
47 throw new XPathException(e.getMessage(), "FOAY0001");
48 }
49 }
50
51 /**
52 * Replace a member of the array
53 *
54 * @param index the position of the member to replace (zero-based)
55 * @param newValue the replacement value
56 * @return the value at the given position.
57 * @throws XPathException if the index is out of range
58 */
59 @Override
60 public ArrayItem put(int index, GroundedValue newValue) throws XPathException {
61 try {
62 ImmList<GroundedValue<?>> v2 = vector.replace(index, newValue);
63 return v2 == vector ? this : new ImmutableArrayItem(v2);
64 } catch (IndexOutOfBoundsException e) {
65 throw new XPathException(e.getMessage(), "FOAY0001");
66 }
67 }
68
69 /**
70 * Insert a new member into an array
71 *
72 * @param position the 0-based position that the new item will assume
73 * @param member the new member to be inserted
74 * @return a new array item with the new member inserted
75 * @throws IndexOutOfBoundsException if position is out of range
76 */
77 @Override
78 public ArrayItem insert(int position, GroundedValue<?> member) {
79 ImmList<GroundedValue<?>> v2 = vector.insert(position, member);
80 return new ImmutableArrayItem(v2);
81 }
82
83 /**
84 * Get the number of members in the array
85 * <p>
86 * <p>Note: the {@link #getLength() method always returns 1, because an array is an item}</p>
87 *
88 * @return the number of members in this array.
89 */
90 @Override
91 public int arrayLength() {
92 return vector.size();
93 }
94
95 /**
96 * Ask whether the array is empty
97 *
98 * @return true if and only if the size of the array is zero
99 */
100 @Override
101 public boolean isEmpty() {
102 return vector.isEmpty();
103 }
104
105 /**
106 * Get the list of all members of the array
107 *
108 * @return an iterator over the members of the array
109 */
110 @Override
111 public Iterable<GroundedValue<?>> members() {
112 return vector;
113 }
114
115 /**
116 * Get a subarray given a start and end position
117 *
118 * @param start the start position (zero based)
119 * @param end the end position (the position of the first item not to be returned)
120 * (zero based)
121 * @throws IndexOutOfBoundsException if start, or start+end, is out of range
122 */
123 @Override
124 public ArrayItem subArray(int start, int end) {
125 return new ImmutableArrayItem(vector.subList(start, end));
126 }
127
128 /**
129 * Concatenate this array with another
130 *
131 * @param other the second array
132 * @return the concatenation of the two arrays; that is, an array
133 * containing first the members of this array, and then the members of the other array
134 */
135 @Override
136 public ArrayItem concat(ArrayItem other) {
137 if (other.arrayLength() == 0) {
138 return this;
139 }
140 ImmList<GroundedValue<?>> v1;
141 if (other instanceof ImmutableArrayItem) {
142 v1 = ((ImmutableArrayItem)other).vector;
143 } else {
144 v1 = new ImmutableArrayItem((SimpleArrayItem)other).vector;
145 }
146 ImmList<GroundedValue<?>> v2 = vector.appendList(v1);
147 return new ImmutableArrayItem(v2);
148 }
149
150 /**
151 * Remove a member from the array
152 *
153 * @param index the position of the member to be removed (zero-based)
154 * @return a new array in which the requested member has been removed.
155 * @throws IndexOutOfBoundsException if index is out of range
156 */
157 @Override
158 public ArrayItem remove(int index) {
159 //try {
160 ImmList<GroundedValue<?>> v2 = vector.remove(index);
161 return v2 == vector ? this : new ImmutableArrayItem(v2);
162 // } catch (IndexOutOfBoundsException e) {
163 // throw new XPathException(e.getMessage(), "FOAR0001");
164 // }
165 }
166
167 /**
168 * Remove zero or more members from the array
169 *
170 * @param positions the positions of the members to be removed (zero-based).
171 * A value that is out of range is ignored.
172 * @return a new array in which the requested member has been removed
173 */
174 @Override
175 public ArrayItem removeSeveral(IntSet positions) {
176 int[] p = new int[positions.size()];
177 int i = 0;
178 IntIterator ii = positions.iterator();
179 while (ii.hasNext()) {
180 p[i++] = ii.next();
181 }
182 Arrays.sort(p);
183 ImmList<GroundedValue<?>> v2 = vector;
184 for (int j=p.length-1; j>=0; j--) {
185 v2 = v2.remove(p[j]);
186 }
187 return v2 == vector ? this : new ImmutableArrayItem(v2);
188 }
189
190 }
191