Debian Codegraph @

net/sf/saxon/expr/sort/MergeIterator.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 net.sf.saxon.om.SequenceIterator;
11 import net.sf.saxon.trans.XPathException;
12 import net.sf.saxon.tree.iter.LookaheadIterator;
13 import net.sf.saxon.type.Type;
14 import net.sf.saxon.value.AtomicValue;
15 import net.sf.saxon.value.ObjectValue;
16
17 /**
18 * An iterator representing the sorted merge of two merge inputs, retaining all duplicates.
19 * This iterator simply returns the items from all the inputs merged into a single sequence;
20 * it does not do any grouping of adjacent items that share the same merge key.
21 */
22
23 public class MergeIterator implements
24 SequenceIterator /*<ObjectValue<ItemWithMergeKeys>>*/,
25 LookaheadIterator /*<ObjectValue<ItemWithMergeKeys>>*/ {
26
27 private SequenceIterator e1;
28 private SequenceIterator e2;
29 private ObjectValue<ItemWithMergeKeys> nextItem1 = null;
30 private ObjectValue<ItemWithMergeKeys> nextItem2 = null;
31 private ItemOrderComparer comparer;
32
33 /**
34 * Create the iterator. The two input iterators must return nodes in merge key
35 * order for this to work.
36 *
37 * @param p1 iterator over the first operand sequence (in document order)
38 * @param p2 iterator over the second operand sequence
39 * @param comparer used to test whether nodes are in document order. Different versions
40 * are used for intra-document and cross-document operations
41 * @throws XPathException if an error occurs reading from either input iterator
42 */
43
44 public MergeIterator(SequenceIterator /*<ObjectValue<ItemWithMergeKeys>>*/ p1,
45 SequenceIterator /*<ObjectValue<ItemWithMergeKeys>>*/ p2,
46 ItemOrderComparer comparer) throws XPathException {
47 this.e1 = p1;
48 this.e2 = p2;
49 this.comparer = comparer;
50
51 nextItem1 = (ObjectValue<ItemWithMergeKeys>)e1.next();
52 nextItem2 = (ObjectValue<ItemWithMergeKeys>)e2.next();
53 }
54
55 public boolean hasNext() {
56 return nextItem1 != null || nextItem2 != null;
57 }
58
59 public ObjectValue<ItemWithMergeKeys> next() throws XPathException {
60
61 // main merge loop: take an item from whichever set has the lower value otherwise take value from first and move iterator on by one.
62
63 if (nextItem1 != null && nextItem2 != null) {
64 int c;
65 try {
66 c = comparer.compare(nextItem1, nextItem2);
67 } catch (ClassCastException e) {
68 ItemWithMergeKeys i1 = nextItem1.getObject();
69 ItemWithMergeKeys i2 = nextItem2.getObject();
70 AtomicValue a1 = i1.sortKeyValues.get(0);
71 AtomicValue a2 = i2.sortKeyValues.get(0);
72 XPathException err = new XPathException("Merge key values are of non-comparable types ("
73 + Type.displayTypeName(a1)
74 + " and " + Type.displayTypeName(a2) + ")", "XTTE2230");
75 err.setIsTypeError(true);
76 throw err;
77 }
78 if (c <= 0) {
79 ObjectValue<ItemWithMergeKeys> current = nextItem1;
80 nextItem1 = (ObjectValue<ItemWithMergeKeys>)e1.next();
81 return current;
82
83 } else /* (c > 0) */ {
84 ObjectValue<ItemWithMergeKeys> current = nextItem2;
85 nextItem2 = (ObjectValue<ItemWithMergeKeys>)e2.next();
86 return current;
87 }
88 }
89
90 // collect the remaining items from whichever set has a residue
91
92 if (nextItem1 != null) {
93 ObjectValue<ItemWithMergeKeys> current = nextItem1;
94 nextItem1 = (ObjectValue<ItemWithMergeKeys>)e1.next();
95 return current;
96 }
97 if (nextItem2 != null) {
98 ObjectValue<ItemWithMergeKeys> current = nextItem2;
99 nextItem2 = (ObjectValue<ItemWithMergeKeys>)e2.next();
100 return current;
101 }
102 return null;
103 }
104
105 public void close() {
106 e1.close();
107 e2.close();
108 }
109
110 /**
111 * Get properties of this iterator, as a bit-significant integer.
112 *
113 * @return the properties of this iterator. This will be some combination of
114 * properties such as {@link #GROUNDED}, {@link #LAST_POSITION_FINDER},
115 * and {@link #LOOKAHEAD}. It is always
116 * acceptable to return the value zero, indicating that there are no known special properties.
117 * It is acceptable for the properties of the iterator to change depending on its state.
118 */
119
120 public int getProperties() {
121 return LOOKAHEAD;
122 }
123
124 }