You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by sm...@apache.org on 2013/05/30 22:54:06 UTC

svn commit: r1488007 - in /mahout/trunk/integration/src: main/java/org/apache/mahout/utils/vectors/VectorHelper.java test/java/org/apache/mahout/utils/vectors/VectorHelperTest.java

Author: smarthi
Date: Thu May 30 20:54:06 2013
New Revision: 1488007

URL: http://svn.apache.org/r1488007
Log:
MAHOUT-1232: VectorHelper.topEntries() throws a NPE when number of NonZero elements in vector < maxEntries - added code comments

Modified:
    mahout/trunk/integration/src/main/java/org/apache/mahout/utils/vectors/VectorHelper.java
    mahout/trunk/integration/src/test/java/org/apache/mahout/utils/vectors/VectorHelperTest.java

Modified: mahout/trunk/integration/src/main/java/org/apache/mahout/utils/vectors/VectorHelper.java
URL: http://svn.apache.org/viewvc/mahout/trunk/integration/src/main/java/org/apache/mahout/utils/vectors/VectorHelper.java?rev=1488007&r1=1488006&r2=1488007&view=diff
==============================================================================
--- mahout/trunk/integration/src/main/java/org/apache/mahout/utils/vectors/VectorHelper.java (original)
+++ mahout/trunk/integration/src/main/java/org/apache/mahout/utils/vectors/VectorHelper.java Thu May 30 20:54:06 2013
@@ -80,12 +80,16 @@ public final class VectorHelper {
   }
 
   public static List<Pair<Integer, Double>> topEntries(Vector vector, int maxEntries) {
+
     // Get the size of nonZero elements in the input vector
     int sizeOfNonZeroElementsInVector = Iterables.size(vector.nonZeroes());
-    // If the sizeOfNonZeroElementsInVector < maxEntries then set maxEntries to sizeOfNonZeroElementsInVector
-    // else the call to queue.pop() returns a Pair(null, null) and the subsequent
-    // call to pair.getFirst() throws a NullPointerException
-    maxEntries = (sizeOfNonZeroElementsInVector < maxEntries) ? sizeOfNonZeroElementsInVector : maxEntries;
+
+    // If the sizeOfNonZeroElementsInVector < maxEntries then set maxEntries = sizeOfNonZeroElementsInVector
+    // otherwise the call to queue.pop() returns a Pair(null, null) and the subsequent call
+    // to pair.getFirst() throws a NullPointerException
+    if (sizeOfNonZeroElementsInVector < maxEntries)
+      maxEntries = sizeOfNonZeroElementsInVector;
+
     PriorityQueue<Pair<Integer, Double>> queue = new TDoublePQ<Integer>(-1, maxEntries);
     for (Element e : vector.nonZeroes()) {
       queue.insertWithOverflow(Pair.of(e.index(), e.get()));

Modified: mahout/trunk/integration/src/test/java/org/apache/mahout/utils/vectors/VectorHelperTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/integration/src/test/java/org/apache/mahout/utils/vectors/VectorHelperTest.java?rev=1488007&r1=1488006&r2=1488007&view=diff
==============================================================================
--- mahout/trunk/integration/src/test/java/org/apache/mahout/utils/vectors/VectorHelperTest.java (original)
+++ mahout/trunk/integration/src/test/java/org/apache/mahout/utils/vectors/VectorHelperTest.java Thu May 30 20:54:06 2013
@@ -63,10 +63,11 @@ public final class VectorHelperTest exte
     v.set(1, 0.0);
     v.set(3, 0.0);
     v.set(8, 2.7);
+    // check if sizeOFNonZeroElementsInVector = maxEntries
     assertEquals(6, VectorHelper.topEntries(v, 6).size());
-    // when sizeOfNonZeroElementsInVector < maxEntries
+    // check if sizeOfNonZeroElementsInVector < maxEntries
     assertTrue(VectorHelper.topEntries(v, 9).size() < 9);
-    // when sizeOfNonZeroElementsInVector > maxEntries
+    // check if sizeOfNonZeroElementsInVector > maxEntries
     assertTrue(VectorHelper.topEntries(v, 5).size() < Iterables.size(v.nonZeroes()));
   }
 
@@ -83,5 +84,4 @@ public final class VectorHelperTest exte
     v.set(8, 0.0);
     assertEquals(0, VectorHelper.topEntries(v, 6).size());
   }
-
 }