| 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.tree.iter; |
| 9 | |
| 10 | import net.sf.saxon.expr.LastPositionFinder; |
| 11 | import net.sf.saxon.om.GroundedValue; |
| 12 | import net.sf.saxon.om.Item; |
| 13 | import net.sf.saxon.om.NodeInfo; |
| 14 | import net.sf.saxon.om.SequenceIterator; |
| 15 | import net.sf.saxon.value.SequenceExtent; |
| 16 | |
| 17 | import java.util.Arrays; |
| 18 | import java.util.List; |
| 19 | |
| 20 | /** |
| 21 | * ArrayIterator is used to enumerate items held in an array. |
| 22 | * The items are always held in the correct sorted order for the sequence. |
| 23 | * |
| 24 | * @author Michael H. Kay |
| 25 | */ |
| 26 | |
| 27 | |
| 28 | public class ArrayIterator<T extends Item<?>> implements UnfailingIterator<T>, |
| 29 | LastPositionFinder, LookaheadIterator<T>, GroundedIterator<T> { |
| 30 | |
| 31 | protected T[] items; |
| 32 | private int index; // position in array of current item, zero-based |
| 33 | // set equal to end+1 when all the items required have been read. |
| 34 | protected int start; // position of first item to be returned, zero-based |
| 35 | protected int end; // position of first item that is NOT returned, zero-based |
| 36 | |
| 37 | /** |
| 38 | * Create an iterator over all the items in an array |
| 39 | * |
| 40 | * @param nodes the array (of any items, not necessarily nodes) to be |
| 41 | * processed by the iterator |
| 42 | */ |
| 43 | |
| 44 | public ArrayIterator(/*@NotNull*/ T[] nodes) { |
| 45 | items = nodes; |
| 46 | start = 0; |
| 47 | end = nodes.length; |
| 48 | index = 0; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Create an iterator over a range of an array. Note that the start position is zero-based |
| 53 | * |
| 54 | * @param items the array (of nodes or simple values) to be processed by |
| 55 | * the iterator |
| 56 | * @param start the position of the first item to be processed |
| 57 | * (numbering from zero). Must be between zero and nodes.length-1; if not, |
| 58 | * undefined exceptions are likely to occur. |
| 59 | * @param end position of first item that is NOT returned, zero-based. Must be |
| 60 | * beween 1 and nodes.length; if not, undefined exceptions are likely to occur. |
| 61 | */ |
| 62 | |
| 63 | public ArrayIterator(T[] items, int start, int end) { |
| 64 | this.items = items; |
| 65 | this.end = end; |
| 66 | this.start = start; |
| 67 | index = start; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Create a new ArrayIterator over the same items, |
| 72 | * with a different start point and end point |
| 73 | * |
| 74 | * @param min the start position (1-based) of the new ArrayIterator |
| 75 | * relative to the original |
| 76 | * @param max the end position (1-based) of the last item to be delivered |
| 77 | * by the new ArrayIterator, relative to the original. For example, min=2, max=3 |
| 78 | * delivers the two items ($base[2], $base[3]). Set this to Integer.MAX_VALUE if |
| 79 | * there is no end limit. |
| 80 | * @return an iterator over the items between the min and max positions |
| 81 | */ |
| 82 | |
| 83 | public SequenceIterator<T> makeSliceIterator(int min, int max) { |
| 84 | T[] items = getArray(); |
| 85 | int currentStart = getStartPosition(); |
| 86 | int currentEnd = getEndPosition(); |
| 87 | if (min < 1) { |
| 88 | min = 1; |
| 89 | } |
| 90 | int newStart = currentStart + (min - 1); |
| 91 | if (newStart < currentStart) { |
| 92 | newStart = currentStart; |
| 93 | } |
| 94 | int newEnd = max == Integer.MAX_VALUE ? currentEnd : newStart + max - min + 1; |
| 95 | if (newEnd > currentEnd) { |
| 96 | newEnd = currentEnd; |
| 97 | } |
| 98 | if (newEnd <= newStart) { |
| 99 | return EmptyIterator.emptyIterator(); |
| 100 | } |
| 101 | return new ArrayIterator<>(items, newStart, newEnd); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Test whether there are any more items |
| 106 | * |
| 107 | * @return true if there are more items |
| 108 | */ |
| 109 | |
| 110 | public boolean hasNext() { |
| 111 | return index < end; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Get the next item in the array |
| 116 | * |
| 117 | * @return the next item in the array |
| 118 | */ |
| 119 | |
| 120 | /*@Nullable*/ |
| 121 | public T next() { |
| 122 | if (index >= end) { |
| 123 | index = end + 1; |
| 124 | return null; |
| 125 | } |
| 126 | return items[index++]; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Get the number of items in the part of the array being processed |
| 131 | * |
| 132 | * @return the number of items; equivalently, the position of the last |
| 133 | * item |
| 134 | */ |
| 135 | public int getLength() { |
| 136 | return end - start; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Get the underlying array |
| 141 | * |
| 142 | * @return the underlying array being processed by the iterator |
| 143 | */ |
| 144 | |
| 145 | public T[] getArray() { |
| 146 | return items; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Get the initial start position |
| 151 | * |
| 152 | * @return the start position of the iterator in the array (zero-based) |
| 153 | */ |
| 154 | |
| 155 | public int getStartPosition() { |
| 156 | return start; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Get the end position in the array |
| 161 | * |
| 162 | * @return the position in the array (zero-based) of the first item not included |
| 163 | * in the iteration |
| 164 | */ |
| 165 | |
| 166 | public int getEndPosition() { |
| 167 | return end; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Return a SequenceValue containing all the items in the sequence returned by this |
| 172 | * SequenceIterator |
| 173 | * |
| 174 | * @return the corresponding SequenceValue |
| 175 | */ |
| 176 | |
| 177 | /*@NotNull*/ |
| 178 | public GroundedValue<T> materialize() { |
| 179 | SequenceExtent<T> seq; |
| 180 | if (start == 0 && end == items.length) { |
| 181 | seq = new SequenceExtent<>(items); |
| 182 | } else { |
| 183 | List<T> sublist = Arrays.asList(items).subList(start, end); |
| 184 | seq = new SequenceExtent<>(sublist); |
| 185 | } |
| 186 | return seq.reduce(); |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public GroundedValue<T> getResidue() { |
| 191 | SequenceExtent<T> seq; |
| 192 | if (start == 0 && index ==0 && end == items.length) { |
| 193 | seq = new SequenceExtent<>(items); |
| 194 | } else { |
| 195 | List<T> sublist = Arrays.asList(items).subList(start + index, end); |
| 196 | seq = new SequenceExtent<>(sublist); |
| 197 | } |
| 198 | return seq.reduce(); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Get properties of this iterator, as a bit-significant integer. |
| 203 | * |
| 204 | * @return the properties of this iterator. This will be some combination of |
| 205 | * properties such as {@link #GROUNDED}, {@link #LAST_POSITION_FINDER}, |
| 206 | * and {@link #LOOKAHEAD}. It is always |
| 207 | * acceptable to return the value zero, indicating that there are no known special properties. |
| 208 | * It is acceptable for the properties of the iterator to change depending on its state. |
| 209 | */ |
| 210 | |
| 211 | public int getProperties() { |
| 212 | return GROUNDED | LAST_POSITION_FINDER | LOOKAHEAD; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * ArrayIterator.OfNodes is a subclass of ArrayIterator where the array always |
| 217 | * contains Nodes; it therefore implements the AxisIterator interface. |
| 218 | */ |
| 219 | |
| 220 | public static class OfNodes extends ArrayIterator<NodeInfo> implements AxisIterator { |
| 221 | public OfNodes(NodeInfo[] list) { |
| 222 | super(list); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | } |
| 227 |