home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 14.1 KB | 504 lines |
- /*
- * Copyright (c) 1996 Krumel & Associates, Inc. All Rights Reserved.
- *
- * www.krumel.com - controls@krumel.com
- *
- * Permission is given to the buyer of this package for one software
- * developer to use this software on one CPU (one workstation) and to make
- * one backup copy. You may uitilize and/or midfy this class for use in your
- * projects. You may distribute or sell any executable which results from
- * using this code in yur application, except a utility or class of similar
- * nature to this product. You may distribute this this product in compiled
- * form only, but soley to be used with your cmpiled executable product
- * for the puposes of dynamic loading. You may NOT redistribute the source
- * code in any form or make it accessible through a network or other
- * distribution media to others. Please refer to the file "copyright.html"
- * for further important copyright and licensing information.
- *
- * The source code is the confidential and proprietary information
- * of Krumel & Associates, Inc. ("Confidential Information"). You shall
- * not disclose such Confidential Information and shall use it only in
- * accordance with the terms of the license agreement you entered into
- * with Krumel & Associates, Inc..
-
- * KRUMEL & ASSOCIATES MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
- * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
- * NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
- * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. KRUMEL & ASSOCIATES SHALL NOT
- * BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
- * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
- */
-
- package symantec.itools.db.awt.genutil;
-
- import java.util.Vector;
-
- /**
- * Class represents an element of a matrix. Each element contains a pointer to
- * the next element. The matrix class maintains a pointer to the first element
- * of a row and uses that element to manipulate row data.
- */
- class MatrixElement implements java.io.Serializable {
- int col;
- Object obj;
- MatrixElement next;
-
- /**
- * Sentinal to indicate no data in row.
- */
- static Object NO_ELEMENT = new Object();
-
- /**
- * Creates a new element with next element set to null.
- */
- MatrixElement(int c, Object o) {
- this(c, o, null);
- }
-
- /**
- * Creates a new element which has a next element
- */
- MatrixElement(int c, Object o, MatrixElement e) {
- col = c;
- obj = o;
- next = e;
- }
-
- /**
- * Creates a deep copy of this row.
- */
- MatrixElement cloneRow() {
- MatrixElement head = new MatrixElement(col, obj);
- MatrixElement curr = head;
- MatrixElement nextElt = next;
-
- while(nextElt!=null) {
- curr.next = new MatrixElement(nextElt.col, nextElt.obj);
- curr = curr.next;
- nextElt = nextElt.next;
- }
-
- return head;
- }
-
- /**
- * Determines if row is contains any data
- */
- final boolean isValid() {
- return obj != NO_ELEMENT;
- }
-
- /**
- * Determine if row contains element
- */
- final boolean contains(int c, Object o) {
- MatrixElement elt = find(c);
-
- if (elt == null || elt.obj == null) {
- return false;
- }
-
- return elt.obj.equals(o);
- }
-
- /**
- * Determine if row contains element
- */
- final boolean contains(int c) {
- return find(c) != null;
- }
-
- /**
- * Sets the object at the specified column. If element already exists, replaces it.
- */
- final void set(int c, Object o) {
- MatrixElement elt = this;
-
- do {
- if (c==elt.col) {
- //elt is the column so change object
- elt.obj = o;
- return;
- } else if (c<elt.col) {
- //element occurs before this column so insert element
- elt.next = new MatrixElement(elt.col, elt.obj, elt.next);
- elt.col = c;
- elt.obj = o;
- return;
- } else if (elt.next==null) {
- elt.next = new MatrixElement(c, o);
- }
-
- elt = elt.next;
- } while (elt != null);
- }
-
- final void unset(int c) {
- MatrixElement elt = find(c);
-
- if (elt == null) {
- return;
- }
-
- if (elt==this && next==null) {
- obj = NO_ELEMENT;
- } else if (elt==this) {
- col = next.col;
- obj = next.obj;
- next = next.next;
- } else {
- if (elt.next==null) {
- MatrixElement prev = findPrev(c);
- prev.next = null;
- } else {
- elt.col = elt.next.col;
- elt.obj = elt.next.obj;
- elt.next = elt.next.next;
- }
- }
- }
-
- /**
- * Gets the object in the column. Return null if column value not present
- */
- final Object objectAt(int c) {
- MatrixElement elt = find(c);
-
- if (elt == null) {
- return null;
- }
-
- return elt.obj;
- }
-
- final MatrixElement find(int c) {
- MatrixElement elt = this;
-
- do {
- if (c == elt.col) {
- return elt;
- } else if (c < elt.col || elt.next == null) {
- return null;
- }
-
- elt = elt.next;
- } while (elt != null);
-
- return null;
- }
-
- final MatrixElement findPrev(int c) {
- MatrixElement elt = this;
- MatrixElement prev = this;
-
- do {
- if (c == elt.col) {
- return prev;
- } else if (c < elt.col) {
- return prev;
- } else if (elt.next == null) {
- return elt;
- }
-
- prev = elt;
- elt = elt.next;
- } while (elt != null);
-
- return prev;
- }
-
- public final String toString() { return obj.toString(); }
- }
-
- /**
- * A two-dimensional sparse vector of objects.
- *
- * @version 1.1 Jun2 2, 1997
- * @author Andy Krumel
- */
- public class Matrix implements Cloneable, java.io.Serializable {
- Vector rows = new Vector();
-
- /**
- * Constructs a new empty matrix.
- */
- public Matrix() {
- }
-
- /**
- * Clones this matrix. The elements are <strong>not</strong> cloned.
- */
- public synchronized Object clone() {
- Matrix m = new Matrix();
- m.rows = (Vector)rows.clone();
-
- return m;
- }
-
- /**
- * Empties the matrix.
- */
- public void removeAllElements() {
- rows.removeAllElements();
- }
-
- /**
- * Adds an element to the matrix.
- * @param r The row to add element.
- * @param c The column to add element
- * @param o The object to put in matrix
- * @exception IllegalArgumentException Thrown if the element already exists in the matrix.
- */
- public synchronized void addElement(int r, int c, Object o) throws IllegalArgumentException {
- ensureRowExists(r);
-
- //if does not contain row, add and return
- MatrixElement elt = (MatrixElement)rows.elementAt(r);
- if (elt == null) {
- //row not in matrix so add
- elt = new MatrixElement(c, o);
- rows.setElementAt(elt, r);
- return;
- } else if (!elt.contains(c)) {
- elt.set(c, o);
-
- return;
- }
-
- //exception - should call update
- throw new IllegalArgumentException("Element already in Matrix");
- }
-
- /**
- * Updates an existing element in the matrix. If the element is not already
- * the matrix, it is added
- * @param r The row to update element.
- * @param c The column to update element
- * @param o The object to put in matrix
- */
- public synchronized void updateElement(int r, int c, Object o) {
- ensureRowExists(r);
- //if does not contain row, add and return
- MatrixElement elt = (MatrixElement)rows.elementAt(r);
-
- if (elt == null) {
- //row not in matrix so add
- elt = new MatrixElement(c, o);
- rows.setElementAt(elt, r);
- return;
- } else {
- elt.set(c, o);
- }
- }
-
- /**
- * Determines if the matrix contains an element at the specified row.
- * @param r The row to look for element.
- */
- public synchronized boolean containsRow(int r) {
- if (rows.size()<=r) {
- return false;
- }
-
- //if does not contain row, add and return
- MatrixElement elt = (MatrixElement)rows.elementAt(r);
-
- return elt!=null;
- }
-
- /**
- * Determines if the matrix contains an element at the specified location.
- * @param r The row to look for element.
- * @param c The column to look for element
- */
- public synchronized boolean contains(int r, int c) {
- if (rows.size()<=r) {
- return false;
- }
-
- //if does not contain row, add and return
- MatrixElement elt = (MatrixElement)rows.elementAt(r);
-
- if (elt == null) {
- return false;
- }
-
- return elt.contains(c);
- }
-
- /**
- * Determines if the matrix contains an element at the specified location.
- * @param r The row to look for element.
- * @param c The column to look for element
- * @param o The element to look for at the location
- */
- public synchronized boolean contains(int r, int c, Object o) {
- if (rows.size()<=r) {
- return false;
- }
-
- //if does not contain row, add and return
- MatrixElement elt = (MatrixElement)rows.elementAt(r);
-
- if (elt == null) {
- return false;
- }
-
- return elt.contains(c, o);
- }
-
- /**
- * Method used by MatrixEnumation
- */
- MatrixElement firstRowElement(int r) {
- return (MatrixElement)rows.elementAt(r);
- }
-
- /**
- * Gets an element at a specified location.
- * @param r The row to look for element.
- * @param c The column to look for element
- * @return The element at the location
- */
- public synchronized Object elementAt(int r, int c) {
- MatrixElement elt = (MatrixElement)rows.elementAt(r);
-
- //if does not contain row, add and return
- if (elt == null) {
- return null;
- }
-
- return elt.objectAt(c);
- }
-
- /**
- * Removes an element at a specified location.
- * @param r The row to look for element.
- * @param c The column to look for element
- */
- public synchronized void removeElementAt(int r, int c) {
- MatrixElement elt = (MatrixElement)rows.elementAt(r);
-
- //if does not contain row, add and return
- if (elt == null) {
- return;
- }
-
- elt.unset(c);
-
- if (!elt.isValid()) {
- rows.setElementAt(null, r);
- }
- }
-
- /**
- * Adds row to the matrix. If the row already exists, that row and all
- * subsequent rows are shifted down.
- */
- public synchronized void addRow(int r) {
- if (rows.size() < r+1) {
- //expand the vector to have a row 'r'
- rows.setSize(r+1);
- return;
- }
-
- rows.insertElementAt(null, r);
- }
-
- /**
- * Removes a specified row from the matrix. Rows appearing after row will
- * be shifted up.
- * @param r The row to remove.
- */
- public void removeRow(int r) {
- rows.removeElementAt(r);
- }
-
- /**
- * Copies a row from one matrix to another. Elements from this matrix
- * are copied via a series of updateElement() calls.
- * @param row The row to copy from this matrix
- * @param toRow The row in the to matrix to put new elements.
- * @param to The matrix to copy the row to.
- */
- public synchronized void copyRow(int row, int toRow, Matrix to) {
- MatrixElement elt = (MatrixElement)rows.elementAt(row);
-
- //if does not contain row
- if (elt == null) {
- to.rows.setElementAt(null, toRow);
- }
-
- to.ensureRowExists(toRow);
- to.rows.setElementAt(elt.cloneRow(), toRow);
- }
-
- /**
- * Gets the number of rows in the matrix
- */
- public int rows() {
- return rows.size();
- }
-
- protected final void ensureRowExists(int r) {
- if (rows.size() < r+1) {
- rows.setSize(r+1);
- }
- }
-
- /**
- * Gets an Enumberation to iterate the elements in the matrix.
- */
- public MatrixEnumeration elements() {
- return new MatrixEnumeration(this);
- }
-
- public String toString() {
- return "Matrix: rows=" + rows();
- }
-
- /* public static void main(String args[]) throws Exception {
- Matrix m = new Matrix();
- m.addElement(0, 0 , "0, 0");
- m.addElement(0, 1, "0, 1");
- m.addElement(2, 2, "2, 2");
- m.addElement(2, 4, "2, 4");
- m.addElement(5,3,"5, 3");
-
- System.out.println(m.elementAt(0, 0));
- System.out.println(m.elementAt(0, 1));
- System.out.println(m.elementAt(2, 4));
- System.out.println(m.elementAt(5, 3));
- m.removeElementAt(0,0);
-
- m.insertRow(2);
- m.addElement(2,1,"new 2,1");
-
- System.out.println("Enumerating");
- MatrixEnumeration e = m.elements();
- while(e.hasMoreElements()) {
- System.out.println(e.currRow() + " - " + e.nextElement());
- }
-
- m.removeRow(2);
- System.out.println("Enumerating");
- e = m.elements();
- while(e.hasMoreElements()) {
- System.out.println(e.nextElement() + " - " + e.currRow() );
- }
- m.addElement(3, 3, "3, 3");
- m.addElement(2, 2, "2, 2");
- m.addElement(2, 0, "2, 0");
- System.out.println("Enumerating");
- e = m.elements();
- System.out.println(e.advanceTo(2));
- System.out.println(e.nextRow());
- while(e.hasMoreElements()) {
- System.out.println(e.nextElement() + " - " + e.currRow() );
- }
- }
- */
- }
-
-
-