Qt 4.8
qnodesort.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qcommonsequencetypes_p.h"
43 #include "qdeduplicateiterator_p.h"
44 #include "qnodesort_p.h"
45 
47 
48 using namespace QPatternist;
49 
51 {
52 }
53 
55  const Item &n2)
56 {
57  Q_ASSERT(n1.isNode());
58  Q_ASSERT(n2.isNode());
59 
60  if(n1.asNode().model() == n2.asNode().model())
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 
74 {
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())
82  else if(nodes.first().isAtomicValue())
83  return makeListIterator(nodes);
84  else
85  {
86  qSort(nodes.begin(), nodes.end(), lessThanUsingNodeModel);
87 
88  return Item::Iterator::Ptr(new DeduplicateIterator(nodes));
89  }
90 }
91 
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(operand.data(), sort);
100  return sort;
101 }
102 
104 {
105  const Expression::Ptr me(SingleContainer::compress(context));
106 
107  /* It make no sense to sort & deduplicate a single node. */
109  return me;
110  else
111  return m_operand;
112 }
113 
115 {
116  return m_operand->staticType();
117 }
118 
120 {
121  SequenceType::List result;
123  return result;
124 }
125 
128 {
129  return visitor->visit(this);
130 }
131 
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  */
140 }
141 
virtual ExpressionVisitorResult::Ptr accept(const ExpressionVisitor::Ptr &visitor) const
Definition: qnodesort.cpp:127
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
DocumentOrder compareOrder(const QXmlNodeModelIndex &other) const
Definition: qitem_p.h:481
static const SequenceType::Ptr ZeroOrMoreItems
NodeSortExpression(const Expression::Ptr &operand)
Definition: qnodesort.cpp:50
QAbstractXmlForwardIterator< T >::Ptr makeListIterator(const QList< T > &list)
bool isNode() const
Determines whether this item is an atomic value, or a node.
Definition: qitem_p.h:349
virtual Item::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &context) const
Definition: qnodesort.cpp:73
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
static const EmptyIterator< Item >::Ptr emptyIterator
virtual SequenceType::Ptr staticType() const
Definition: qnodesort.cpp:114
virtual Expression::Ptr compress(const StaticContext::Ptr &context)
QExplicitlySharedDataPointer< QAbstractXmlForwardIterator< Item > > Ptr
A smart pointer wrapping an instance of a QAbstractXmlForwardIterator subclass.
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
T * data() const
Returns a pointer to the shared data object.
Definition: qshareddata.h:145
virtual SequenceType::List expectedOperandTypes() const
Definition: qnodesort.cpp:119
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
virtual Expression::Ptr compress(const StaticContext::Ptr &context)
Definition: qnodesort.cpp:103
static void sort(T *array, int count, LessThan lessThan)
virtual Properties properties() const
Definition: qnodesort.cpp:132
The namespace for the internal API of QtXmlPatterns.
Performs deduplication of the nodes on its source list.
virtual Cardinality cardinality() const =0
const QAbstractXmlNodeModel * model() const
Returns the QAbstractXmlNodeModel that this node index refers to.
void qSort(RandomAccessIterator start, RandomAccessIterator end)
Definition: qalgorithms.h:177
virtual SequenceType::Ptr staticType() const =0
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
static bool lessThanUsingNodeModel(const Item &n1, const Item &n2)
Definition: qnodesort.cpp:54
const QXmlNodeModelIndex & asNode() const
Definition: qitem_p.h:379
Represents an item in the XPath 2.0 Data Model.
Definition: qitem_p.h:182
The QFlags class provides a type-safe way of storing OR-combinations of enum values.
Definition: qglobal.h:2313
void wrapExpressionWith(const SourceLocationReflection *const existingNode, const QExplicitlySharedDataPointer< Expression > &newNode)
Ensures source locations are handled in such a manner that existingNode wraps newNode.
static Expression::Ptr wrapAround(const Expression::Ptr &operand, const StaticContext::Ptr &context)
Definition: qnodesort.cpp:92
virtual Item::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &context) const
Base class for expressions that has exactly one operand.
#define Q_FUNC_INFO
Definition: qglobal.h:1871