You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hama.apache.org by yx...@apache.org on 2013/07/21 18:29:53 UTC
svn commit: r1505413 - in /hama/trunk: CHANGES.txt
ml/src/main/java/org/apache/hama/ml/math/DenseDoubleVector.java
ml/src/main/java/org/apache/hama/ml/math/DoubleVector.java
ml/src/test/java/org/apache/hama/ml/math/TestDenseDoubleVector.java
Author: yxjiang
Date: Sun Jul 21 16:29:53 2013
New Revision: 1505413
URL: http://svn.apache.org/r1505413
Log:
HAMA-782: The arguments of DoubleVector.slice(int, int) method will mislead the user.
Modified:
hama/trunk/CHANGES.txt
hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DenseDoubleVector.java
hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleVector.java
hama/trunk/ml/src/test/java/org/apache/hama/ml/math/TestDenseDoubleVector.java
Modified: hama/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hama/trunk/CHANGES.txt?rev=1505413&r1=1505412&r2=1505413&view=diff
==============================================================================
--- hama/trunk/CHANGES.txt (original)
+++ hama/trunk/CHANGES.txt Sun Jul 21 16:29:53 2013
@@ -5,7 +5,8 @@ Release 0.6.3 (unreleased changes)
NEW FEATURES
BUG FIXES
-
+
+ HAMA-782: The arguments of DoubleVector.slice(int, int) method will mislead the user. (Yexi Jiang)
HAMA-780: New launched child processes by fault tolerance may not be able to contact each other (kennethxian)
HAMA-772: When selected KeyValueTextInputFormat, workers get only one value for key (Ikhtiyor Ahmedov via edwardyoon)
HAMA-776: localQueue is set as Send queue in init function of AbstractMessageManager (kennethxian)
Modified: hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DenseDoubleVector.java
URL: http://svn.apache.org/viewvc/hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DenseDoubleVector.java?rev=1505413&r1=1505412&r2=1505413&view=diff
==============================================================================
--- hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DenseDoubleVector.java (original)
+++ hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DenseDoubleVector.java Sun Jul 21 16:29:53 2013
@@ -354,22 +354,37 @@ public final class DenseDoubleVector imp
*/
@Override
public DoubleVector slice(int length) {
- return slice(0, length);
+ return slice(0, length - 1);
}
-
+
+ @Override
+ public DoubleVector sliceUnsafe(int length) {
+ return sliceUnsafe(0, length - 1);
+ }
+
/*
* (non-Javadoc)
* @see de.jungblut.math.DoubleVector#slice(int, int)
*/
@Override
- public DoubleVector slice(int offset, int length) {
- DoubleVector nv = new DenseDoubleVector(length - offset);
- int index = 0;
- for (int i = offset; i < length; i++) {
- nv.set(index++, vector[i]);
+ public DoubleVector slice(int start, int end) {
+ Preconditions.checkArgument(start >= 0 && start <= end
+ && end < vector.length, "The given from and to is invalid");
+
+ return sliceUnsafe(start, end);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public DoubleVector sliceUnsafe(int start, int end) {
+ DoubleVector newVec = new DenseDoubleVector(end - start + 1);
+ for (int i = start, j = 0; i <= end; ++i, ++j) {
+ newVec.set(j, vector[i]);
}
- return nv;
+ return newVec;
}
/*
Modified: hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleVector.java
URL: http://svn.apache.org/viewvc/hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleVector.java?rev=1505413&r1=1505412&r2=1505413&view=diff
==============================================================================
--- hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleVector.java (original)
+++ hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleVector.java Sun Jul 21 16:29:53 2013
@@ -243,23 +243,43 @@ public interface DoubleVector {
public double dot(DoubleVector vector);
/**
- * Slices this vector from index 0 to the given length.
+ * Validates the input and slices this vector from index 0 to the given length.
*
* @param length must be > 0 and smaller than the dimension of the vector.
* @return a new vector that is only length long.
*/
public DoubleVector slice(int length);
+
+ /**
+ * Slices this vector from index 0 to the given length.
+ *
+ * @param length must be > 0 and smaller than the dimension of the vector.
+ * @return a new vector that is only length long.
+ */
+ public DoubleVector sliceUnsafe(int length);
+
+ /**
+ * Validates the input and then slices this vector from start to end, both are
+ * INCLUSIVE. For example vec = [0, 1, 2, 3, 4, 5], vec.slice(2, 5) = [2, 3,
+ * 4, 5].
+ *
+ * @param offset must be > 0 and smaller than the dimension of the vector
+ * @param length must be > 0 and smaller than the dimension of the vector.
+ * This must be greater than the offset.
+ * @return a new vector that is only (length) long.
+ */
+ public DoubleVector slice(int start, int end);
/**
- * Slices this vector from index offset with the given length. So you end at
- * the upper bound of (offset+length).
+ * Slices this vector from start to end, both are INCLUSIVE. For example vec =
+ * [0, 1, 2, 3, 4, 5], vec.slice(2, 5) = [2, 3, 4, 5].
*
* @param offset must be > 0 and smaller than the dimension of the vector
* @param length must be > 0 and smaller than the dimension of the vector.
* This must be greater than the offset.
* @return a new vector that is only (length) long.
*/
- public DoubleVector slice(int offset, int length);
+ public DoubleVector sliceUnsafe(int start, int end);
/**
* @return the maximum element value in this vector.
Modified: hama/trunk/ml/src/test/java/org/apache/hama/ml/math/TestDenseDoubleVector.java
URL: http://svn.apache.org/viewvc/hama/trunk/ml/src/test/java/org/apache/hama/ml/math/TestDenseDoubleVector.java?rev=1505413&r1=1505412&r2=1505413&view=diff
==============================================================================
--- hama/trunk/ml/src/test/java/org/apache/hama/ml/math/TestDenseDoubleVector.java (original)
+++ hama/trunk/ml/src/test/java/org/apache/hama/ml/math/TestDenseDoubleVector.java Sun Jul 21 16:29:53 2013
@@ -20,9 +20,7 @@ package org.apache.hama.ml.math;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
/**
* Testcase for {@link DenseDoubleVector}
@@ -155,4 +153,36 @@ public class TestDenseDoubleVector {
DoubleVector vec2 = new DenseDoubleVector(arr2);
vec1.add(vec2);
}
+
+ @Test
+ public void testSliceNormal() {
+ double[] arr1 = new double[] {2, 3, 4, 5, 6};
+ double[] arr2 = new double[] {4, 5, 6};
+ double[] arr3 = new double[] {2, 3, 4};
+ DoubleVector vec1 = new DenseDoubleVector(arr1);
+ assertArrayEquals(arr2, vec1.slice(2, 4).toArray(), 0.000001);
+ DoubleVector vec2 = new DenseDoubleVector(arr1);
+ assertArrayEquals(arr3, vec2.slice(3).toArray(), 0.000001);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testSliceAbnormal() {
+ double[] arr1 = new double[] {2, 3, 4, 5, 6};
+ DoubleVector vec = new DenseDoubleVector(arr1);
+ vec.slice(2, 5);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testSliceAbnormalEndTooLarge() {
+ double[] arr1 = new double[] {2, 3, 4, 5, 6};
+ DoubleVector vec = new DenseDoubleVector(arr1);
+ vec.slice(2, 5);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testSliceAbnormalStartLargerThanEnd() {
+ double[] arr1 = new double[] {2, 3, 4, 5, 6};
+ DoubleVector vec = new DenseDoubleVector(arr1);
+ vec.slice(4, 3);
+ }
}