michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "txNodeSet.h" michael@0: #include "txLog.h" michael@0: #include "nsMemory.h" michael@0: #include "txXPathTreeWalker.h" michael@0: #include michael@0: michael@0: /** michael@0: * Implementation of an XPath nodeset michael@0: */ michael@0: michael@0: #ifdef NS_BUILD_REFCNT_LOGGING michael@0: #define LOG_CHUNK_MOVE(_start, _new_start, _count) \ michael@0: { \ michael@0: txXPathNode *start = const_cast(_start); \ michael@0: while (start < _start + _count) { \ michael@0: NS_LogDtor(start, "txXPathNode", sizeof(*start)); \ michael@0: ++start; \ michael@0: } \ michael@0: start = const_cast(_new_start); \ michael@0: while (start < _new_start + _count) { \ michael@0: NS_LogCtor(start, "txXPathNode", sizeof(*start)); \ michael@0: ++start; \ michael@0: } \ michael@0: } michael@0: #else michael@0: #define LOG_CHUNK_MOVE(_start, _new_start, _count) michael@0: #endif michael@0: michael@0: static const int32_t kTxNodeSetMinSize = 4; michael@0: static const int32_t kTxNodeSetGrowFactor = 2; michael@0: michael@0: #define kForward 1 michael@0: #define kReversed -1 michael@0: michael@0: txNodeSet::txNodeSet(txResultRecycler* aRecycler) michael@0: : txAExprResult(aRecycler), michael@0: mStart(nullptr), michael@0: mEnd(nullptr), michael@0: mStartBuffer(nullptr), michael@0: mEndBuffer(nullptr), michael@0: mDirection(kForward), michael@0: mMarks(nullptr) michael@0: { michael@0: } michael@0: michael@0: txNodeSet::txNodeSet(const txXPathNode& aNode, txResultRecycler* aRecycler) michael@0: : txAExprResult(aRecycler), michael@0: mStart(nullptr), michael@0: mEnd(nullptr), michael@0: mStartBuffer(nullptr), michael@0: mEndBuffer(nullptr), michael@0: mDirection(kForward), michael@0: mMarks(nullptr) michael@0: { michael@0: if (!ensureGrowSize(1)) { michael@0: return; michael@0: } michael@0: michael@0: new(mStart) txXPathNode(aNode); michael@0: ++mEnd; michael@0: } michael@0: michael@0: txNodeSet::txNodeSet(const txNodeSet& aSource, txResultRecycler* aRecycler) michael@0: : txAExprResult(aRecycler), michael@0: mStart(nullptr), michael@0: mEnd(nullptr), michael@0: mStartBuffer(nullptr), michael@0: mEndBuffer(nullptr), michael@0: mDirection(kForward), michael@0: mMarks(nullptr) michael@0: { michael@0: append(aSource); michael@0: } michael@0: michael@0: txNodeSet::~txNodeSet() michael@0: { michael@0: delete [] mMarks; michael@0: michael@0: if (mStartBuffer) { michael@0: destroyElements(mStart, mEnd); michael@0: michael@0: nsMemory::Free(mStartBuffer); michael@0: } michael@0: } michael@0: michael@0: nsresult txNodeSet::add(const txXPathNode& aNode) michael@0: { michael@0: NS_ASSERTION(mDirection == kForward, michael@0: "only append(aNode) is supported on reversed nodesets"); michael@0: michael@0: if (isEmpty()) { michael@0: return append(aNode); michael@0: } michael@0: michael@0: bool dupe; michael@0: txXPathNode* pos = findPosition(aNode, mStart, mEnd, dupe); michael@0: michael@0: if (dupe) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: // save pos, ensureGrowSize messes with the pointers michael@0: int32_t moveSize = mEnd - pos; michael@0: int32_t offset = pos - mStart; michael@0: if (!ensureGrowSize(1)) { michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: // set pos to where it was michael@0: pos = mStart + offset; michael@0: michael@0: if (moveSize > 0) { michael@0: LOG_CHUNK_MOVE(pos, pos + 1, moveSize); michael@0: memmove(pos + 1, pos, moveSize * sizeof(txXPathNode)); michael@0: } michael@0: michael@0: new(pos) txXPathNode(aNode); michael@0: ++mEnd; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult txNodeSet::add(const txNodeSet& aNodes) michael@0: { michael@0: return add(aNodes, copyElements, nullptr); michael@0: } michael@0: michael@0: nsresult txNodeSet::addAndTransfer(txNodeSet* aNodes) michael@0: { michael@0: // failure is out-of-memory, transfer didn't happen michael@0: nsresult rv = add(*aNodes, transferElements, destroyElements); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: #ifdef TX_DONT_RECYCLE_BUFFER michael@0: if (aNodes->mStartBuffer) { michael@0: nsMemory::Free(aNodes->mStartBuffer); michael@0: aNodes->mStartBuffer = aNodes->mEndBuffer = nullptr; michael@0: } michael@0: #endif michael@0: aNodes->mStart = aNodes->mEnd = aNodes->mStartBuffer; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: /** michael@0: * add(aNodeSet, aTransferOp) michael@0: * michael@0: * The code is optimized to make a minimum number of calls to michael@0: * Node::compareDocumentPosition. The idea is this: michael@0: * We have the two nodesets (number indicate "document position") michael@0: * michael@0: * 1 3 7 <- source 1 michael@0: * 2 3 6 8 9 <- source 2 michael@0: * _ _ _ _ _ _ _ _ <- result michael@0: * michael@0: * michael@0: * When merging these nodesets into the result, the nodes are transfered michael@0: * in chunks to the end of the buffer so that each chunk does not contain michael@0: * a node from the other nodeset, in document order. michael@0: * michael@0: * We select the last non-transfered node in the first nodeset and find michael@0: * where in the other nodeset it would be inserted. In this case we would michael@0: * take the 7 from the first nodeset and find the position between the michael@0: * 6 and 8 in the second. We then take the nodes after the insert-position michael@0: * and transfer them to the end of the resulting nodeset. Which in this case michael@0: * means that we first transfered the 8 and 9 nodes, giving us the following: michael@0: * michael@0: * 1 3 7 <- source 1 michael@0: * 2 3 6 <- source 2 michael@0: * _ _ _ _ _ _ 8 9 <- result michael@0: * michael@0: * The corresponding procedure is done for the second nodeset, that is michael@0: * the insertion position of the 6 in the first nodeset is found, which michael@0: * is between the 3 and the 7. The 7 is memmoved (as it stays within michael@0: * the same nodeset) to the result buffer. michael@0: * michael@0: * As the result buffer is filled from the end, it is safe to share the michael@0: * buffer between this nodeset and the result. michael@0: * michael@0: * This is repeated until both of the nodesets are empty. michael@0: * michael@0: * If we find a duplicate node when searching for where insertposition we michael@0: * check for sequences of duplicate nodes, which can be optimized. michael@0: * michael@0: */ michael@0: nsresult txNodeSet::add(const txNodeSet& aNodes, transferOp aTransfer, michael@0: destroyOp aDestroy) michael@0: { michael@0: NS_ASSERTION(mDirection == kForward, michael@0: "only append(aNode) is supported on reversed nodesets"); michael@0: michael@0: if (aNodes.isEmpty()) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: if (!ensureGrowSize(aNodes.size())) { michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: // This is probably a rather common case, so lets try to shortcut. michael@0: if (mStart == mEnd || michael@0: txXPathNodeUtils::comparePosition(mEnd[-1], *aNodes.mStart) < 0) { michael@0: aTransfer(mEnd, aNodes.mStart, aNodes.mEnd); michael@0: mEnd += aNodes.size(); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: // Last element in this nodeset michael@0: txXPathNode* thisPos = mEnd; michael@0: michael@0: // Last element of the other nodeset michael@0: txXPathNode* otherPos = aNodes.mEnd; michael@0: michael@0: // Pointer to the insertion point in this nodeset michael@0: txXPathNode* insertPos = mEndBuffer; michael@0: michael@0: bool dupe; michael@0: txXPathNode* pos; michael@0: int32_t count; michael@0: while (thisPos > mStart || otherPos > aNodes.mStart) { michael@0: // Find where the last remaining node of this nodeset would michael@0: // be inserted in the other nodeset. michael@0: if (thisPos > mStart) { michael@0: pos = findPosition(thisPos[-1], aNodes.mStart, otherPos, dupe); michael@0: michael@0: if (dupe) { michael@0: const txXPathNode *deletePos = thisPos; michael@0: --thisPos; // this is already added michael@0: // check dupe sequence michael@0: while (thisPos > mStart && pos > aNodes.mStart && michael@0: thisPos[-1] == pos[-1]) { michael@0: --thisPos; michael@0: --pos; michael@0: } michael@0: michael@0: if (aDestroy) { michael@0: aDestroy(thisPos, deletePos); michael@0: } michael@0: } michael@0: } michael@0: else { michael@0: pos = aNodes.mStart; michael@0: } michael@0: michael@0: // Transfer the otherNodes after the insertion point to the result michael@0: count = otherPos - pos; michael@0: if (count > 0) { michael@0: insertPos -= count; michael@0: aTransfer(insertPos, pos, otherPos); michael@0: otherPos -= count; michael@0: } michael@0: michael@0: // Find where the last remaining node of the otherNodeset would michael@0: // be inserted in this nodeset. michael@0: if (otherPos > aNodes.mStart) { michael@0: pos = findPosition(otherPos[-1], mStart, thisPos, dupe); michael@0: michael@0: if (dupe) { michael@0: const txXPathNode *deletePos = otherPos; michael@0: --otherPos; // this is already added michael@0: // check dupe sequence michael@0: while (otherPos > aNodes.mStart && pos > mStart && michael@0: otherPos[-1] == pos[-1]) { michael@0: --otherPos; michael@0: --pos; michael@0: } michael@0: michael@0: if (aDestroy) { michael@0: aDestroy(otherPos, deletePos); michael@0: } michael@0: } michael@0: } michael@0: else { michael@0: pos = mStart; michael@0: } michael@0: michael@0: // Move the nodes from this nodeset after the insertion point michael@0: // to the result michael@0: count = thisPos - pos; michael@0: if (count > 0) { michael@0: insertPos -= count; michael@0: LOG_CHUNK_MOVE(pos, insertPos, count); michael@0: memmove(insertPos, pos, count * sizeof(txXPathNode)); michael@0: thisPos -= count; michael@0: } michael@0: } michael@0: mStart = insertPos; michael@0: mEnd = mEndBuffer; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: /** michael@0: * Append API michael@0: * These functions should be used with care. michael@0: * They are intended to be used when the caller assures that the resulting michael@0: * nodeset remains in document order. michael@0: * Abuse will break document order, and cause errors in the result. michael@0: * These functions are significantly faster than the add API, as no michael@0: * order info operations will be performed. michael@0: */ michael@0: michael@0: nsresult michael@0: txNodeSet::append(const txXPathNode& aNode) michael@0: { michael@0: if (!ensureGrowSize(1)) { michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: if (mDirection == kForward) { michael@0: new(mEnd) txXPathNode(aNode); michael@0: ++mEnd; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: new(--mStart) txXPathNode(aNode); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: txNodeSet::append(const txNodeSet& aNodes) michael@0: { michael@0: NS_ASSERTION(mDirection == kForward, michael@0: "only append(aNode) is supported on reversed nodesets"); michael@0: michael@0: if (aNodes.isEmpty()) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: int32_t appended = aNodes.size(); michael@0: if (!ensureGrowSize(appended)) { michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: copyElements(mEnd, aNodes.mStart, aNodes.mEnd); michael@0: mEnd += appended; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: txNodeSet::mark(int32_t aIndex) michael@0: { michael@0: NS_ASSERTION(aIndex >= 0 && mStart && mEnd - mStart > aIndex, michael@0: "index out of bounds"); michael@0: if (!mMarks) { michael@0: int32_t length = size(); michael@0: mMarks = new bool[length]; michael@0: NS_ENSURE_TRUE(mMarks, NS_ERROR_OUT_OF_MEMORY); michael@0: memset(mMarks, 0, length * sizeof(bool)); michael@0: } michael@0: if (mDirection == kForward) { michael@0: mMarks[aIndex] = true; michael@0: } michael@0: else { michael@0: mMarks[size() - aIndex - 1] = true; michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: txNodeSet::sweep() michael@0: { michael@0: if (!mMarks) { michael@0: // sweep everything michael@0: clear(); michael@0: } michael@0: michael@0: int32_t chunk, pos = 0; michael@0: int32_t length = size(); michael@0: txXPathNode* insertion = mStartBuffer; michael@0: michael@0: while (pos < length) { michael@0: while (pos < length && !mMarks[pos]) { michael@0: // delete unmarked michael@0: mStart[pos].~txXPathNode(); michael@0: ++pos; michael@0: } michael@0: // find chunk to move michael@0: chunk = 0; michael@0: while (pos < length && mMarks[pos]) { michael@0: ++pos; michael@0: ++chunk; michael@0: } michael@0: // move chunk michael@0: if (chunk > 0) { michael@0: LOG_CHUNK_MOVE(mStart + pos - chunk, insertion, chunk); michael@0: memmove(insertion, mStart + pos - chunk, michael@0: chunk * sizeof(txXPathNode)); michael@0: insertion += chunk; michael@0: } michael@0: } michael@0: mStart = mStartBuffer; michael@0: mEnd = insertion; michael@0: delete [] mMarks; michael@0: mMarks = nullptr; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: txNodeSet::clear() michael@0: { michael@0: destroyElements(mStart, mEnd); michael@0: #ifdef TX_DONT_RECYCLE_BUFFER michael@0: if (mStartBuffer) { michael@0: nsMemory::Free(mStartBuffer); michael@0: mStartBuffer = mEndBuffer = nullptr; michael@0: } michael@0: #endif michael@0: mStart = mEnd = mStartBuffer; michael@0: delete [] mMarks; michael@0: mMarks = nullptr; michael@0: mDirection = kForward; michael@0: } michael@0: michael@0: int32_t michael@0: txNodeSet::indexOf(const txXPathNode& aNode, uint32_t aStart) const michael@0: { michael@0: NS_ASSERTION(mDirection == kForward, michael@0: "only append(aNode) is supported on reversed nodesets"); michael@0: michael@0: if (!mStart || mStart == mEnd) { michael@0: return -1; michael@0: } michael@0: michael@0: txXPathNode* pos = mStart + aStart; michael@0: for (; pos < mEnd; ++pos) { michael@0: if (*pos == aNode) { michael@0: return pos - mStart; michael@0: } michael@0: } michael@0: michael@0: return -1; michael@0: } michael@0: michael@0: const txXPathNode& michael@0: txNodeSet::get(int32_t aIndex) const michael@0: { michael@0: if (mDirection == kForward) { michael@0: return mStart[aIndex]; michael@0: } michael@0: michael@0: return mEnd[-aIndex - 1]; michael@0: } michael@0: michael@0: short michael@0: txNodeSet::getResultType() michael@0: { michael@0: return txAExprResult::NODESET; michael@0: } michael@0: michael@0: bool michael@0: txNodeSet::booleanValue() michael@0: { michael@0: return !isEmpty(); michael@0: } michael@0: double michael@0: txNodeSet::numberValue() michael@0: { michael@0: nsAutoString str; michael@0: stringValue(str); michael@0: michael@0: return txDouble::toDouble(str); michael@0: } michael@0: michael@0: void michael@0: txNodeSet::stringValue(nsString& aStr) michael@0: { michael@0: NS_ASSERTION(mDirection == kForward, michael@0: "only append(aNode) is supported on reversed nodesets"); michael@0: if (isEmpty()) { michael@0: return; michael@0: } michael@0: txXPathNodeUtils::appendNodeValue(get(0), aStr); michael@0: } michael@0: michael@0: const nsString* michael@0: txNodeSet::stringValuePointer() michael@0: { michael@0: return nullptr; michael@0: } michael@0: michael@0: bool txNodeSet::ensureGrowSize(int32_t aSize) michael@0: { michael@0: // check if there is enough place in the buffer as is michael@0: if (mDirection == kForward && aSize <= mEndBuffer - mEnd) { michael@0: return true; michael@0: } michael@0: michael@0: if (mDirection == kReversed && aSize <= mStart - mStartBuffer) { michael@0: return true; michael@0: } michael@0: michael@0: // check if we just have to align mStart to have enough space michael@0: int32_t oldSize = mEnd - mStart; michael@0: int32_t oldLength = mEndBuffer - mStartBuffer; michael@0: int32_t ensureSize = oldSize + aSize; michael@0: if (ensureSize <= oldLength) { michael@0: // just move the buffer michael@0: txXPathNode* dest = mStartBuffer; michael@0: if (mDirection == kReversed) { michael@0: dest = mEndBuffer - oldSize; michael@0: } michael@0: LOG_CHUNK_MOVE(mStart, dest, oldSize); michael@0: memmove(dest, mStart, oldSize * sizeof(txXPathNode)); michael@0: mStart = dest; michael@0: mEnd = dest + oldSize; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: // This isn't 100% safe. But until someone manages to make a 1gig nodeset michael@0: // it should be ok. michael@0: int32_t newLength = std::max(oldLength, kTxNodeSetMinSize); michael@0: michael@0: while (newLength < ensureSize) { michael@0: newLength *= kTxNodeSetGrowFactor; michael@0: } michael@0: michael@0: txXPathNode* newArr = static_cast michael@0: (nsMemory::Alloc(newLength * michael@0: sizeof(txXPathNode))); michael@0: if (!newArr) { michael@0: return false; michael@0: } michael@0: michael@0: txXPathNode* dest = newArr; michael@0: if (mDirection == kReversed) { michael@0: dest += newLength - oldSize; michael@0: } michael@0: michael@0: if (oldSize > 0) { michael@0: LOG_CHUNK_MOVE(mStart, dest, oldSize); michael@0: memcpy(dest, mStart, oldSize * sizeof(txXPathNode)); michael@0: } michael@0: michael@0: if (mStartBuffer) { michael@0: #ifdef DEBUG michael@0: memset(mStartBuffer, 0, michael@0: (mEndBuffer - mStartBuffer) * sizeof(txXPathNode)); michael@0: #endif michael@0: nsMemory::Free(mStartBuffer); michael@0: } michael@0: michael@0: mStartBuffer = newArr; michael@0: mEndBuffer = mStartBuffer + newLength; michael@0: mStart = dest; michael@0: mEnd = dest + oldSize; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: txXPathNode* michael@0: txNodeSet::findPosition(const txXPathNode& aNode, txXPathNode* aFirst, michael@0: txXPathNode* aLast, bool& aDupe) const michael@0: { michael@0: aDupe = false; michael@0: if (aLast - aFirst <= 2) { michael@0: // If we search 2 nodes or less there is no point in further divides michael@0: txXPathNode* pos = aFirst; michael@0: for (; pos < aLast; ++pos) { michael@0: int cmp = txXPathNodeUtils::comparePosition(aNode, *pos); michael@0: if (cmp < 0) { michael@0: return pos; michael@0: } michael@0: michael@0: if (cmp == 0) { michael@0: aDupe = true; michael@0: michael@0: return pos; michael@0: } michael@0: } michael@0: return pos; michael@0: } michael@0: michael@0: // (cannot add two pointers) michael@0: txXPathNode* midpos = aFirst + (aLast - aFirst) / 2; michael@0: int cmp = txXPathNodeUtils::comparePosition(aNode, *midpos); michael@0: if (cmp == 0) { michael@0: aDupe = true; michael@0: michael@0: return midpos; michael@0: } michael@0: michael@0: if (cmp > 0) { michael@0: return findPosition(aNode, midpos + 1, aLast, aDupe); michael@0: } michael@0: michael@0: // midpos excluded as end of range michael@0: michael@0: return findPosition(aNode, aFirst, midpos, aDupe); michael@0: } michael@0: michael@0: /* static */ michael@0: void michael@0: txNodeSet::copyElements(txXPathNode* aDest, michael@0: const txXPathNode* aStart, const txXPathNode* aEnd) michael@0: { michael@0: const txXPathNode* pos = aStart; michael@0: while (pos < aEnd) { michael@0: new(aDest) txXPathNode(*pos); michael@0: ++aDest; michael@0: ++pos; michael@0: } michael@0: } michael@0: michael@0: /* static */ michael@0: void michael@0: txNodeSet::transferElements(txXPathNode* aDest, michael@0: const txXPathNode* aStart, const txXPathNode* aEnd) michael@0: { michael@0: LOG_CHUNK_MOVE(aStart, aDest, (aEnd - aStart)); michael@0: memcpy(aDest, aStart, (aEnd - aStart) * sizeof(txXPathNode)); michael@0: }