Author: robinanil
Date: Thu Feb 11 14:58:59 2010
New Revision: 909008

URL: http://svn.apache.org/viewvc?rev=909008&view=rev
Log:
SparseMatrix uses OpenIntObjectHashMap

Modified:
    
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/SparseMatrix.java

Modified: 
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/SparseMatrix.java
URL: 
http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/SparseMatrix.java?rev=909008&r1=909007&r2=909008&view=diff
==============================================================================
--- 
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/SparseMatrix.java 
(original)
+++ 
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/SparseMatrix.java 
Thu Feb 11 14:58:59 2010
@@ -17,56 +17,61 @@
 
 package org.apache.mahout.math;
 
-import java.util.HashMap;
 import java.util.Map;
 
+import org.apache.mahout.math.map.OpenIntObjectHashMap;
+
 /** Doubly sparse matrix. Implemented as a Map of RandomAccessSparseVector 
rows */
 public class SparseMatrix extends AbstractMatrix {
-
+  
   private int[] cardinality;
-
-  private Map<Integer, Vector> rows;
-
+  
+  private OpenIntObjectHashMap<Vector> rows;
+  
   public SparseMatrix() {
     super();
   }
-
+  
   /**
    * Construct a matrix of the given cardinality with the given row map
-   *
-   * @param cardinality the int[2] cardinality desired
-   * @param rows        a Map<Integer, RandomAccessSparseVector> of rows
+   * 
+   * @param cardinality
+   *          the int[2] cardinality desired
+   * @param rows
+   *          a Map<Integer, RandomAccessSparseVector> of rows
    */
-  public SparseMatrix(int[] cardinality, Map<Integer, 
RandomAccessSparseVector> rows) {
+  public SparseMatrix(int[] cardinality,
+                      Map<Integer,RandomAccessSparseVector> rows) {
     this.cardinality = cardinality.clone();
-    this.rows = new HashMap<Integer, Vector>();
-    for (Map.Entry<Integer, RandomAccessSparseVector> entry : rows.entrySet()) 
{
+    this.rows = new OpenIntObjectHashMap<Vector>();
+    for (Map.Entry<Integer,RandomAccessSparseVector> entry : rows.entrySet()) {
       this.rows.put(entry.getKey(), entry.getValue().clone());
     }
   }
-
+  
   /**
    * Construct a matrix of the given cardinality
-   *
-   * @param cardinality the int[2] cardinality desired
+   * 
+   * @param cardinality
+   *          the int[2] cardinality desired
    */
   public SparseMatrix(int[] cardinality) {
     this.cardinality = cardinality.clone();
-    this.rows = new HashMap<Integer, Vector>();
+    this.rows = new OpenIntObjectHashMap<Vector>();
   }
-
+  
   public int[] size() {
     return cardinality;
   }
-
+  
   @Override
   public Matrix clone() {
     SparseMatrix clone = (SparseMatrix) super.clone();
     clone.cardinality = cardinality.clone();
-    clone.rows = (Map<Integer, Vector>) ((HashMap<Integer, Vector>) 
rows).clone();
+    clone.rows = rows.clone();
     return clone;
   }
-
+  
   public double getQuick(int row, int column) {
     Vector r = rows.get(row);
     if (r == null) {
@@ -75,15 +80,15 @@
       return r.getQuick(column);
     }
   }
-
+  
   public Matrix like() {
     return new SparseMatrix(cardinality);
   }
-
+  
   public Matrix like(int rows, int columns) {
-    return new SparseMatrix(new int[]{rows, columns});
+    return new SparseMatrix(new int[] {rows, columns});
   }
-
+  
   public void setQuick(int row, int column, double value) {
     Vector r = rows.get(row);
     if (r == null) {
@@ -92,17 +97,17 @@
     }
     r.setQuick(column, value);
   }
-
+  
   public int[] getNumNondefaultElements() {
     int[] result = new int[2];
     result[ROW] = rows.size();
-    for (Map.Entry<Integer, Vector> integerVectorEntry : rows.entrySet()) {
-      result[COL] = Math.max(result[COL], integerVectorEntry.getValue()
+    for (Vector vectorEntry : rows.values()) {
+      result[COL] = Math.max(result[COL], vectorEntry
           .getNumNondefaultElements());
     }
     return result;
   }
-
+  
   public Matrix viewPart(int[] offset, int[] size) {
     if (size[ROW] > cardinality[ROW] || size[COL] > cardinality[COL]) {
       throw new CardinalityException();
@@ -113,7 +118,7 @@
     }
     return new MatrixView(this, offset, size);
   }
-
+  
   public Matrix assignColumn(int column, Vector other) {
     if (other.size() != cardinality[ROW] || column >= cardinality[COL]) {
       throw new CardinalityException();
@@ -131,7 +136,7 @@
     }
     return this;
   }
-
+  
   public Matrix assignRow(int row, Vector other) {
     if (row >= cardinality[ROW] || other.size() != cardinality[COL]) {
       throw new CardinalityException();
@@ -139,7 +144,7 @@
     rows.put(row, other);
     return this;
   }
-
+  
   public Vector getColumn(int column) {
     if (column < 0 || column >= cardinality[COL]) {
       throw new IndexException();
@@ -150,7 +155,7 @@
     }
     return new DenseVector(d);
   }
-
+  
   public Vector getRow(int row) {
     if (row < 0 || row >= cardinality[ROW]) {
       throw new IndexException();
@@ -161,5 +166,5 @@
     }
     return res;
   }
-
+  
 }


Reply via email to