This is an automated email from the ASF dual-hosted git repository.
myui pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-hivemall.git
The following commit(s) were added to refs/heads/master by this push:
new 651d85c [HIVEMALL-235] Fix a bug in expansion of array where size is
zero
651d85c is described below
commit 651d85c6312efa1805268c453db88af23de03eb7
Author: Makoto Yui <[email protected]>
AuthorDate: Wed Jan 30 13:50:25 2019 +0900
[HIVEMALL-235] Fix a bug in expansion of array where size is zero
## What changes were proposed in this pull request?
Fix a bug in expansion of array where size is zero.
See for detail
https://github.com/apache/incubator-hivemall/pull/178/commits/d7695d461056b21eab25465e015c582edc2b57ce
## What type of PR is it?
Bug Fix
## What is the Jira issue?
https://issues.apache.org/jira/browse/HIVEMALL-235
## How was this patch tested?
unit tests
## Checklist
- [x] Did you apply source code formatter, i.e., `./bin/format_code.sh`,
for your commit?
- [ ] Did you run system tests on Hive (or Spark)?
Author: Makoto Yui <[email protected]>
Closes #180 from myui/HIVEMALL-235.
---
.../utils/collections/lists/DoubleArrayList.java | 22 +++++++++++++++-------
.../utils/collections/lists/FloatArrayList.java | 22 +++++++++++++++-------
.../utils/collections/lists/IntArrayList.java | 22 +++++++++++++++-------
.../utils/collections/lists/LongArrayList.java | 22 +++++++++++++++-------
...ArrayListTest.java => DoubleArrayListTest.java} | 21 ++++++---------------
...gArrayListTest.java => FloatArrayListTest.java} | 21 ++++++---------------
...ongArrayListTest.java => IntArrayListTest.java} | 21 ++++++---------------
.../utils/collections/lists/LongArrayListTest.java | 9 ++++++++-
8 files changed, 86 insertions(+), 74 deletions(-)
diff --git
a/core/src/main/java/hivemall/utils/collections/lists/DoubleArrayList.java
b/core/src/main/java/hivemall/utils/collections/lists/DoubleArrayList.java
index feb614f..517dafc 100644
--- a/core/src/main/java/hivemall/utils/collections/lists/DoubleArrayList.java
+++ b/core/src/main/java/hivemall/utils/collections/lists/DoubleArrayList.java
@@ -19,7 +19,10 @@
package hivemall.utils.collections.lists;
import java.io.Serializable;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
public final class DoubleArrayList implements Serializable {
@@ -27,6 +30,7 @@ public final class DoubleArrayList implements Serializable {
public static final int DEFAULT_CAPACITY = 12;
/** array entity */
+ @Nonnull
private double[] data;
private int used;
@@ -39,8 +43,8 @@ public final class DoubleArrayList implements Serializable {
this.used = 0;
}
- public DoubleArrayList(double[] initValues) {
- this.data = initValues;
+ public DoubleArrayList(@CheckForNull double[] initValues) {
+ this.data = Objects.requireNonNull(initValues);
this.used = initValues.length;
}
@@ -67,16 +71,20 @@ public final class DoubleArrayList implements Serializable {
/**
* dynamic expansion.
*/
- private void expand(int max) {
- while (data.length < max) {
- final int len = data.length;
- double[] newArray = new double[len * 2];
- System.arraycopy(data, 0, newArray, 0, len);
+ private void expand(final int minimumCapacity) {
+ while (data.length < minimumCapacity) {
+ int oldLen = data.length;
+ int newLen = (int) Math.max(minimumCapacity, Math.min(oldLen * 2L,
Integer.MAX_VALUE));
+ double[] newArray = new double[newLen];
+ System.arraycopy(data, 0, newArray, 0, oldLen);
this.data = newArray;
}
}
public double remove() {
+ if (used == 0) {
+ throw new NoSuchElementException("No elements to remove");
+ }
return data[--used];
}
diff --git
a/core/src/main/java/hivemall/utils/collections/lists/FloatArrayList.java
b/core/src/main/java/hivemall/utils/collections/lists/FloatArrayList.java
index 54b214d..129e7c3 100644
--- a/core/src/main/java/hivemall/utils/collections/lists/FloatArrayList.java
+++ b/core/src/main/java/hivemall/utils/collections/lists/FloatArrayList.java
@@ -19,7 +19,10 @@
package hivemall.utils.collections.lists;
import java.io.Serializable;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
public final class FloatArrayList implements Serializable {
@@ -28,6 +31,7 @@ public final class FloatArrayList implements Serializable {
public static final int DEFAULT_CAPACITY = 12;
/** array entity */
+ @Nonnull
private float[] data;
private int used;
@@ -40,8 +44,8 @@ public final class FloatArrayList implements Serializable {
this.used = 0;
}
- public FloatArrayList(float[] initValues) {
- this.data = initValues;
+ public FloatArrayList(@CheckForNull float[] initValues) {
+ this.data = Objects.requireNonNull(initValues);
this.used = initValues.length;
}
@@ -68,16 +72,20 @@ public final class FloatArrayList implements Serializable {
/**
* dynamic expansion.
*/
- private void expand(int max) {
- while (data.length < max) {
- final int len = data.length;
- float[] newArray = new float[len * 2];
- System.arraycopy(data, 0, newArray, 0, len);
+ private void expand(final int minimumCapacity) {
+ while (data.length < minimumCapacity) {
+ int oldLen = data.length;
+ int newLen = (int) Math.max(minimumCapacity, Math.min(oldLen * 2L,
Integer.MAX_VALUE));
+ float[] newArray = new float[newLen];
+ System.arraycopy(data, 0, newArray, 0, oldLen);
this.data = newArray;
}
}
public float remove() {
+ if (used == 0) {
+ throw new NoSuchElementException("No elements to remove");
+ }
return data[--used];
}
diff --git
a/core/src/main/java/hivemall/utils/collections/lists/IntArrayList.java
b/core/src/main/java/hivemall/utils/collections/lists/IntArrayList.java
index fed61cc..4fe7d76 100644
--- a/core/src/main/java/hivemall/utils/collections/lists/IntArrayList.java
+++ b/core/src/main/java/hivemall/utils/collections/lists/IntArrayList.java
@@ -21,7 +21,10 @@ package hivemall.utils.collections.lists;
import hivemall.utils.lang.ArrayUtils;
import java.io.Serializable;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
public final class IntArrayList implements Serializable {
@@ -29,6 +32,7 @@ public final class IntArrayList implements Serializable {
public static final int DEFAULT_CAPACITY = 12;
/** array entity */
+ @Nonnull
private int[] data;
private int used;
@@ -41,8 +45,8 @@ public final class IntArrayList implements Serializable {
this.used = 0;
}
- public IntArrayList(int[] initValues) {
- this.data = initValues;
+ public IntArrayList(@CheckForNull int[] initValues) {
+ this.data = Objects.requireNonNull(initValues);
this.used = initValues.length;
}
@@ -69,16 +73,20 @@ public final class IntArrayList implements Serializable {
/**
* dynamic expansion.
*/
- private void expand(final int max) {
- while (data.length < max) {
- final int len = data.length;
- int[] newArray = new int[len * 2];
- System.arraycopy(data, 0, newArray, 0, len);
+ private void expand(final int minimumCapacity) {
+ while (data.length < minimumCapacity) {
+ int oldLen = data.length;
+ int newLen = (int) Math.max(minimumCapacity, Math.min(oldLen * 2L,
Integer.MAX_VALUE));
+ int[] newArray = new int[newLen];
+ System.arraycopy(data, 0, newArray, 0, oldLen);
this.data = newArray;
}
}
public int remove() {
+ if (used == 0) {
+ throw new NoSuchElementException("No elements to remove");
+ }
return data[--used];
}
diff --git
a/core/src/main/java/hivemall/utils/collections/lists/LongArrayList.java
b/core/src/main/java/hivemall/utils/collections/lists/LongArrayList.java
index 0786872..3144054 100644
--- a/core/src/main/java/hivemall/utils/collections/lists/LongArrayList.java
+++ b/core/src/main/java/hivemall/utils/collections/lists/LongArrayList.java
@@ -19,7 +19,10 @@
package hivemall.utils.collections.lists;
import java.io.Serializable;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
public final class LongArrayList implements Serializable {
@@ -28,6 +31,7 @@ public final class LongArrayList implements Serializable {
public static final int DEFAULT_CAPACITY = 12;
/** array entity */
+ @Nonnull
private long[] data;
private int used;
@@ -40,8 +44,8 @@ public final class LongArrayList implements Serializable {
this.used = 0;
}
- public LongArrayList(@Nonnull long[] initValues) {
- this.data = initValues;
+ public LongArrayList(@CheckForNull long[] initValues) {
+ this.data = Objects.requireNonNull(initValues);
this.used = initValues.length;
}
@@ -68,16 +72,20 @@ public final class LongArrayList implements Serializable {
/**
* dynamic expansion.
*/
- private void expand(final int max) {
- while (data.length < max) {
- final int len = data.length;
- long[] newArray = new long[len * 2];
- System.arraycopy(data, 0, newArray, 0, len);
+ private void expand(final int minimumCapacity) {
+ while (data.length < minimumCapacity) {
+ int oldLen = data.length;
+ int newLen = (int) Math.max(minimumCapacity, Math.min(oldLen * 2L,
Integer.MAX_VALUE));
+ long[] newArray = new long[newLen];
+ System.arraycopy(data, 0, newArray, 0, oldLen);
this.data = newArray;
}
}
public long remove() {
+ if (used == 0) {
+ throw new NoSuchElementException("No elements to remove");
+ }
return data[--used];
}
diff --git
a/core/src/test/java/hivemall/utils/collections/lists/LongArrayListTest.java
b/core/src/test/java/hivemall/utils/collections/lists/DoubleArrayListTest.java
similarity index 56%
copy from
core/src/test/java/hivemall/utils/collections/lists/LongArrayListTest.java
copy to
core/src/test/java/hivemall/utils/collections/lists/DoubleArrayListTest.java
index c40ea7e..83d49e2 100644
--- a/core/src/test/java/hivemall/utils/collections/lists/LongArrayListTest.java
+++
b/core/src/test/java/hivemall/utils/collections/lists/DoubleArrayListTest.java
@@ -18,26 +18,17 @@
*/
package hivemall.utils.collections.lists;
-
import org.junit.Assert;
import org.junit.Test;
-public class LongArrayListTest {
+public class DoubleArrayListTest {
@Test
- public void testRemoveIndex() {
- LongArrayList list = new LongArrayList();
- list.add(0).add(1).add(2).add(3);
- Assert.assertEquals(1, list.remove(1));
- Assert.assertEquals(3, list.size());
- Assert.assertArrayEquals(new long[] {0, 2, 3}, list.toArray());
- Assert.assertEquals(3, list.remove(2));
- Assert.assertArrayEquals(new long[] {0, 2}, list.toArray());
- Assert.assertEquals(0, list.remove(0));
- Assert.assertArrayEquals(new long[] {2}, list.toArray());
- list.add(0).add(1);
- Assert.assertEquals(3, list.size());
- Assert.assertArrayEquals(new long[] {2, 0, 1}, list.toArray());
+ public void testExpandZeroSizedList() {
+ DoubleArrayList list = new DoubleArrayList(0);
+ list.add(100.d);
+ Assert.assertEquals(1, list.size());
+ Assert.assertEquals(100.d, list.get(0), 1e-8);
}
}
diff --git
a/core/src/test/java/hivemall/utils/collections/lists/LongArrayListTest.java
b/core/src/test/java/hivemall/utils/collections/lists/FloatArrayListTest.java
similarity index 56%
copy from
core/src/test/java/hivemall/utils/collections/lists/LongArrayListTest.java
copy to
core/src/test/java/hivemall/utils/collections/lists/FloatArrayListTest.java
index c40ea7e..b405a62 100644
--- a/core/src/test/java/hivemall/utils/collections/lists/LongArrayListTest.java
+++
b/core/src/test/java/hivemall/utils/collections/lists/FloatArrayListTest.java
@@ -18,26 +18,17 @@
*/
package hivemall.utils.collections.lists;
-
import org.junit.Assert;
import org.junit.Test;
-public class LongArrayListTest {
+public class FloatArrayListTest {
@Test
- public void testRemoveIndex() {
- LongArrayList list = new LongArrayList();
- list.add(0).add(1).add(2).add(3);
- Assert.assertEquals(1, list.remove(1));
- Assert.assertEquals(3, list.size());
- Assert.assertArrayEquals(new long[] {0, 2, 3}, list.toArray());
- Assert.assertEquals(3, list.remove(2));
- Assert.assertArrayEquals(new long[] {0, 2}, list.toArray());
- Assert.assertEquals(0, list.remove(0));
- Assert.assertArrayEquals(new long[] {2}, list.toArray());
- list.add(0).add(1);
- Assert.assertEquals(3, list.size());
- Assert.assertArrayEquals(new long[] {2, 0, 1}, list.toArray());
+ public void testExpandZeroSizedList() {
+ FloatArrayList list = new FloatArrayList(0);
+ list.add(100.f);
+ Assert.assertEquals(1, list.size());
+ Assert.assertEquals(100.f, list.get(0), 1e-8);
}
}
diff --git
a/core/src/test/java/hivemall/utils/collections/lists/LongArrayListTest.java
b/core/src/test/java/hivemall/utils/collections/lists/IntArrayListTest.java
similarity index 56%
copy from
core/src/test/java/hivemall/utils/collections/lists/LongArrayListTest.java
copy to
core/src/test/java/hivemall/utils/collections/lists/IntArrayListTest.java
index c40ea7e..de8d79e 100644
--- a/core/src/test/java/hivemall/utils/collections/lists/LongArrayListTest.java
+++ b/core/src/test/java/hivemall/utils/collections/lists/IntArrayListTest.java
@@ -18,26 +18,17 @@
*/
package hivemall.utils.collections.lists;
-
import org.junit.Assert;
import org.junit.Test;
-public class LongArrayListTest {
+public class IntArrayListTest {
@Test
- public void testRemoveIndex() {
- LongArrayList list = new LongArrayList();
- list.add(0).add(1).add(2).add(3);
- Assert.assertEquals(1, list.remove(1));
- Assert.assertEquals(3, list.size());
- Assert.assertArrayEquals(new long[] {0, 2, 3}, list.toArray());
- Assert.assertEquals(3, list.remove(2));
- Assert.assertArrayEquals(new long[] {0, 2}, list.toArray());
- Assert.assertEquals(0, list.remove(0));
- Assert.assertArrayEquals(new long[] {2}, list.toArray());
- list.add(0).add(1);
- Assert.assertEquals(3, list.size());
- Assert.assertArrayEquals(new long[] {2, 0, 1}, list.toArray());
+ public void testExpandZeroSizedList() {
+ IntArrayList list = new IntArrayList(0);
+ list.add(100);
+ Assert.assertEquals(1, list.size());
+ Assert.assertEquals(100, list.get(0));
}
}
diff --git
a/core/src/test/java/hivemall/utils/collections/lists/LongArrayListTest.java
b/core/src/test/java/hivemall/utils/collections/lists/LongArrayListTest.java
index c40ea7e..c859dd8 100644
--- a/core/src/test/java/hivemall/utils/collections/lists/LongArrayListTest.java
+++ b/core/src/test/java/hivemall/utils/collections/lists/LongArrayListTest.java
@@ -18,7 +18,6 @@
*/
package hivemall.utils.collections.lists;
-
import org.junit.Assert;
import org.junit.Test;
@@ -40,4 +39,12 @@ public class LongArrayListTest {
Assert.assertArrayEquals(new long[] {2, 0, 1}, list.toArray());
}
+ @Test
+ public void testExpandZeroSizedList() {
+ LongArrayList list = new LongArrayList(0);
+ list.add(100);
+ Assert.assertEquals(1, list.size());
+ Assert.assertEquals(100, list.get(0));
+ }
+
}