1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtXmlPatterns module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qcommonsequencetypes_p.h" |
41 | #include "qdeduplicateiterator_p.h" |
42 | #include "qnodesort_p.h" |
43 | |
44 | #include <algorithm> |
45 | |
46 | QT_BEGIN_NAMESPACE |
47 | |
48 | using namespace QPatternist; |
49 | |
50 | NodeSortExpression::NodeSortExpression(const Expression::Ptr &op) : SingleContainer(op) |
51 | { |
52 | } |
53 | |
54 | bool NodeSortExpression::lessThanUsingNodeModel(const Item &n1, |
55 | const Item &n2) |
56 | { |
57 | Q_ASSERT(n1.isNode()); |
58 | Q_ASSERT(n2.isNode()); |
59 | |
60 | if(n1.asNode().model() == n2.asNode().model()) |
61 | return n1.asNode().compareOrder(other: n2.asNode()) == QXmlNodeModelIndex::Precedes; |
62 | else |
63 | { |
64 | /* The two nodes are from different trees. The sort order is implementation |
65 | * defined, but it must be stable. |
66 | * |
67 | * We do this by looking at the pointer difference. The value means nothing, |
68 | * but it is stable, and that's what we're looking for. */ |
69 | return n1.asNode().model() - n2.asNode().model() < 0; |
70 | } |
71 | } |
72 | |
73 | Item::Iterator::Ptr NodeSortExpression::evaluateSequence(const DynamicContext::Ptr &context) const |
74 | { |
75 | Q_ASSERT_X(m_operand->staticType()->cardinality().allowsMany(), Q_FUNC_INFO, |
76 | "It makes no sense to sort a single node." ); |
77 | |
78 | Item::List nodes(m_operand->evaluateSequence(context)->toList()); |
79 | |
80 | if(nodes.isEmpty()) |
81 | return CommonValues::emptyIterator; |
82 | else if(nodes.first().isAtomicValue()) |
83 | return makeListIterator(list: nodes); |
84 | else |
85 | { |
86 | std::sort(first: nodes.begin(), last: nodes.end(), comp: lessThanUsingNodeModel); |
87 | |
88 | return Item::Iterator::Ptr(new DeduplicateIterator(nodes)); |
89 | } |
90 | } |
91 | |
92 | Expression::Ptr NodeSortExpression::wrapAround(const Expression::Ptr &operand, |
93 | const StaticContext::Ptr &context) |
94 | { |
95 | Q_ASSERT(operand); |
96 | Q_ASSERT(context); |
97 | |
98 | const Expression::Ptr sort(new NodeSortExpression(operand)); |
99 | context->wrapExpressionWith(existingNode: operand.data(), newNode: sort); |
100 | return sort; |
101 | } |
102 | |
103 | Expression::Ptr NodeSortExpression::compress(const StaticContext::Ptr &context) |
104 | { |
105 | const Expression::Ptr me(SingleContainer::compress(context)); |
106 | |
107 | /* It make no sense to sort & deduplicate a single node. */ |
108 | if(m_operand->staticType()->cardinality().allowsMany()) |
109 | return me; |
110 | else |
111 | return m_operand; |
112 | } |
113 | |
114 | SequenceType::Ptr NodeSortExpression::staticType() const |
115 | { |
116 | return m_operand->staticType(); |
117 | } |
118 | |
119 | SequenceType::List NodeSortExpression::expectedOperandTypes() const |
120 | { |
121 | SequenceType::List result; |
122 | result.append(t: CommonSequenceTypes::ZeroOrMoreItems); |
123 | return result; |
124 | } |
125 | |
126 | ExpressionVisitorResult::Ptr |
127 | NodeSortExpression::accept(const ExpressionVisitor::Ptr &visitor) const |
128 | { |
129 | return visitor->visit(this); |
130 | } |
131 | |
132 | Expression::Properties NodeSortExpression::properties() const |
133 | { |
134 | /* The reason we disable elimination is that the assert for sorting a |
135 | * single node in evaluateSequence() triggers unless our compress() routine |
136 | * has been run. Anyhow, it's not that we would manage to write away anyway, |
137 | * since the node source in most(all?) cases prevents it. |
138 | */ |
139 | return AffectsOrderOnly | DisableElimination; |
140 | } |
141 | |
142 | QT_END_NAMESPACE |
143 | |