You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by ka...@apache.org on 2008/04/12 18:44:20 UTC

svn commit: r647469 - in /lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix: AbstractVector.java DenseVector.java SparseVector.java Vector.java VectorView.java

Author: kalle
Date: Sat Apr 12 09:44:14 2008
New Revision: 647469

URL: http://svn.apache.org/viewvc?rev=647469&view=rev
Log:
MAHOUT-34, Vector implements Iterable<Vector.Element>

Modified:
    lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/AbstractVector.java
    lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/DenseVector.java
    lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseVector.java
    lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/Vector.java
    lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/VectorView.java

Modified: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/AbstractVector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/AbstractVector.java?rev=647469&r1=647468&r2=647469&view=diff
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/AbstractVector.java (original)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/AbstractVector.java Sat Apr 12 09:44:14 2008
@@ -106,6 +106,30 @@
   public abstract Vector viewPart(int offset, int length)
       throws CardinalityException, IndexException;
 
+
+  /**
+   *  Returns an iterator for traversing the Vector, but not in
+   *  any particular order. The actual implementations may  make 
+   *  some guarantees about the order in which the vector is 
+   *  traversed. Otherwise, the traversal order is undefined.
+   *  @see java.lang.Iterable#iterator()
+   */
+  public abstract java.util.Iterator<Vector.Element> iterator();
+
+  /*
+   * (non-Javadoc)
+   * @see org.apache.mahout.matrix.Vector#getElement
+   */
+  //@Override JDK 1.6
+  public Vector.Element getElement(int index) { return new Element(index); }
+
+  public class Element implements Vector.Element {
+    private int ind;
+    public Element(int ind) { this.ind=ind; }
+    public double get() { return getQuick(ind); }
+    public int index() { return ind; }
+    public void set(double value) { setQuick(ind,value); }
+  }
   /*
    * (non-Javadoc)
    * 

Modified: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/DenseVector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/DenseVector.java?rev=647469&r1=647468&r2=647469&view=diff
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/DenseVector.java (original)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/DenseVector.java Sat Apr 12 09:44:14 2008
@@ -131,4 +131,22 @@
     else
       return other.haveSharedCells(this);
   }
+
+  /**
+   * Returns an iterator that traverses this Vector from 0 
+   * to cardinality-1, in that order.
+   * @see java.lang.Iterable#iterator
+   */
+  @Override
+  public java.util.Iterator<Vector.Element> iterator() {
+    return new Iterator();
+  }
+
+  private class Iterator implements java.util.Iterator<Vector.Element> {
+    private int ind;
+    public Iterator() { ind=0; }
+    public boolean hasNext() { return ind<values.length; }
+    public Vector.Element next() { return new Element(ind++); }
+    public void remove() { throw new UnsupportedOperationException(); }
+  }
 }

Modified: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseVector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseVector.java?rev=647469&r1=647468&r2=647469&view=diff
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseVector.java (original)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseVector.java Sat Apr 12 09:44:14 2008
@@ -148,4 +148,16 @@
     return new SparseVector(newCardinality);
   }
 
+  @Override
+  public java.util.Iterator<Vector.Element> iterator() {
+    return new Iterator();
+  }
+
+  private class Iterator implements java.util.Iterator<Vector.Element> {
+    private java.util.Iterator<Map.Entry<Integer,Double>> it;
+    public Iterator() { it=values.entrySet().iterator(); }
+    public boolean hasNext() { return it.hasNext(); }
+    public Element next() { return new Element(it.next().getKey()); }
+    public void remove() { throw new UnsupportedOperationException(); }
+  }
 }

Modified: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/Vector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/Vector.java?rev=647469&r1=647468&r2=647469&view=diff
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/Vector.java (original)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/Vector.java Sat Apr 12 09:44:14 2008
@@ -23,7 +23,7 @@
  * The basic interface including numerous convenience functions
  * 
  */
-public interface Vector {
+public interface Vector extends Iterable<Vector.Element>{
 
   /**
    * Return a formatted WritableComparable suitable for output
@@ -93,6 +93,29 @@
   Vector copy();
 
   /**
+   * Return an object of Vector.Element representing an element
+   * of this Vector. Useful when designing new iterator types.
+   * @param index Index of the Vector.Element required
+   * @return The Vector.Element Object
+   */
+  Element getElement(int index);
+
+  public interface Element {
+    /**
+     * @return the value of this vector element.
+     */
+    double get();
+    /**
+     * @return the index of this vector element.
+     */
+    int index();
+    /**
+     * @param value Set the current element to value.
+     */
+    void set(double value);
+  }
+
+  /**
    * Return a new matrix containing the values of the recipient divided by the
    * argument
    * 
@@ -277,4 +300,5 @@
   // DoubleDoubleFunction map);
   // NewVector assign(Vector y, DoubleDoubleFunction function, IntArrayList
   // nonZeroIndexes);
+
 }

Modified: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/VectorView.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/VectorView.java?rev=647469&r1=647468&r2=647469&view=diff
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/VectorView.java (original)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/VectorView.java Sat Apr 12 09:44:14 2008
@@ -19,6 +19,8 @@
 import org.apache.hadoop.io.Text;
 import org.apache.hadoop.io.WritableComparable;
 
+import java.util.Iterator;
+
 /**
  * Implements subset view of a Vector
  */
@@ -113,5 +115,34 @@
       return other == this || vector.haveSharedCells(other);
     else
       return other.haveSharedCells(vector);
+  }
+
+  /*
+   * (Non-Javadoc)
+   * Returns true if index is a valid index in the underlying Vector
+   */
+  private boolean isInView(int index) { 
+    return index>=offset && index<offset+cardinality;
+  }
+
+  @Override
+  public Iterator<Vector.Element> iterator() { return new ViewIterator(); }
+  public class ViewIterator implements Iterator<Vector.Element> {
+    Iterator<Vector.Element> it;
+    Vector.Element el;
+    public ViewIterator() {
+      it=vector.iterator();
+      while(it.hasNext())
+      {	el=it.next();
+	if(isInView(el.index())) return;
+      }
+      el=null;	// No element was found
+    }
+    public Vector.Element next() { return el; }
+    public boolean hasNext() { return el!=null; }
+    /** @throws UnsupportedOperationException all the time. method not
+     * implemented.
+     */
+    public void remove() { throw new UnsupportedOperationException(); }
   }
 }