1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "txXPathTreeWalker.h"
12 * Implementation of an XPath nodeset
15 #ifdef NS_BUILD_REFCNT_LOGGING
16 # define LOG_CHUNK_MOVE(_start, _new_start, _count) \
18 txXPathNode* start = const_cast<txXPathNode*>(_start); \
19 while (start < _start + _count) { \
20 NS_LogDtor(start, "txXPathNode", sizeof(*start)); \
23 start = const_cast<txXPathNode*>(_new_start); \
24 while (start < _new_start + _count) { \
25 NS_LogCtor(start, "txXPathNode", sizeof(*start)); \
30 # define LOG_CHUNK_MOVE(_start, _new_start, _count)
33 static const int32_t kTxNodeSetMinSize
= 4;
34 static const int32_t kTxNodeSetGrowFactor
= 2;
39 txNodeSet::txNodeSet(txResultRecycler
* aRecycler
)
40 : txAExprResult(aRecycler
),
43 mStartBuffer(nullptr),
48 txNodeSet::txNodeSet(const txXPathNode
& aNode
, txResultRecycler
* aRecycler
)
49 : txAExprResult(aRecycler
),
52 mStartBuffer(nullptr),
56 if (!ensureGrowSize(1)) {
60 new (mStart
) txXPathNode(aNode
);
64 txNodeSet::txNodeSet(const txNodeSet
& aSource
, txResultRecycler
* aRecycler
)
65 : txAExprResult(aRecycler
),
68 mStartBuffer(nullptr),
75 txNodeSet::~txNodeSet() {
79 destroyElements(mStart
, mEnd
);
85 nsresult
txNodeSet::add(const txXPathNode
& aNode
) {
86 NS_ASSERTION(mDirection
== kForward
,
87 "only append(aNode) is supported on reversed nodesets");
94 txXPathNode
* pos
= findPosition(aNode
, mStart
, mEnd
, dupe
);
100 // save pos, ensureGrowSize messes with the pointers
101 int32_t moveSize
= mEnd
- pos
;
102 int32_t offset
= pos
- mStart
;
103 if (!ensureGrowSize(1)) {
104 return NS_ERROR_OUT_OF_MEMORY
;
106 // set pos to where it was
107 pos
= mStart
+ offset
;
110 LOG_CHUNK_MOVE(pos
, pos
+ 1, moveSize
);
111 // This move is okay even though txXPathNode is not trivially copyable as
112 // the created hole at `pos` is used for inplace new below.
113 memmove((void*)(pos
+ 1), pos
, moveSize
* sizeof(txXPathNode
));
116 new (pos
) txXPathNode(aNode
);
122 nsresult
txNodeSet::add(const txNodeSet
& aNodes
) {
123 return add(aNodes
, copyElements
, nullptr);
126 nsresult
txNodeSet::addAndTransfer(txNodeSet
* aNodes
) {
127 // failure is out-of-memory, transfer didn't happen
128 nsresult rv
= add(*aNodes
, transferElements
, destroyElements
);
129 NS_ENSURE_SUCCESS(rv
, rv
);
131 #ifdef TX_DONT_RECYCLE_BUFFER
132 if (aNodes
->mStartBuffer
) {
133 free(aNodes
->mStartBuffer
);
134 aNodes
->mStartBuffer
= aNodes
->mEndBuffer
= nullptr;
137 aNodes
->mStart
= aNodes
->mEnd
= aNodes
->mStartBuffer
;
143 * add(aNodeSet, aTransferOp)
145 * The code is optimized to make a minimum number of calls to
146 * Node::compareDocumentPosition. The idea is this:
147 * We have the two nodesets (number indicate "document position")
150 * 2 3 6 8 9 <- source 2
151 * _ _ _ _ _ _ _ _ <- result
154 * When merging these nodesets into the result, the nodes are transfered
155 * in chunks to the end of the buffer so that each chunk does not contain
156 * a node from the other nodeset, in document order.
158 * We select the last non-transfered node in the first nodeset and find
159 * where in the other nodeset it would be inserted. In this case we would
160 * take the 7 from the first nodeset and find the position between the
161 * 6 and 8 in the second. We then take the nodes after the insert-position
162 * and transfer them to the end of the resulting nodeset. Which in this case
163 * means that we first transfered the 8 and 9 nodes, giving us the following:
167 * _ _ _ _ _ _ 8 9 <- result
169 * The corresponding procedure is done for the second nodeset, that is
170 * the insertion position of the 6 in the first nodeset is found, which
171 * is between the 3 and the 7. The 7 is memmoved (as it stays within
172 * the same nodeset) to the result buffer.
174 * As the result buffer is filled from the end, it is safe to share the
175 * buffer between this nodeset and the result.
177 * This is repeated until both of the nodesets are empty.
179 * If we find a duplicate node when searching for where insertposition we
180 * check for sequences of duplicate nodes, which can be optimized.
183 nsresult
txNodeSet::add(const txNodeSet
& aNodes
, transferOp aTransfer
,
184 destroyOp aDestroy
) {
185 NS_ASSERTION(mDirection
== kForward
,
186 "only append(aNode) is supported on reversed nodesets");
188 if (aNodes
.isEmpty()) {
192 if (!ensureGrowSize(aNodes
.size())) {
193 return NS_ERROR_OUT_OF_MEMORY
;
196 // This is probably a rather common case, so lets try to shortcut.
197 if (mStart
== mEnd
||
198 txXPathNodeUtils::comparePosition(mEnd
[-1], *aNodes
.mStart
) < 0) {
199 aTransfer(mEnd
, aNodes
.mStart
, aNodes
.mEnd
);
200 mEnd
+= aNodes
.size();
205 // Last element in this nodeset
206 txXPathNode
* thisPos
= mEnd
;
208 // Last element of the other nodeset
209 txXPathNode
* otherPos
= aNodes
.mEnd
;
211 // Pointer to the insertion point in this nodeset
212 txXPathNode
* insertPos
= mEndBuffer
;
217 while (thisPos
> mStart
|| otherPos
> aNodes
.mStart
) {
218 // Find where the last remaining node of this nodeset would
219 // be inserted in the other nodeset.
220 if (thisPos
> mStart
) {
221 pos
= findPosition(thisPos
[-1], aNodes
.mStart
, otherPos
, dupe
);
224 const txXPathNode
* deletePos
= thisPos
;
225 --thisPos
; // this is already added
226 // check dupe sequence
227 while (thisPos
> mStart
&& pos
> aNodes
.mStart
&&
228 thisPos
[-1] == pos
[-1]) {
234 aDestroy(thisPos
, deletePos
);
241 // Transfer the otherNodes after the insertion point to the result
242 count
= otherPos
- pos
;
245 aTransfer(insertPos
, pos
, otherPos
);
249 // Find where the last remaining node of the otherNodeset would
250 // be inserted in this nodeset.
251 if (otherPos
> aNodes
.mStart
) {
252 pos
= findPosition(otherPos
[-1], mStart
, thisPos
, dupe
);
255 const txXPathNode
* deletePos
= otherPos
;
256 --otherPos
; // this is already added
257 // check dupe sequence
258 while (otherPos
> aNodes
.mStart
&& pos
> mStart
&&
259 otherPos
[-1] == pos
[-1]) {
265 aDestroy(otherPos
, deletePos
);
272 // Move the nodes from this nodeset after the insertion point
274 count
= thisPos
- pos
;
277 LOG_CHUNK_MOVE(pos
, insertPos
, count
);
278 memmove((void*)insertPos
, pos
, count
* sizeof(txXPathNode
));
290 * These functions should be used with care.
291 * They are intended to be used when the caller assures that the resulting
292 * nodeset remains in document order.
293 * Abuse will break document order, and cause errors in the result.
294 * These functions are significantly faster than the add API, as no
295 * order info operations will be performed.
298 nsresult
txNodeSet::append(const txXPathNode
& aNode
) {
299 if (!ensureGrowSize(1)) {
300 return NS_ERROR_OUT_OF_MEMORY
;
303 if (mDirection
== kForward
) {
304 new (mEnd
) txXPathNode(aNode
);
310 new (--mStart
) txXPathNode(aNode
);
315 nsresult
txNodeSet::append(const txNodeSet
& aNodes
) {
316 NS_ASSERTION(mDirection
== kForward
,
317 "only append(aNode) is supported on reversed nodesets");
319 if (aNodes
.isEmpty()) {
323 int32_t appended
= aNodes
.size();
324 if (!ensureGrowSize(appended
)) {
325 return NS_ERROR_OUT_OF_MEMORY
;
328 copyElements(mEnd
, aNodes
.mStart
, aNodes
.mEnd
);
334 nsresult
txNodeSet::mark(int32_t aIndex
) {
335 NS_ASSERTION(aIndex
>= 0 && mStart
&& mEnd
- mStart
> aIndex
,
336 "index out of bounds");
338 int32_t length
= size();
339 mMarks
= new bool[length
];
340 memset(mMarks
, 0, length
* sizeof(bool));
342 if (mDirection
== kForward
) {
343 mMarks
[aIndex
] = true;
345 mMarks
[size() - aIndex
- 1] = true;
351 nsresult
txNodeSet::sweep() {
357 int32_t chunk
, pos
= 0;
358 int32_t length
= size();
359 txXPathNode
* insertion
= mStartBuffer
;
361 while (pos
< length
) {
362 while (pos
< length
&& !mMarks
[pos
]) {
364 mStart
[pos
].~txXPathNode();
367 // find chunk to move
369 while (pos
< length
&& mMarks
[pos
]) {
375 LOG_CHUNK_MOVE(mStart
+ pos
- chunk
, insertion
, chunk
);
376 memmove((void*)insertion
, mStart
+ pos
- chunk
,
377 chunk
* sizeof(txXPathNode
));
381 mStart
= mStartBuffer
;
389 void txNodeSet::clear() {
390 destroyElements(mStart
, mEnd
);
391 #ifdef TX_DONT_RECYCLE_BUFFER
394 mStartBuffer
= mEndBuffer
= nullptr;
397 mStart
= mEnd
= mStartBuffer
;
400 mDirection
= kForward
;
403 int32_t txNodeSet::indexOf(const txXPathNode
& aNode
, uint32_t aStart
) const {
404 NS_ASSERTION(mDirection
== kForward
,
405 "only append(aNode) is supported on reversed nodesets");
407 if (!mStart
|| mStart
== mEnd
) {
411 txXPathNode
* pos
= mStart
+ aStart
;
412 for (; pos
< mEnd
; ++pos
) {
421 const txXPathNode
& txNodeSet::get(int32_t aIndex
) const {
422 if (mDirection
== kForward
) {
423 return mStart
[aIndex
];
426 return mEnd
[-aIndex
- 1];
429 short txNodeSet::getResultType() { return txAExprResult::NODESET
; }
431 bool txNodeSet::booleanValue() { return !isEmpty(); }
432 double txNodeSet::numberValue() {
436 return txDouble::toDouble(str
);
439 void txNodeSet::stringValue(nsString
& aStr
) {
440 NS_ASSERTION(mDirection
== kForward
,
441 "only append(aNode) is supported on reversed nodesets");
445 txXPathNodeUtils::appendNodeValue(get(0), aStr
);
448 const nsString
* txNodeSet::stringValuePointer() { return nullptr; }
450 bool txNodeSet::ensureGrowSize(int32_t aSize
) {
451 // check if there is enough place in the buffer as is
452 if (mDirection
== kForward
&& aSize
<= mEndBuffer
- mEnd
) {
456 if (mDirection
== kReversed
&& aSize
<= mStart
- mStartBuffer
) {
460 // check if we just have to align mStart to have enough space
461 int32_t oldSize
= mEnd
- mStart
;
462 int32_t oldLength
= mEndBuffer
- mStartBuffer
;
463 int32_t ensureSize
= oldSize
+ aSize
;
464 if (ensureSize
<= oldLength
) {
465 // just move the buffer
466 txXPathNode
* dest
= mStartBuffer
;
467 if (mDirection
== kReversed
) {
468 dest
= mEndBuffer
- oldSize
;
470 LOG_CHUNK_MOVE(mStart
, dest
, oldSize
);
471 memmove((void*)dest
, mStart
, oldSize
* sizeof(txXPathNode
));
473 mEnd
= dest
+ oldSize
;
478 // This isn't 100% safe. But until someone manages to make a 1gig nodeset
480 int32_t newLength
= std::max(oldLength
, kTxNodeSetMinSize
);
482 while (newLength
< ensureSize
) {
483 newLength
*= kTxNodeSetGrowFactor
;
486 txXPathNode
* newArr
=
487 static_cast<txXPathNode
*>(moz_xmalloc(newLength
* sizeof(txXPathNode
)));
489 txXPathNode
* dest
= newArr
;
490 if (mDirection
== kReversed
) {
491 dest
+= newLength
- oldSize
;
495 LOG_CHUNK_MOVE(mStart
, dest
, oldSize
);
496 memcpy((void*)dest
, mStart
, oldSize
* sizeof(txXPathNode
));
501 memset(mStartBuffer
, 0, (mEndBuffer
- mStartBuffer
) * sizeof(txXPathNode
));
506 mStartBuffer
= newArr
;
507 mEndBuffer
= mStartBuffer
+ newLength
;
509 mEnd
= dest
+ oldSize
;
514 txXPathNode
* txNodeSet::findPosition(const txXPathNode
& aNode
,
515 txXPathNode
* aFirst
, txXPathNode
* aLast
,
518 if (aLast
- aFirst
<= 2) {
519 // If we search 2 nodes or less there is no point in further divides
520 txXPathNode
* pos
= aFirst
;
521 for (; pos
< aLast
; ++pos
) {
522 int cmp
= txXPathNodeUtils::comparePosition(aNode
, *pos
);
536 // (cannot add two pointers)
537 txXPathNode
* midpos
= aFirst
+ (aLast
- aFirst
) / 2;
538 int cmp
= txXPathNodeUtils::comparePosition(aNode
, *midpos
);
546 return findPosition(aNode
, midpos
+ 1, aLast
, aDupe
);
549 // midpos excluded as end of range
551 return findPosition(aNode
, aFirst
, midpos
, aDupe
);
555 void txNodeSet::copyElements(txXPathNode
* aDest
, const txXPathNode
* aStart
,
556 const txXPathNode
* aEnd
) {
557 const txXPathNode
* pos
= aStart
;
559 new (aDest
) txXPathNode(*pos
);
566 void txNodeSet::transferElements(txXPathNode
* aDest
, const txXPathNode
* aStart
,
567 const txXPathNode
* aEnd
) {
568 LOG_CHUNK_MOVE(aStart
, aDest
, (aEnd
- aStart
));
569 memcpy((void*)aDest
, aStart
, (aEnd
- aStart
) * sizeof(txXPathNode
));