com.quinncurtis.chart2djava
Class DoubleArray2D

java.lang.Object
  extended by com.quinncurtis.chart2djava.DoubleArray2D

public class DoubleArray2D
extends java.lang.Object

The DoubleArray2D is an improved version of the standard Array type because it allows automatic and dynamic resizing of a 2D array. The array has a NumColumns property that specifies the number of columns in the 2D array, and a NumRows properties the specifies the number of rows in the 2D array. Internally the data buffer is setup as an Array of DoubleArray objects. The rowCapacity property specifies the size of the internal Array buffer that holds a collection of DoubleArray objects. The internal buffer can be sized much larger than the NumRows property, allowing the array to grow in without forcing a time consuming reallocation and the subsequent copying of all of the data values.


Constructor Summary
DoubleArray2D()
          Default constructor.
DoubleArray2D(double[][] rc)
          Constructor initializes the array using the specified data values.
DoubleArray2D(double[][] rc, int colmaxcap)
          Constructor initializes the array using the specified data values.
DoubleArray2D(int rows, int columns)
          Constructor creates an array of Length n, initialized to zero.
 
Method Summary
 int addColumn(double[] r)
          Adds a new column of data to the end of the array, increasing the number of columns by one.
 int addColumn(DoubleArray r)
          Adds a new column of data to the end of the array, increasing the number of columns by one.
 int addColumnRange(double[][] rc)
          Adds multiple new columns of data to the end of the array, increasing the number of columns by by the number of columns in the source data array.
 int addRow(double[] r)
          Adds a new row of data to the end of the array, increasing the number of rows by one.
 int addRow(DoubleArray r)
          Adds a new row of data to the end of the array, increasing the number of rows by one.
static void arrayCopy(double[][] source, int sourcerow, int sourcecol, double[][] dest, int destrow, int destcol, int count)
           
static void arrayCopy(double[][] source, int sourcerow, int sourcecol, double[] dest, int destcol, int count)
           
static void arrayCopy(double[] source, int sourcecol, double[][] dest, int destrow, int destcol, int count)
           
 void clear()
          Resets the NumColumns and NumRows properties to 0.
 java.lang.Object clone()
          Returns an object that is a clone of this array object.
 void copy(DoubleArray2D source)
          Copies the source array.
static void copyArray(DoubleArray2D source, int sourcerow, int sourceoffset, DoubleArray2D dest, int destrow, int destoffset, int count)
          Static routine copies a row of a DoubleArray2D to a row of another DoubleArray2D.
static void copyArray(DoubleArray2D source, int sourcerow, int sourceoffset, DoubleArray dest, int destoffset, int count)
          Static routine copies a row of a DoubleArray2D to a DoubleArray.
static void copyArray(DoubleArray source, int sourceoffset, DoubleArray2D dest, int destrow, int destoffset, int count)
          Static routine copies a row of a DoubleArray2D to a DoubleArray.
 DoubleArray[] DataBuffer()
          This method returns a reference to the internal dataBuffer.
 int deleteColumn(int index)
          Removes a column in the array at the specified column index, decreasing the NumColumns property of the array by one.
 int deleteRow(int index)
          Removes a row in the array at the specified row index, decreasing the NumRows property of the array by one.
 double[] getColumn(int column)
          Returns all of the row values in the specified column.
 double getElement(int row, int col)
          Returns the value of the array element at the specified row and column.
 double[][] getElements()
          Returns the elements of the array as an Array object.
 int getLength(int dim)
          Returns the size for each dimension of the array.
 int getNumColumns()
          Returns the current number of columns in the array.
 int getNumRows()
          Returns the current number of rows in the array.
 double[] getRow(int row)
          Returns all of the column values in the specified row.
 DoubleArray getRowObject(int row)
          Returns a reference to the DoubleArray object that holds the data for the given row.
 int insertColumn(int index, double[] r)
          Inserts a column in the array at the specified column index, increasing the NumColumns property of the array by one.
 int insertColumn(int index, DoubleArray r)
          Inserts a column in the array at the specified column index, increasing the NumColumns property of the array by one.
 int insertRow(int index, double[] r)
          Inserts a new row of data at the specified index, increasing the number of rows by one.
 int insertRow(int index, DoubleArray r)
          Inserts a new row of data at the specified index, increasing the number of rows by one.
 int length()
          This method returns the number of elements in the array, NumRows * NumColumns.
 int numColumns()
          Returns the number of columns in the array.
 int numRows()
          Returns the number of rows in the array.
 int removeColumnAt(int index)
          Removes a column in the array at the specified column index, decreasing the NumColumns property of the array by one.
 int removeRowAt(int index)
          Removes a row in the array at the specified row index, decreasing the NumRows property of the array by one.
 void reset()
          Resets the NumColumns and NumRows properties to 0.
 void resizeColumnCapacity(int newcolcapacity)
          This method reallocates the internal column data buffers, where each row of the array is represented by an ArrayDouble object, to the new column capacity value.
 int resizeNumColumns(int newnumcols)
          Resizes the number of columns in the 2D array.
 int resizeNumRows(int newlength)
          Resizes the number of rows in the 2D array.
 void resizeRowCapacity(int newcapacity)
          This method reallocates the size of the internal row data buffers.
 void setColumn(int column, double[] source)
          Sets all of the row values in the specified column.
 void setColumn(int column, DoubleArray source)
          Sets all of the row values in the specified column.
 void setElement(int row, int col, double r)
          Sets the value of a specific element in the 2D array.
 void setElements(double[][] source)
          Sets the elements of the array.
 void setElements(DoubleArray2D source)
          Sets the elements of the array.
 void setRow(int row, double[] source)
          Sets all of the column values in the specified row.
 void setRow(int row, DoubleArray source)
          Sets all of the column values in the specified row.
 void shiftLeft(int shiftcount, boolean fillzero)
          Shifts the column elements of each row of the array to the left.
 int shiftLeftThenResize(int shiftcount, boolean trim)
          Shifts the column elements of each row of the array to the left.
 void shiftRight(int shiftcount, boolean fillzero)
          Shifts the column elements of each row of the array to the right.
 void TrimToSize()
          The RowCapacity property is set equal to the NumRows property of the array and the internal buffer is resized to the new RowCapacity value.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DoubleArray2D

public DoubleArray2D()
Default constructor.


DoubleArray2D

public DoubleArray2D(double[][] rc)
Constructor initializes the array using the specified data values. The dimensions of the array is set to the dimensions of the initializing array. Each row of the array is represented by an DoubleArray object.

Parameters:
rc - The initializing data values.

DoubleArray2D

public DoubleArray2D(double[][] rc,
                     int colmaxcap)
Constructor initializes the array using the specified data values. The dimensions of the array is set to the dimensions of the initializing array. Each row of the array is represented by an DoubleArray object.

Parameters:
rc - The initializing data values.
colmaxcap - The rowCapacity property of each rows DoubleArray object is set to this value.

DoubleArray2D

public DoubleArray2D(int rows,
                     int columns)
Constructor creates an array of Length n, initialized to zero.

Parameters:
rows - The number of rows for the 2D array.
columns - The number of columns for the 2D array.
Method Detail

addColumn

public int addColumn(double[] r)
Adds a new column of data to the end of the array, increasing the number of columns by one. If the rows DoubleArray.maxCapacity property is exceeded the maxCapacity property is automatically doubled and the internal buffer resized accordingly.

Parameters:
r - A new column of data, size[NumRows], added at the end of the array.
Returns:
Returns the new value of the NumColumns property.

addColumn

public int addColumn(DoubleArray r)
Adds a new column of data to the end of the array, increasing the number of columns by one. If the rows DoubleArray.maxCapacity property is exceeded the maxCapacity property is automatically doubled and the internal buffer resized accordingly.

Parameters:
r - A new column of data, size[NumRows], added at the end of the array.
Returns:
Returns the new value of the NumColumns property.

addColumnRange

public int addColumnRange(double[][] rc)
Adds multiple new columns of data to the end of the array, increasing the number of columns by by the number of columns in the source data array. If the rows DoubleArray.maxCapacity property is exceeded the maxCapacity property is automatically doubled and the internal buffer resized accordingly.

Parameters:
rc - A 2D array of new column data, with the number of rows equal to NumRows, added at the end of the array.
Returns:
Returns the new value of the NumColumns property.

addRow

public int addRow(double[] r)
Adds a new row of data to the end of the array, increasing the number of rows by one. If the RowCapacity property is exceeded the RowCapacity property is automatically increased by one and the internal buffer resized accordingly.

Parameters:
r - A new row of data, size[NumColumns], added at the end of the array.
Returns:
Returns the new value of the NumRows property.

addRow

public int addRow(DoubleArray r)
Adds a new row of data to the end of the array, increasing the number of rows by one. If the RowCapacity property is exceeded the RowCapacity property is automatically increased by one and the internal buffer resized accordingly.

Parameters:
r - A new row of data, size[NumColumns], added at the end of the array.
Returns:
Returns the new value of the NumRows property.

arrayCopy

public static void arrayCopy(double[][] source,
                             int sourcerow,
                             int sourcecol,
                             double[][] dest,
                             int destrow,
                             int destcol,
                             int count)

arrayCopy

public static void arrayCopy(double[][] source,
                             int sourcerow,
                             int sourcecol,
                             double[] dest,
                             int destcol,
                             int count)

arrayCopy

public static void arrayCopy(double[] source,
                             int sourcecol,
                             double[][] dest,
                             int destrow,
                             int destcol,
                             int count)

clear

public void clear()
Resets the NumColumns and NumRows properties to 0. Each rows maxCapacity property is set to 16. The RowCapacity property is also set to 16.


clone

public java.lang.Object clone()
Returns an object that is a clone of this array object.

Overrides:
clone in class java.lang.Object
Returns:
Returns a clone of this array object.

copy

public void copy(DoubleArray2D source)
Copies the source array.

Parameters:
source - The source array object.

copyArray

public static void copyArray(DoubleArray2D source,
                             int sourcerow,
                             int sourceoffset,
                             DoubleArray2D dest,
                             int destrow,
                             int destoffset,
                             int count)
Static routine copies a row of a DoubleArray2D to a row of another DoubleArray2D.

Parameters:
source - The source DoubleArray2D array.
sourcerow - The row of the source array to copy from.
sourceoffset - The column offset to start copying from in the source array.
dest - The destination DoubleArray array.
destrow - The row of the destination array to copy to.
destoffset - The index to start copying to in the destination array.
count - The number of array elements to copy.

copyArray

public static void copyArray(DoubleArray2D source,
                             int sourcerow,
                             int sourceoffset,
                             DoubleArray dest,
                             int destoffset,
                             int count)
Static routine copies a row of a DoubleArray2D to a DoubleArray.

Parameters:
source - The source DoubleArray2D array.
sourcerow - The row of the source array to copy from.
sourceoffset - The column offset to start copying from in the source array.
dest - The destination DoubleArray array.
destoffset - The index to start copying to in the destination array.
count - The number of array elements to copy.

copyArray

public static void copyArray(DoubleArray source,
                             int sourceoffset,
                             DoubleArray2D dest,
                             int destrow,
                             int destoffset,
                             int count)
Static routine copies a row of a DoubleArray2D to a DoubleArray.

Parameters:
source - The source DoubleArray2D array.
sourceoffset - The column offset to start copying from in the source array.
dest - The destination DoubleArray array.
destrow - The row of the destination array to copy to.
destoffset - The index to start copying to in the destination array.
count - The number of array elements to copy.

DataBuffer

public DoubleArray[] DataBuffer()
This method returns a reference to the internal dataBuffer. This reference is an Array of DoubleArray objects.


deleteColumn

public int deleteColumn(int index)
Removes a column in the array at the specified column index, decreasing the NumColumns property of the array by one.

Parameters:
index - The array column to be deleted.
Returns:
Returns the new value of the NumColumns property.

deleteRow

public int deleteRow(int index)
Removes a row in the array at the specified row index, decreasing the NumRows property of the array by one.

Parameters:
index - The array row to be deleted.
Returns:
Returns the new value of the NumRows property.

getColumn

public double[] getColumn(int column)
Returns all of the row values in the specified column.

Parameters:
column - The column index
Returns:
Returns all of the row values in the specified column.

getElement

public double getElement(int row,
                         int col)
Returns the value of the array element at the specified row and column.

Parameters:
row - The row index
col - The column index
Returns:
Returns the value of the array element at the specified row and column.

getElements

public double[][] getElements()
Returns the elements of the array as an Array object.


getLength

public int getLength(int dim)
Returns the size for each dimension of the array.

Parameters:
dim - A value of 0 returns the number of rows in the array, a value of 1 returns the number of columns in the array.
Returns:
Returns the size of the specified dimension.

getNumColumns

public int getNumColumns()
Returns the current number of columns in the array. The current number of columns is equal to the DoubleArray.length property for each DoubleArray object storing the data for each row of the array. Since the array must be rectangular, every row in the array must have the same length.

Returns:
Returns the new value of the NumColumns property.

getNumRows

public int getNumRows()
Returns the current number of rows in the array.

Returns:
Returns the new value of the NumRows property.

getRow

public double[] getRow(int row)
Returns all of the column values in the specified row.

Parameters:
row - The row index
Returns:
Returns all of the column values in the specified row.

getRowObject

public DoubleArray getRowObject(int row)
Returns a reference to the DoubleArray object that holds the data for the given row.

Parameters:
row - The row index
Returns:
Returns a reference to the DoubleArray object that holds the data for the given row.

insertColumn

public int insertColumn(int index,
                        double[] r)
Inserts a column in the array at the specified column index, increasing the NumColumns property of the array by one. If the maxCapacity property for that row is exceeded the maxCapacity property is automatically doubled and the internal buffer resized accordingly.

Parameters:
index - The array column where the new values are inserted.
r - An array (size NumRows) of new values to insert.
Returns:
Returns the new value of the NumColumns property.

insertColumn

public int insertColumn(int index,
                        DoubleArray r)
Inserts a column in the array at the specified column index, increasing the NumColumns property of the array by one. If the maxCapacity property for that row is exceeded the maxCapacity property is automatically doubled and the internal buffer resized accordingly.

Parameters:
index - The array column where the new values are inserted.
r - An array (size NumRows) of new values to insert.
Returns:
Returns the new value of the NumColumns property.

insertRow

public int insertRow(int index,
                     double[] r)
Inserts a new row of data at the specified index, increasing the number of rows by one. If the RowCapacity property is exceeded the RowCapacity property is automatically increased by one and the internal buffer resized accordingly.

Parameters:
index - Insert the new data at this row index.
r - A new row of data, size[NumColumns], inserted at the specified index.
Returns:
Returns the new value of the NumRows property.

insertRow

public int insertRow(int index,
                     DoubleArray r)
Inserts a new row of data at the specified index, increasing the number of rows by one. If the RowCapacity property is exceeded the RowCapacity property is automatically increased by one and the internal buffer resized accordingly.

Parameters:
index - Insert the new data at this row index.
r - A new row of data, size[NumColumns], inserted at the specified index.
Returns:
Returns the new value of the NumRows property.

length

public int length()
This method returns the number of elements in the array, NumRows * NumColumns.


numColumns

public int numColumns()
Returns the number of columns in the array.


numRows

public int numRows()
Returns the number of rows in the array.


removeColumnAt

public int removeColumnAt(int index)
Removes a column in the array at the specified column index, decreasing the NumColumns property of the array by one.

Parameters:
index - The array column to be deleted.
Returns:
Returns the new value of the NumColumns property.

removeRowAt

public int removeRowAt(int index)
Removes a row in the array at the specified row index, decreasing the NumRows property of the array by one.

Parameters:
index - The array row to be deleted.
Returns:
Returns the new value of the NumRows property.

reset

public void reset()
Resets the NumColumns and NumRows properties to 0. Each rows maxCapacity property is set to 16. The RowCapacity property is also set to 16.


resizeColumnCapacity

public void resizeColumnCapacity(int newcolcapacity)
This method reallocates the internal column data buffers, where each row of the array is represented by an ArrayDouble object, to the new column capacity value.

Parameters:
newcolcapacity - The internal DoubleArray objects are resized to the new capacity. The NumColumns property remains unchanged if the new capacity is larger than the old capacity, only the buffer size is changed, not the current number of columns in the array.

resizeNumColumns

public int resizeNumColumns(int newnumcols)
Resizes the number of columns in the 2D array. The columns maxCapacity property for each rows data is iteratively doubled until it exceeds the new length. Each row of the 2D array is resized to the new maxCapacity value.

Parameters:
newnumcols - The number of columns in the array is resized to this value.
Returns:
Returns the new value of the NumColumns property.

resizeNumRows

public int resizeNumRows(int newlength)
Resizes the number of rows in the 2D array. The RowCapacity property is iteratively doubled until it exceeds the new size, the internal row buffer buffer is resized to the new RowCapacity value.

Parameters:
newlength - The number of rows in the array is resized to this value.
Returns:
Returns the new value of the NumRows property.

resizeRowCapacity

public void resizeRowCapacity(int newcapacity)
This method reallocates the size of the internal row data buffers.

Parameters:
newcapacity - This method reallocates the size of the internal row data buffer. The NumRows property remains unchanged if the new capacity is larger than the old capacity, only the row buffer size is changed, not the current number of rows in the array.

setColumn

public void setColumn(int column,
                      double[] source)
Sets all of the row values in the specified column.

Parameters:
column - The column index
source - The source array, size[NumRows], to copy to the specified column.

setColumn

public void setColumn(int column,
                      DoubleArray source)
Sets all of the row values in the specified column.

Parameters:
column - The column index
source - The source array, size[NumRows], to copy to the specified column.

setElement

public void setElement(int row,
                       int col,
                       double r)
Sets the value of a specific element in the 2D array.

Parameters:
row - The row index
col - The column index *
r - The value to assign the specified array element.

setElements

public void setElements(double[][] source)
Sets the elements of the array. The array is resized to match the size of the source array.

Parameters:
source - The source 2D Array object.

setElements

public void setElements(DoubleArray2D source)
Sets the elements of the array. The array is resized to match the size of the source array.

Parameters:
source - The source 2D Array object.

setRow

public void setRow(int row,
                   double[] source)
Sets all of the column values in the specified row.

Parameters:
row - The row index
source - The source array, size[NumColumns], to copy to the specified row.

setRow

public void setRow(int row,
                   DoubleArray source)
Sets all of the column values in the specified row.

Parameters:
row - The row index
source - The source array, size[NumColumns], to copy to the specified row.

shiftLeft

public void shiftLeft(int shiftcount,
                      boolean fillzero)
Shifts the column elements of each row of the array to the left. If you have a 2D array with 10 columns and shift the array 2 columns to the left, column 7 ends up with the values originally in column 9, column 6 ends up with the value originally in column 8, etc.

Parameters:
shiftcount - The columns are shifted to left by shiftcount elements.
fillzero - Set to true if columns to the right of the final shift point are to be set to zero. In the example above this would apply to columns 8 and 9 in the final, shifted array.

shiftLeftThenResize

public int shiftLeftThenResize(int shiftcount,
                               boolean trim)
Shifts the column elements of each row of the array to the left. If you have a 2D array with 10 columns and shift the array 2 columns to the left, column 7 ends up with the values originally in column 9, column 6 ends up with the value originally in column 8, etc. The NumColumns property is resized to equal to the original NumColumns value minus the shift count (NumColumns - shiftcount).

Parameters:
shiftcount - The columns are shifted to left by shiftcount elements.
trim - Set to true if each rows maxCapacity property is to be set equal to that rows Length (or NumColumns) property, sizing the internal data buffers to the exact array size.
Returns:
Returns the new value of the Length property.

shiftRight

public void shiftRight(int shiftcount,
                       boolean fillzero)
Shifts the column elements of each row of the array to the right. If you have a 2D array with 10 columns and shift the array 2 elements to the right, column 2 ends up with the value originally in column 0, column 3 ends up with the value originally in column 1, etc.

Parameters:
shiftcount - The columns are shifted to right by shiftcount elements.
fillzero - Set to true if columns to the left of the final shift point are to be set to zero. In the example above this would apply to columns 0 and 1 in the final, shifted array.

TrimToSize

public void TrimToSize()
The RowCapacity property is set equal to the NumRows property of the array and the internal buffer is resized to the new RowCapacity value. The DoubleArray object used as the data buffer for each row is also "trimmed". This leaves no "slack" in the internal buffers that allow the array to grow without forcing a reallocation and copy of all data elements should the size of the array be increased.