15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/*
25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * modification, are permitted provided that the following conditions
65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * are met:
75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 1. Redistributions of source code must retain the above copyright
85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *    notice, this list of conditions and the following disclaimer.
95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 2. Redistributions in binary form must reproduce the above copyright
105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *    notice, this list of conditions and the following disclaimer in the
115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *    documentation and/or other materials provided with the distribution.
125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2302772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */
255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#ifndef XPathNodeSet_h
275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#define XPathNodeSet_h
285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
295d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)#include "core/dom/Node.h"
30e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)#include "wtf/Forward.h"
31e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)#include "wtf/Vector.h"
325c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
33c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
345c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
355d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)namespace XPath {
365c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
375d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)class NodeSet : public NoBaseWillBeGarbageCollected<NodeSet> {
385d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
395d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)public:
405d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    static PassOwnPtrWillBeRawPtr<NodeSet> create() { return adoptPtrWillBeNoop(new NodeSet); }
415d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    static PassOwnPtrWillBeRawPtr<NodeSet> create(const NodeSet&);
425d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    void trace(Visitor* visitor) { visitor->trace(m_nodes); }
4302772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
445d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    size_t size() const { return m_nodes.size(); }
455d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    bool isEmpty() const { return !m_nodes.size(); }
465d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    Node* operator[](unsigned i) const { return m_nodes.at(i).get(); }
475d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    void reserveCapacity(size_t newCapacity) { m_nodes.reserveCapacity(newCapacity); }
485d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    void clear() { m_nodes.clear(); }
495d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    void swap(NodeSet& other) { std::swap(m_isSorted, other.m_isSorted); std::swap(m_subtreesAreDisjoint, other.m_subtreesAreDisjoint); m_nodes.swap(other.m_nodes); }
505c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
515d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    // NodeSet itself does not verify that nodes in it are unique.
525d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    void append(PassRefPtrWillBeRawPtr<Node> node) { m_nodes.append(node); }
535d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    void append(const NodeSet& nodeSet) { m_nodes.appendVector(nodeSet.m_nodes); }
545c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
555d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    // Returns the set's first node in document order, or 0 if the set is empty.
565d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    Node* firstNode() const;
575c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
585d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    // Returns 0 if the set is empty.
595d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    Node* anyNode() const;
605c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
615d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    // NodeSet itself doesn't check if it contains nodes in document order - the
625d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    // caller should tell it if it does not.
635d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    void markSorted(bool isSorted) { m_isSorted = isSorted; }
645d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    bool isSorted() const { return m_isSorted || m_nodes.size() < 2; }
655c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
665d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    void sort() const;
675c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
685d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    // No node in the set is ancestor of another. Unlike m_isSorted, this is
695d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    // assumed to be false, unless the caller sets it to true.
705d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    void markSubtreesDisjoint(bool disjoint) { m_subtreesAreDisjoint = disjoint; }
715d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    bool subtreesAreDisjoint() const { return m_subtreesAreDisjoint || m_nodes.size() < 2; }
725c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
735d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    void reverse();
7402772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
755d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)private:
765d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    NodeSet() : m_isSorted(true), m_subtreesAreDisjoint(false) { }
775d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    void traversalSort() const;
785c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
795d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    bool m_isSorted;
805d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    bool m_subtreesAreDisjoint;
815d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    WillBeHeapVector<RefPtrWillBeMember<Node> > m_nodes;
825d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)};
835c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
845c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
855c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
865d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)}
875c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#endif // XPathNodeSet_h
88