tapestry.repairmonitor
Class Matrix

java.lang.Object
  |
  +--tapestry.repairmonitor.Matrix
All Implemented Interfaces:
Cloneable, QuickSerializable

public class Matrix
extends Object
implements Cloneable, QuickSerializable

A sparse array of Arrays. Elements are indexed by arbitrary QuickSerializable objects.

This class is synchronized.

Version:
$Id: Matrix.java,v 1.1 2004/05/06 22:34:27 hweather Exp $
Author:
Dennis Geels

Field Summary
protected  HashMap map
          The internal storage mechanism.
 
Constructor Summary
Matrix()
          Construct a new Matrix.
Matrix(InputBuffer buffer)
          Implied from ostore.util.QuickSerializable interface Construct an Matrix from its QuickSerializable form.
Matrix(InputBuffer buffer, HashMap decompression_map, HashMap row_decompression_map)
          Construct an Matrix from its QuickSerializable form.
 
Method Summary
 void add(Matrix addend)
          Adds the contents of the specified Matrix to this one.
 void add(QuickSerializable id, Array addend)
          Adds the specified Array to this Matrix.
 Object clone()
          Performs a slightly deeper copy than Object.clone.
 boolean contains(QuickSerializable id)
          Shows whether the specified ID is explicitly stored in this Matrix.
 int full_size()
          Returns the full size of the Matrix.
 Array get(QuickSerializable id)
          Returns the Array indexed by the specified ID.
 Iterator ids()
          Returns an Iterator over the IDs stored explicitly in this Matrix.
static void max(double[][] matrix, int[][] indices)
          Finds the indices of the largest elements of the specified Matrix.
 Array put(QuickSerializable id, Array row)
          Places the specified mapping into this Matrix.
 Array remove(QuickSerializable id)
          Removes the row associated with id from this Matrix, and returns the removed row.
 void serialize(OutputBuffer buffer)
           
 void serialize(OutputBuffer buffer, HashMap compression_map, HashMap row_compression_map)
           
 int size()
          Returns the size of the Matrix.
 String toString()
          Produce a human-readable version of this Matrix.
static String toString(double[][] matrix)
          Returns a human-readable representation of the specified matrix.
static String toString(double[][] matrix, int[][] indices)
          Returns a human-readable representation of the specified matrix, with indices.
static double[][] transpose(double[][] matrix)
          Returns the transpose of the specified matrix.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

map

protected HashMap map
The internal storage mechanism.

Constructor Detail

Matrix

public Matrix()
Construct a new Matrix.


Matrix

public Matrix(InputBuffer buffer)
       throws QSException
Implied from ostore.util.QuickSerializable interface Construct an Matrix from its QuickSerializable form.


Matrix

public Matrix(InputBuffer buffer,
              HashMap decompression_map,
              HashMap row_decompression_map)
       throws QSException
Construct an Matrix from its QuickSerializable form. See serialize(OutputBuffer, HashMap, HashMap) for more details.

Parameters:
decompression_map - An Integer->QuickSerializable mapping for decompressing IDs.
row_decompression_map - An Integer->QuickSerializable mapping for decompressing IDs within rows.
Method Detail

toString

public static String toString(double[][] matrix)
Returns a human-readable representation of the specified matrix.

Parameters:
matrix - the matrix to print
Returns:
a human-readable representation of the specified matrix.

toString

public static String toString(double[][] matrix,
                              int[][] indices)
Returns a human-readable representation of the specified matrix, with indices. This method should be used to print sub-matrices, or simply because the indices are helpful. This method calls Array.toString(double[], int[]) on each row of the matrix.

Parameters:
matrix - the matrix to print
indices - the indices, in order, of the elements to print
Returns:
a human-readable representation of the specified matrix.
Throws:
IllegalArgumentException - if width of indices greater than width of corresponding row of matrix.
See Also:
max(double[][], int[][])

transpose

public static final double[][] transpose(double[][] matrix)
Returns the transpose of the specified matrix. The width of the new matrix is the length of the specified matrix, and vice versa. WARNING This method is in some ways even more expensive than toString.

Parameters:
matrix - the matrix to flip
Returns:
the transpose of the specified matrix.

max

public static void max(double[][] matrix,
                       int[][] indices)
Finds the indices of the largest elements of the specified Matrix. This method calls Array.max(double[], int[]) on each row of the matrix. Thus the width of indices determines the number of top elements of each row to find. To find the top elements of each column (instead of by row) use transpose to flip the matrix first.

Parameters:
matrix - the matrix to check
indices - the matrix into which to put the (sorted) indices of the largest elements of matrix.
Throws:
IllegalArgumentException - if width of indices greater than width of corresponding row of matrix.

size

public int size()
Returns the size of the Matrix.

Returns:
the number of Arrays stored in this Matrix.

full_size

public int full_size()
Returns the full size of the Matrix.

Returns:
the total number of elements explicitly stored in the Arrays stored in this Matrix.

ids

public Iterator ids()
Returns an Iterator over the IDs stored explicitly in this Matrix.

Returns:
a HashIterator over the backing map's keys.

contains

public boolean contains(QuickSerializable id)
Shows whether the specified ID is explicitly stored in this Matrix.

Parameters:
id - The unique ID for the row to check.
Returns:
true iff id is explicitly mapped to a row.

get

public Array get(QuickSerializable id)
Returns the Array indexed by the specified ID.

Parameters:
id - The unique ID for a row of this Matrix.
Returns:
the Array stored under id, or null if the ID is unknown.

put

public Array put(QuickSerializable id,
                 Array row)
Places the specified mapping into this Matrix. If id already maps to an Array, the old one is replaced and returned.

Parameters:
id - The index under which to store this row.
row - The Array to store for this index.
Returns:
the Array previously mapped by this index, or null if the ID is unknown.

remove

public Array remove(QuickSerializable id)
Removes the row associated with id from this Matrix, and returns the removed row. The specified id will no longer be associated with any row in this Matrix.

Parameters:
id - The index to be disassociated from the Matrix.
Returns:
the Array previously associated with the index being removed, or null if the index is unknown.

add

public void add(Matrix addend)
Adds the contents of the specified Matrix to this one. Arrays for known IDs are added to the current rows. Unknown IDs are inserted directly (not cloned).

Parameters:
addend - The Matrix whose contents to add

add

public void add(QuickSerializable id,
                Array addend)
Adds the specified Array to this Matrix. If the ID is known, the Array is added to the current row. Otherwise it is cloned and inserted.

Parameters:
id - The unique ID for addend
addend - The Array to add

clone

public Object clone()
Performs a slightly deeper copy than Object.clone. The mapping is cloned, so the new Matrix can be modified independently of this one. Neither the IDs nor the rows are themselves cloned.

Overrides:
clone in class Object
Returns:
a new Matrix with the same set of elements as this one.

toString

public String toString()
Produce a human-readable version of this Matrix. This method consumes O(full_size()) time and String length.

Overrides:
toString in class Object
Returns:
a String of < id:row > pairs.

serialize

public void serialize(OutputBuffer buffer)
Specified by:
serialize in interface QuickSerializable

serialize

public void serialize(OutputBuffer buffer,
                      HashMap compression_map,
                      HashMap row_compression_map)