2 |
26 Feb 07 |
jari |
1 |
/* |
2 |
26 Feb 07 |
jari |
Copyright @ 1999-2003, The Institute for Genomic Research (TIGR). |
2 |
26 Feb 07 |
jari |
All rights reserved. |
2 |
26 Feb 07 |
jari |
4 |
*/ |
2 |
26 Feb 07 |
jari |
5 |
/* |
2 |
26 Feb 07 |
jari |
* $RCSfile: FloatArray.java,v $ |
2 |
26 Feb 07 |
jari |
* $Revision: 1.1.1.1 $ |
2 |
26 Feb 07 |
jari |
* $Date: 2003/08/21 21:04:23 $ |
2 |
26 Feb 07 |
jari |
* $Author: braisted $ |
2 |
26 Feb 07 |
jari |
* $State: Exp $ |
2 |
26 Feb 07 |
jari |
11 |
*/ |
2 |
26 Feb 07 |
jari |
12 |
package org.tigr.util; |
2 |
26 Feb 07 |
jari |
13 |
|
2 |
26 Feb 07 |
jari |
14 |
public class FloatArray { |
2 |
26 Feb 07 |
jari |
15 |
|
2 |
26 Feb 07 |
jari |
16 |
private float[] elementData; |
2 |
26 Feb 07 |
jari |
17 |
private int size; |
2 |
26 Feb 07 |
jari |
18 |
|
2 |
26 Feb 07 |
jari |
19 |
/** |
2 |
26 Feb 07 |
jari |
* Constructs an empty array with the specified initial capacity. |
2 |
26 Feb 07 |
jari |
21 |
*/ |
2 |
26 Feb 07 |
jari |
22 |
public FloatArray(int initialCapacity) { |
2 |
26 Feb 07 |
jari |
23 |
this.elementData = new float[initialCapacity]; |
2 |
26 Feb 07 |
jari |
24 |
} |
2 |
26 Feb 07 |
jari |
25 |
|
2 |
26 Feb 07 |
jari |
26 |
/** |
2 |
26 Feb 07 |
jari |
* Returns an array containing all of the floats in this container |
2 |
26 Feb 07 |
jari |
* in the correct order. |
2 |
26 Feb 07 |
jari |
29 |
*/ |
2 |
26 Feb 07 |
jari |
30 |
public float[] toArray() { |
2 |
26 Feb 07 |
jari |
31 |
float[] result = new float[size]; |
2 |
26 Feb 07 |
jari |
32 |
System.arraycopy(elementData, 0, result, 0, size); |
2 |
26 Feb 07 |
jari |
33 |
return result; |
2 |
26 Feb 07 |
jari |
34 |
} |
2 |
26 Feb 07 |
jari |
35 |
|
2 |
26 Feb 07 |
jari |
36 |
/** |
2 |
26 Feb 07 |
jari |
* Returns the float value at the specified position in this container. |
2 |
26 Feb 07 |
jari |
38 |
*/ |
2 |
26 Feb 07 |
jari |
39 |
public float get(int index) { |
2 |
26 Feb 07 |
jari |
40 |
RangeCheck(index); |
2 |
26 Feb 07 |
jari |
41 |
return elementData[index]; |
2 |
26 Feb 07 |
jari |
42 |
} |
2 |
26 Feb 07 |
jari |
43 |
|
2 |
26 Feb 07 |
jari |
44 |
/** |
2 |
26 Feb 07 |
jari |
* Replaces the float value at the specified position in this container with |
2 |
26 Feb 07 |
jari |
* the specified one. |
2 |
26 Feb 07 |
jari |
47 |
*/ |
2 |
26 Feb 07 |
jari |
48 |
public void set(int index, float value) { |
2 |
26 Feb 07 |
jari |
49 |
RangeCheck(index); |
2 |
26 Feb 07 |
jari |
50 |
elementData[index] = value; |
2 |
26 Feb 07 |
jari |
51 |
} |
2 |
26 Feb 07 |
jari |
52 |
|
2 |
26 Feb 07 |
jari |
53 |
/** |
2 |
26 Feb 07 |
jari |
* Appends the specified value to the end of this container. |
2 |
26 Feb 07 |
jari |
55 |
*/ |
2 |
26 Feb 07 |
jari |
56 |
public boolean add(float i) { |
2 |
26 Feb 07 |
jari |
57 |
ensureCapacity(size + 1); |
2 |
26 Feb 07 |
jari |
58 |
elementData[size++] = i; |
2 |
26 Feb 07 |
jari |
59 |
return true; |
2 |
26 Feb 07 |
jari |
60 |
} |
2 |
26 Feb 07 |
jari |
61 |
|
2 |
26 Feb 07 |
jari |
62 |
/** |
2 |
26 Feb 07 |
jari |
* Check if the given index is in range. If not, throw an appropriate |
2 |
26 Feb 07 |
jari |
* runtime exception. |
2 |
26 Feb 07 |
jari |
65 |
*/ |
2 |
26 Feb 07 |
jari |
66 |
private void RangeCheck(int index) { |
2 |
26 Feb 07 |
jari |
67 |
if (index >= size || index < 0) |
2 |
26 Feb 07 |
jari |
68 |
throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size); |
2 |
26 Feb 07 |
jari |
69 |
} |
2 |
26 Feb 07 |
jari |
70 |
|
2 |
26 Feb 07 |
jari |
71 |
/** |
2 |
26 Feb 07 |
jari |
* Increases the capacity of this <tt>FloatArray</tt> instance, if |
2 |
26 Feb 07 |
jari |
* necessary, to ensure that it can hold at least the number of floats |
2 |
26 Feb 07 |
jari |
* specified by the minimum capacity argument. |
2 |
26 Feb 07 |
jari |
75 |
* |
2 |
26 Feb 07 |
jari |
* @param minCapacity the desired minimum capacity. |
2 |
26 Feb 07 |
jari |
77 |
*/ |
2 |
26 Feb 07 |
jari |
78 |
private void ensureCapacity(int minCapacity) { |
2 |
26 Feb 07 |
jari |
79 |
int oldCapacity = elementData.length; |
2 |
26 Feb 07 |
jari |
80 |
if (minCapacity > oldCapacity) { |
2 |
26 Feb 07 |
jari |
81 |
float[] oldData = elementData; |
2 |
26 Feb 07 |
jari |
82 |
int newCapacity = (oldCapacity * 3)/2 + 1; |
2 |
26 Feb 07 |
jari |
83 |
if (newCapacity < minCapacity) |
2 |
26 Feb 07 |
jari |
84 |
newCapacity = minCapacity; |
2 |
26 Feb 07 |
jari |
85 |
elementData = new float[newCapacity]; |
2 |
26 Feb 07 |
jari |
86 |
System.arraycopy(oldData, 0, elementData, 0, size); |
2 |
26 Feb 07 |
jari |
87 |
} |
2 |
26 Feb 07 |
jari |
88 |
} |
2 |
26 Feb 07 |
jari |
89 |
|
2 |
26 Feb 07 |
jari |
90 |
} |