| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.sort; |
| 9 |
|
| 10 |
import com.saxonica.ee.stream.ManualGroupIterator; |
| 11 |
import net.sf.saxon.Configuration; |
| 12 |
import net.sf.saxon.expr.LastPositionFinder; |
| 13 |
import net.sf.saxon.expr.XPathContext; |
| 14 |
import net.sf.saxon.expr.XPathContextMajor; |
| 15 |
import net.sf.saxon.om.*; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
|
| 18 |
import java.util.Arrays; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class SortedGroupIterator extends SortedIterator implements GroupIterator { |
| 29 |
|
| 30 |
public SortedGroupIterator(XPathContext context, |
| 31 |
GroupIterator base, |
| 32 |
SortKeyEvaluator sortKeyEvaluator, |
| 33 |
AtomicComparer[] comparators |
| 34 |
) { |
| 35 |
super(context, base, sortKeyEvaluator, comparators, true); |
| 36 |
setHostLanguage(Configuration.XSLT); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
protected void buildArray() throws XPathException { |
| 46 |
int allocated; |
| 47 |
if ((base.getProperties() & SequenceIterator.LAST_POSITION_FINDER) != 0) { |
| 48 |
allocated = ((LastPositionFinder) base).getLength(); |
| 49 |
} else { |
| 50 |
allocated = 100; |
| 51 |
} |
| 52 |
|
| 53 |
values = new GroupToBeSorted[allocated]; |
| 54 |
count = 0; |
| 55 |
|
| 56 |
XPathContextMajor c2 = context.newContext(); |
| 57 |
c2.setCurrentIterator((FocusIterator<Item<?>>)base); |
| 58 |
GroupIterator groupIter = (GroupIterator)((FocusTrackingIterator)base).getUnderlyingIterator(); |
| 59 |
c2.setCurrentGroupIterator(groupIter); |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
Item item; |
| 64 |
while ((item = base.next()) != null) { |
| 65 |
if (count == allocated) { |
| 66 |
allocated *= 2; |
| 67 |
values = Arrays.copyOf(values, allocated); |
| 68 |
} |
| 69 |
GroupToBeSorted gtbs = new GroupToBeSorted(comparators.length); |
| 70 |
values[count] = gtbs; |
| 71 |
gtbs.value = item; |
| 72 |
for (int n = 0; n < comparators.length; n++) { |
| 73 |
gtbs.sortKeyValues[n] = sortKeyEvaluator.evaluateSortKey(n, c2); |
| 74 |
} |
| 75 |
gtbs.originalPosition = count++; |
| 76 |
gtbs.currentGroupingKey = groupIter.getCurrentGroupingKey(); |
| 77 |
gtbs.currentGroup = new MemoSequence<>(groupIter.iterateCurrentGroup()); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
public AtomicSequence getCurrentGroupingKey() { |
| 83 |
return ((GroupToBeSorted) values[position - 1]).currentGroupingKey; |
| 84 |
} |
| 85 |
|
| 86 |
public SequenceIterator<?> iterateCurrentGroup() throws XPathException { |
| 87 |
return ((GroupToBeSorted) values[position - 1]).currentGroup.iterate(); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public ManualGroupIterator getSnapShot(XPathContext context) throws XPathException { |
| 93 |
return new ManualSortedGroupIterator(); |
| 94 |
} |
| 95 |
|
| 96 |
private class ManualSortedGroupIterator extends ManualGroupIterator { |
| 97 |
|
| 98 |
ManualSortedGroupIterator() { |
| 99 |
super((Item) values[position - 1].value, position); |
| 100 |
setLastPositionFinder(new LastPositionFinder() { |
| 101 |
public int getLength() throws XPathException { |
| 102 |
return values.length; |
| 103 |
} |
| 104 |
}); |
| 105 |
} |
| 106 |
|
| 107 |
public AtomicSequence getCurrentGroupingKey() { |
| 108 |
return ((GroupToBeSorted) values[position() - 1]).currentGroupingKey; |
| 109 |
} |
| 110 |
|
| 111 |
public SequenceIterator iterateCurrentGroup() throws XPathException { |
| 112 |
return ((GroupToBeSorted) values[position() - 1]).currentGroup.iterate(); |
| 113 |
} |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
} |
| 119 |
|