This source file includes following definitions.
- parentWithDepth
- sortBlock
- sort
- findRootNode
- traversalSort
- reverse
- firstNode
- anyNode
#include "config.h"
#include "core/xml/XPathNodeSet.h"
#include "core/dom/Attr.h"
#include "core/dom/Element.h"
#include "core/dom/NodeTraversal.h"
namespace WebCore {
namespace XPath {
const unsigned traversalSortCutoff = 10000;
static inline Node* parentWithDepth(unsigned depth, const Vector<Node*>& parents)
{
ASSERT(parents.size() >= depth + 1);
return parents[parents.size() - 1 - depth];
}
static void sortBlock(unsigned from, unsigned to, Vector<Vector<Node*> >& parentMatrix, bool mayContainAttributeNodes)
{
ASSERT(from + 1 < to);
unsigned minDepth = UINT_MAX;
for (unsigned i = from; i < to; ++i) {
unsigned depth = parentMatrix[i].size() - 1;
if (minDepth > depth)
minDepth = depth;
}
unsigned commonAncestorDepth = minDepth;
Node* commonAncestor;
while (true) {
commonAncestor = parentWithDepth(commonAncestorDepth, parentMatrix[from]);
if (commonAncestorDepth == 0)
break;
bool allEqual = true;
for (unsigned i = from + 1; i < to; ++i) {
if (commonAncestor != parentWithDepth(commonAncestorDepth, parentMatrix[i])) {
allEqual = false;
break;
}
}
if (allEqual)
break;
--commonAncestorDepth;
}
if (commonAncestorDepth == minDepth) {
for (unsigned i = from; i < to; ++i)
if (commonAncestor == parentMatrix[i][0]) {
parentMatrix[i].swap(parentMatrix[from]);
if (from + 2 < to)
sortBlock(from + 1, to, parentMatrix, mayContainAttributeNodes);
return;
}
}
if (mayContainAttributeNodes && commonAncestor->isElementNode()) {
unsigned sortedEnd = from;
for (unsigned i = sortedEnd; i < to; ++i) {
Node* n = parentMatrix[i][0];
if (n->isAttributeNode() && toAttr(n)->ownerElement() == commonAncestor)
parentMatrix[i].swap(parentMatrix[sortedEnd++]);
}
if (sortedEnd != from) {
if (to - sortedEnd > 1)
sortBlock(sortedEnd, to, parentMatrix, mayContainAttributeNodes);
return;
}
}
HashSet<Node*> parentNodes;
for (unsigned i = from; i < to; ++i)
parentNodes.add(parentWithDepth(commonAncestorDepth + 1, parentMatrix[i]));
unsigned previousGroupEnd = from;
unsigned groupEnd = from;
for (Node* n = commonAncestor->firstChild(); n; n = n->nextSibling()) {
if (parentNodes.contains(n)) {
for (unsigned i = groupEnd; i < to; ++i)
if (parentWithDepth(commonAncestorDepth + 1, parentMatrix[i]) == n)
parentMatrix[i].swap(parentMatrix[groupEnd++]);
if (groupEnd - previousGroupEnd > 1)
sortBlock(previousGroupEnd, groupEnd, parentMatrix, mayContainAttributeNodes);
ASSERT(previousGroupEnd != groupEnd);
previousGroupEnd = groupEnd;
#ifndef NDEBUG
parentNodes.remove(n);
#endif
}
}
ASSERT(parentNodes.isEmpty());
}
void NodeSet::sort() const
{
if (m_isSorted)
return;
unsigned nodeCount = m_nodes.size();
if (nodeCount < 2) {
const_cast<bool&>(m_isSorted) = true;
return;
}
if (nodeCount > traversalSortCutoff) {
traversalSort();
return;
}
bool containsAttributeNodes = false;
Vector<Vector<Node*> > parentMatrix(nodeCount);
for (unsigned i = 0; i < nodeCount; ++i) {
Vector<Node*>& parentsVector = parentMatrix[i];
Node* n = m_nodes[i].get();
parentsVector.append(n);
if (n->isAttributeNode()) {
n = toAttr(n)->ownerElement();
parentsVector.append(n);
containsAttributeNodes = true;
}
while ((n = n->parentNode()))
parentsVector.append(n);
}
sortBlock(0, nodeCount, parentMatrix, containsAttributeNodes);
Vector<RefPtr<Node> > sortedNodes;
sortedNodes.reserveInitialCapacity(nodeCount);
for (unsigned i = 0; i < nodeCount; ++i)
sortedNodes.append(parentMatrix[i][0]);
const_cast<Vector<RefPtr<Node> >&>(m_nodes).swap(sortedNodes);
}
static Node* findRootNode(Node* node)
{
if (node->isAttributeNode())
node = toAttr(node)->ownerElement();
if (node->inDocument())
node = &node->document();
else {
while (Node* parent = node->parentNode())
node = parent;
}
return node;
}
void NodeSet::traversalSort() const
{
HashSet<Node*> nodes;
bool containsAttributeNodes = false;
unsigned nodeCount = m_nodes.size();
ASSERT(nodeCount > 1);
for (unsigned i = 0; i < nodeCount; ++i) {
Node* node = m_nodes[i].get();
nodes.add(node);
if (node->isAttributeNode())
containsAttributeNodes = true;
}
Vector<RefPtr<Node> > sortedNodes;
sortedNodes.reserveInitialCapacity(nodeCount);
for (Node* n = findRootNode(m_nodes.first().get()); n; n = NodeTraversal::next(*n)) {
if (nodes.contains(n))
sortedNodes.append(n);
if (!containsAttributeNodes || !n->isElementNode())
continue;
Element* element = toElement(n);
if (!element->hasAttributes())
continue;
unsigned attributeCount = element->attributeCount();
for (unsigned i = 0; i < attributeCount; ++i) {
RefPtr<Attr> attr = element->attrIfExists(element->attributeItem(i).name());
if (attr && nodes.contains(attr.get()))
sortedNodes.append(attr);
}
}
ASSERT(sortedNodes.size() == nodeCount);
const_cast<Vector<RefPtr<Node> >&>(m_nodes).swap(sortedNodes);
}
void NodeSet::reverse()
{
if (m_nodes.isEmpty())
return;
unsigned from = 0;
unsigned to = m_nodes.size() - 1;
while (from < to) {
m_nodes[from].swap(m_nodes[to]);
++from;
--to;
}
}
Node* NodeSet::firstNode() const
{
if (isEmpty())
return 0;
sort();
return m_nodes.at(0).get();
}
Node* NodeSet::anyNode() const
{
if (isEmpty())
return 0;
return m_nodes.at(0).get();
}
}
}