double_vec
Class double_vec

java.lang.Object
  |
  +--double_vec.double_vec

public class double_vec
extends java.lang.Object

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 template. The double_vec class supports a growable array to which elements can be continuously added.

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:

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.

Usage

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).

Copyright and Use

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
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

StartArraySize

private static final int StartArraySize

num_elem

private int num_elem

array_size

private int array_size

array

double[] array
Constructor Detail

double_vec

public double_vec()
Allocate an array whose initial size is StartArraySize
Method Detail

twice

private void twice()

Double the amount of memory allocated for the array. Return true if memory allocation succeeded, false otherwise.


length

public int length()
Return the number of elements currently in the array

setElementAt

public void setElementAt(double val,
                         int ix)
                  throws java.lang.IndexOutOfBoundsException
Assign array element ix the value val.

elementAt

public double elementAt(int ix)
                 throws java.lang.IndexOutOfBoundsException
Return the array element at index ix. If ix is out of range an IndexOutOfBoundsException will be thrown.

getData

public double[] getData()
Return a reference to the double_vec array.

append

public void append(double val)
Append an item to the end of the array

expand

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.


remove

public void remove()
Remove one item from the end of the array.

set_size

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").