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