From de9e8b8c78fc5e15a8f70000115733ce4e484a3a Mon Sep 17 00:00:00 2001 From: Gilles Sadowski Date: Mon, 12 Nov 2012 13:33:26 +0000 Subject: [PATCH] MATH-894 New method "getCapacity". Deprecated "getInternalLength". git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1408280 13f79535-47bb-0310-9956-ffa450edef68 --- .../math3/util/ResizableDoubleArray.java | 14 ++++++++ .../math3/util/ResizableDoubleArrayTest.java | 36 +++++++++---------- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/apache/commons/math3/util/ResizableDoubleArray.java b/src/main/java/org/apache/commons/math3/util/ResizableDoubleArray.java index 00c3e433f..9c30f0bec 100644 --- a/src/main/java/org/apache/commons/math3/util/ResizableDoubleArray.java +++ b/src/main/java/org/apache/commons/math3/util/ResizableDoubleArray.java @@ -632,11 +632,25 @@ public class ResizableDoubleArray implements DoubleArray, Serializable { * of the public interface of this class. * * @return the length of the internal storage array. + * @deprecated As of 3.1. Please use {@link #getCapacity()} instead. */ + @Deprecated synchronized int getInternalLength() { return internalArray.length; } + /** + * Gets the currently allocated size of the internal data structure used + * for storing elements. + * This is not to be confused with {@link #getNumElements() the number of + * elements actually stored}. + * + * @return the length of the internal array. + */ + public int getCapacity() { + return internalArray.length; + } + /** * Returns the number of elements currently in the array. Please note * that this is different from the length of the internal storage array. diff --git a/src/test/java/org/apache/commons/math3/util/ResizableDoubleArrayTest.java b/src/test/java/org/apache/commons/math3/util/ResizableDoubleArrayTest.java index fd85eb0b8..1f98e54f2 100644 --- a/src/test/java/org/apache/commons/math3/util/ResizableDoubleArrayTest.java +++ b/src/test/java/org/apache/commons/math3/util/ResizableDoubleArrayTest.java @@ -52,7 +52,7 @@ public class ResizableDoubleArrayTest extends DoubleArrayAbstractTest { ResizableDoubleArray testDa = new ResizableDoubleArray(2); Assert.assertEquals(0, testDa.getNumElements()); - Assert.assertEquals(2, testDa.getInternalLength()); + Assert.assertEquals(2, testDa.getCapacity()); Assert.assertEquals(defaultExpansionFactor, testDa.getExpansionFactor(), 0); Assert.assertEquals(defaultContractionCriteria, testDa.getContractionCriteria(), 0); Assert.assertEquals(defaultMode, testDa.getExpansionMode()); @@ -72,7 +72,7 @@ public class ResizableDoubleArrayTest extends DoubleArrayAbstractTest { testDa = new ResizableDoubleArray(2, 2.0f); Assert.assertEquals(0, testDa.getNumElements()); - Assert.assertEquals(2, testDa.getInternalLength()); + Assert.assertEquals(2, testDa.getCapacity()); Assert.assertEquals(defaultExpansionFactor, testDa.getExpansionFactor(), 0); Assert.assertEquals(defaultContractionCriteria, testDa.getContractionCriteria(), 0); Assert.assertEquals(defaultMode, testDa.getExpansionMode()); @@ -90,7 +90,7 @@ public class ResizableDoubleArrayTest extends DoubleArrayAbstractTest { testDa = new ResizableDoubleArray(2, 2.0f, 3.0f); Assert.assertEquals(0, testDa.getNumElements()); - Assert.assertEquals(2, testDa.getInternalLength()); + Assert.assertEquals(2, testDa.getCapacity()); Assert.assertEquals(defaultExpansionFactor, testDa.getExpansionFactor(), 0); Assert.assertEquals(3.0f, testDa.getContractionCriteria(), 0); Assert.assertEquals(defaultMode, testDa.getExpansionMode()); @@ -105,7 +105,7 @@ public class ResizableDoubleArrayTest extends DoubleArrayAbstractTest { testDa = new ResizableDoubleArray(2, 2.0f, 3.0f, ResizableDoubleArray.ADDITIVE_MODE); Assert.assertEquals(0, testDa.getNumElements()); - Assert.assertEquals(2, testDa.getInternalLength()); + Assert.assertEquals(2, testDa.getCapacity()); Assert.assertEquals(defaultExpansionFactor, testDa.getExpansionFactor(), 0); Assert.assertEquals(3.0f, testDa.getContractionCriteria(), 0); Assert.assertEquals(ResizableDoubleArray.ADDITIVE_MODE, @@ -157,16 +157,16 @@ public class ResizableDoubleArrayTest extends DoubleArrayAbstractTest { da.addElement(2.0); da.addElement(4.0); da.addElement(6.0); - Assert.assertEquals(16, ((ResizableDoubleArray) da).getInternalLength()); + Assert.assertEquals(16, ((ResizableDoubleArray) da).getCapacity()); Assert.assertEquals(3, da.getNumElements()); da.setElement(3, 7.0); - Assert.assertEquals(16, ((ResizableDoubleArray) da).getInternalLength()); + Assert.assertEquals(16, ((ResizableDoubleArray) da).getCapacity()); Assert.assertEquals(4, da.getNumElements()); da.setElement(10, 10.0); - Assert.assertEquals(16, ((ResizableDoubleArray) da).getInternalLength()); + Assert.assertEquals(16, ((ResizableDoubleArray) da).getCapacity()); Assert.assertEquals(11, da.getNumElements()); da.setElement(9, 10.0); - Assert.assertEquals(16, ((ResizableDoubleArray) da).getInternalLength()); + Assert.assertEquals(16, ((ResizableDoubleArray) da).getCapacity()); Assert.assertEquals(11, da.getNumElements()); try { @@ -180,12 +180,12 @@ public class ResizableDoubleArrayTest extends DoubleArrayAbstractTest { ResizableDoubleArray testDa = new ResizableDoubleArray(2, 2.0f, 3.0f, ResizableDoubleArray.ADDITIVE_MODE); - Assert.assertEquals(2, testDa.getInternalLength()); + Assert.assertEquals(2, testDa.getCapacity()); testDa.addElement(1d); testDa.addElement(1d); - Assert.assertEquals(2, testDa.getInternalLength()); + Assert.assertEquals(2, testDa.getCapacity()); testDa.addElement(1d); - Assert.assertEquals(4, testDa.getInternalLength()); + Assert.assertEquals(4, testDa.getCapacity()); } @Override @@ -194,7 +194,7 @@ public class ResizableDoubleArrayTest extends DoubleArrayAbstractTest { super.testAdd1000(); Assert.assertEquals("Internal Storage length should be 1024 if we started out with initial capacity of " + "16 and an expansion factor of 2.0", - 1024, ((ResizableDoubleArray) da).getInternalLength()); + 1024, ((ResizableDoubleArray) da).getCapacity()); } @Test @@ -214,14 +214,14 @@ public class ResizableDoubleArrayTest extends DoubleArrayAbstractTest { // ADDITIVE_MODE (x's are occupied storage locations, 0's are open) testDa = new ResizableDoubleArray(2, 2.0f, 2.5f, ResizableDoubleArray.ADDITIVE_MODE); - Assert.assertEquals(2, testDa.getInternalLength()); + Assert.assertEquals(2, testDa.getCapacity()); testDa.addElements(new double[] { 1d }); // x,0 testDa.addElements(new double[] { 2d }); // x,x testDa.addElements(new double[] { 3d }); // x,x,x,0 -- expanded Assert.assertEquals(1d, testDa.getElement(0), 0); Assert.assertEquals(2d, testDa.getElement(1), 0); Assert.assertEquals(3d, testDa.getElement(2), 0); - Assert.assertEquals(4, testDa.getInternalLength()); // x,x,x,0 + Assert.assertEquals(4, testDa.getCapacity()); // x,x,x,0 Assert.assertEquals(3, testDa.getNumElements()); } @@ -249,26 +249,26 @@ public class ResizableDoubleArrayTest extends DoubleArrayAbstractTest { // ADDITIVE_MODE (x's are occupied storage locations, 0's are open) ResizableDoubleArray testDa = new ResizableDoubleArray(2, 2.0f, 2.5f, ResizableDoubleArray.ADDITIVE_MODE); - Assert.assertEquals(2, testDa.getInternalLength()); + Assert.assertEquals(2, testDa.getCapacity()); testDa.addElement(1d); // x,0 testDa.addElement(2d); // x,x testDa.addElement(3d); // x,x,x,0 -- expanded Assert.assertEquals(1d, testDa.getElement(0), 0); Assert.assertEquals(2d, testDa.getElement(1), 0); Assert.assertEquals(3d, testDa.getElement(2), 0); - Assert.assertEquals(4, testDa.getInternalLength()); // x,x,x,0 + Assert.assertEquals(4, testDa.getCapacity()); // x,x,x,0 Assert.assertEquals(3, testDa.getNumElements()); testDa.addElementRolling(4d); Assert.assertEquals(2d, testDa.getElement(0), 0); Assert.assertEquals(3d, testDa.getElement(1), 0); Assert.assertEquals(4d, testDa.getElement(2), 0); - Assert.assertEquals(4, testDa.getInternalLength()); // 0,x,x,x + Assert.assertEquals(4, testDa.getCapacity()); // 0,x,x,x Assert.assertEquals(3, testDa.getNumElements()); testDa.addElementRolling(5d); // 0,0,x,x,x,0 -- time to contract Assert.assertEquals(3d, testDa.getElement(0), 0); Assert.assertEquals(4d, testDa.getElement(1), 0); Assert.assertEquals(5d, testDa.getElement(2), 0); - Assert.assertEquals(4, testDa.getInternalLength()); // contracted -- x,x,x,0 + Assert.assertEquals(4, testDa.getCapacity()); // contracted -- x,x,x,0 Assert.assertEquals(3, testDa.getNumElements()); try { testDa.getElement(4);