Fixes MATH-384

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1053283 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mikkel Meyer Andersen 2010-12-28 09:03:53 +00:00
parent 0f004b25f7
commit 38155d7ef3
4 changed files with 92 additions and 0 deletions

View File

@ -123,6 +123,20 @@ public class DescriptiveStatistics implements StatisticalSummary, Serializable {
setWindowSize(window);
}
/**
* Construct a DescriptiveStatistics instance with an infinite window
* and the initial data values in double[] initialDoubleArray.
* If initialDoubleArray is null, then this constructor corresponds to
* DescriptiveStatistics()
*
* @param initialDoubleArray the initial double[].
*/
public DescriptiveStatistics(double[] initialDoubleArray) {
if (initialDoubleArray != null) {
eDA = new ResizableDoubleArray(initialDoubleArray);
}
}
/**
* Copy constructor. Construct a new DescriptiveStatistics instance that
* is a copy of original.

View File

@ -67,6 +67,13 @@ public interface DoubleArray {
*/
void addElement(double value);
/**
* Adds elements to the end of this expandable array
*
* @param values to be added to end of array
*/
void addElements(double[] values);
/**
* <p>
* Adds an element to the end of the array and removes the first

View File

@ -160,6 +160,29 @@ public class ResizableDoubleArray implements DoubleArray, Serializable {
internalArray = new double[this.initialCapacity];
}
/**
* Create a ResizableArray from an existing double[] with the
* initial capacity and numElements corresponding to the size of
* the supplied double[] array. If the supplied array is null, a
* new empty array with the default initial capacity will be created.
* Other properties take default values:
* <ul>
* <li><code>initialCapacity = 16</code></li>
* <li><code>expansionMode = MULTIPLICATIVE_MODE</code></li>
* <li><code>expansionFactor = 2.5</code></li>
* <li><code>contractionFactor = 2.0</code></li>
* </ul>
*/
public ResizableDoubleArray(double[] initialArray) {
if (initialArray == null) {
internalArray = new double[initialCapacity];
} else {
internalArray = initialArray;
initialCapacity = initialArray.length;
numElements = initialArray.length;
}
}
/**
* <p>
* Create a ResizableArray with the specified initial capacity
@ -274,6 +297,20 @@ public class ResizableDoubleArray implements DoubleArray, Serializable {
contract();
}
}
/**
* Adds several element to the end of this expandable array.
*
* @param values to be added to end of array
*/
public synchronized void addElements(double[] values) {
final double[] tempArray = new double[numElements + values.length + 1];
System.arraycopy(internalArray, startIndex, tempArray, 0, numElements);
System.arraycopy(values, 0, tempArray, numElements, values.length);
internalArray = tempArray;
startIndex = 0;
numElements += values.length;
}
/**
* <p>

View File

@ -59,6 +59,13 @@ public class ResizableDoubleArrayTest extends DoubleArrayAbstractTest {
} catch (IllegalArgumentException ex) {
// expected
}
testDa = new ResizableDoubleArray((double[]) null);
assertEquals(0, testDa.getNumElements());
double[] initialArray = new double[] { 0, 1, 2 };
testDa = new ResizableDoubleArray(initialArray);
assertEquals(3, testDa.getNumElements());
testDa = new ResizableDoubleArray(2, 2.0f);
assertEquals(0, testDa.getNumElements());
@ -184,6 +191,33 @@ public class ResizableDoubleArrayTest extends DoubleArrayAbstractTest {
"16 and an expansion factor of 2.0",
1024, ((ResizableDoubleArray) da).getInternalLength());
}
public void testAddElements() {
ResizableDoubleArray testDa = new ResizableDoubleArray();
// MULTIPLICATIVE_MODE
testDa.addElements(new double[] {4, 5, 6});
assertEquals(3, testDa.getNumElements(), 0);
assertEquals(4, testDa.getElement(0), 0);
assertEquals(5, testDa.getElement(1), 0);
assertEquals(6, testDa.getElement(2), 0);
testDa.addElements(new double[] {4, 5, 6});
assertEquals(6, testDa.getNumElements());
// ADDITIVE_MODE (x's are occupied storage locations, 0's are open)
testDa = new ResizableDoubleArray(2, 2.0f, 2.5f,
ResizableDoubleArray.ADDITIVE_MODE);
assertEquals(2, testDa.getInternalLength());
testDa.addElements(new double[] { 1d }); // x,0
testDa.addElements(new double[] { 2d }); // x,x
testDa.addElements(new double[] { 3d }); // x,x,x,0 -- expanded
assertEquals(1d, testDa.getElement(0), 0);
assertEquals(2d, testDa.getElement(1), 0);
assertEquals(3d, testDa.getElement(2), 0);
assertEquals(4, testDa.getInternalLength()); // x,x,x,0
assertEquals(3, testDa.getNumElements());
}
@Override
public void testAddElementRolling() {