package com.dwave.util;
import java.util.Vector;
import java.util.Hashtable;
/**
* @author Michael Coury
* @author Tim Duty
* @version 1.0, 05/16/2001
*/
public class doubleTable implements java.io.Serializable {
double[] indexColumn;
Vector dataStorage, // possibly use LinkedList instead.....
columnTitleList,
columnKeyList;
Hashtable keyTable; // maybe use a HashMap instead.....
String tableTitle = "",
indexTitle = "Index";
/**
* Create a new Table
*/
public doubleTable() { this(100); }
/**
* Create a new Table
*/
public doubleTable(int aNum) {
dataStorage = new Vector(aNum, 1);
columnTitleList = new Vector(aNum, 1);
columnKeyList = new Vector(1, 1);
keyTable = new Hashtable(aNum, .75F);
}
/**
* Add a column to the table
*/
public void addColumn(String key, double[] column, String title) {
keyTable.put(key, new Integer(dataStorage.size()));
dataStorage.addElement(column);
columnTitleList.addElement(title);
columnKeyList.addElement(key);
}
/**
* Add a column to the table
*/
public void addColumn(String key, double[] column) {
keyTable.put(key, new Integer(dataStorage.size()));
dataStorage.addElement(column);
columnTitleList.addElement("");
columnKeyList.addElement(key);
}
/**
* retrieve a column by column number
*/
public double[] columnAt(int index){ return (double[])dataStorage.elementAt(index); }
/**
* Retrieve a columns key by column number
*/
public String columnKeyAt(int index){ return (String) columnKeyList.elementAt(index); }
/**
* Check if the Table contains a column with this key
*/
public boolean containsColumn(String key){ return keyTable.containsKey(key); }
/**
* Retrieve a column by it's key
*/
public double[] getColumn(String key){
int i = ((Integer)keyTable.get(key)).intValue();
if(i >= 0)
return (double[])dataStorage.get(i);
else
return null;
}
/**
* Retrieve all column keys as an double[]
*/
public double[] getColumnKeysAsArray(){
int keylen = columnKeyList.size();
double[] keys = new double[keylen];
for(int i = 0; i < keylen; i++)
keys[i] = ((Double)columnKeyList.elementAt(i)).doubleValue();
return keys;
}
/**
* Retrieve all column keys, including the index, as an double[]
*/
public String[] getColumnKeysAsArray(boolean withIndex){
int keylen = columnKeyList.size()+1;
String[] keys = new String[keylen];
keys[0] = "Index";
for(int i = 0; i < keylen - 1; i++)
keys[i + 1]= (String)columnKeyList.elementAt(i);
return keys;
}
/**
* Retrieve all column keys as a Vector
*/
public Vector getColumnKeysAsVector(){
Vector keys = new Vector();
keys.addElement("Index");
for(int i = 0; i < columnKeyList.size(); i++)
keys.addElement(columnKeyList.elementAt(i));
return keys;
}
/**
* Retrieve all columns as an double[]
*/
public double[] getColumnsAsArray(){
int collen = dataStorage.size();
double[] columns = new double[collen];
for(int i = 0; i < collen; i++)
columns[i]= ((Double)dataStorage.elementAt(i)).doubleValue();
return columns;
}
/**
* Retrieve all columns, including the index, as an double[]
*/
public Object[] getColumnsAsArray(boolean withIndex){
int collen = dataStorage.size();
Object[] columns = new Object[collen + 1];
columns[0] = indexColumn;
for(int i = 0; i < collen - 1; i++)
columns[i + 1] = dataStorage.elementAt(i);
return columns;
}
/**
* Retrieve all columns as a Vector
*/
public Vector getColumnsAsVector(){
Vector columns = new Vector();
columns.addElement(getIndexColumn());
for(int i = 0; i < dataStorage.size(); i++)
columns.addElement(dataStorage.elementAt(i));
return columns;
}
/**
* Retrieve an element in a specified row in a specified column
*/
public double getElement(String columnKey, int row){
int i = ((Integer)keyTable.get(columnKey)).intValue();
if(i >= 0)
return ((double[])dataStorage.get(i))[row];
else
return 0;
}
/**
* Returns the index column
*/
public double[] getIndexColumn(){ return indexColumn; }
/**
* Returns the index's title
*/
public String getIndexTitle(){ return indexTitle; }
/**
* Returns the number of columns in the Table
*/
public int getNumberOfColumns(){ return dataStorage.size(); }
/**
* Returns the number of rows in the Table
*/
public int getNumberOfRows() { return indexColumn.length; }
/**
* Retrieves the contents of a specified row as a double[]
*/
public double[] getRowAsArray(int row){
try {
double[] somedouble = new double[getNumberOfColumns()];
int length = somedouble.length;
for(int i = 0; i < length; i++)
somedouble[i] = ((double[])dataStorage.elementAt(i))[row];
double[] doublearray1d1 = somedouble;
return doublearray1d1;
}
catch(Exception e) {
e.printStackTrace();
}
double[] doublearray1d = new double[0];
return doublearray1d;
}
/**
* Retrieves the contents of the Table as a Vector
* of the data by row
public Vector getRowsAsVector(){
Vector result = new Vector(getNumberOfRows());
for(int row = 0; row < getNumberOfRows(); row++) {
Vector v = new Vector(getNumberOfColumns() + 1);
v.addElement(new Double(indexColumn.get(row)));
for(int col = 0; col < getNumberOfColumns(); col++)
v.addElement(new Double(((double[])dataStorage.elementAt(col)).get(row)));
result.addElement(v);
}
return result;
} */
/**
* Returns the table's title
*/
public String getTitle(){ return tableTitle; }
/**
* Replace an element at a specified column and row with a new value
*/
public void putElement(String key, int row, double value){
int i = ((Integer)keyTable.get(key)).intValue();
if (i >= 0)
((double[])dataStorage.get(i))[row] = value;
}
/**
* Set the Table's index column
*/
public void setIndexColumn(double[] column) { indexColumn = column; }
/**
* Set the Table's index column
*/
public void setIndexColumn(double[] column, String title) {
indexTitle = title;
indexColumn = column;
}
/**
* Set the Table's title
*/
public void setTitle(String aTitle) { tableTitle = aTitle; }
}