Debian Codegraph @

net/sf/saxon/om/MemoSequence.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.om;
9
10 import net.sf.saxon.expr.LastPositionFinder;
11 import net.sf.saxon.trans.XPathException;
12 import net.sf.saxon.tree.iter.ArrayIterator;
13 import net.sf.saxon.tree.iter.EmptyIterator;
14 import net.sf.saxon.tree.iter.GroundedIterator;
15 import net.sf.saxon.tree.iter.SingletonIterator;
16 import net.sf.saxon.value.EmptySequence;
17 import net.sf.saxon.value.SequenceExtent;
18
19 import java.util.Arrays;
20
21 /**
22 * A Sequence implementation that represents a lazy evaluation of a supplied iterator. Items
23 * are read from the base iterator only when they are first required, and are then remembered
24 * within the local storage of the MemoSequence, which eventually (if the sequence is read
25 * to the end) contains the entire value.
26 */
27
28 public class MemoSequence<T extends Item<?>> implements Sequence<T> {
29
30 SequenceIterator<T> inputIterator;
31
32 private T[] reservoir = null;
33 private int used;
34 protected int state;
35
36 // State in which no items have yet been read
37 private static final int UNREAD = 0;
38
39 // State in which zero or more items are in the reservoir and it is not known
40 // whether more items exist
41 private static final int MAYBE_MORE = 1;
42
43 // State in which all the items are in the reservoir
44 private static final int ALL_READ = 3;
45
46 // State in which we are getting the base iterator. If the closure is called in this state,
47 // it indicates a recursive entry, which is only possible on an error path
48 private static final int BUSY = 4;
49
50 // State in which we know that the value is an empty sequence
51 protected static final int EMPTY = 5;
52
53 public MemoSequence(SequenceIterator<T> iterator) {
54 this.inputIterator = iterator;
55 }
56
57 public T head() throws XPathException {
58 return iterate().next();
59 }
60
61
62 public synchronized SequenceIterator<T> iterate() throws XPathException {
63
64 switch (state) {
65 case UNREAD:
66 state = BUSY;
67 if (inputIterator instanceof EmptyIterator) {
68 state = EMPTY;
69 return inputIterator;
70 }
71 reservoir = (T[])new Item[50];
72 used = 0;
73 state = MAYBE_MORE;
74 return new ProgressiveIterator();
75
76 case MAYBE_MORE:
77 return new ProgressiveIterator();
78
79 case ALL_READ:
80 switch (used) {
81 case 0:
82 state = EMPTY;
83 return EmptyIterator.emptyIterator();
84 case 1:
85 assert reservoir != null;
86 return SingletonIterator.makeIterator(reservoir[0]);
87 default:
88 return new ArrayIterator<>(reservoir, 0, used);
89 }
90
91 case BUSY:
92 // recursive entry: can happen if there is a circularity involving variable and function definitions
93 // Can also happen if variable evaluation is attempted in a debugger, hence the cautious message
94 XPathException de = new XPathException("Attempt to access a variable while it is being evaluated");
95 de.setErrorCode("XTDE0640");
96 //de.setXPathContext(context);
97 throw de;
98
99 case EMPTY:
100 return EmptyIterator.emptyIterator();
101
102 default:
103 throw new IllegalStateException("Unknown iterator state");
104
105 }
106 }
107
108 /**
109 * Get the Nth item in the sequence (0-based), reading new items into the internal reservoir if necessary
110 * @param n the index of the required item
111 * @return the Nth item if it exists, or null otherwise
112 * @throws XPathException if the input sequence cannot be read
113 */
114
115 public synchronized Item itemAt(int n) throws XPathException {
116 if (n < 0) {
117 return null;
118 }
119 if (reservoir != null && n < used) {
120 return reservoir[n];
121 }
122 if (state == ALL_READ || state == EMPTY) {
123 return null;
124 }
125 if (state == UNREAD) {
126 T item = inputIterator.next();
127 if (item == null) {
128 state = EMPTY;
129 return null;
130 } else {
131 state = MAYBE_MORE;
132 reservoir = (T[])new Item[50];
133 append(item);
134 if (n == 0) {
135 return item;
136 }
137 }
138 }
139 // We have read some items from the input sequence but not enough. Read as many more as are needed.
140 int diff = n - used + 1;
141 while (diff-- > 0) {
142 T i = inputIterator.next();
143 if (i == null) {
144 state = ALL_READ;
145 condense();
146 return null;
147 }
148 append(i);
149 state = MAYBE_MORE;
150 }
151 //noinspection ConstantConditions
152 return reservoir[n];
153
154 }
155
156
157 /**
158 * Append an item to the reservoir
159 *
160 * @param item the item to be added
161 */
162
163 private void append(T item) {
164 assert reservoir != null;
165 if (used >= reservoir.length) {
166 reservoir = Arrays.copyOf(reservoir, used * 2);
167 }
168 reservoir[used++] = item;
169 }
170
171 /**
172 * Release unused space in the reservoir (provided the amount of unused space is worth reclaiming)
173 */
174
175 private void condense() {
176 if (reservoir != null && reservoir.length - used > 30) {
177 reservoir = Arrays.copyOf(reservoir, used);
178 }
179 }
180
181
182 /**
183 * A ProgressiveIterator starts by reading any items already held in the reservoir;
184 * when the reservoir is exhausted, it reads further items from the inputIterator,
185 * copying them into the reservoir as they are read.
186 */
187
188 public final class ProgressiveIterator
189 implements SequenceIterator<T>, LastPositionFinder, GroundedIterator<T> {
190
191 int position = -1; // zero-based position in the reservoir of the
192 // item most recently read
193
194 /**
195 * Create a ProgressiveIterator
196 */
197
198 public ProgressiveIterator() {
199 }
200
201 /**
202 * Get the containing MemoSequence
203 *
204 */
205
206 public MemoSequence getMemoSequence() {
207 return MemoSequence.this;
208 }
209
210 /*@Nullable*/
211 public T next() throws XPathException {
212 synchronized (MemoSequence.this) {
213 // synchronized for the case where a multi-threaded xsl:for-each is reading the variable
214 if (position == -2) { // means we've already returned null once, keep doing so if called again.
215 return null;
216 }
217 if (++position < used) {
218 assert reservoir != null;
219 return reservoir[position];
220 } else if (state == ALL_READ) {
221 // someone else has read the input to completion in the meantime
222 position = -2;
223 return null;
224 } else {
225 assert inputIterator != null;
226 T i = inputIterator.next();
227 if (i == null) {
228 state = ALL_READ;
229 condense();
230 position = -2;
231 return null;
232 }
233 position = used;
234 append(i);
235 state = MAYBE_MORE;
236 return i;
237 }
238 }
239 }
240
241 public void close() {
242 }
243
244 /**
245 * Get the last position (that is, the number of items in the sequence)
246 */
247
248 public int getLength() throws XPathException {
249 if (state == ALL_READ) {
250 return used;
251 } else if (state == EMPTY) {
252 return 0;
253 } else {
254 // save the current position
255 int savePos = position;
256 // fill the reservoir
257 while (next() != null) {}
258 // reset the current position
259 position = savePos;
260 // return the total number of items
261 return used;
262 }
263 }
264
265 /**
266 * Return a value containing all the items in the sequence returned by this
267 * SequenceIterator
268 *
269 * @return the corresponding value
270 */
271
272 /*@Nullable*/
273 public GroundedValue<T> materialize() throws XPathException {
274 if (state == ALL_READ) {
275 return makeExtent();
276 } else if (state == EMPTY) {
277 return EmptySequence.getInstance();
278 } else {
279 // save the current position
280 int savePos = position;
281 // fill the reservoir
282 while (next() != null) {
283 }
284 // reset the current position
285 position = savePos;
286 // return all the items
287 return makeExtent();
288 }
289 }
290
291 private GroundedValue<T> makeExtent() {
292 if (used == reservoir.length) {
293 if (used == 0) {
294 return EmptySequence.getInstance();
295 } else if (used == 1) {
296 T item = reservoir[0];
297 return (Item<T>)reservoir[0];
298 } else {
299 return new SequenceExtent<>(reservoir);
300 }
301 } else {
302 return SequenceExtent.makeSequenceExtent(Arrays.asList(reservoir).subList(0, used));
303 }
304 }
305
306 @Override
307 public GroundedValue<T> getResidue() throws XPathException {
308 if (state == EMPTY || position >= used || position == -2) {
309 return EmptySequence.getInstance();
310 } else if (state == ALL_READ) {
311 return SequenceExtent.makeSequenceExtent(Arrays.asList(reservoir).subList(position + 1, used));
312 } else {
313 // save the current position
314 int savePos = position;
315 // fill the reservoir
316 while (next() != null) {
317 }
318 // reset the current position
319 position = savePos;
320 // return all the items
321 return SequenceExtent.makeSequenceExtent(Arrays.asList(reservoir).subList(position + 1, used));
322 }
323 }
324
325
326 /**
327 * Get properties of this iterator, as a bit-significant integer.
328 *
329 * @return the properties of this iterator. This will be some combination of
330 * properties such as {@link #GROUNDED} and {@link #LAST_POSITION_FINDER}. It is always
331 * acceptable to return the value zero, indicating that there are no known special properties.
332 */
333
334 public int getProperties() {
335 // bug 1740 shows that it is better to report the iterator as grounded even though this
336 // may trigger eager evaluation of the underlying sequence.
337 return GROUNDED | LAST_POSITION_FINDER;
338 }
339 }
340
341 }
342