1 2 17 18 19 package org.apache.poi.util; 20 21 import java.util.*; 22 23 49 50 public class IntList 51 { 52 private int[] _array; 53 private int _limit; 54 private int fillval = 0; 55 private static final int _default_size = 128; 56 57 60 61 public IntList() 62 { 63 this(_default_size); 64 } 65 66 public IntList(final int initialCapacity) 67 { 68 this(initialCapacity,0); 69 } 70 71 72 77 78 public IntList(final IntList list) 79 { 80 this(list._array.length); 81 System.arraycopy(list._array, 0, _array, 0, _array.length); 82 _limit = list._limit; 83 } 84 85 90 91 public IntList(final int initialCapacity, int fillvalue) 92 { 93 _array = new int[ initialCapacity ]; 94 if (fillval != 0) { 95 fillval = fillvalue; 96 fillArray(fillval, _array, 0); 97 } 98 _limit = 0; 99 } 100 101 private void fillArray(int val, int[] array, int index) { 102 for (int k = index; k < array.length; k++) { 103 array[k] = val; 104 } 105 } 106 107 116 117 public void add(final int index, final int value) 118 { 119 if (index > _limit) 120 { 121 throw new IndexOutOfBoundsException (); 122 } 123 else if (index == _limit) 124 { 125 add(value); 126 } 127 else 128 { 129 130 if (_limit == _array.length) 132 { 133 growArray(_limit * 2); 134 } 135 System.arraycopy(_array, index, _array, index + 1, 136 _limit - index); 137 _array[ index ] = value; 138 _limit++; 139 } 140 } 141 142 150 151 public boolean add(final int value) 152 { 153 if (_limit == _array.length) 154 { 155 growArray(_limit * 2); 156 } 157 _array[ _limit++ ] = value; 158 return true; 159 } 160 161 175 176 public boolean addAll(final IntList c) 177 { 178 if (c._limit != 0) 179 { 180 if ((_limit + c._limit) > _array.length) 181 { 182 growArray(_limit + c._limit); 183 } 184 System.arraycopy(c._array, 0, _array, _limit, c._limit); 185 _limit += c._limit; 186 } 187 return true; 188 } 189 190 211 212 public boolean addAll(final int index, final IntList c) 213 { 214 if (index > _limit) 215 { 216 throw new IndexOutOfBoundsException (); 217 } 218 if (c._limit != 0) 219 { 220 if ((_limit + c._limit) > _array.length) 221 { 222 growArray(_limit + c._limit); 223 } 224 225 System.arraycopy(_array, index, _array, index + c._limit, 227 _limit - index); 228 229 System.arraycopy(c._array, 0, _array, index, c._limit); 231 _limit += c._limit; 232 } 233 return true; 234 } 235 236 240 241 public void clear() 242 { 243 _limit = 0; 244 } 245 246 255 256 public boolean contains(final int o) 257 { 258 boolean rval = false; 259 260 for (int j = 0; !rval && (j < _limit); j++) 261 { 262 if (_array[ j ] == o) 263 { 264 rval = true; 265 } 266 } 267 return rval; 268 } 269 270 279 280 public boolean containsAll(final IntList c) 281 { 282 boolean rval = true; 283 284 if (this != c) 285 { 286 for (int j = 0; rval && (j < c._limit); j++) 287 { 288 if (!contains(c._array[ j ])) 289 { 290 rval = false; 291 } 292 } 293 } 294 return rval; 295 } 296 297 312 313 public boolean equals(final Object o) 314 { 315 boolean rval = this == o; 316 317 if (!rval && (o != null) && (o.getClass() == this.getClass())) 318 { 319 IntList other = ( IntList ) o; 320 321 if (other._limit == _limit) 322 { 323 324 rval = true; 326 for (int j = 0; rval && (j < _limit); j++) 327 { 328 rval = _array[ j ] == other._array[ j ]; 329 } 330 } 331 } 332 return rval; 333 } 334 335 345 346 public int get(final int index) 347 { 348 if (index >= _limit) 349 { 350 throw new IndexOutOfBoundsException (); 351 } 352 return _array[ index ]; 353 } 354 355 374 375 public int hashCode() 376 { 377 int hash = 0; 378 379 for (int j = 0; j < _limit; j++) 380 { 381 hash = (31 * hash) + _array[ j ]; 382 } 383 return hash; 384 } 385 386 398 399 public int indexOf(final int o) 400 { 401 int rval = 0; 402 403 for (; rval < _limit; rval++) 404 { 405 if (o == _array[ rval ]) 406 { 407 break; 408 } 409 } 410 if (rval == _limit) 411 { 412 rval = -1; } 414 return rval; 415 } 416 417 422 423 public boolean isEmpty() 424 { 425 return _limit == 0; 426 } 427 428 440 441 public int lastIndexOf(final int o) 442 { 443 int rval = _limit - 1; 444 445 for (; rval >= 0; rval--) 446 { 447 if (o == _array[ rval ]) 448 { 449 break; 450 } 451 } 452 return rval; 453 } 454 455 468 469 public int remove(final int index) 470 { 471 if (index >= _limit) 472 { 473 throw new IndexOutOfBoundsException (); 474 } 475 int rval = _array[ index ]; 476 477 System.arraycopy(_array, index + 1, _array, index, _limit - index); 478 _limit--; 479 return rval; 480 } 481 482 493 494 public boolean removeValue(final int o) 495 { 496 boolean rval = false; 497 498 for (int j = 0; !rval && (j < _limit); j++) 499 { 500 if (o == _array[ j ]) 501 { 502 if (j+1 < _limit) { 503 System.arraycopy(_array, j + 1, _array, j, _limit - j); 504 } 505 _limit--; 506 rval = true; 507 } 508 } 509 return rval; 510 } 511 512 521 522 public boolean removeAll(final IntList c) 523 { 524 boolean rval = false; 525 526 for (int j = 0; j < c._limit; j++) 527 { 528 if (removeValue(c._array[ j ])) 529 { 530 rval = true; 531 } 532 } 533 return rval; 534 } 535 536 547 548 public boolean retainAll(final IntList c) 549 { 550 boolean rval = false; 551 552 for (int j = 0; j < _limit; ) 553 { 554 if (!c.contains(_array[ j ])) 555 { 556 remove(j); 557 rval = true; 558 } 559 else 560 { 561 j++; 562 } 563 } 564 return rval; 565 } 566 567 579 580 public int set(final int index, final int element) 581 { 582 if (index >= _limit) 583 { 584 throw new IndexOutOfBoundsException (); 585 } 586 int rval = _array[ index ]; 587 588 _array[ index ] = element; 589 return rval; 590 } 591 592 599 600 public int size() 601 { 602 return _limit; 603 } 604 605 613 614 public int [] toArray() 615 { 616 int[] rval = new int[ _limit ]; 617 618 System.arraycopy(_array, 0, rval, 0, _limit); 619 return rval; 620 } 621 622 633 634 public int [] toArray(final int [] a) 635 { 636 int[] rval; 637 638 if (a.length == _limit) 639 { 640 System.arraycopy(_array, 0, a, 0, _limit); 641 rval = a; 642 } 643 else 644 { 645 rval = toArray(); 646 } 647 return rval; 648 } 649 650 private void growArray(final int new_size) 651 { 652 int size = (new_size == _array.length) ? new_size + 1 653 : new_size; 654 int[] new_array = new int[ size ]; 655 656 if (fillval != 0) { 657 fillArray(fillval, new_array, _array.length); 658 } 659 660 System.arraycopy(_array, 0, new_array, 0, _limit); 661 _array = new_array; 662 } 663 } 665 | Popular Tags |