Xalan-C++ API Reference  1.12.0
NodeSorter.hpp
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 #if !defined(XALAN_NODESORTER_HEADER_GUARD)
19 #define XALAN_NODESORTER_HEADER_GUARD
20 
21 // Base include file. Must be first.
22 #include "XSLTDefinitions.hpp"
23 
24 
25 
26 #include <functional>
27 
28 
29 
31 
32 
33 
34 #include <xalanc/XPath/XObject.hpp>
35 
36 
37 
39 
40 
41 
42 namespace XALAN_CPP_NAMESPACE {
43 
44 
45 
46 class MutableNodeRefList;
47 class StylesheetExecutionContext;
48 class XalanNode;
49 class XPath;
50 
51 
54 
57 
58 
59 /**
60  * This class can sort vectors of nodes according to a select pattern.
61  */
63 {
64 public:
65 
67  {
68  public:
69 
71  XalanNode* theNode = 0,
72  XalanSize_t thePosition = 0) :
73  m_node(theNode),
74  m_position(thePosition)
75  {
76  }
77 
79  XalanSize_t m_position;
80  };
81 
84 
85  explicit
86  NodeSorter(MemoryManager& theManager);
87 
88  ~NodeSorter();
89 
92  {
93  return m_keys;
94  }
95 
96  /**
97  * Given a list of nodes, sort each node according to the criteria in the
98  * keys. The list is assumed to be in document order.
99  *
100  * @param executionContext current execution context
101  * @param v list of Nodes
102  */
103  void
104  sort(
105  StylesheetExecutionContext& executionContext,
106  MutableNodeRefList& theList);
107 
108  /**
109  * Return the results of a compare of two nodes.
110  */
112  {
113  public:
114 
115  /**
116  * Construct a NodeSortKeyCompare object, to perform the sort
117  *
118  * @param executionContext current execution context
119  * @param theNodes vector or nodes to be sorted
120  * @param theNodeSortKeys vector of keys upon which to sort
121  */
123  StylesheetExecutionContext& executionContext,
124  NodeSorter& theSorter,
125  const NodeVectorType& theNodes,
126  const NodeSortKeyVectorType& theNodeSortKeys) :
127  m_executionContext(executionContext),
128  m_sorter(theSorter),
129  m_nodes(theNodes),
130  m_nodeSortKeys(theNodeSortKeys)
131  {
132  }
133 
134  /**
135  * Compare two nodes, returning a value to indicate the
136  * result
137  *
138  * @param theLHS the first node to compare
139  * @param theRHS the second node to compare
140  * @param theKeyIndex the index of the key to use
141  * @result < 0 if theLHS is less than theRHS, 0 if they are equal, and > 0 if theLHS is greater than theRHS
142  */
143  int
144  compare(
145  const NodeVectorType::value_type& theLHS,
146  const NodeVectorType::value_type& theRHS,
147  XalanSize_t theKeyIndex = 0) const;
148 
149  /**
150  * Compare two nodes as a less predicate.
151  *
152  * @param theLHS the first node to compare
153  * @param theRHS the second node to compare
154  * @param theKeyIndex the index of the key to use
155  * @return true if theLHS is less than theRHS
156  */
157  bool
159  const NodeVectorType::value_type& theLHS,
160  const NodeVectorType::value_type& theRHS,
161  XalanSize_t theKeyIndex = 0) const
162  {
163  return compare(theLHS, theRHS, theKeyIndex) < 0 ? true : false;
164  }
165 
166  protected:
167 
168  double
169  getNumberResult(
170  const NodeSortKey& theKey,
171  XalanSize_t theKeyIndex,
172  const NodeVectorType::value_type& theEntry) const;
173 
174  const XalanDOMString&
175  getStringResult(
176  const NodeSortKey& theKey,
177  XalanSize_t theKeyIndex,
178  const NodeVectorType::value_type& theEntry) const;
179 
180  private:
181 
182  StylesheetExecutionContext& m_executionContext;
183  NodeSorter& m_sorter;
184  const NodeVectorType& m_nodes;
185  const NodeSortKeyVectorType& m_nodeSortKeys;
186  };
187 
188  friend struct NodeSortKeyCompare;
189 
193 
197 
199 
200 #if defined(XALAN_NODESORTER_CACHE_XOBJECTS)
202 #else
204 #endif
205 
206 private:
207 
208  /**
209  * Given a vector of nodes, sort each node according to the criteria in the
210  * keys.
211  *
212  * @param executionContext current execution context
213  */
214  void
215  sort(StylesheetExecutionContext& executionContext);
216 
217  // Data members...
218  NumberResultsCacheType m_numberResultsCache;
219 
220  StringResultsCacheType m_stringResultsCache;
221 
222  NodeSortKeyVectorType m_keys;
223 
224  NodeVectorType m_scratchVector;
225 };
226 
227 
228 
229 }
230 
231 
232 
233 #endif // XALAN_NODESORTER_HEADER_GUARD
xalanc::NodeSorter::NodeSortKeyCompare::NodeSortKeyCompare
NodeSortKeyCompare(StylesheetExecutionContext &executionContext, NodeSorter &theSorter, const NodeVectorType &theNodes, const NodeSortKeyVectorType &theNodeSortKeys)
Construct a NodeSortKeyCompare object, to perform the sort.
Definition: NodeSorter.hpp:122
xalanc::NodeSorter::NumberResultsCacheType
NumberCacheType NumberResultsCacheType
Definition: NodeSorter.hpp:198
XALAN_CPP_NAMESPACE
#define XALAN_CPP_NAMESPACE
Xalan-C++ namespace, including major and minor version.
Definition: XalanVersion.hpp:76
xalanc::NodeSortKey
Simple data structure class for use by the NodeSorter class.
Definition: NodeSortKey.hpp:49
xalanc::NodeSorter::NodeSortKeyCompare
Return the results of a compare of two nodes.
Definition: NodeSorter.hpp:111
xalanc::NodeSorter
This class can sort vectors of nodes according to a select pattern.
Definition: NodeSorter.hpp:62
xalanc::StylesheetExecutionContext
Definition: StylesheetExecutionContext.hpp:106
xalanc::XalanNode
Definition: XalanNode.hpp:38
xalanc::XalanVector< double >
XALAN_USES_MEMORY_MANAGER
#define XALAN_USES_MEMORY_MANAGER(Type)
Definition: XalanMemoryManagement.hpp:589
xalanc::XPath
Definition: XPath.hpp:67
xalanc::MutableNodeRefList
Local implementation of MutableNodeRefList.
Definition: MutableNodeRefList.hpp:46
XALAN_XSLT_EXPORT
#define XALAN_XSLT_EXPORT
Definition: XSLTDefinitions.hpp:27
xalanc::NodeSorter::NumberCacheType
XalanVector< NumberVectorType > NumberCacheType
Definition: NodeSorter.hpp:194
XalanVector.hpp
xalanc::NodeSorter::NumberVectorType
NumberVectorTypeDecl NumberVectorType
Definition: NodeSorter.hpp:190
xalanc::NodeSorter::StringVectorType
StringVectorTypeDecl StringVectorType
Definition: NodeSorter.hpp:192
xalanc::NodeSorter::VectorEntry::VectorEntry
VectorEntry(XalanNode *theNode=0, XalanSize_t thePosition=0)
Definition: NodeSorter.hpp:70
xalanc::NodeSorter::NodeVectorType
XalanVector< VectorEntry > NodeVectorType
Definition: NodeSorter.hpp:82
xalanc::NodeSorter::VectorEntry
Definition: NodeSorter.hpp:66
xalanc::compare
compare(const CharVectorType &theLHS, const CharVectorType &theRHS)
Compare the contents of two strings.
xalanc::NumberVectorTypeDecl
XalanVector< double > NumberVectorTypeDecl
Definition: NodeSorter.hpp:49
xalanc::NodeSorter::getSortKeys
NodeSortKeyVectorType & getSortKeys()
Definition: NodeSorter.hpp:91
xalanc::NodeSorter::NodeSortKeyCompare::operator()
bool operator()(const NodeVectorType::value_type &theLHS, const NodeVectorType::value_type &theRHS, XalanSize_t theKeyIndex=0) const
Compare two nodes as a less predicate.
Definition: NodeSorter.hpp:158
xalanc::NodeSorter::VectorEntry::m_position
XalanSize_t m_position
Definition: NodeSorter.hpp:79
xalanc::NodeSorter::NodeSortKeyVectorType
XalanVector< NodeSortKey > NodeSortKeyVectorType
Definition: NodeSorter.hpp:83
XSLTDefinitions.hpp
xalanc::XalanVector< VectorEntry >::value_type
VectorEntry value_type
Definition: XalanVector.hpp:63
xalanc::XalanDOMString
Definition: XalanDOMString.hpp:45
xalanc::NodeSorter::StringResultsCacheType
StringCacheType StringResultsCacheType
Definition: NodeSorter.hpp:203
xalanc::NodeSorter::XObjectCacheType
XalanVector< XObjectVectorType > XObjectCacheType
Definition: NodeSorter.hpp:195
xalanc::NodeSorter::VectorEntry::m_node
XalanNode * m_node
Definition: NodeSorter.hpp:78
NodeSortKey.hpp
XObject.hpp
xalanc::NodeSorter::StringCacheType
XalanVector< StringVectorType > StringCacheType
Definition: NodeSorter.hpp:196
xalanc::NodeSorter::XObjectVectorType
XalanVector< XObjectPtr > XObjectVectorType
Definition: NodeSorter.hpp:191