Bug 1928997: Update tabs icon in Unified Search popup r=desktop-theme-reviewers,daleh...
[gecko.git] / dom / xslt / xpath / txNodeSet.cpp
blob2ac16d20fbc072a07e687295964ebddbbf205e74
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/. */
6 #include "txNodeSet.h"
7 #include "txLog.h"
8 #include "txXPathTreeWalker.h"
9 #include <algorithm>
11 /**
12 * Implementation of an XPath nodeset
15 #ifdef NS_BUILD_REFCNT_LOGGING
16 # define LOG_CHUNK_MOVE(_start, _new_start, _count) \
17 { \
18 txXPathNode* start = const_cast<txXPathNode*>(_start); \
19 while (start < _start + _count) { \
20 NS_LogDtor(start, "txXPathNode", sizeof(*start)); \
21 ++start; \
22 } \
23 start = const_cast<txXPathNode*>(_new_start); \
24 while (start < _new_start + _count) { \
25 NS_LogCtor(start, "txXPathNode", sizeof(*start)); \
26 ++start; \
27 } \
29 #else
30 # define LOG_CHUNK_MOVE(_start, _new_start, _count)
31 #endif
33 static const int32_t kTxNodeSetMinSize = 4;
34 static const int32_t kTxNodeSetGrowFactor = 2;
36 #define kForward 1
37 #define kReversed -1
39 txNodeSet::txNodeSet(txResultRecycler* aRecycler)
40 : txAExprResult(aRecycler),
41 mStart(nullptr),
42 mEnd(nullptr),
43 mStartBuffer(nullptr),
44 mEndBuffer(nullptr),
45 mDirection(kForward),
46 mMarks(nullptr) {}
48 txNodeSet::txNodeSet(const txXPathNode& aNode, txResultRecycler* aRecycler)
49 : txAExprResult(aRecycler),
50 mStart(nullptr),
51 mEnd(nullptr),
52 mStartBuffer(nullptr),
53 mEndBuffer(nullptr),
54 mDirection(kForward),
55 mMarks(nullptr) {
56 if (!ensureGrowSize(1)) {
57 return;
60 new (mStart) txXPathNode(aNode);
61 ++mEnd;
64 txNodeSet::txNodeSet(const txNodeSet& aSource, txResultRecycler* aRecycler)
65 : txAExprResult(aRecycler),
66 mStart(nullptr),
67 mEnd(nullptr),
68 mStartBuffer(nullptr),
69 mEndBuffer(nullptr),
70 mDirection(kForward),
71 mMarks(nullptr) {
72 append(aSource);
75 txNodeSet::~txNodeSet() {
76 delete[] mMarks;
78 if (mStartBuffer) {
79 destroyElements(mStart, mEnd);
81 free(mStartBuffer);
85 nsresult txNodeSet::add(const txXPathNode& aNode) {
86 NS_ASSERTION(mDirection == kForward,
87 "only append(aNode) is supported on reversed nodesets");
89 if (isEmpty()) {
90 return append(aNode);
93 bool dupe;
94 txXPathNode* pos = findPosition(aNode, mStart, mEnd, dupe);
96 if (dupe) {
97 return NS_OK;
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;
109 if (moveSize > 0) {
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);
117 ++mEnd;
119 return NS_OK;
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;
136 #endif
137 aNodes->mStart = aNodes->mEnd = aNodes->mStartBuffer;
139 return NS_OK;
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")
149 * 1 3 7 <- source 1
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:
165 * 1 3 7 <- source 1
166 * 2 3 6 <- source 2
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()) {
189 return NS_OK;
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();
202 return NS_OK;
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;
214 bool dupe;
215 txXPathNode* pos;
216 int32_t count;
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);
223 if (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]) {
229 --thisPos;
230 --pos;
233 if (aDestroy) {
234 aDestroy(thisPos, deletePos);
237 } else {
238 pos = aNodes.mStart;
241 // Transfer the otherNodes after the insertion point to the result
242 count = otherPos - pos;
243 if (count > 0) {
244 insertPos -= count;
245 aTransfer(insertPos, pos, otherPos);
246 otherPos -= count;
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);
254 if (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]) {
260 --otherPos;
261 --pos;
264 if (aDestroy) {
265 aDestroy(otherPos, deletePos);
268 } else {
269 pos = mStart;
272 // Move the nodes from this nodeset after the insertion point
273 // to the result
274 count = thisPos - pos;
275 if (count > 0) {
276 insertPos -= count;
277 LOG_CHUNK_MOVE(pos, insertPos, count);
278 memmove((void*)insertPos, pos, count * sizeof(txXPathNode));
279 thisPos -= count;
282 mStart = insertPos;
283 mEnd = mEndBuffer;
285 return NS_OK;
289 * Append API
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);
305 ++mEnd;
307 return NS_OK;
310 new (--mStart) txXPathNode(aNode);
312 return NS_OK;
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()) {
320 return NS_OK;
323 int32_t appended = aNodes.size();
324 if (!ensureGrowSize(appended)) {
325 return NS_ERROR_OUT_OF_MEMORY;
328 copyElements(mEnd, aNodes.mStart, aNodes.mEnd);
329 mEnd += appended;
331 return NS_OK;
334 nsresult txNodeSet::mark(int32_t aIndex) {
335 NS_ASSERTION(aIndex >= 0 && mStart && mEnd - mStart > aIndex,
336 "index out of bounds");
337 if (!mMarks) {
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;
344 } else {
345 mMarks[size() - aIndex - 1] = true;
348 return NS_OK;
351 nsresult txNodeSet::sweep() {
352 if (!mMarks) {
353 // sweep everything
354 clear();
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]) {
363 // delete unmarked
364 mStart[pos].~txXPathNode();
365 ++pos;
367 // find chunk to move
368 chunk = 0;
369 while (pos < length && mMarks[pos]) {
370 ++pos;
371 ++chunk;
373 // move chunk
374 if (chunk > 0) {
375 LOG_CHUNK_MOVE(mStart + pos - chunk, insertion, chunk);
376 memmove((void*)insertion, mStart + pos - chunk,
377 chunk * sizeof(txXPathNode));
378 insertion += chunk;
381 mStart = mStartBuffer;
382 mEnd = insertion;
383 delete[] mMarks;
384 mMarks = nullptr;
386 return NS_OK;
389 void txNodeSet::clear() {
390 destroyElements(mStart, mEnd);
391 #ifdef TX_DONT_RECYCLE_BUFFER
392 if (mStartBuffer) {
393 free(mStartBuffer);
394 mStartBuffer = mEndBuffer = nullptr;
396 #endif
397 mStart = mEnd = mStartBuffer;
398 delete[] mMarks;
399 mMarks = nullptr;
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) {
408 return -1;
411 txXPathNode* pos = mStart + aStart;
412 for (; pos < mEnd; ++pos) {
413 if (*pos == aNode) {
414 return pos - mStart;
418 return -1;
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() {
433 nsAutoString str;
434 stringValue(str);
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");
442 if (isEmpty()) {
443 return;
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) {
453 return true;
456 if (mDirection == kReversed && aSize <= mStart - mStartBuffer) {
457 return true;
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));
472 mStart = dest;
473 mEnd = dest + oldSize;
475 return true;
478 // This isn't 100% safe. But until someone manages to make a 1gig nodeset
479 // it should be ok.
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;
494 if (oldSize > 0) {
495 LOG_CHUNK_MOVE(mStart, dest, oldSize);
496 memcpy((void*)dest, mStart, oldSize * sizeof(txXPathNode));
499 if (mStartBuffer) {
500 #ifdef DEBUG
501 memset(mStartBuffer, 0, (mEndBuffer - mStartBuffer) * sizeof(txXPathNode));
502 #endif
503 free(mStartBuffer);
506 mStartBuffer = newArr;
507 mEndBuffer = mStartBuffer + newLength;
508 mStart = dest;
509 mEnd = dest + oldSize;
511 return true;
514 txXPathNode* txNodeSet::findPosition(const txXPathNode& aNode,
515 txXPathNode* aFirst, txXPathNode* aLast,
516 bool& aDupe) const {
517 aDupe = false;
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);
523 if (cmp < 0) {
524 return pos;
527 if (cmp == 0) {
528 aDupe = true;
530 return pos;
533 return pos;
536 // (cannot add two pointers)
537 txXPathNode* midpos = aFirst + (aLast - aFirst) / 2;
538 int cmp = txXPathNodeUtils::comparePosition(aNode, *midpos);
539 if (cmp == 0) {
540 aDupe = true;
542 return midpos;
545 if (cmp > 0) {
546 return findPosition(aNode, midpos + 1, aLast, aDupe);
549 // midpos excluded as end of range
551 return findPosition(aNode, aFirst, midpos, aDupe);
554 /* static */
555 void txNodeSet::copyElements(txXPathNode* aDest, const txXPathNode* aStart,
556 const txXPathNode* aEnd) {
557 const txXPathNode* pos = aStart;
558 while (pos < aEnd) {
559 new (aDest) txXPathNode(*pos);
560 ++aDest;
561 ++pos;
565 /* static */
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));