Debian Codegraph @

net/sf/saxon/expr/sort/SortedGroupIterator.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.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 * A SortedGroupIterator is a modified SortedIterator. It sorts a sequence of groups,
22 * and is itself a GroupIterator. The modifications retain extra information about
23 * the items being sorted. The items are each the leading item of a group, and as well
24 * as the item itself, the iterator preserves information about the group: specifically,
25 * an iterator over the items in the group, and the value of the grouping key (if any).
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 * Override the method that builds the array of values and sort keys.
41 *
42 * @throws XPathException in the event of a dynamic error
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 // initialise the array with data
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 /*@Nullable*/
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 //#if EE==true
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 //#endif
117
118 }
119