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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is TransforMiiX XSLT processor code.
*
* The Initial Developer of the Original Code is
* The MITRE Corporation.
* Portions created by the Initial Developer are Copyright (C) 1999
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Keith Visco <kvisco@ziplink.net> (Original Author)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "txNodeSet.h"
#include "TxLog.h"
#include "nsMemory.h"
/**
* Implementation of an XPath nodeset
*/
static const PRInt32 kTxNodeSetMinSize = 4;
static const PRInt32 kTxNodeSetGrowFactor = 2;
#define kForward 1
#define kReversed -1
txNodeSet::txNodeSet(txResultRecycler* aRecycler)
: txAExprResult(aRecycler),
mStart(nsnull),
mEnd(nsnull),
mStartBuffer(nsnull),
mEndBuffer(nsnull),
mDirection(kForward),
mMarks(nsnull)
{
}
txNodeSet::txNodeSet(const txXPathNode& aNode, txResultRecycler* aRecycler)
: txAExprResult(aRecycler),
mStart(nsnull),
mEnd(nsnull),
mStartBuffer(nsnull),
mEndBuffer(nsnull),
mDirection(kForward),
mMarks(nsnull)
{
if (!ensureGrowSize(1)) {
return;
}
new(mStart) txXPathNode(aNode);
++mEnd;
}
txNodeSet::txNodeSet(const txNodeSet& aSource, txResultRecycler* aRecycler)
: txAExprResult(aRecycler),
mStart(nsnull),
mEnd(nsnull),
mStartBuffer(nsnull),
mEndBuffer(nsnull),
mDirection(kForward),
mMarks(nsnull)
{
append(aSource);
}
txNodeSet::~txNodeSet()
{
delete [] mMarks;
if (mStartBuffer) {
while (mStart < mEnd) {
mStart->~txXPathNode();
++mStart;
}
nsMemory::Free(mStartBuffer);
}
}
nsresult txNodeSet::add(const txXPathNode& aNode)
{
NS_ASSERTION(mDirection == kForward,
"only append(aNode) is supported on reversed nodesets");
if (isEmpty()) {
return append(aNode);
}
PRBool dupe;
txXPathNode* pos = findPosition(aNode, mStart, mEnd, dupe);
if (dupe) {
return NS_OK;
}
// save pos, ensureGrowSize messes with the pointers
PRInt32 moveSize = mEnd - pos;
PRInt32 offset = pos - mStart;
if (!ensureGrowSize(1)) {
return NS_ERROR_OUT_OF_MEMORY;
}
// set pos to where it was
pos = mStart + offset;
if (moveSize > 0) {
memmove(pos + 1, pos, moveSize * sizeof(txXPathNode));
}
new(pos) txXPathNode(aNode);
++mEnd;
return NS_OK;
}
nsresult txNodeSet::add(const txNodeSet& aNodes)
{
return add(aNodes, copyElements);
}
nsresult txNodeSet::addAndTransfer(txNodeSet* aNodes)
{
// failure is out-of-memory, transfer didn't happen
nsresult rv = add(*aNodes, transferElements);
NS_ENSURE_SUCCESS(rv, rv);
#ifdef TX_DONT_RECYCLE_BUFFER
if (aNodes->mStartBuffer) {
nsMemory::Free(aNodes->mStartBuffer);
aNodes->mStartBuffer = aNodes->mEndBuffer = nsnull;
}
#endif
aNodes->mStart = aNodes->mEnd = aNodes->mStartBuffer;
return NS_OK;
}
/**
* add(aNodeSet, aTransferOp)
*
* The code is optimized to make a minimum number of calls to
* Node::compareDocumentPosition. The idea is this:
* We have the two nodesets (number indicate "document position")
*
* 1 3 7 <- source 1
* 2 3 6 8 9 <- source 2
* _ _ _ _ _ _ _ _ <- result
*
*
* When merging these nodesets into the result, the nodes are transfered
* in chunks to the end of the buffer so that each chunk does not contain
* a node from the other nodeset, in document order.
*
* We select the last non-transfered node in the first nodeset and find
* where in the other nodeset it would be inserted. In this case we would
* take the 7 from the first nodeset and find the position between the
* 6 and 8 in the second. We then take the nodes after the insert-position
* and transfer them to the end of the resulting nodeset. Which in this case
* means that we first transfered the 8 and 9 nodes, giving us the following:
*
* 1 3 7 <- source 1
* 2 3 6 <- source 2
* _ _ _ _ _ _ 8 9 <- result
*
* The corresponding procedure is done for the second nodeset, that is
* the insertion position of the 6 in the first nodeset is found, which
* is between the 3 and the 7. The 7 is memmoved (as it stays within
* the same nodeset) to the result buffer.
*
* As the result buffer is filled from the end, it is safe to share the
* buffer between this nodeset and the result.
*
* This is repeated until both of the nodesets are empty.
*
* If we find a duplicate node when searching for where insertposition we
* check for sequences of duplicate nodes, which can be optimized.
*
*/
nsresult txNodeSet::add(const txNodeSet& aNodes, transferOp aTransfer)
{
NS_ASSERTION(mDirection == kForward,
"only append(aNode) is supported on reversed nodesets");
if (aNodes.isEmpty()) {
return NS_OK;
}
if (!ensureGrowSize(aNodes.size())) {
return NS_ERROR_OUT_OF_MEMORY;
}
// This is probably a rather common case, so lets try to shortcut.
if (mStart == mEnd ||
txXPathNodeUtils::comparePosition(mEnd[-1], *aNodes.mStart) < 0) {
aTransfer(mEnd, aNodes.mStart, aNodes.mEnd);
mEnd += aNodes.size();
return NS_OK;
}
// Last element in this nodeset
txXPathNode* thisPos = mEnd;
// Last element of the other nodeset
txXPathNode* otherPos = aNodes.mEnd;
// Pointer to the insertion point in this nodeset
txXPathNode* insertPos = mEndBuffer;
PRBool dupe;
txXPathNode* pos;
PRInt32 count;
while (thisPos > mStart || otherPos > aNodes.mStart) {
// Find where the last remaining node of this nodeset would
// be inserted in the other nodeset.
if (thisPos > mStart) {
pos = findPosition(thisPos[-1], aNodes.mStart, otherPos, dupe);
if (dupe) {
--thisPos; // this is already added
// check dupe sequence
while (thisPos > mStart && pos > aNodes.mStart &&
thisPos[-1] == pos[-1]) {
--thisPos;
--pos;
}
}
}
else {
pos = aNodes.mStart;
}
// Transfer the otherNodes after the insertion point to the result
count = otherPos - pos;
if (count > 0) {
insertPos -= count;
aTransfer(insertPos, pos, otherPos);
otherPos -= count;
}
// Find where the last remaining node of the otherNodeset would
// be inserted in this nodeset.
if (otherPos > aNodes.mStart) {
pos = findPosition(otherPos[-1], mStart, thisPos, dupe);
if (dupe) {
--otherPos; // this is already added
// check dupe sequence
while (otherPos > aNodes.mStart && pos > mStart &&
otherPos[-1] == pos[-1]) {
--otherPos;
--pos;
}
}
}
else {
pos = mStart;
}
// Move the nodes from this nodeset after the insertion point
// to the result
count = thisPos - pos;
if (count > 0) {
insertPos -= count;
memmove(insertPos, pos, count * sizeof(txXPathNode));
thisPos -= count;
}
}
mStart = insertPos;
mEnd = mEndBuffer;
return NS_OK;
}
/**
* 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.
*/
nsresult
txNodeSet::append(const txXPathNode& aNode)
{
if (!ensureGrowSize(1)) {
return NS_ERROR_OUT_OF_MEMORY;
}
if (mDirection == kForward) {
new(mEnd) txXPathNode(aNode);
++mEnd;
return NS_OK;
}
new(--mStart) txXPathNode(aNode);
return NS_OK;
}
nsresult
txNodeSet::append(const txNodeSet& aNodes)
{
NS_ASSERTION(mDirection == kForward,
"only append(aNode) is supported on reversed nodesets");
if (aNodes.isEmpty()) {
return NS_OK;
}
PRInt32 appended = aNodes.size();
if (!ensureGrowSize(appended)) {
return NS_ERROR_OUT_OF_MEMORY;
}
copyElements(mEnd, aNodes.mStart, aNodes.mEnd);
mEnd += appended;
return NS_OK;
}
nsresult
txNodeSet::mark(PRInt32 aIndex)
{
NS_ASSERTION(aIndex >= 0 && mStart && mEnd - mStart > aIndex,
"index out of bounds");
if (!mMarks) {
PRInt32 length = size();
mMarks = new PRPackedBool[length];
NS_ENSURE_TRUE(mMarks, NS_ERROR_OUT_OF_MEMORY);
memset(mMarks, 0, length * sizeof(PRPackedBool));
}
if (mDirection == kForward) {
mMarks[aIndex] = PR_TRUE;
}
else {
mMarks[size() - aIndex - 1] = PR_TRUE;
}
return NS_OK;
}
nsresult
txNodeSet::sweep()
{
if (!mMarks) {
// sweep everything
clear();
}
PRInt32 chunk, pos = 0;
PRInt32 length = size();
txXPathNode* insertion = mStartBuffer;
while (pos < length) {
while (pos < length && !mMarks[pos]) {
// delete unmarked
mStart[pos].~txXPathNode();
++pos;
}
// find chunk to move
chunk = 0;
while (pos < length && mMarks[pos]) {
++pos;
++chunk;
}
// move chunk
if (chunk > 0) {
memmove(insertion, mStart + pos - chunk,
chunk * sizeof(txXPathNode));
insertion += chunk;
}
}
mStart = mStartBuffer;
mEnd = insertion;
delete [] mMarks;
mMarks = nsnull;
return NS_OK;
}
void
txNodeSet::clear()
{
while (mStart < mEnd) {
mStart->~txXPathNode();
++mStart;
}
#ifdef TX_DONT_RECYCLE_BUFFER
if (mStartBuffer) {
nsMemory::Free(mStartBuffer);
mStartBuffer = mEndBuffer = nsnull;
}
#endif
mStart = mEnd = mStartBuffer;
delete [] mMarks;
mMarks = nsnull;
mDirection = kForward;
}
PRInt32
txNodeSet::indexOf(const txXPathNode& aNode) const
{
NS_ASSERTION(mDirection == kForward,
"only append(aNode) is supported on reversed nodesets");
if (!mStart || mStart == mEnd) {
return -1;
}
PRInt32 counter = 0;
txXPathNode* pos = mStart;
for (; pos < mEnd; ++counter, ++pos) {
if (*pos == aNode) {
return counter;
}
}
return -1;
}
const txXPathNode&
txNodeSet::get(PRInt32 aIndex) const
{
if (mDirection == kForward) {
return mStart[aIndex];
}
return mEnd[-aIndex - 1];
}
short
txNodeSet::getResultType()
{
return txAExprResult::NODESET;
}
PRBool
txNodeSet::booleanValue()
{
return !isEmpty();
}
double
txNodeSet::numberValue()
{
nsAutoString str;
stringValue(str);
return Double::toDouble(str);
}
void
txNodeSet::stringValue(nsAString& aStr)
{
NS_ASSERTION(mDirection == kForward,
"only append(aNode) is supported on reversed nodesets");
if (isEmpty()) {
return;
}
txXPathNodeUtils::appendNodeValue(get(0), aStr);
}
nsAString*
txNodeSet::stringValuePointer()
{
return nsnull;
}
PRBool txNodeSet::ensureGrowSize(PRInt32 aSize)
{
// check if there is enough place in the buffer as is
if (mDirection == kForward && aSize <= mEndBuffer - mEnd) {
return PR_TRUE;
}
if (mDirection == kReversed && aSize <= mStart - mStartBuffer) {
return PR_TRUE;
}
// check if we just have to align mStart to have enough space
PRInt32 oldSize = mEnd - mStart;
PRInt32 oldLength = mEndBuffer - mStartBuffer;
PRInt32 ensureSize = oldSize + aSize;
if (ensureSize <= oldLength) {
// just move the buffer
txXPathNode* dest = mStartBuffer;
if (mDirection == kReversed) {
dest = mEndBuffer - oldSize;
}
memmove(dest, mStart, oldSize * sizeof(txXPathNode));
mStart = dest;
mEnd = dest + oldSize;
return PR_TRUE;
}
// This isn't 100% safe. But until someone manages to make a 1gig nodeset
// it should be ok.
PRInt32 newLength = PR_MAX(oldLength, kTxNodeSetMinSize);
while (newLength < ensureSize) {
newLength *= kTxNodeSetGrowFactor;
}
txXPathNode* newArr = NS_STATIC_CAST(txXPathNode*,
nsMemory::Alloc(newLength *
sizeof(txXPathNode)));
if (!newArr) {
return PR_FALSE;
}
txXPathNode* dest = newArr;
if (mDirection == kReversed) {
dest += newLength - oldSize;
}
if (oldSize > 0) {
memcpy(dest, mStart, oldSize * sizeof(txXPathNode));
}
if (mStartBuffer) {
#ifdef DEBUG
memset(mStartBuffer, 0,
(mEndBuffer - mStartBuffer) * sizeof(txXPathNode));
#endif
nsMemory::Free(mStartBuffer);
}
mStartBuffer = newArr;
mEndBuffer = mStartBuffer + newLength;
mStart = dest;
mEnd = dest + oldSize;
return PR_TRUE;
}
txXPathNode*
txNodeSet::findPosition(const txXPathNode& aNode, txXPathNode* aFirst,
txXPathNode* aLast, PRBool& aDupe) const
{
aDupe = PR_FALSE;
if (aLast - aFirst <= 2) {
// If we search 2 nodes or less there is no point in further divides
txXPathNode* pos = aFirst;
for (; pos < aLast; ++pos) {
PRIntn cmp = txXPathNodeUtils::comparePosition(aNode, *pos);
if (cmp < 0) {
return pos;
}
if (cmp == 0) {
aDupe = PR_TRUE;
return pos;
}
}
return pos;
}
// (cannot add two pointers)
txXPathNode* midpos = aFirst + (aLast - aFirst) / 2;
PRIntn cmp = txXPathNodeUtils::comparePosition(aNode, *midpos);
if (cmp == 0) {
aDupe = PR_TRUE;
return midpos;
}
if (cmp > 0) {
return findPosition(aNode, midpos + 1, aLast, aDupe);
}
// midpos excluded as end of range
return findPosition(aNode, aFirst, midpos, aDupe);
}
/* static */
void
txNodeSet::copyElements(txXPathNode* aDest,
const txXPathNode* aStart, const txXPathNode* aEnd)
{
const txXPathNode* pos = aStart;
while (pos < aEnd) {
new(aDest) txXPathNode(*pos);
++aDest;
++pos;
}
}
/* static */
void
txNodeSet::transferElements(txXPathNode* aDest,
const txXPathNode* aStart, const txXPathNode* aEnd)
{
memcpy(aDest, aStart, (aEnd - aStart) * sizeof(txXPathNode));
}
|