| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.sort; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.expr.ErrorIterator; |
| 12 |
import net.sf.saxon.expr.LastPositionFinder; |
| 13 |
import net.sf.saxon.expr.XPathContext; |
| 14 |
import net.sf.saxon.om.FocusTrackingIterator; |
| 15 |
import net.sf.saxon.om.Item; |
| 16 |
import net.sf.saxon.om.SequenceIterator; |
| 17 |
import net.sf.saxon.om.StandardNames; |
| 18 |
import net.sf.saxon.trans.NoDynamicContextException; |
| 19 |
import net.sf.saxon.trans.XPathException; |
| 20 |
import net.sf.saxon.tree.iter.LookaheadIterator; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class SortedIterator implements SequenceIterator<Item<?>>, LastPositionFinder, LookaheadIterator<Item<?>>, Sortable { |
| 27 |
|
| 28 |
|
| 29 |
protected SequenceIterator<?> base; |
| 30 |
|
| 31 |
|
| 32 |
protected SortKeyEvaluator sortKeyEvaluator; |
| 33 |
|
| 34 |
|
| 35 |
protected AtomicComparer[] comparators; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
protected ObjectToBeSorted[] values; |
| 42 |
|
| 43 |
|
| 44 |
protected int count = -1; |
| 45 |
|
| 46 |
|
| 47 |
protected int position = 0; |
| 48 |
|
| 49 |
|
| 50 |
protected XPathContext context; |
| 51 |
|
| 52 |
|
| 53 |
private int hostLanguage; |
| 54 |
|
| 55 |
protected SortedIterator() { |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public SortedIterator(XPathContext context, SequenceIterator<?> base, |
| 69 |
SortKeyEvaluator sortKeyEvaluator, AtomicComparer[] comparators, boolean createNewContext) { |
| 70 |
|
| 71 |
if (createNewContext) { |
| 72 |
this.context = context.newMinorContext(); |
| 73 |
this.base = this.context.trackFocus(base); |
| 74 |
this.context.setTemporaryOutputState(StandardNames.XSL_SORT); |
| 75 |
|
| 76 |
} else { |
| 77 |
this.base = base; |
| 78 |
this.context = context; |
| 79 |
} |
| 80 |
|
| 81 |
this.sortKeyEvaluator = sortKeyEvaluator; |
| 82 |
this.comparators = new AtomicComparer[comparators.length]; |
| 83 |
for (int n = 0; n < comparators.length; n++) { |
| 84 |
this.comparators[n] = comparators[n].provideContext(context); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public void setHostLanguage(int language) { |
| 98 |
hostLanguage = language; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public boolean hasNext() { |
| 113 |
if (position < 0) { |
| 114 |
return false; |
| 115 |
} |
| 116 |
if (count < 0) { |
| 117 |
|
| 118 |
if (base instanceof LookaheadIterator) { |
| 119 |
return ((LookaheadIterator) base).hasNext(); |
| 120 |
} else { |
| 121 |
try { |
| 122 |
doSort(); |
| 123 |
return count > 0; |
| 124 |
} catch (XPathException err) { |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
count = -1; |
| 129 |
base = new FocusTrackingIterator<>(new ErrorIterator(err)); |
| 130 |
return true; |
| 131 |
} |
| 132 |
} |
| 133 |
} else { |
| 134 |
return position < count; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public Item next() throws XPathException { |
| 144 |
if (position < 0) { |
| 145 |
return null; |
| 146 |
} |
| 147 |
if (count < 0) { |
| 148 |
doSort(); |
| 149 |
} |
| 150 |
if (position < count) { |
| 151 |
return (Item) values[position++].value; |
| 152 |
} else { |
| 153 |
position = -1; |
| 154 |
return null; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
public int getLength() throws XPathException { |
| 159 |
if (count < 0) { |
| 160 |
doSort(); |
| 161 |
} |
| 162 |
return count; |
| 163 |
} |
| 164 |
|
| 165 |
public void close() { |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
public int getProperties() { |
| 179 |
return LAST_POSITION_FINDER; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
protected void buildArray() throws XPathException { |
| 189 |
int allocated; |
| 190 |
if ((base.getProperties() & SequenceIterator.LAST_POSITION_FINDER) != 0) { |
| 191 |
allocated = ((LastPositionFinder) base).getLength(); |
| 192 |
} else { |
| 193 |
allocated = 100; |
| 194 |
} |
| 195 |
|
| 196 |
values = new ItemToBeSorted[allocated]; |
| 197 |
count = 0; |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
Item item; |
| 202 |
while ((item = base.next()) != null) { |
| 203 |
if (count == allocated) { |
| 204 |
allocated *= 2; |
| 205 |
ObjectToBeSorted[] nk2 = new ObjectToBeSorted[allocated]; |
| 206 |
System.arraycopy(values, 0, nk2, 0, count); |
| 207 |
values = nk2; |
| 208 |
} |
| 209 |
ItemToBeSorted itbs = new ItemToBeSorted(comparators.length); |
| 210 |
values[count] = itbs; |
| 211 |
itbs.value = item; |
| 212 |
|
| 213 |
|
| 214 |
for (int n = 0; n < comparators.length; n++) { |
| 215 |
itbs.sortKeyValues[n] = sortKeyEvaluator.evaluateSortKey(n, context); |
| 216 |
} |
| 217 |
|
| 218 |
itbs.originalPosition = count++; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
if (allocated * 2 < count || (allocated - count) > 2000) { |
| 224 |
ObjectToBeSorted[] nk2 = new ObjectToBeSorted[count]; |
| 225 |
System.arraycopy(values, 0, nk2, 0, count); |
| 226 |
values = nk2; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
private void doSort() throws XPathException { |
| 231 |
buildArray(); |
| 232 |
if (count < 2) { |
| 233 |
return; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
try { |
| 240 |
GenericSorter.quickSort(0, count, this); |
| 241 |
} catch (ClassCastException e) { |
| 242 |
XPathException err = new XPathException("Non-comparable types found while sorting: " + e.getMessage()); |
| 243 |
if (hostLanguage == Configuration.XSLT) { |
| 244 |
err.setErrorCode("XTDE1030"); |
| 245 |
} else { |
| 246 |
err.setErrorCode("XPTY0004"); |
| 247 |
} |
| 248 |
throw err; |
| 249 |
} |
| 250 |
|
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
public int compare(int a, int b) { |
| 261 |
try { |
| 262 |
for (int i = 0; i < comparators.length; i++) { |
| 263 |
int comp = comparators[i].compareAtomicValues( |
| 264 |
values[a].sortKeyValues[i], values[b].sortKeyValues[i]); |
| 265 |
if (comp != 0) { |
| 266 |
|
| 267 |
return comp; |
| 268 |
} |
| 269 |
} |
| 270 |
} catch (NoDynamicContextException e) { |
| 271 |
throw new AssertionError("Sorting without dynamic context: " + e.getMessage()); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
return values[a].originalPosition - values[b].originalPosition; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
public void swap(int a, int b) { |
| 284 |
ObjectToBeSorted temp = values[a]; |
| 285 |
values[a] = values[b]; |
| 286 |
values[b] = temp; |
| 287 |
} |
| 288 |
|
| 289 |
} |
| 290 |
|