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 floatTable implements java.io.Serializable {
float[] 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 floatTable() { this(100); }
/**
* Create a new Table
*/
public floatTable(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, float[] 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, float[] column) {
keyTable.put(key, new Integer(dataStorage.size()));
dataStorage.addElement(column);
columnTitleList.addElement("");
columnKeyList.addElement(key);
}
/**
* retrieve a column by column number
*/
public float[] columnAt(int index){ return (float[])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 float[] getColumn(String key){
int i = ((Integer)keyTable.get(key)).intValue();
if(i >= 0)
return (float[])dataStorage.get(i);
else
return null;
}
/**
* Retrieve all column keys as an float[]
*/
public float[] getColumnKeysAsArray(){
int keylen = columnKeyList.size();
float[] keys = new float[keylen];
for(int i = 0; i < keylen; i++)
keys[i] = ((Float)columnKeyList.elementAt(i)).floatValue();
return keys;
}
/**
* Retrieve all column keys, including the index, as an float[]
*/
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 float[]
*/
public float[] getColumnsAsArray(){
int collen = dataStorage.size();
float[] columns = new float[collen];
for(int i = 0; i < collen; i++)
columns[i]= ((Float)dataStorage.elementAt(i)).floatValue();
return columns;
}
/**
* Retrieve all columns, including the index, as an float[]
*/
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 float getElement(String columnKey, int row){
int i = ((Integer)keyTable.get(columnKey)).intValue();
if(i >= 0)
return ((float[])dataStorage.get(i))[row];
else
return 0;
}
/**
* Returns the index column
*/
public float[] 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 float[]
*/
public float[] getRowAsArray(int row){
try {
float[] somefloat = new float[getNumberOfColumns()];
int length = somefloat.length;
for(int i = 0; i < length; i++)
somefloat[i] = ((float[])dataStorage.elementAt(i))[row];
float[] floatarray1d1 = somefloat;
return floatarray1d1;
}
catch(Exception e) {
e.printStackTrace();
}
float[] floatarray1d = new float[0];
return floatarray1d;
}
/**
* 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 float(indexColumn.get(row)));
for(int col = 0; col < getNumberOfColumns(); col++)
v.addElement(new float(((float[])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, float value){
int i = ((Integer)keyTable.get(key)).intValue();
if (i >= 0)
((float[])dataStorage.get(i))[row] = value;
}
/**
* Set the Table's index column
*/
public void setIndexColumn(float[] column) { indexColumn = column; }
/**
* Set the Table's index column
*/
public void setIndexColumn(float[] column, String title) {
indexTitle = title;
indexColumn = column;
}
/**
* Set the Table's title
*/
public void setTitle(String aTitle) { tableTitle = aTitle; }
}