~ubuntu-branches/ubuntu/oneiric/commons-math/oneiric

« back to all changes in this revision

Viewing changes to src/main/java/org/apache/commons/math/util/ResizableDoubleArray.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan, Torsten Werner, Damien Raude-Morvan
  • Date: 2011-03-07 21:14:46 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110307211446-4zea7og4eeyzhpai
Tags: 2.2-1
[ Torsten Werner ]
* Change maintainers into Maintainers.

[ Damien Raude-Morvan ]
* New upstream release (Closes: #617209).
* d/control: Bump Standards-Version to 3.9.1 (no changes needed).
* d/copyright: Refresh years, upgrade to DEP5 r166 and relicence my work
  under Apache-2.0.
* d/ant.properties: Set junit.jar to /usr/share/java/junit4.jar
  to ensure unit tests are launched.
* d/docs: Install upstream RELEASE-NOTES.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
import java.util.Arrays;
21
21
 
22
22
import org.apache.commons.math.MathRuntimeException;
 
23
import org.apache.commons.math.exception.util.LocalizedFormats;
23
24
 
24
25
/**
25
26
 * <p>
69
70
 * properties enforce this requirement, throwing IllegalArgumentException if it
70
71
 * is violated.
71
72
 * </p>
72
 
 * @version $Revision: 811833 $ $Date: 2009-09-06 12:27:50 -0400 (Sun, 06 Sep 2009) $
 
73
 * @version $Revision: 1073474 $ $Date: 2011-02-22 20:52:17 +0100 (mar. 22 févr. 2011) $
73
74
 */
74
75
public class ResizableDoubleArray implements DoubleArray, Serializable {
75
76
 
160
161
    }
161
162
 
162
163
    /**
 
164
     * Create a ResizableArray from an existing double[] with the
 
165
     * initial capacity and numElements corresponding to the size of
 
166
     * the supplied double[] array. If the supplied array is null, a
 
167
     * new empty array with the default initial capacity will be created.
 
168
     * The input array is copied, not referenced.
 
169
     * Other properties take default values:
 
170
     * <ul>
 
171
     * <li><code>initialCapacity = 16</code></li>
 
172
     * <li><code>expansionMode = MULTIPLICATIVE_MODE</code></li>
 
173
     * <li><code>expansionFactor = 2.5</code></li>
 
174
     * <li><code>contractionFactor = 2.0</code></li>
 
175
     * </ul>
 
176
     *
 
177
     * @param initialArray initial array
 
178
     * @since 2.2
 
179
     */
 
180
    public ResizableDoubleArray(double[] initialArray) {
 
181
        if (initialArray == null) {
 
182
            this.internalArray = new double[initialCapacity];
 
183
        } else {
 
184
            this.internalArray = new double[initialArray.length];
 
185
            System.arraycopy(initialArray, 0, this.internalArray, 0, initialArray.length);
 
186
            initialCapacity = initialArray.length;
 
187
            numElements = initialArray.length;
 
188
        }
 
189
    }
 
190
 
 
191
    /**
163
192
     * <p>
164
193
     * Create a ResizableArray with the specified initial capacity
165
194
     * and expansion factor.  The remaining properties take default
275
304
    }
276
305
 
277
306
    /**
 
307
     * Adds several element to the end of this expandable array.
 
308
     *
 
309
     * @param values to be added to end of array
 
310
     * @since 2.2
 
311
     */
 
312
    public synchronized void addElements(double[] values) {
 
313
        final double[] tempArray = new double[numElements + values.length + 1];
 
314
        System.arraycopy(internalArray, startIndex, tempArray, 0, numElements);
 
315
        System.arraycopy(values, 0, tempArray, numElements, values.length);
 
316
        internalArray = tempArray;
 
317
        startIndex = 0;
 
318
        numElements += values.length;
 
319
    }
 
320
 
 
321
    /**
278
322
     * <p>
279
323
     * Adds an element to the end of the array and removes the first
280
324
     * element in the array.  Returns the discarded first element.
321
365
    public synchronized double substituteMostRecentElement(double value) {
322
366
        if (numElements < 1) {
323
367
            throw MathRuntimeException.createArrayIndexOutOfBoundsException(
324
 
                    "cannot substitute an element from an empty array");
 
368
                    LocalizedFormats.CANNOT_SUBSTITUTE_ELEMENT_FROM_EMPTY_ARRAY);
325
369
        }
326
370
 
327
371
        double discarded = internalArray[startIndex + (numElements - 1)];
346
390
 
347
391
        if (contraction < expansion) {
348
392
            throw MathRuntimeException.createIllegalArgumentException(
349
 
                    "contraction criteria ({0}) smaller than the expansion factor ({1}).  This would " +
350
 
                    "lead to a never ending loop of expansion and contraction as a newly expanded " +
351
 
                    "internal storage array would immediately satisfy the criteria for contraction",
 
393
                    LocalizedFormats.CONTRACTION_CRITERIA_SMALLER_THAN_EXPANSION_FACTOR,
352
394
                    contraction, expansion);
353
395
        }
354
396
 
355
397
        if (contraction <= 1.0) {
356
398
            throw MathRuntimeException.createIllegalArgumentException(
357
 
                    "contraction criteria smaller than one ({0}).  This would lead to a never ending " +
358
 
                    "loop of expansion and contraction as an internal storage array length equal " +
359
 
                    "to the number of elements would satisfy the contraction criteria.",
 
399
                    LocalizedFormats.CONTRACTION_CRITERIA_SMALLER_THAN_ONE,
360
400
                    contraction);
361
401
        }
362
402
 
363
403
        if (expansion <= 1.0) {
364
404
            throw MathRuntimeException.createIllegalArgumentException(
365
 
                    "expansion factor smaller than one ({0})",
 
405
                    LocalizedFormats.EXPANSION_FACTOR_SMALLER_THAN_ONE,
366
406
                    expansion);
367
407
        }
368
408
    }
449
489
    private synchronized void discardExtremeElements(int i,boolean front) {
450
490
        if (i > numElements) {
451
491
            throw MathRuntimeException.createIllegalArgumentException(
452
 
                    "cannot discard {0} elements from a {1} elements array",
 
492
                    LocalizedFormats.TOO_MANY_ELEMENTS_TO_DISCARD_FROM_ARRAY,
453
493
                    i, numElements);
454
494
       } else if (i < 0) {
455
495
           throw MathRuntimeException.createIllegalArgumentException(
456
 
                   "cannot discard a negative number of elements ({0})",
 
496
                   LocalizedFormats.CANNOT_DISCARD_NEGATIVE_NUMBER_OF_ELEMENTS,
457
497
                   i);
458
498
        } else {
459
499
            // "Subtract" this number of discarded from numElements
476
516
     */
477
517
    protected synchronized void expand() {
478
518
 
479
 
        // notice the use of Math.ceil(), this guarantees that we will always
 
519
        // notice the use of FastMath.ceil(), this guarantees that we will always
480
520
        // have an array of at least currentSize + 1.   Assume that the
481
521
        // current initial capacity is 1 and the expansion factor
482
522
        // is 1.000000000000000001.  The newly calculated size will be
483
523
        // rounded up to 2 after the multiplication is performed.
484
524
        int newSize = 0;
485
525
        if (expansionMode == MULTIPLICATIVE_MODE) {
486
 
            newSize = (int) Math.ceil(internalArray.length * expansionFactor);
 
526
            newSize = (int) FastMath.ceil(internalArray.length * expansionFactor);
487
527
        } else {
488
 
            newSize = internalArray.length + Math.round(expansionFactor);
 
528
            newSize = internalArray.length + FastMath.round(expansionFactor);
489
529
        }
490
530
        double[] tempArray = new double[newSize];
491
531
 
533
573
    public synchronized double getElement(int index) {
534
574
        if (index >= numElements) {
535
575
            throw MathRuntimeException.createArrayIndexOutOfBoundsException(
536
 
                    "the index specified: {0} is larger than the current maximal index {1}",
 
576
                    LocalizedFormats.INDEX_LARGER_THAN_MAX,
537
577
                    index, numElements - 1);
538
578
        } else if (index >= 0) {
539
579
            return internalArray[startIndex + index];
540
580
        } else {
541
581
            throw MathRuntimeException.createArrayIndexOutOfBoundsException(
542
 
                    "elements cannot be retrieved from a negative array index {0}",
 
582
                    LocalizedFormats.CANNOT_RETRIEVE_AT_NEGATIVE_INDEX,
543
583
                    index);
544
584
        }
545
585
    }
668
708
    public synchronized void setElement(int index, double value) {
669
709
        if (index < 0) {
670
710
            throw MathRuntimeException.createArrayIndexOutOfBoundsException(
671
 
                    "cannot set an element at a negative index {0}",
 
711
                    LocalizedFormats.CANNOT_SET_AT_NEGATIVE_INDEX,
672
712
                    index);
673
713
        }
674
714
        if (index + 1 > numElements) {
710
750
        if (expansionMode != MULTIPLICATIVE_MODE &&
711
751
                expansionMode != ADDITIVE_MODE) {
712
752
            throw MathRuntimeException.createIllegalArgumentException(
713
 
                    "unsupported expansion mode {0}, supported modes are {1} ({2}) and {3} ({4})",
 
753
                    LocalizedFormats.UNSUPPORTED_EXPANSION_MODE,
714
754
                    expansionMode, MULTIPLICATIVE_MODE, "MULTIPLICATIVE_MODE",
715
755
                    ADDITIVE_MODE, "ADDITIVE_MODE");
716
756
        }
733
773
            }
734
774
        } else {
735
775
            throw MathRuntimeException.createIllegalArgumentException(
736
 
                    "initial capacity ({0}) is not positive",
 
776
                    LocalizedFormats.INITIAL_CAPACITY_NOT_POSITIVE,
737
777
                    initialCapacity);
738
778
        }
739
779
    }
751
791
        // If index is negative thrown an error
752
792
        if (i < 0) {
753
793
            throw MathRuntimeException.createIllegalArgumentException(
754
 
                    "index ({0}) is not positive",
 
794
                    LocalizedFormats.INDEX_NOT_POSITIVE,
755
795
                    i);
756
796
        }
757
797