Package dev.nm.misc.datastructure
Class FlexibleTable
- java.lang.Object
-
- dev.nm.misc.datastructure.FlexibleTable
-
- All Implemented Interfaces:
MatrixAccess
,MatrixTable
,Table
- Direct Known Subclasses:
SimplexTable
public class FlexibleTable extends Object implements MatrixTable
This is a 2D table that can shrink or grow by row or by column. The labels to the rows and columns are for printing and debugging. The access to the table is by indices. All indices count from 1. Translation between labels and indices are provided.
-
-
Constructor Summary
Constructors Constructor Description FlexibleTable(int nRows, int nCols)
Constructs a table using default labeling.FlexibleTable(FlexibleTable that)
Copy constructor.FlexibleTable(Object[] rowLabels, Object[] colLabels)
Constructs a table by row and column labels, initializing the content to 0.FlexibleTable(Object[] rowLabels, Object[] colLabels, double[][] cells)
Constructs a flexible table that can shrink or grow.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addColAt(int i)
Adds a column at i.void
addColAt(int i, Object label)
Adds a column at i.void
addRowAt(int i)
Adds a row at i.void
addRowAt(int i, Object label)
Adds a row at i.void
deleteCol(int i)
Deletes column i.void
deleteRow(int i)
Deletes row i.boolean
equals(Object obj)
double
get(int i, int j)
Get the matrix entry at [i,j].Object
getColLabel(int i)
Gets the label for column i.Vector
getColumn(int col)
int
getIndexFromColLabel(Object label)
Translates a column label to a column index.int
getIndexFromRowLabel(Object label)
Translates a row label to a row index.Vector
getRow(int row)
Object
getRowLabel(int i)
Gets the label for row i.int
hashCode()
int
nCols()
Gets the number of columns.int
nRows()
Gets the number of rows.void
renameCol(int i, Object label)
Renames column i.void
renameRow(int i, Object label)
Renames row i.void
set(int i, int j, double value)
Set the matrix entry at [i,j] to a value.DenseMatrix
toMatrix()
Gets a copy of the flexible table in the form of a matrix.String
toString()
-
-
-
Constructor Detail
-
FlexibleTable
public FlexibleTable(Object[] rowLabels, Object[] colLabels, double[][] cells)
Constructs a flexible table that can shrink or grow.- Parameters:
rowLabels
- the row labelscolLabels
- the column labelscells
- the table content
-
FlexibleTable
public FlexibleTable(Object[] rowLabels, Object[] colLabels)
Constructs a table by row and column labels, initializing the content to 0.- Parameters:
rowLabels
- the row labelscolLabels
- the column labels
-
FlexibleTable
public FlexibleTable(int nRows, int nCols)
Constructs a table using default labeling.- Parameters:
nRows
- number of rowsnCols
- number of columns
-
FlexibleTable
public FlexibleTable(FlexibleTable that)
Copy constructor.- Parameters:
that
- anotherFlexibleTable
-
-
Method Detail
-
nRows
public int nRows()
Description copied from interface:Table
Gets the number of rows. Rows count from 1.
-
nCols
public int nCols()
Description copied from interface:Table
Gets the number of columns. Columns count from 1.
-
set
public void set(int i, int j, double value)
Description copied from interface:MatrixAccess
Set the matrix entry at [i,j] to a value. This is the only method that may change a matrix.- Specified by:
set
in interfaceMatrixAccess
- Parameters:
i
- the row indexj
- the column indexvalue
- the value to set A[i,j] to
-
get
public double get(int i, int j)
Description copied from interface:MatrixAccess
Get the matrix entry at [i,j].- Specified by:
get
in interfaceMatrixAccess
- Parameters:
i
- the row indexj
- the column index- Returns:
- A[i,j]
-
getRow
public Vector getRow(int row) throws MatrixAccessException
- Throws:
MatrixAccessException
-
getColumn
public Vector getColumn(int col) throws MatrixAccessException
- Throws:
MatrixAccessException
-
getIndexFromRowLabel
public int getIndexFromRowLabel(Object label)
Translates a row label to a row index. The index counts from 1.- Parameters:
label
- a row label- Returns:
- the row index
-
getIndexFromColLabel
public int getIndexFromColLabel(Object label)
Translates a column label to a column index. The index counts from 1.- Parameters:
label
- a column label- Returns:
- the column index
-
getRowLabel
public Object getRowLabel(int i)
Gets the label for row i.- Parameters:
i
- a row index, counting from 1- Returns:
- the label for row i
-
getColLabel
public Object getColLabel(int i)
Gets the label for column i.- Parameters:
i
- a column index, counting from 1- Returns:
- the label for column i
-
renameRow
public void renameRow(int i, Object label)
Renames row i.- Parameters:
i
- a row index, counting from 1label
- the new label
-
renameCol
public void renameCol(int i, Object label)
Renames column i.- Parameters:
i
- a column index, counting from 1label
- the new label
-
deleteRow
public void deleteRow(int i)
Deletes row i.- Parameters:
i
- a row index, counting from 1
-
deleteCol
public void deleteCol(int i)
Deletes column i.- Parameters:
i
- a column index, counting from 1
-
addRowAt
public void addRowAt(int i, Object label)
Adds a row at i.- Parameters:
i
- the row index, counting from 1label
- the row label
-
addRowAt
public void addRowAt(int i)
Adds a row at i.- Parameters:
i
- the row index, counting from 1
-
addColAt
public void addColAt(int i, Object label)
Adds a column at i.- Parameters:
i
- the column index, counting from 1label
- the column label
-
addColAt
public void addColAt(int i)
Adds a column at i.- Parameters:
i
- the column index, counting from 1
-
toMatrix
public DenseMatrix toMatrix()
Gets a copy of the flexible table in the form of a matrix.- Returns:
- a matrix representation
-
-