|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--double_vec.double_vec
class double_vec
The double_vec class is based on a C++ template (sadly Java does
not have generics yet). This class (and the template it is based
on) is similar in design to the C++ Standard Template Library
The double_vec class is designed for dense arrays where the all elements of the array are used (e.g., there are no empty elements).
Usage:Elements are added to the end of the array via the append() function.
Elements in the array can be accessed via the elementAt() function (Java has no operator overloading so the [] operator cannot be used).
A doubling algorithm is used when the data size is expanded because it minimizes the amount of copying that must be done. The array will quickly grow to a the size needed to accomodate the data set and no more copying will be necessary. For large arrays this has the drawback that more memory may be allocated than is needed, since the amount of memory used grows exponentially.
The template on which the double_vec class was based was used to implement a String container class. As a result, this class may be overkill as a double container.
Using the elementAt() and setElementAt() functions can obscure the clarity of numeric code. One way around this is to use the getData() function to return the array and the length() function to return the amount of data in the array. (Note that the array will usually be larger than the number of data elements).
You may use this source code without limitation and without fee as long as you include:
This software was written and is copyrighted by Ian Kaplan, Bear Products International, www.bearcave.com, 2001.
This software is provided "as is", without any warrenty or claim as to its usefulness. Anyone who uses this source code uses it at their own risk. Nor is any support provided by Ian Kaplan and Bear Products International.
Please send any bug fixes or suggested source changes to:
iank@bearcave.com
Field Summary | |
(package private) double[] |
array
|
private int |
array_size
|
private int |
num_elem
|
private static int |
StartArraySize
|
Constructor Summary | |
double_vec()
Allocate an array whose initial size is StartArraySize |
Method Summary | |
void |
append(double val)
Append an item to the end of the array |
double |
elementAt(int ix)
Return the array element at index ix. |
void |
expand(int amount)
Expand the number of array data slots by "amount" elements. |
double[] |
getData()
Return a reference to the double_vec array. |
int |
length()
Return the number of elements currently in the array |
void |
remove()
Remove one item from the end of the array. |
void |
set_size(int new_size)
Set the number of data elements in the array to a new value (note that this will usually be smaller than the array size, unless a power of two is chosen for "new_size"). |
void |
setElementAt(double val,
int ix)
Assign array element ix the value val. |
private void |
twice()
Double the amount of memory allocated for the array. |
Methods inherited from class java.lang.Object |
|
Field Detail |
private static final int StartArraySize
private int num_elem
private int array_size
double[] array
Constructor Detail |
public double_vec()
Method Detail |
private void twice()
Double the amount of memory allocated for the array. Return true if memory allocation succeeded, false otherwise.
public int length()
public void setElementAt(double val, int ix) throws java.lang.IndexOutOfBoundsException
public double elementAt(int ix) throws java.lang.IndexOutOfBoundsException
public double[] getData()
public void append(double val)
public void expand(int amount)
Expand the number of array data slots by "amount" elements. Note that "array_size" is the total amount of storage available for data slots. "num_elem" is the number of data slots occupied by data. The bounds over which the array can be indexed is governed by num_elem. Note that after expand() is called the new data elements can be read, but their value is undefined until they are initialized.
public void remove()
public void set_size(int new_size)
Set the number of data elements in the array to a new value (note that this will usually be smaller than the array size, unless a power of two is chosen for "new_size").
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |