summaryrefslogtreecommitdiffstats
path: root/dom/xslt/xpath/txNodeSet.h
blob: bd33be628d27cfdb60425195e54de3cc0f874514 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * Implementation of an XPath NodeSet
 */

#ifndef txNodeSet_h__
#define txNodeSet_h__

#include "txExprResult.h"
#include "nsError.h"
#include "txXPathNode.h"

class txNodeSet : public txAExprResult
{
public:
    /**
     * Creates a new empty NodeSet
     */
    explicit txNodeSet(txResultRecycler* aRecycler);

    /**
     * Creates a new NodeSet with one node.
     */
    txNodeSet(const txXPathNode& aNode, txResultRecycler* aRecycler);

    /**
     * Creates a new txNodeSet, copying the node references from the source
     * NodeSet.
     */
    txNodeSet(const txNodeSet& aSource, txResultRecycler* aRecycler);

    /**
     * Destructor for txNodeSet, deletes the nodes.
     */
    virtual ~txNodeSet();

    /**
     * Adds the specified txXPathNode to this NodeSet if it is not already
     * in this NodeSet. The node is inserted according to document order.
     *
     * @param  aNode the txXPathNode to add to the NodeSet
     * @return errorcode.
     */
    nsresult add(const txXPathNode& aNode);

    /**
     * Adds the nodes in specified NodeSet to this NodeSet. The resulting
     * NodeSet is sorted in document order and does not contain any duplicate
     * nodes.
     *
     * @param  aNodes the NodeSet to add, must be in document order.
     * @return errorcode.
     */
    nsresult add(const txNodeSet& aNodes);
    nsresult addAndTransfer(txNodeSet* aNodes);

    /**
     * Append API
     * These functions should be used with care.
     * They are intended to be used when the caller assures that the resulting
     * NodeSet remains in document order.
     * Abuse will break document order, and cause errors in the result.
     * These functions are significantly faster than the add API, as no
     * order info operations will be performed.
     */

    /**
     * Appends the specified Node to the end of this NodeSet
     * @param  aNode the Node to append to the NodeSet
     * @return errorcode.
     */
    nsresult append(const txXPathNode& aNode);

    /**
     * Appends the nodes in the specified NodeSet to the end of this NodeSet
     * @param  aNodes the NodeSet to append to the NodeSet
     * @return errorcode.
     */
    nsresult append(const txNodeSet& aNodes);

    /**
     * API to implement reverse axes in LocationStep.
     *
     * Before adding nodes to the nodeset for a reversed axis, call
     * setReverse(). This will make the append(aNode) and get() methods treat
     * the nodeset as required. Do only call append(aNode), get(), mark()
     * and sweep() while the nodeset is reversed.
     * Afterwards, call unsetReverse(). The nodes are stored in document
     * order internally.
     */
    void setReverse()
    {
        mDirection = -1;
    }
    void unsetReverse()
    {
        mDirection = 1;
    }

    /**
     * API to implement predicates in PredicateExpr
     *
     * mark(aIndex) marks the specified member of the nodeset.
     * sweep() clears all members of the nodeset that haven't been
     * marked before and clear the mMarks array.
     */
    nsresult mark(int32_t aIndex);
    nsresult sweep();

    /**
     * Removes all nodes from this nodeset
     */
    void clear();

    /**
     * Returns the index of the specified Node,
     * or -1 if the Node is not contained in the NodeSet
     * @param  aNode the Node to get the index for
     * @param  aStart index to start searching at
     * @return index of specified node or -1 if the node does not exist
     */
    int32_t indexOf(const txXPathNode& aNode, uint32_t aStart = 0) const;

    /**
     * Returns true if the specified Node is contained in the set.
     * @param  aNode the Node to search for
     * @return true if specified Node is contained in the NodeSet
     */
    bool contains(const txXPathNode& aNode) const
    {
        return indexOf(aNode) >= 0;
    }

    /**
     * Returns the Node at the specified node in this NodeSet.
     * @param  aIndex the node of the Node to return
     * @return Node at specified node
     */
    const txXPathNode& get(int32_t aIndex) const;

    /**
     * Returns true if there are no Nodes in the NodeSet.
     * @return true if there are no Nodes in the NodeSet.
     */
    bool isEmpty() const
    {
        return mStart ? mStart == mEnd : true;
    }

    /**
     * Returns the number of elements in the NodeSet
     * @return the number of elements in the NodeSet
     */
    int32_t size() const
    {
        return mStart ? mEnd - mStart : 0;
    }

    TX_DECL_EXPRRESULT

private:
    /**
     * Ensure that this nodeset can take another aSize nodes.
     *
     * Changes mStart and mEnd as well as mBufferStart and mBufferEnd.
     */
    bool ensureGrowSize(int32_t aSize);

    /**
     * Finds position in the buffer where a node should be inserted
     * to keep the nodeset in document order. Searches the positions
     * aFirst-aLast, including aFirst, but not aLast.
     * @param  aNode   Node to find insert position for.
     * @param  aFirst  First item of the search range, included.
     * @param  aLast   Last item of the search range, excluded.
     * @param  aDupe   out-param. Will be set to true if the node already
     *                 exists in the NodeSet, false if it should be
     *                 inserted.
     * @return pointer where to insert the node. The node should be inserted
     *         before the given node. This value is always set, even if aNode
     *         already exists in the NodeSet
     */
    txXPathNode* findPosition(const txXPathNode& aNode, 
                              txXPathNode* aFirst,
                              txXPathNode* aLast, bool& aDupe) const;

    static void copyElements(txXPathNode* aDest, const txXPathNode* aStart,
                             const txXPathNode* aEnd);
    static void transferElements(txXPathNode* aDest, const txXPathNode* aStart,
                                 const txXPathNode* aEnd);
    static void destroyElements(const txXPathNode* aStart,
                                const txXPathNode* aEnd)
    {
        while (aStart < aEnd) {
            aStart->~txXPathNode();
            ++aStart;
        }
    }

    typedef void (*transferOp) (txXPathNode* aDest, const txXPathNode* aStart,
                                const txXPathNode* aEnd);
    typedef void (*destroyOp) (const txXPathNode* aStart,
                               const txXPathNode* aEnd);
    nsresult add(const txNodeSet& aNodes, transferOp aTransfer,
                 destroyOp aDestroy);

    txXPathNode *mStart, *mEnd, *mStartBuffer, *mEndBuffer;
    int32_t mDirection;
    // used for mark() and sweep() in predicates
    bool* mMarks;
};

#endif