1 16 19 package org.apache.xml.utils; 20 21 import java.io.Serializable ; 22 23 import org.apache.xml.dtm.DTM; 24 25 29 public class NodeVector implements Serializable , Cloneable 30 { 31 32 36 private int m_blocksize; 37 38 42 private int m_map[]; 43 44 48 protected int m_firstFree = 0; 49 50 54 private int m_mapSize; 56 59 public NodeVector() 60 { 61 m_blocksize = 32; 62 m_mapSize = 0; 63 } 64 65 70 public NodeVector(int blocksize) 71 { 72 m_blocksize = blocksize; 73 m_mapSize = 0; 74 } 75 76 83 public Object clone() throws CloneNotSupportedException 84 { 85 86 NodeVector clone = (NodeVector) super.clone(); 87 88 if ((null != this.m_map) && (this.m_map == clone.m_map)) 89 { 90 clone.m_map = new int[this.m_map.length]; 91 92 System.arraycopy(this.m_map, 0, clone.m_map, 0, this.m_map.length); 93 } 94 95 return clone; 96 } 97 98 103 public int size() 104 { 105 return m_firstFree; 106 } 107 108 113 public void addElement(int value) 114 { 115 116 if ((m_firstFree + 1) >= m_mapSize) 117 { 118 if (null == m_map) 119 { 120 m_map = new int[m_blocksize]; 121 m_mapSize = m_blocksize; 122 } 123 else 124 { 125 m_mapSize += m_blocksize; 126 127 int newMap[] = new int[m_mapSize]; 128 129 System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1); 130 131 m_map = newMap; 132 } 133 } 134 135 m_map[m_firstFree] = value; 136 137 m_firstFree++; 138 } 139 140 145 public final void push(int value) 146 { 147 148 int ff = m_firstFree; 149 150 if ((ff + 1) >= m_mapSize) 151 { 152 if (null == m_map) 153 { 154 m_map = new int[m_blocksize]; 155 m_mapSize = m_blocksize; 156 } 157 else 158 { 159 m_mapSize += m_blocksize; 160 161 int newMap[] = new int[m_mapSize]; 162 163 System.arraycopy(m_map, 0, newMap, 0, ff + 1); 164 165 m_map = newMap; 166 } 167 } 168 169 m_map[ff] = value; 170 171 ff++; 172 173 m_firstFree = ff; 174 } 175 176 181 public final int pop() 182 { 183 184 m_firstFree--; 185 186 int n = m_map[m_firstFree]; 187 188 m_map[m_firstFree] = DTM.NULL; 189 190 return n; 191 } 192 193 199 public final int popAndTop() 200 { 201 202 m_firstFree--; 203 204 m_map[m_firstFree] = DTM.NULL; 205 206 return (m_firstFree == 0) ? DTM.NULL : m_map[m_firstFree - 1]; 207 } 208 209 212 public final void popQuick() 213 { 214 215 m_firstFree--; 216 217 m_map[m_firstFree] = DTM.NULL; 218 } 219 220 227 public final int peepOrNull() 228 { 229 return ((null != m_map) && (m_firstFree > 0)) 230 ? m_map[m_firstFree - 1] : DTM.NULL; 231 } 232 233 241 public final void pushPair(int v1, int v2) 242 { 243 244 if (null == m_map) 245 { 246 m_map = new int[m_blocksize]; 247 m_mapSize = m_blocksize; 248 } 249 else 250 { 251 if ((m_firstFree + 2) >= m_mapSize) 252 { 253 m_mapSize += m_blocksize; 254 255 int newMap[] = new int[m_mapSize]; 256 257 System.arraycopy(m_map, 0, newMap, 0, m_firstFree); 258 259 m_map = newMap; 260 } 261 } 262 263 m_map[m_firstFree] = v1; 264 m_map[m_firstFree + 1] = v2; 265 m_firstFree += 2; 266 } 267 268 273 public final void popPair() 274 { 275 276 m_firstFree -= 2; 277 m_map[m_firstFree] = DTM.NULL; 278 m_map[m_firstFree + 1] = DTM.NULL; 279 } 280 281 288 public final void setTail(int n) 289 { 290 m_map[m_firstFree - 1] = n; 291 } 292 293 300 public final void setTailSub1(int n) 301 { 302 m_map[m_firstFree - 2] = n; 303 } 304 305 312 public final int peepTail() 313 { 314 return m_map[m_firstFree - 1]; 315 } 316 317 324 public final int peepTailSub1() 325 { 326 return m_map[m_firstFree - 2]; 327 } 328 329 334 public void insertInOrder(int value) 335 { 336 337 for (int i = 0; i < m_firstFree; i++) 338 { 339 if (value < m_map[i]) 340 { 341 insertElementAt(value, i); 342 343 return; 344 } 345 } 346 347 addElement(value); 348 } 349 350 359 public void insertElementAt(int value, int at) 360 { 361 362 if (null == m_map) 363 { 364 m_map = new int[m_blocksize]; 365 m_mapSize = m_blocksize; 366 } 367 else if ((m_firstFree + 1) >= m_mapSize) 368 { 369 m_mapSize += m_blocksize; 370 371 int newMap[] = new int[m_mapSize]; 372 373 System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1); 374 375 m_map = newMap; 376 } 377 378 if (at <= (m_firstFree - 1)) 379 { 380 System.arraycopy(m_map, at, m_map, at + 1, m_firstFree - at); 381 } 382 383 m_map[at] = value; 384 385 m_firstFree++; 386 } 387 388 393 public void appendNodes(NodeVector nodes) 394 { 395 396 int nNodes = nodes.size(); 397 398 if (null == m_map) 399 { 400 m_mapSize = nNodes + m_blocksize; 401 m_map = new int[m_mapSize]; 402 } 403 else if ((m_firstFree + nNodes) >= m_mapSize) 404 { 405 m_mapSize += (nNodes + m_blocksize); 406 407 int newMap[] = new int[m_mapSize]; 408 409 System.arraycopy(m_map, 0, newMap, 0, m_firstFree + nNodes); 410 411 m_map = newMap; 412 } 413 414 System.arraycopy(nodes.m_map, 0, m_map, m_firstFree, nNodes); 415 416 m_firstFree += nNodes; 417 } 418 419 425 public void removeAllElements() 426 { 427 428 if (null == m_map) 429 return; 430 431 for (int i = 0; i < m_firstFree; i++) 432 { 433 m_map[i] = DTM.NULL; 434 } 435 436 m_firstFree = 0; 437 } 438 439 442 public void RemoveAllNoClear() 443 { 444 445 if (null == m_map) 446 return; 447 448 m_firstFree = 0; 449 } 450 451 462 public boolean removeElement(int s) 463 { 464 465 if (null == m_map) 466 return false; 467 468 for (int i = 0; i < m_firstFree; i++) 469 { 470 int node = m_map[i]; 471 472 if (node == s) 473 { 474 if (i > m_firstFree) 475 System.arraycopy(m_map, i + 1, m_map, i - 1, m_firstFree - i); 476 else 477 m_map[i] = DTM.NULL; 478 479 m_firstFree--; 480 481 return true; 482 } 483 } 484 485 return false; 486 } 487 488 496 public void removeElementAt(int i) 497 { 498 499 if (null == m_map) 500 return; 501 502 if (i > m_firstFree) 503 System.arraycopy(m_map, i + 1, m_map, i - 1, m_firstFree - i); 504 else 505 m_map[i] = DTM.NULL; 506 } 507 508 518 public void setElementAt(int node, int index) 519 { 520 521 if (null == m_map) 522 { 523 m_map = new int[m_blocksize]; 524 m_mapSize = m_blocksize; 525 } 526 527 if(index == -1) 528 addElement(node); 529 530 m_map[index] = node; 531 } 532 533 540 public int elementAt(int i) 541 { 542 543 if (null == m_map) 544 return DTM.NULL; 545 546 return m_map[i]; 547 } 548 549 556 public boolean contains(int s) 557 { 558 559 if (null == m_map) 560 return false; 561 562 for (int i = 0; i < m_firstFree; i++) 563 { 564 int node = m_map[i]; 565 566 if (node == s) 567 return true; 568 } 569 570 return false; 571 } 572 573 584 public int indexOf(int elem, int index) 585 { 586 587 if (null == m_map) 588 return -1; 589 590 for (int i = index; i < m_firstFree; i++) 591 { 592 int node = m_map[i]; 593 594 if (node == elem) 595 return i; 596 } 597 598 return -1; 599 } 600 601 611 public int indexOf(int elem) 612 { 613 614 if (null == m_map) 615 return -1; 616 617 for (int i = 0; i < m_firstFree; i++) 618 { 619 int node = m_map[i]; 620 621 if (node == elem) 622 return i; 623 } 624 625 return -1; 626 } 627 628 637 public void sort(int a[], int lo0, int hi0) throws Exception 638 { 639 640 int lo = lo0; 641 int hi = hi0; 642 643 if (lo >= hi) 645 { 646 return; 647 } 648 else if (lo == hi - 1) 649 { 650 651 654 if (a[lo] > a[hi]) 655 { 656 int T = a[lo]; 657 658 a[lo] = a[hi]; 659 a[hi] = T; 660 } 661 662 return; 663 } 664 665 668 int pivot = a[(lo + hi) / 2]; 669 670 a[(lo + hi) / 2] = a[hi]; 671 a[hi] = pivot; 672 673 while (lo < hi) 674 { 675 676 680 while (a[lo] <= pivot && lo < hi) 681 { 682 lo++; 683 } 684 685 689 while (pivot <= a[hi] && lo < hi) 690 { 691 hi--; 692 } 693 694 697 if (lo < hi) 698 { 699 int T = a[lo]; 700 701 a[lo] = a[hi]; 702 a[hi] = T; 703 704 } 706 707 } 711 712 715 a[hi0] = a[hi]; 716 a[hi] = pivot; 717 718 723 sort(a, lo0, lo - 1); 724 sort(a, hi + 1, hi0); 725 } 726 727 736 public void sort() throws Exception 737 { 738 sort(m_map, 0, m_firstFree - 1); 739 } 740 } 741 | Popular Tags |