Debian Codegraph @

net/sf/saxon/expr/SingletonIntersectExpression.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;
9
10 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
11 import net.sf.saxon.expr.parser.ExpressionTool;
12 import net.sf.saxon.expr.parser.ExpressionVisitor;
13 import net.sf.saxon.expr.parser.RebindingMap;
14 import net.sf.saxon.om.Item;
15 import net.sf.saxon.om.NodeInfo;
16 import net.sf.saxon.om.SequenceIterator;
17 import net.sf.saxon.trans.XPathException;
18 import net.sf.saxon.tree.iter.EmptyIterator;
19 import net.sf.saxon.tree.iter.SingletonIterator;
20
21 /**
22 * This expression is equivalent to (A intersect B) in the case where A has cardinality
23 * zero-or-one. This is handled as a special case because the standard sort-merge algorithm
24 * involves an unnecessary sort on B.
25 */
26 public class SingletonIntersectExpression extends VennExpression {
27
28 /**
29 * Special case of an intersect expression where the first argument is a singleton
30 *
31 * @param p1 the first argument, always a singleton
32 * @param op the operator, always Token.INTERSECT
33 * @param p2 the second argument
34 */
35
36 public SingletonIntersectExpression(final Expression p1, final int op, final Expression p2) {
37 super(p1, op, p2);
38 }
39
40 /**
41 * Simplify the expression
42 *
43 */
44 @Override
45 public Expression simplify() throws XPathException {
46 return this;
47 }
48
49
50 /**
51 * Perform optimisation of an expression and its subexpressions.
52 * <p>This method is called after all references to functions and variables have been resolved
53 * to the declaration of the function or variable, and after all type checking has been done.</p>
54 *
55 * @param visitor an expression visitor
56 * @param contextItemType the static type of "." at the point where this expression is invoked.
57 * The parameter is set to null if it is known statically that the context item will be undefined.
58 * If the type of the context item is not known statically, the argument is set to
59 * {@link net.sf.saxon.type.Type#ITEM_TYPE}
60 * @return the original expression, rewritten if appropriate to optimize execution
61 * @throws net.sf.saxon.trans.XPathException
62 * if an error is discovered during this phase
63 * (typically a type error)
64 */
65 @Override
66 public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException {
67 return this;
68 }
69
70
71 /**
72 * Copy an expression. This makes a deep copy.
73 *
74 * @return the copy of the original expression
75 * @param rebindings
76 */
77
78 /*@NotNull*/
79 @Override
80 public Expression copy(RebindingMap rebindings) {
81 SingletonIntersectExpression exp = new SingletonIntersectExpression(getLhsExpression().copy(rebindings), operator, getRhsExpression().copy(rebindings));
82 ExpressionTool.copyLocationInfo(this, exp);
83 return exp;
84 }
85
86 /**
87 * Iterate over the value of the expression. The result will always be sorted in document order,
88 * with duplicates eliminated
89 *
90 * @param c The context for evaluation
91 * @return a SequenceIterator representing the union of the two operands
92 */
93
94 /*@NotNull*/
95 @Override
96 public SequenceIterator<?> iterate(XPathContext c) throws XPathException {
97 NodeInfo m = (NodeInfo) getLhsExpression().evaluateItem(c);
98 if (m == null) {
99 return EmptyIterator.getInstance();
100 }
101 SequenceIterator iter = getRhsExpression().iterate(c);
102 NodeInfo n;
103 while ((n = (NodeInfo) iter.next()) != null) {
104 if (n.equals(m)) {
105 return SingletonIterator.makeIterator(m);
106 }
107 }
108 return EmptyIterator.getInstance();
109 }
110
111 /**
112 * Get the effective boolean value. In the case of a union expression, this
113 * is reduced to an OR expression, for efficiency
114 */
115
116 @Override
117 public boolean effectiveBooleanValue(XPathContext c) throws XPathException {
118 NodeInfo m = (NodeInfo) getLhsExpression().evaluateItem(c);
119 return m != null && containsNode(getRhsExpression().iterate(c), m);
120 }
121
122 /**
123 * Ask whether the sequence supplied in the first argument contains the node
124 * supplied in the second
125 * @param iter an iterator over nodes. The iterator will be closed if the node is found.
126 * @param m the node to be tested
127 * @return true if (and only if) the sequence contains the node
128 * @throws XPathException if evaluating the iterator fails
129 */
130
131 public static boolean containsNode(SequenceIterator iter, NodeInfo m) throws XPathException {
132 NodeInfo n;
133 while ((n = (NodeInfo) iter.next()) != null) {
134 if (n.equals(m)) {
135 iter.close();
136 return true;
137 }
138 }
139 return false;
140 }
141
142 public String getExpressionName() {
143 return "singleton-intersect";
144 }
145
146 /**
147 * Display the operator used by this binary expression
148 *
149 * @return String representation of the operator (for diagnostic display only)
150 */
151
152 protected String displayOperator() {
153 return "among";
154 }
155
156 /**
157 * Get the element name used to identify this expression in exported expression format
158 *
159 * @return the element name used to identify this expression
160 */
161 @Override
162 protected String tag() {
163 return "among";
164 }
165 }
166