001: package uk.org.ponder.doubleutil;
002:
003: /** A simple container class representing a vector of native integers.
004: */
005:
006: public class doubleVector {
007: private double[] doubles;
008: private int size;
009:
010: public class doubleVectorIterator implements doubleIterator {
011: int index = 0;
012:
013: public boolean hasNextDouble() {
014: return index != size;
015: }
016:
017: public void next() {
018: ++index;
019: }
020:
021: public void setDouble(double toset) {
022: doubles[index] = toset;
023: }
024:
025: public double getDouble() {
026: return doubles[index];
027: }
028: }
029:
030: public doubleIterator beginIterator() {
031: return new doubleVectorIterator();
032: }
033:
034: /** Constructs an intVector with the specified initial capacity.
035: * @param initalcapacity The required initial capacity.
036: */
037: public doubleVector(int initialcapacity) {
038: doubles = new double[initialcapacity];
039: size = 0;
040: }
041:
042: public void ensureIndex(int i) {
043: if (i >= size) {
044: setSize(i + 1);
045: }
046: }
047:
048: /** Returns the integer at the specified index.
049: * @param i The index of the required integer.
050: * @return The integer at index <code>i</code>
051: */
052: public double doubleAt(int i) {
053: return doubles[i];
054: }
055:
056: public double doubleAtSafe(int i) {
057: ensureIndex(i);
058: return doubleAt(i);
059: }
060:
061: /** Assigns a value to a member of the vector.
062: * @param i the index to assign a value at.
063: * @param value the value to assign.
064: */
065:
066: public void setDoubleAt(int i, double value) {
067: doubles[i] = value;
068: }
069:
070: public void setDoubleAtSafe(int i, double value) {
071: ensureIndex(i);
072: setDoubleAt(i, value);
073: }
074:
075: /** Returns the current size of this vector.
076: * @return the current size of this vector.
077: */
078: public int size() {
079: return size;
080: }
081:
082: /** Sets the new size of this vector.
083: * @param newsize The new size of the vector.
084: */
085:
086: public void setSize(int newsize) {
087: if (newsize > doubles.length)
088: reallocate(newsize + doubles.length);
089: size = newsize;
090: }
091:
092: private void reallocate(int newsize) {
093: double[] newdoubles = new double[newsize];
094: System.arraycopy(doubles, 0, newdoubles, 0, size);
095: doubles = newdoubles;
096: }
097:
098: /** Appends a new element to the end of this vector, reallocating its storage space if necessary.
099: * @param i The integer value to be appended to the vector.
100: */
101: public void addElement(double i) {
102: if (size == doubles.length) {
103: reallocate(doubles.length * 2);
104: }
105: doubles[size] = i;
106: ++size;
107: }
108:
109: /** Removes the element at the specified index.
110: * @param i The index of the element to be removed from this vector.
111: */
112: public void removeElementAt(int i) {
113: System.arraycopy(doubles, i + 1, doubles, i, size - (i + 1));
114: --size;
115: }
116:
117: /** Removes and returns the final element of the vector.
118: * @return The value of the final element of the vector, which was removed.
119: */
120: public double popElement() {
121: return doubles[--size];
122: }
123:
124: /** Returns the final element of the vector.
125: * @return The value of the final element of the vector.
126: */
127:
128: public double peek() {
129: return doubles[size - 1];
130: }
131:
132: /** Sets this vector to zero size, effectively removing all its elements.
133: */
134: public void clear() {
135: size = 0;
136: }
137:
138: /** Determines whether this vector is empty.
139: * @return <code>true</code> if this vector is empty.
140: */
141: public boolean isEmpty() {
142: return size == 0;
143: }
144:
145: /** Assigns to this intVector the contents of another, overwriting our contents.
146: * @param other The intVector to assign to us.
147: */
148:
149: public void assign(doubleVector other) {
150: doubles = new double[other.doubles.length];
151: System.arraycopy(other.doubles, 0, doubles, 0, doubles.length);
152: size = other.size;
153: }
154:
155: /** Renders this intVector as a String for debugging purposes.
156: * @return the contents of this intVector as a debug string.
157: */
158:
159: public String toString() {
160: StringBuffer togo = new StringBuffer();
161: for (int i = 0; i < size - 1; ++i) {
162: togo.append(doubles[i]).append(' ');
163: }
164: togo.append(doubles[size - 1]);
165: return togo.toString();
166: }
167:
168: /** Returns the contents of this doubleVector as an array of doubles.
169: * @return A double array with the contents of this doubleVector.
170: */
171: public double[] asArray() {
172: double[] togo = new double[size];
173: System.arraycopy(doubles, 0, togo, 0, size);
174: return togo;
175: }
176: }
|