• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #ifndef XPathNodeSet_h
27 #define XPathNodeSet_h
28 
29 #if ENABLE(XPATH)
30 
31 #include <wtf/Vector.h>
32 #include <wtf/Forward.h>
33 
34 #include "Node.h"
35 
36 namespace WebCore {
37 
38     namespace XPath {
39 
40         class NodeSet : public FastAllocBase {
41         public:
NodeSet()42             NodeSet() : m_isSorted(true), m_subtreesAreDisjoint(false) { }
43 
size()44             size_t size() const { return m_nodes.size(); }
isEmpty()45             bool isEmpty() const { return !m_nodes.size(); }
46             Node* operator[](unsigned i) const { return m_nodes.at(i).get(); }
reserveCapacity(size_t newCapacity)47             void reserveCapacity(size_t newCapacity) { m_nodes.reserveCapacity(newCapacity); }
clear()48             void clear() { m_nodes.clear(); }
swap(NodeSet & other)49             void swap(NodeSet& other) { std::swap(m_isSorted, other.m_isSorted); std::swap(m_subtreesAreDisjoint, other.m_subtreesAreDisjoint); m_nodes.swap(other.m_nodes); }
50 
51             // NodeSet itself does not verify that nodes in it are unique.
append(Node * node)52             void append(Node* node) { m_nodes.append(node); }
append(PassRefPtr<Node> node)53             void append(PassRefPtr<Node> node) { m_nodes.append(node); }
append(const NodeSet & nodeSet)54             void append(const NodeSet& nodeSet) { m_nodes.append(nodeSet.m_nodes); }
55 
56             // Returns the set's first node in document order, or 0 if the set is empty.
57             Node* firstNode() const;
58 
59             // Returns 0 if the set is empty.
60             Node* anyNode() const;
61 
62             // NodeSet itself doesn't check if it contains nodes in document order - the caller should tell it if it does not.
markSorted(bool isSorted)63             void markSorted(bool isSorted) { m_isSorted = isSorted; }
isSorted()64             bool isSorted() const { return m_isSorted || m_nodes.size() < 2; }
65 
66             void sort() const;
67 
68             // No node in the set is ancestor of another. Unlike m_isSorted, this is assumed to be false, unless the caller sets it to true.
markSubtreesDisjoint(bool disjoint)69             void markSubtreesDisjoint(bool disjoint) { m_subtreesAreDisjoint = disjoint; }
subtreesAreDisjoint()70             bool subtreesAreDisjoint() const { return m_subtreesAreDisjoint || m_nodes.size() < 2; }
71 
72             void reverse();
73 
74         private:
75             bool m_isSorted;
76             bool m_subtreesAreDisjoint;
77             Vector<RefPtr<Node> > m_nodes;
78         };
79 
80     }
81 }
82 
83 #endif // ENABLE(XPATH)
84 #endif // XPathNodeSet_h
85