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 THIRD_PARTY_BLINK_RENDERER_CORE_XML_XPATH_NODE_SET_H_
27 #define THIRD_PARTY_BLINK_RENDERER_CORE_XML_XPATH_NODE_SET_H_
28 
29 #include "third_party/blink/renderer/core/dom/node.h"
30 #include "third_party/blink/renderer/platform/wtf/forward.h"
31 #include "third_party/blink/renderer/platform/wtf/vector.h"
32 
33 namespace blink {
34 
35 namespace xpath {
36 
37 class NodeSet final : public GarbageCollected<NodeSet> {
38  public:
Create()39   static NodeSet* Create() { return MakeGarbageCollected<NodeSet>(); }
40   static NodeSet* Create(const NodeSet&);
41 
NodeSet()42   NodeSet() : is_sorted_(true), subtrees_are_disjoint_(false) {}
43 
Trace(Visitor * visitor)44   void Trace(Visitor* visitor) { visitor->Trace(nodes_); }
45 
size()46   wtf_size_t size() const { return nodes_.size(); }
IsEmpty()47   bool IsEmpty() const { return !nodes_.size(); }
48   Node* operator[](unsigned i) const { return nodes_.at(i).Get(); }
begin()49   HeapVector<Member<Node>>::iterator begin() { return nodes_.begin(); }
end()50   HeapVector<Member<Node>>::iterator end() { return nodes_.end(); }
begin()51   HeapVector<Member<Node>>::const_iterator begin() const {
52     return nodes_.begin();
53   }
end()54   HeapVector<Member<Node>>::const_iterator end() const { return nodes_.end(); }
ReserveCapacity(wtf_size_t new_capacity)55   void ReserveCapacity(wtf_size_t new_capacity) {
56     nodes_.ReserveCapacity(new_capacity);
57   }
clear()58   void clear() { nodes_.clear(); }
Swap(NodeSet & other)59   void Swap(NodeSet& other) {
60     std::swap(is_sorted_, other.is_sorted_);
61     std::swap(subtrees_are_disjoint_, other.subtrees_are_disjoint_);
62     nodes_.swap(other.nodes_);
63   }
64 
65   // NodeSet itself does not verify that nodes in it are unique.
Append(Node * node)66   void Append(Node* node) { nodes_.push_back(node); }
Append(const NodeSet & node_set)67   void Append(const NodeSet& node_set) { nodes_.AppendVector(node_set.nodes_); }
68 
69   // Returns the set's first node in document order, or 0 if the set is empty.
70   Node* FirstNode() const;
71 
72   // Returns 0 if the set is empty.
73   Node* AnyNode() const;
74 
75   // NodeSet itself doesn't check if it contains nodes in document order - the
76   // caller should tell it if it does not.
MarkSorted(bool is_sorted)77   void MarkSorted(bool is_sorted) { is_sorted_ = is_sorted; }
IsSorted()78   bool IsSorted() const { return is_sorted_ || nodes_.size() < 2; }
79 
80   void Sort() const;
81 
82   // No node in the set is ancestor of another. Unlike m_isSorted, this is
83   // assumed to be false, unless the caller sets it to true.
MarkSubtreesDisjoint(bool disjoint)84   void MarkSubtreesDisjoint(bool disjoint) {
85     subtrees_are_disjoint_ = disjoint;
86   }
SubtreesAreDisjoint()87   bool SubtreesAreDisjoint() const {
88     return subtrees_are_disjoint_ || nodes_.size() < 2;
89   }
90 
91   void Reverse();
92 
93  private:
94   void TraversalSort() const;
95 
96   bool is_sorted_;
97   bool subtrees_are_disjoint_;
98   HeapVector<Member<Node>> nodes_;
99 };
100 
101 }  // namespace xpath
102 
103 }  // namespace blink
104 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_XML_XPATH_NODE_SET_H_
105