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 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
|
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* XSEC
*
* XSECXPathNodeList := A structure to hold node lists from XPath
* evaluations
*
* $Id: XSECXPathNodeList.cpp 1125514 2011-05-20 19:08:33Z scantor $
*
*/
// XSEC
#include <xsec/utils/XSECXPathNodeList.hpp>
#include <xsec/framework/XSECError.hpp>
#include <string.h>
XERCES_CPP_NAMESPACE_USE
// --------------------------------------------------------------------------------
// Constructors and Destructors.
// --------------------------------------------------------------------------------
XSECXPathNodeList::XSECXPathNodeList(unsigned int initialSize) {
m_num = 0;
mp_tree = NULL;
}
XSECXPathNodeList::XSECXPathNodeList(const XSECXPathNodeList &other) {
m_num = other.m_num;
mp_tree = copy_tree(other.mp_tree);
mp_current = NULL;
}
XSECXPathNodeList::~XSECXPathNodeList() {
// Delete all the elements in the node list
delete_tree(mp_tree);
}
XSECXPathNodeList & XSECXPathNodeList::operator= (const XSECXPathNodeList & toCopy) {
// For now simply delete the old list and set with the new
// Large overhead as we call other functions, but simplest way to
// implement for now
mp_tree = copy_tree(toCopy.mp_tree);
m_num = toCopy.m_num;
mp_current = NULL;
return *this;
}
// --------------------------------------------------------------------------------
// Utility Functions.
// --------------------------------------------------------------------------------
XSECXPathNodeList::btn * XSECXPathNodeList::findNodeIndex(const DOMNode *n) const {
btn * t = mp_tree;
while (t != NULL && t->v != n) {
if (n > t->v)
t = t->r;
else
t = t->l;
}
return t;
}
void XSECXPathNodeList::delete_tree(btn * t) {
if (t == NULL)
return;
btn * parent, * n;
n = t;
while (n != NULL) {
if (n->l)
n = n->l;
else if (n->r)
n = n->r;
else {
parent = n->p;
if (parent != NULL) {
if (parent->l == n)
parent->l = NULL;
else
parent->r = NULL;
}
// Delete this node
delete n;
n = parent;
}
}
}
XSECXPathNodeList::btn * XSECXPathNodeList::copy_tree(btn * t) const {
if (t == NULL)
return NULL;
btn * n, *c, *cp, *ret;
c = cp = NULL;
n = t;
bool create = true;
ret = NULL;
while (n != NULL) {
if (create) {
XSECnew(c, btn);
c->l = NULL;
c->r = NULL;
c->v = n->v;
// R we at top?
if (ret == NULL) {
ret = c;
c->p = NULL;
cp = NULL;
}
else {
c->p = cp;
if (cp != NULL) {
if (n->p->l == n)
cp->l = c;
else
cp->r = c;
}
}
}
// Go down!
if (c->l == NULL && n->l != NULL) {
cp = c;
n = n->l;
create = true;
}
else if (c->r == NULL && n->r != NULL) {
cp = c;
n = n->r;
create = true;
}
else {
// Go Back Up!
n = n->p;
c = cp;
if (cp != NULL)
cp = cp->p;
create = false;
}
}
return ret;
}
// --------------------------------------------------------------------------------
// Adding and Deleting Nodes.
// --------------------------------------------------------------------------------
long XSECXPathNodeList::balance_count(btn * t) const {
if (t == NULL)
return 0;
long r = (t->r == NULL ? 0 : t->r->h);
long l = (t->l == NULL ? 0 : t->l->h);
return r - l;
}
long XSECXPathNodeList::calc_height(btn * t) {
if (t == NULL)
return 0;
if (t->l == NULL) {
if (t->r == NULL)
return 1;
return t->r->h + 1;
}
else {
if (t->r == NULL)
return t->l->h + 1;
}
return ((t->l->h > t->r->h ? t->l->h : t->r->h) + 1);
}
void XSECXPathNodeList::rotate_right(btn * t) {
// Rotate me right!
btn * lc = t->l;
// First - are we at the root?
if (t == mp_tree) {
lc->p = NULL;
mp_tree = lc;
}
else {
if (t->p->l == t) {
t->p->l = lc;
}
else {
t->p->r = lc;
}
lc->p = t->p;
}
// Do the rotate
t->l = lc->r;
if (t->l)
t->l->p = t;
lc->r = t;
t->p = lc;
// Recalculate heights
lc = t;
while (lc != NULL) {
lc->h = calc_height(lc);
lc = lc->p;
}
}
void XSECXPathNodeList::rotate_left(btn * t) {
// Rotate me left!
btn * rc = t->r;
// First - are we at the root?
if (t == mp_tree) {
rc->p = NULL;
mp_tree = rc;
}
else {
if (t->p->r == t) {
t->p->r = rc;
}
else {
t->p->l = rc;
}
rc->p = t->p;
}
// Do the rotate
t->r = rc->l;
if (t->r)
t->r->p = t;
rc->l = t;
t->p = rc;
// Recalculate heights
rc = t;
while (rc != NULL) {
rc->h = calc_height(rc);
rc = rc->p;
}
}
void XSECXPathNodeList::addNode(const DOMNode *n) {
btn * v;
if (m_num == 0) {
XSECnew(mp_tree, btn);
mp_tree->l = mp_tree->r = NULL;
mp_tree->v = n;
mp_tree->p = NULL;
mp_tree->h = 1;
m_num = 1;
return;
}
// Find the node
btn * t = mp_tree;
btn * last = NULL;
while (t != NULL && t->v != n) {
last = t;
if (n > t->v)
t = t->r;
else
t = t->l;
}
if (t != NULL)
// Node already exists in tree!
return;
// Work out the insertion.
XSECnew(v, btn);
v->v = n;
v->r = v->l = NULL;
v->h = 1;
v->p = last;
// Determine on which leg to place the new value
if (n > last->v)
last->r = v;
else
last->l = v;
// Recalculate heights
t = last;
long newh;
while (t != NULL) {
newh = calc_height(t);
if (newh > t->h) {
t->h = newh;
t = t->p;
}
else
// If the height is the same here, then nothing will have changed above
t = NULL;
}
// Rebalance!
t = last;
while (t != NULL) {
long bal = balance_count(t);
long balrc = balance_count(t->r);
long ballc = balance_count(t->l);
// Case 1 - Balance is OK
if (bal <= 1 && bal >= -1) {
// Do nothing!
}
// Case 2a - Balance is -2 and LC = -1
else if (bal == -2 && ballc == -1) {
// Rotate current node right
rotate_right(t);
}
// Or balance is +2 and RC = +1
else if (bal == 2 && balrc == 1) {
// Rotate current node left
rotate_left(t);
}
// Case 2b = Balance is -2 and LC = +1
else if (bal == -2 && ballc == 1) {
// Double Right rotation
rotate_left(t->l);
rotate_right(t);
}
else {
rotate_right(t->r);
rotate_left(t);
}
t = t->p;
}
}
void XSECXPathNodeList::removeNode(const DOMNode *n) {
btn * t, * last;
last = NULL;
btn * i = findNodeIndex(n);
if (i == NULL)
// Not found!
return;
// Delete from tree
if (i == mp_tree) {
// Bugger - we are at the top of the tree
// OK - No children?
if (i->l == NULL && i->r == NULL) {
// WooHoo! Easy!
delete mp_tree;
mp_tree = NULL;
}
// One Child?
if (i->l != NULL && i->r == NULL) {
// WooHoo! Easy!
mp_tree = i->l;
mp_tree->p = NULL;
delete i;
}
if (i->r != NULL && i->l == NULL) {
// WooHoo! Easy!
mp_tree = i->r;
mp_tree->p = NULL;
delete i;
}
// Oh dear = we have two children and now some heartache
if (i->r->l == NULL && i->r->r == NULL) {
// No subtree on right hand side.
mp_tree = mp_tree->l;
mp_tree->p = NULL;
t = mp_tree->r;
last = mp_tree;
while (t != NULL) {
last = t;
if (i->r->v < t->v)
t = t->l;
else
t = t->r;
}
if (i->r->v < last->v) {
last->l = i->r;
i->r->p = last;
}
else {
last->r = i->r;
i->r->p = last;
}
}
else {
// Need to find the "in-order" successor
t = i->r;
while (t != NULL) {
last = t;
t=t->l;
}
if (last == i->r) {
// can't have been a left hand leg on the next node!)
last->l = i->l;
if (last->l != NULL)
last->l->p = last;
mp_tree = last;
last->p = NULL;
delete i;
}
else {
// OK - Last is now the next biggest node, and it doesn;t
// have anything on it's left (otherwise there would be something smaller)
last->p->l = last->r;
last->r->p = last->p;
last->l = i->l;
last->r = i->r;
if (last->r != NULL)
last->r->p = last;
if (last->l != NULL)
last->l->p = last;
mp_tree = last;
last->p = NULL;
delete i;
}
}
}
else {
/* i != mp_tree */
// OK - No children?
if (i->l == NULL && i->r == NULL) {
// WooHoo! Easy!
if (i->p->l == i)
i->p->l = NULL;
else
i->p->r = NULL;
delete i;
}
// One Child?
if (i->l != NULL && i->r == NULL) {
// WooHoo! Easy!
if (i->p->l == i) {
i->p->l = i->l;
i->l->p = i->p;
}
else {
i->p->r = i->l;
i->r->p = i->p;
}
delete i;
}
if (i->r != NULL && i->l == NULL) {
// WooHoo! Easy!
if (i->p->l == i) {
i->p->l = i->r;
i->r->p = i->p;
}
else {
i->p->r = i->r;
i->r->p = i->p;
}
delete i;
}
// Oh dear = we have two children and now some heartache
if (i->r->l == NULL && i->r->r == NULL) {
// No subtree on right hand side.
if (i->p->l == i) {
// Was left hand child
i->p->l = i->l;
i->l->p = i->p;
t = i->l;
// Find insertion point for dangling node
while (t != NULL) {
last = t;
t = t->r;
}
last->r = i->r;
i->r->p = last;
}
else {
// Was right hand child
i->p->r = i->l;
i->l->p = i->p;
t = i->l;
// Find insertion point for dangling node
while (t != NULL) {
last = t;
t = t->r;
}
last->r = i->r;
i->r->p = last;
}
}
else {
// Subtree - so need to find the "in-order" successor
t = i->r;
while (t != NULL) {
last = t;
t=t->l;
}
// OK - Last is now the next biggest node, and it doesn;t
// have anything on it's left (otherwise there would be something smaller)
last->p->l = last->r;
last->r->p = last->p;
last->l = i->l;
last->r = i->r;
if (last->r != NULL)
last->r->p = last;
if (last->l != NULL)
last->l->p = last;
mp_tree = last;
last->p = NULL;
delete i;
}
}
m_num--;
}
void XSECXPathNodeList::clear() {
m_num = 0;
delete_tree(mp_tree);
mp_tree = NULL;
}
// --------------------------------------------------------------------------------
// Information functions.
// --------------------------------------------------------------------------------
bool XSECXPathNodeList::hasNode(const DOMNode *n) const {
btn * i = findNodeIndex(n);
return (i != NULL);
}
const DOMNode *XSECXPathNodeList::getFirstNode(void) const {
if (mp_tree == NULL)
return NULL;
// Find the smallest node
mp_current = mp_tree;
while (mp_current->l != NULL)
mp_current = mp_current->l;
return mp_current->v;
}
const DOMNode *XSECXPathNodeList::getNextNode(void) const {
if (mp_current == NULL)
return NULL;
btn * t = mp_current;
if (t->r != NULL) {
// Find next biggest node
t = t->r;
while (t->l != NULL)
t = t->l;
mp_current = t;
}
else {
// Go backwards!
t = mp_current->p;
while (t != NULL && t->r == mp_current) {
mp_current = t;
t = t->p;
}
if (t == NULL) {
mp_current = NULL;
return NULL;
}
mp_current = t;
}
return t->v;
}
// --------------------------------------------------------------------------------
// Intersect with another list
// --------------------------------------------------------------------------------
void XSECXPathNodeList::intersect(const XSECXPathNodeList &toIntersect) {
// Create a new list
XSECXPathNodeList ret;
const DOMNode * n = getFirstNode();
while (n != NULL) {
if (toIntersect.hasNode(n))
ret.addNode(n);
n = getNextNode();
}
// Swap lists
btn * t = mp_tree;
mp_tree = ret.mp_tree;
ret.mp_tree = t;
m_num = ret.m_num;
return;
}
|