Revision: 7912
Author: rchan...@google.com
Date: Tue Apr 13 05:32:59 2010
Log: 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
http://code.google.com/p/google-web-toolkit/source/detail?r=7912
Added:
/trunk/bikeshed/test/com/google/gwt/collections/ClientMutableArrayTest.java
/trunk/bikeshed/test/com/google/gwt/collections/GwtImmutableArrayTest.java
/trunk/bikeshed/test/com/google/gwt/collections/MutableArrayBenchmarkTest.java
Modified:
/trunk/bikeshed/src/com/google/gwt/collections/CollectionFactory.java
/trunk/bikeshed/src/com/google/gwt/collections/MutableArray.java
/trunk/bikeshed/test/com/google/gwt/collections/ObjectArrayTest.java
=======================================
--- /dev/null
+++
/trunk/bikeshed/test/com/google/gwt/collections/ClientMutableArrayTest.java
Tue Apr 13 05:32:59 2010
@@ -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";
+ }
+
+}
=======================================
--- /dev/null
+++
/trunk/bikeshed/test/com/google/gwt/collections/GwtImmutableArrayTest.java
Tue Apr 13 05:32:59 2010
@@ -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";
+ }
+
+}
=======================================
--- /dev/null
+++
/trunk/bikeshed/test/com/google/gwt/collections/MutableArrayBenchmarkTest.java
Tue Apr 13 05:32:59 2010
@@ -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);
+ }
+ }
+
+}
=======================================
--- /trunk/bikeshed/src/com/google/gwt/collections/CollectionFactory.java
Fri Mar 26 10:56:02 2010
+++ /trunk/bikeshed/src/com/google/gwt/collections/CollectionFactory.java
Tue Apr 13 05:32:59 2010
@@ -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;
+ }
}
=======================================
--- /trunk/bikeshed/src/com/google/gwt/collections/MutableArray.java Mon
Apr 12 07:40:33 2010
+++ /trunk/bikeshed/src/com/google/gwt/collections/MutableArray.java Tue
Apr 13 05:32:59 2010
@@ -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
=======================================
--- /trunk/bikeshed/test/com/google/gwt/collections/ObjectArrayTest.java
Thu Apr 1 09:34:49 2010
+++ /trunk/bikeshed/test/com/google/gwt/collections/ObjectArrayTest.java
Tue Apr 13 05:32:59 2010
@@ -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));
+ }
}
--
http://groups.google.com/group/Google-Web-Toolkit-Contributors
To unsubscribe, reply using "remove me" as the subject.