Added method to change the size of a MutableArray. Added factory method to
construct MeutableArray of specific size. These provide a O(n) way to initialize
a MutableArray as shown by com.google.gwt.collections.MutableArrayBenchmarkTest
Review at http://gwt-code-reviews.appspot.com/319801
git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@7912 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/bikeshed/src/com/google/gwt/collections/CollectionFactory.java b/bikeshed/src/com/google/gwt/collections/CollectionFactory.java
index c98e743..815751a 100644
--- a/bikeshed/src/com/google/gwt/collections/CollectionFactory.java
+++ b/bikeshed/src/com/google/gwt/collections/CollectionFactory.java
@@ -23,5 +23,11 @@
public static <E> MutableArray<E> createMutableArray() {
return new MutableArray<E>();
}
+
+ public static <E> MutableArray<E> createMutableArray(int size, E fillValue) {
+ MutableArray<E> r = new MutableArray<E>();
+ r.setSize(size, fillValue);
+ return r;
+ }
}
diff --git a/bikeshed/src/com/google/gwt/collections/MutableArray.java b/bikeshed/src/com/google/gwt/collections/MutableArray.java
index 3614a8c..c76cf48 100644
--- a/bikeshed/src/com/google/gwt/collections/MutableArray.java
+++ b/bikeshed/src/com/google/gwt/collections/MutableArray.java
@@ -15,6 +15,8 @@
*/
package com.google.gwt.collections;
+import java.util.Arrays;
+
/**
* An array whose content and length can change over time.
*
@@ -22,6 +24,7 @@
*/
public class MutableArray<E> extends Array<E> {
+ // TODO: refactor the unchecked elems construction into a separate method
// The elements in the array
E[] elems;
@@ -141,6 +144,46 @@
assert false : "index " + index + " in range [0, " + size() + "), but set(int,E) failed";
}
}
+
+ /**
+ * Changes the array size. If {@code newSize} is less than the current size, the array is
+ * truncated. If {@code newSize} is greater than the current size the array is grown and
+ * the new elements of the array filled up with {@code fillValue}.
+ */
+ @SuppressWarnings("unchecked")
+ @LinearTime
+ public void setSize(int newSize, E fillValue) {
+ Assertions.assertNotFrozen(this);
+ assert newSize >= 0 : "expecting newSize >= 0, got " + newSize;
+
+ int fillStart;
+
+ if (newSize == size()) {
+ return;
+ } else if (newSize == 0) {
+ elems = null;
+ return;
+ }
+
+ if (elems == null) {
+ fillStart = 0;
+ } else if (newSize < elems.length) {
+ // nothing to fill
+ fillStart = newSize;
+ } else {
+ fillStart = elems.length;
+ }
+
+ E[] newElems = (E[]) new Object[newSize];
+
+ if (fillStart != 0) {
+ System.arraycopy(elems, 0, newElems, 0, fillStart);
+ }
+
+ Arrays.fill(newElems, fillStart, newSize, fillValue);
+
+ elems = newElems;
+ }
@Override
@ConstantTime
diff --git a/bikeshed/test/com/google/gwt/collections/ClientMutableArrayTest.java b/bikeshed/test/com/google/gwt/collections/ClientMutableArrayTest.java
new file mode 100644
index 0000000..162a41e
--- /dev/null
+++ b/bikeshed/test/com/google/gwt/collections/ClientMutableArrayTest.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2009 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.collections;
+
+/**
+ * Re-run {@link ObjectArrayTest} tests under GWT.
+ */
+public class ClientMutableArrayTest extends ObjectArrayTest {
+ @Override
+ public String getModuleName() {
+ return "com.google.gwt.collections.Collections";
+ }
+
+}
diff --git a/bikeshed/test/com/google/gwt/collections/GwtImmutableArrayTest.java b/bikeshed/test/com/google/gwt/collections/GwtImmutableArrayTest.java
new file mode 100644
index 0000000..d7a5f22
--- /dev/null
+++ b/bikeshed/test/com/google/gwt/collections/GwtImmutableArrayTest.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2009 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.collections;
+
+/**
+ * Re-run {@link ImmutableArrayTest} tests under GWT.
+ */
+public class GwtImmutableArrayTest extends ImmutableArrayTest {
+ @Override
+ public String getModuleName() {
+ return "com.google.gwt.collections.Collections";
+ }
+
+}
diff --git a/bikeshed/test/com/google/gwt/collections/MutableArrayBenchmarkTest.java b/bikeshed/test/com/google/gwt/collections/MutableArrayBenchmarkTest.java
new file mode 100644
index 0000000..9d59bfe
--- /dev/null
+++ b/bikeshed/test/com/google/gwt/collections/MutableArrayBenchmarkTest.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2009 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.collections;
+
+import com.google.gwt.benchmarks.client.Benchmark;
+import com.google.gwt.benchmarks.client.IntRange;
+import com.google.gwt.benchmarks.client.Operator;
+import com.google.gwt.benchmarks.client.RangeField;
+
+/**
+ * Benchmarks the performance of various MutableArray methods.
+ */
+public class MutableArrayBenchmarkTest extends Benchmark {
+
+ final IntRange elemRange = new IntRange(0, 5000, Operator.ADD, 100);
+
+ @Override
+ public String getModuleName() {
+ return "com.google.gwt.collections.Collections";
+ }
+
+ public void testAddGrowth() {
+ }
+
+ public void testAddGrowth(@RangeField("elemRange") Integer numElements) {
+ MutableArray<Integer> ma = CollectionFactory.createMutableArray();
+
+ for (int i = 0; i < numElements; i++) {
+ ma.add(i);
+ }
+ }
+
+ public void testSetSizeGrowth() {
+ }
+
+ public void testSetSizeGrowth(@RangeField("elemRange") Integer numElements) {
+ MutableArray<Integer> ma = CollectionFactory.createMutableArray();
+
+ ma.setSize(numElements, null);
+ for (int i = 0; i < numElements; i++) {
+ ma.set(i, i);
+ }
+ }
+
+}
diff --git a/bikeshed/test/com/google/gwt/collections/ObjectArrayTest.java b/bikeshed/test/com/google/gwt/collections/ObjectArrayTest.java
index ed63876..988ebc5 100644
--- a/bikeshed/test/com/google/gwt/collections/ObjectArrayTest.java
+++ b/bikeshed/test/com/google/gwt/collections/ObjectArrayTest.java
@@ -265,5 +265,34 @@
Array<String> a = b;
assertEquals(0, a.size());
}
+
+ public void testSetSize() {
+ MutableArray<String> b = createMutableArray();
+
+ b.setSize(1, "fillValue");
+ assertEquals(1, b.size());
+ assertEquals("fillValue", b.get(0));
+
+ b.setSize(2, "anotherValue");
+ assertEquals(2, b.size());
+ assertEquals("fillValue", b.get(0));
+ assertEquals("anotherValue", b.get(1));
+
+ b.setSize(1, null);
+ assertEquals(1, b.size());
+ assertEquals("fillValue", b.get(0));
+
+ b.setSize(0, null);
+ assertEquals(0, b.size());
+ assertEquals(null, b.elems);
+ }
+
+ public void testCreateNonEmptyArray() {
+ MutableArray<String> b = createMutableArray(2, "apples");
+
+ assertEquals(2, b.size());
+ assertEquals("apples", b.get(0));
+ assertEquals("apples", b.get(1));
+ }
}