| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 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 |
|
| 37 |
private static final int UNREAD = 0; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
private static final int MAYBE_MORE = 1; |
| 42 |
|
| 43 |
|
| 44 |
private static final int ALL_READ = 3; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
private static final int BUSY = 4; |
| 49 |
|
| 50 |
|
| 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 |
|
| 93 |
|
| 94 |
XPathException de = new XPathException("Attempt to access a variable while it is being evaluated"); |
| 95 |
de.setErrorCode("XTDE0640"); |
| 96 |
|
| 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 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 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 |
|
| 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 |
|
| 152 |
return reservoir[n]; |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 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 |
|
| 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 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
public final class ProgressiveIterator |
| 189 |
implements SequenceIterator<T>, LastPositionFinder, GroundedIterator<T> { |
| 190 |
|
| 191 |
int position = -1; |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public ProgressiveIterator() { |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public MemoSequence getMemoSequence() { |
| 207 |
return MemoSequence.this; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
public T next() throws XPathException { |
| 212 |
synchronized (MemoSequence.this) { |
| 213 |
|
| 214 |
if (position == -2) { |
| 215 |
return null; |
| 216 |
} |
| 217 |
if (++position < used) { |
| 218 |
assert reservoir != null; |
| 219 |
return reservoir[position]; |
| 220 |
} else if (state == ALL_READ) { |
| 221 |
|
| 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 |
|
| 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 |
|
| 255 |
int savePos = position; |
| 256 |
|
| 257 |
while (next() != null) {} |
| 258 |
|
| 259 |
position = savePos; |
| 260 |
|
| 261 |
return used; |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 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 |
|
| 280 |
int savePos = position; |
| 281 |
|
| 282 |
while (next() != null) { |
| 283 |
} |
| 284 |
|
| 285 |
position = savePos; |
| 286 |
|
| 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 |
|
| 314 |
int savePos = position; |
| 315 |
|
| 316 |
while (next() != null) { |
| 317 |
} |
| 318 |
|
| 319 |
position = savePos; |
| 320 |
|
| 321 |
return SequenceExtent.makeSequenceExtent(Arrays.asList(reservoir).subList(position + 1, used)); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
public int getProperties() { |
| 335 |
|
| 336 |
|
| 337 |
return GROUNDED | LAST_POSITION_FINDER; |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
} |
| 342 |
|