Qt 4.8
Public Functions | Static Public Functions | Static Private Functions | List of all members
QPatternist::NodeSortExpression Class Reference

De-duplicates and sorts in document order the content that its operand returns. More...

#include <qnodesort_p.h>

Inheritance diagram for QPatternist::NodeSortExpression:
QPatternist::SingleContainer QPatternist::Expression QSharedData QPatternist::CppCastingHelper< Expression > QPatternist::SourceLocationReflection

Public Functions

virtual ExpressionVisitorResult::Ptr accept (const ExpressionVisitor::Ptr &visitor) const
 
virtual Expression::Ptr compress (const StaticContext::Ptr &context)
 
virtual Item::Iterator::Ptr evaluateSequence (const DynamicContext::Ptr &context) const
 
virtual SequenceType::List expectedOperandTypes () const
 
 NodeSortExpression (const Expression::Ptr &operand)
 
virtual Properties properties () const
 
virtual SequenceType::Ptr staticType () const
 
- Public Functions inherited from QPatternist::SingleContainer
virtual bool compressOperands (const StaticContext::Ptr &)
 
virtual Expression::List operands () const
 
virtual void setOperands (const Expression::List &operands)
 
- Public Functions inherited from QPatternist::Expression
virtual const SourceLocationReflectionactualReflection () const
 Returns this. More...
 
virtual void announceFocusType (const ItemType::Ptr &itemType)
 
Properties deepProperties () const
 Computes the union of properties for this Expression and all its descending children. More...
 
virtual Properties dependencies () const
 
virtual QString description () const
 
virtual bool evaluateEBV (const DynamicContext::Ptr &context) const
 
virtual Item evaluateSingleton (const DynamicContext::Ptr &context) const
 
virtual void evaluateToSequenceReceiver (const DynamicContext::Ptr &context) const
 
virtual ItemType::Ptr expectedContextItemType () const
 
 Expression ()
 
bool has (const Property prop) const
 
bool hasDependency (const Property prop) const
 
virtual ID id () const
 
bool is (const ID id) const
 
bool isEvaluated () const
 
virtual ItemType::Ptr newFocusType () const
 
virtual QList< QExplicitlySharedDataPointer< OptimizationPass > > optimizationPasses () const
 
virtual PatternPriority patternPriority () const
 
const Expression::Ptrrewrite (const Expression::Ptr &to, const StaticContext::Ptr &context) const
 Rewrites this Expression to to, and return to. More...
 
virtual Expression::Ptr typeCheck (const StaticContext::Ptr &context, const SequenceType::Ptr &reqType)
 
virtual ~Expression ()
 
- Public Functions inherited from QSharedData
 QSharedData ()
 Constructs a QSharedData object with a reference count of 0. More...
 
 QSharedData (const QSharedData &)
 Constructs a QSharedData object with reference count 0. More...
 
- Public Functions inherited from QPatternist::CppCastingHelper< Expression >
const TCastTarget * as () const
 
TCastTarget * as ()
 
- Public Functions inherited from QPatternist::SourceLocationReflection
virtual QSourceLocation sourceLocation () const
 
 SourceLocationReflection ()
 
virtual ~SourceLocationReflection ()
 

Static Public Functions

static Expression::Ptr wrapAround (const Expression::Ptr &operand, const StaticContext::Ptr &context)
 
- Static Public Functions inherited from QPatternist::Expression
static void rewrite (Expression::Ptr &old, const Expression::Ptr &New, const StaticContext::Ptr &context)
 

Static Private Functions

static bool lessThanUsingNodeModel (const Item &n1, const Item &n2)
 

Additional Inherited Members

- Public Types inherited from QPatternist::Expression
typedef QExplicitlySharedDataPointer< const ExpressionConstPtr
 A smart pointer wrapping const Expression instances. More...
 
enum  ID {
  IDBooleanValue = 1, IDCountFN, IDEmptyFN, IDExistsFN,
  IDExpressionSequence, IDGeneralComparison, IDIfThenClause, IDIgnorableExpression,
  IDIntegerValue, IDPositionFN, IDStringValue, IDValueComparison,
  IDRangeVariableReference, IDContextItem, IDUserFunctionCallsite, IDExpressionVariableReference,
  IDAttributeConstructor, IDUpperCaseFN, IDLowerCaseFN, IDFirstItemPredicate,
  IDEmptySequence, IDReturnOrderBy, IDLetClause, IDForClause,
  IDPath, IDNamespaceConstructor, IDArgumentReference, IDGenericPredicate,
  IDAxisStep, IDFloat, IDCombineNodes, IDUnresolvedVariableReference,
  IDCardinalityVerifier
}
 
typedef QList< Expression::PtrList
 
typedef QFlags< PropertyProperties
 
enum  Property {
  UseContextItem = 1, DisableElimination = 1 << 1, IsEvaluated = 1 << 2, DisableTypingDeduction = 1 << 3,
  EmptynessFollowsChild = 1 << 4, RewriteToEmptyOnEmpty = 1 << 5, RequiresFocus = 1 << 6, AffectsOrderOnly = 1 << 7,
  RequiresContextItem = (1 << 8) | RequiresFocus, CreatesFocusForLast = 1 << 9, LastOperandIsCollation = 1 << 10, DependsOnLocalVariable = (1 << 11) | DisableElimination,
  EvaluationCacheRedundant = (1 << 12), IsNodeConstructor = 1 << 13, RequiresCurrentItem = 1 << 14
}
 
typedef QExplicitlySharedDataPointer< ExpressionPtr
 A smart pointer wrapping mutable Expression instances. More...
 
typedef ::QAbstractXmlForwardIterator< Expression::PtrQAbstractXmlForwardIterator
 
typedef QVector< Expression::PtrVector
 
- Public Variables inherited from QSharedData
QAtomicInt ref
 
- Protected Functions inherited from QPatternist::SingleContainer
 SingleContainer (const Expression::Ptr &operand)
 
- Protected Functions inherited from QPatternist::Expression
void typeCheckOperands (const StaticContext::Ptr &context)
 
- Protected Functions inherited from QPatternist::CppCastingHelper< Expression >
 CppCastingHelper ()
 
- Protected Variables inherited from QPatternist::SingleContainer
Expression::Ptr m_operand
 

Detailed Description

De-duplicates and sorts in document order the content that its operand returns.

Author
Frans Englich frans.nosp@m..eng.nosp@m.lich@.nosp@m.noki.nosp@m.a.com

Definition at line 70 of file qnodesort_p.h.

Constructors and Destructors

◆ NodeSortExpression()

NodeSortExpression::NodeSortExpression ( const Expression::Ptr operand)

Definition at line 50 of file qnodesort.cpp.

Referenced by wrapAround().

50  : SingleContainer(op)
51 {
52 }
SingleContainer(const Expression::Ptr &operand)

Functions

◆ accept()

ExpressionVisitorResult::Ptr NodeSortExpression::accept ( const ExpressionVisitor::Ptr visitor) const
virtual

Implements QPatternist::Expression.

Definition at line 127 of file qnodesort.cpp.

128 {
129  return visitor->visit(this);
130 }

◆ compress()

Expression::Ptr NodeSortExpression::compress ( const StaticContext::Ptr context)
virtual

compress() is the last stage performs in compiling an expression, done after the initial AST build and calling typeCheck(). compress() performs crucial simplifications, either by having drastic performance implications or that some expressions depend on it for proper behavior.

The default implementation performs a sparse conditional constant propagation. In short, a recursive process is performed in the AST which examines if the Expression's operands are constant values, and if so, performs a const fold(AST rewrite) into the result of evaluating the expression in question. This default behavior can be disabled by letting properties() return DisableElimination.

This compress() stage can be relative effective due to the design of XPath, in part because intrinsic functions are heavily used. Many Expressions override compress() and do optimizations specific to what they do. Also, many Expressions performs optimizations in their typeCheck().

Parameters
contextthe static context. Supplies compile time information, and is the channel for communicating error messages.
See also
Wikipedia, the free encyclopedia, Sparse conditional constant propagation
Wikipedia, the free encyclopedia, Intrinsic function
Wikipedia, the free encyclopedia, Compiler optimization

Reimplemented from QPatternist::Expression.

Definition at line 103 of file qnodesort.cpp.

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 }
virtual Expression::Ptr compress(const StaticContext::Ptr &context)
virtual Cardinality cardinality() const =0
virtual SequenceType::Ptr staticType() const =0

◆ evaluateSequence()

Item::Iterator::Ptr NodeSortExpression::evaluateSequence ( const DynamicContext::Ptr context) const
virtual

Evaluate this Expression by iterating over it. This is a central function for evaluating expressions.

Expressions must always always return a valid QAbstractXmlForwardIterator and may never return 0. If an empty result is of interest to be returned, the EmptyIterator should be returned.

The default implementation returns a SingletonIterator over the item returned from evaluateSingleton().

Note
This function may raise an exception when calling, not only when QAbstractXmlForwardIterator::next() is called on the return value. This is because in some cases evaluateSingleton() is called directly.

Reimplemented from QPatternist::Expression.

Definition at line 73 of file qnodesort.cpp.

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 }
QAbstractXmlForwardIterator< T >::Ptr makeListIterator(const QList< T > &list)
static const EmptyIterator< Item >::Ptr emptyIterator
QExplicitlySharedDataPointer< QAbstractXmlForwardIterator< Item > > Ptr
A smart pointer wrapping an instance of a QAbstractXmlForwardIterator subclass.
Performs deduplication of the nodes on its source list.
virtual Cardinality cardinality() const =0
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
QList< Item > List
Definition: qitem_p.h:195
virtual Item::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &context) const
#define Q_FUNC_INFO
Definition: qglobal.h:1871

◆ expectedOperandTypes()

SequenceType::List NodeSortExpression::expectedOperandTypes ( ) const
virtual

Returns a list of Sequence Types, describing the type of each of the expression's operands. Hence, this function has a relationship to the operands() function:

- The lengths of the lists returned by expectedOperandTypes()
and operands() should always be equal in length, since one
cannot describe the type of a non-existent operand(and all
operands must have type information).
- A significant difference between the two functions is that while
the type of objects in the list returned by operands() may vary
between compilations/static context, simply because the particular
Expression is part of different XPath expressions, the
types in the list returned by expectedOperandTypes is always the same
since the function/operator signature never changes.

This function should not be confused with staticType(), which returns the static type of the expression itself, not its operands. The function call is an expression where this is clear: the type of the return value is not the same as the arguments' types. The static type of the operands supplied to the expression can be determined via the staticType() function of the instances returned by operands().

If the expression has no operands, an empty list should be returned.

Implements QPatternist::Expression.

Definition at line 119 of file qnodesort.cpp.

120 {
121  SequenceType::List result;
123  return result;
124 }
static const SequenceType::Ptr ZeroOrMoreItems
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ lessThanUsingNodeModel()

bool NodeSortExpression::lessThanUsingNodeModel ( const Item n1,
const Item n2 
)
inlinestaticprivate

Definition at line 54 of file qnodesort.cpp.

Referenced by evaluateSequence().

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 }
DocumentOrder compareOrder(const QXmlNodeModelIndex &other) const
Definition: qitem_p.h:481
bool isNode() const
Determines whether this item is an atomic value, or a node.
Definition: qitem_p.h:349
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
const QAbstractXmlNodeModel * model() const
Returns the QAbstractXmlNodeModel that this node index refers to.
const QXmlNodeModelIndex & asNode() const
Definition: qitem_p.h:379

◆ properties()

Expression::Properties NodeSortExpression::properties ( ) const
virtual
Returns
a bitwise OR'd value of properties, describing the characteristics of the expression. These properties affects how this Expression is treated in for example type checking stages.

The default implementation returns 0. Override and let the function return a different value, if that's of interest.

An important decision when re-implementing properties() is whether to OR in the properties() of ones operands. For instance, if an operand has RequiresFocus set, that flag nost likely applies to the apparent as well, since it depends on its operand.

See also
deepProperties()
Returns
Expression::None, meaning no special properties

Reimplemented from QPatternist::Expression.

Definition at line 132 of file qnodesort.cpp.

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 }

◆ staticType()

SequenceType::Ptr NodeSortExpression::staticType ( ) const
virtual
Returns
the static type of this Expression. For example, an 'and' expression have as static type xs:boolean

Implements QPatternist::Expression.

Definition at line 114 of file qnodesort.cpp.

115 {
116  return m_operand->staticType();
117 }
virtual SequenceType::Ptr staticType() const =0

◆ wrapAround()

Expression::Ptr NodeSortExpression::wrapAround ( const Expression::Ptr operand,
const StaticContext::Ptr context 
)
static

Ensures that result delivered from operand, is in document order.

Definition at line 92 of file qnodesort.cpp.

Referenced by QPatternist::CombineNodes::typeCheck(), QPatternist::Path::typeCheck(), and QPatternist::yyparse().

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 }
NodeSortExpression(const Expression::Ptr &operand)
Definition: qnodesort.cpp:50
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
T * data() const
Returns a pointer to the shared data object.
Definition: qshareddata.h:145
static void sort(T *array, int count, LessThan lessThan)
void wrapExpressionWith(const SourceLocationReflection *const existingNode, const QExplicitlySharedDataPointer< Expression > &newNode)
Ensures source locations are handled in such a manner that existingNode wraps newNode.

The documentation for this class was generated from the following files: