1 2 17 18 19 package org.apache.poi.util; 20 21 import java.util.*; 22 23 49 50 public class DoubleList 51 { 52 private double[] _array; 53 private int _limit; 54 private static final int _default_size = 128; 55 56 59 60 public DoubleList() 61 { 62 this(_default_size); 63 } 64 65 70 71 public DoubleList(final DoubleList list) 72 { 73 this(list._array.length); 74 System.arraycopy(list._array, 0, _array, 0, _array.length); 75 _limit = list._limit; 76 } 77 78 83 84 public DoubleList(final int initialCapacity) 85 { 86 _array = new double[ initialCapacity ]; 87 _limit = 0; 88 } 89 90 99 100 public void add(final int index, final double value) 101 { 102 if (index > _limit) 103 { 104 throw new IndexOutOfBoundsException (); 105 } 106 else if (index == _limit) 107 { 108 add(value); 109 } 110 else 111 { 112 113 if (_limit == _array.length) 115 { 116 growArray(_limit * 2); 117 } 118 System.arraycopy(_array, index, _array, index + 1, 119 _limit - index); 120 _array[ index ] = value; 121 _limit++; 122 } 123 } 124 125 133 134 public boolean add(final double value) 135 { 136 if (_limit == _array.length) 137 { 138 growArray(_limit * 2); 139 } 140 _array[ _limit++ ] = value; 141 return true; 142 } 143 144 158 159 public boolean addAll(final DoubleList c) 160 { 161 if (c._limit != 0) 162 { 163 if ((_limit + c._limit) > _array.length) 164 { 165 growArray(_limit + c._limit); 166 } 167 System.arraycopy(c._array, 0, _array, _limit, c._limit); 168 _limit += c._limit; 169 } 170 return true; 171 } 172 173 194 195 public boolean addAll(final int index, final DoubleList c) 196 { 197 if (index > _limit) 198 { 199 throw new IndexOutOfBoundsException (); 200 } 201 if (c._limit != 0) 202 { 203 if ((_limit + c._limit) > _array.length) 204 { 205 growArray(_limit + c._limit); 206 } 207 208 System.arraycopy(_array, index, _array, index + c._limit, 210 _limit - index); 211 212 System.arraycopy(c._array, 0, _array, index, c._limit); 214 _limit += c._limit; 215 } 216 return true; 217 } 218 219 223 224 public void clear() 225 { 226 _limit = 0; 227 } 228 229 238 239 public boolean contains(final double o) 240 { 241 boolean rval = false; 242 243 for (int j = 0; !rval && (j < _limit); j++) 244 { 245 if (_array[ j ] == o) 246 { 247 rval = true; 248 } 249 } 250 return rval; 251 } 252 253 262 263 public boolean containsAll(final DoubleList c) 264 { 265 boolean rval = true; 266 267 if (this != c) 268 { 269 for (int j = 0; rval && (j < c._limit); j++) 270 { 271 if (!contains(c._array[ j ])) 272 { 273 rval = false; 274 } 275 } 276 } 277 return rval; 278 } 279 280 295 296 public boolean equals(final Object o) 297 { 298 boolean rval = this == o; 299 300 if (!rval && (o != null) && (o.getClass() == this.getClass())) 301 { 302 DoubleList other = ( DoubleList ) o; 303 304 if (other._limit == _limit) 305 { 306 307 rval = true; 309 for (int j = 0; rval && (j < _limit); j++) 310 { 311 rval = _array[ j ] == other._array[ j ]; 312 } 313 } 314 } 315 return rval; 316 } 317 318 328 329 public double get(final int index) 330 { 331 if (index >= _limit) 332 { 333 throw new IndexOutOfBoundsException (); 334 } 335 return _array[ index ]; 336 } 337 338 358 359 public int hashCode() 360 { 361 int hash = 0; 362 363 for (int j = 0; j < _limit; j++) 364 { 365 hash = (31 * hash) + ((int) _array[ j ]); 366 } 367 return hash; 368 } 369 370 382 383 public int indexOf(final double o) 384 { 385 int rval = 0; 386 387 for (; rval < _limit; rval++) 388 { 389 if (o == _array[ rval ]) 390 { 391 break; 392 } 393 } 394 if (rval == _limit) 395 { 396 rval = -1; } 398 return rval; 399 } 400 401 406 407 public boolean isEmpty() 408 { 409 return _limit == 0; 410 } 411 412 424 425 public int lastIndexOf(final double o) 426 { 427 int rval = _limit - 1; 428 429 for (; rval >= 0; rval--) 430 { 431 if (o == _array[ rval ]) 432 { 433 break; 434 } 435 } 436 return rval; 437 } 438 439 452 453 public double remove(final int index) 454 { 455 if (index >= _limit) 456 { 457 throw new IndexOutOfBoundsException (); 458 } 459 double rval = _array[ index ]; 460 461 System.arraycopy(_array, index + 1, _array, index, _limit - index); 462 _limit--; 463 return rval; 464 } 465 466 477 478 public boolean removeValue(final double o) 479 { 480 boolean rval = false; 481 482 for (int j = 0; !rval && (j < _limit); j++) 483 { 484 if (o == _array[ j ]) 485 { 486 System.arraycopy(_array, j + 1, _array, j, _limit - j); 487 _limit--; 488 rval = true; 489 } 490 } 491 return rval; 492 } 493 494 503 504 public boolean removeAll(final DoubleList c) 505 { 506 boolean rval = false; 507 508 for (int j = 0; j < c._limit; j++) 509 { 510 if (removeValue(c._array[ j ])) 511 { 512 rval = true; 513 } 514 } 515 return rval; 516 } 517 518 529 530 public boolean retainAll(final DoubleList c) 531 { 532 boolean rval = false; 533 534 for (int j = 0; j < _limit; ) 535 { 536 if (!c.contains(_array[ j ])) 537 { 538 remove(j); 539 rval = true; 540 } 541 else 542 { 543 j++; 544 } 545 } 546 return rval; 547 } 548 549 561 562 public double set(final int index, final double element) 563 { 564 if (index >= _limit) 565 { 566 throw new IndexOutOfBoundsException (); 567 } 568 double rval = _array[ index ]; 569 570 _array[ index ] = element; 571 return rval; 572 } 573 574 581 582 public int size() 583 { 584 return _limit; 585 } 586 587 595 596 public double [] toArray() 597 { 598 double[] rval = new double[ _limit ]; 599 600 System.arraycopy(_array, 0, rval, 0, _limit); 601 return rval; 602 } 603 604 615 616 public double [] toArray(final double [] a) 617 { 618 double[] rval; 619 620 if (a.length == _limit) 621 { 622 System.arraycopy(_array, 0, a, 0, _limit); 623 rval = a; 624 } 625 else 626 { 627 rval = toArray(); 628 } 629 return rval; 630 } 631 632 private void growArray(final int new_size) 633 { 634 int size = (new_size == _array.length) ? new_size + 1 635 : new_size; 636 double[] new_array = new double[ size ]; 637 638 System.arraycopy(_array, 0, new_array, 0, _limit); 639 _array = new_array; 640 } 641 } | Popular Tags |