001: package uk.org.ponder.intutil;
002:
003: import java.util.Arrays;
004:
005: /** A simple container class representing a vector of native integers.
006: */
007:
008: public class intVector implements Comparable {
009: private int[] ints;
010: private int size;
011:
012: public class intVectorIterator implements intIterator {
013: int index = 0;
014:
015: public boolean valid() {
016: return index < size;
017: }
018:
019: public void next() {
020: ++index;
021: }
022:
023: public void setInt(int toset) {
024: ints[index] = toset;
025: }
026:
027: public int getInt() {
028: return ints[index];
029: }
030:
031: public boolean equals(Object other) {
032: intVectorIterator ivi = (intVectorIterator) other;
033: return //ivi.intVector.this == intVector.this && // QQQQQ how to express this?
034: ivi.index == index;
035: }
036: }
037:
038: public intIterator beginIterator() {
039: return new intVectorIterator();
040: }
041:
042: /** Constructs an intVector with the specified initial capacity.
043: * @param initalcapacity The required initial capacity.
044: */
045: public intVector(int initialcapacity) {
046: ints = new int[initialcapacity];
047: size = 0;
048: }
049:
050: public intVector(int[] array) {
051: this .ints = array;
052: this .size = array.length;
053: }
054:
055: public void ensureIndex(int index) {
056: if (index >= size) {
057: int oldsize = size;
058: setSize(index + 1);
059: for (int i = oldsize; i < index + 1; ++i) {
060: ints[i] = 0;
061: }
062: }
063: }
064:
065: public int[] getBackingStore() {
066: return ints;
067: }
068:
069: /** Returns the integer at the specified index.
070: * @param i The index of the required integer.
071: * @return The integer at index <code>i</code>
072: */
073: public int intAt(int i) {
074: return ints[i];
075: }
076:
077: public int intAtSafe(int i) {
078: ensureIndex(i);
079: return intAt(i);
080: }
081:
082: /** Assigns a value to a member of the vector.
083: * @param i the index to assign a value at.
084: * @param value the value to assign.
085: */
086:
087: public void setIntAt(int i, int value) {
088: if (i < 0 || i > size) {
089: throw new ArrayIndexOutOfBoundsException("Index " + i
090: + " out of bounds [0," + size + ") in setIntAt");
091: }
092: ints[i] = value;
093: }
094:
095: /** Assigns a value to a member of the vector, expanding the vector if the supplied index exceeds
096: * the vector bounds.
097: * @param i the index to assign a value at
098: * @param value the value to assign
099: */
100: public void setIntAtSafe(int i, int value) {
101: ensureIndex(i);
102: setIntAt(i, value);
103: }
104:
105: /** Returns the current size of this vector.
106: * @return the current size of this vector.
107: */
108: public int size() {
109: return size;
110: }
111:
112: /** Sets the new size of this vector.
113: * @param newsize The new size of the vector.
114: */
115:
116: public void setSize(int newsize) {
117: if (newsize > ints.length)
118: reallocate(newsize + ints.length);
119: size = newsize;
120: }
121:
122: private void reallocate(int newsize) {
123: int[] newints = new int[newsize];
124: System.arraycopy(ints, 0, newints, 0, size);
125: ints = newints;
126: }
127:
128: /** Appends a new element to the end of this vector, reallocating its storage space if necessary.
129: * @param i The integer value to be appended to the vector.
130: */
131: public void addElement(int i) {
132: if (size == ints.length) {
133: reallocate(ints.length * 2);
134: }
135: ints[size] = i;
136: ++size;
137: }
138:
139: /** Appends all the elements from the supplied intVector */
140: public void addAll(intVector toadd) {
141: int newsize = size + toadd.size;
142: if (newsize >= ints.length) {
143: reallocate(newsize * 2);
144: }
145: System.arraycopy(toadd.ints, 0, ints, size, toadd.size);
146: size = newsize;
147: }
148:
149: /** Inserts the supplied value at the index position specified - following elements
150: * will be shifted to the right and the vector capacity expanded if necessary.
151: * @param i The index to add the value at.
152: * @param value The value to be added.
153: */
154: public void insertElementAt(int i, int value) {
155: if (size == ints.length) {
156: reallocate(ints.length * 2);
157: }
158:
159: System.arraycopy(ints, i, ints, i + 1, size - i);
160: ints[i] = value;
161: ++size;
162: }
163:
164: /** Removes the element at the specified index.
165: * @param i The index of the element to be removed from this vector.
166: */
167: public int removeElementAt(int i) {
168: int togo = ints[i];
169: System.arraycopy(ints, i + 1, ints, i, size - (i + 1));
170: --size;
171: return togo;
172: }
173:
174: /** Removes and returns the final element of the vector.
175: * @return The value of the final element of the vector, which was removed.
176: */
177: public int popElement() {
178: return ints[--size];
179: }
180:
181: /** Returns the final element of the vector.
182: * @return The value of the final element of the vector.
183: */
184:
185: public int peek() {
186: return ints[size - 1];
187: }
188:
189: /** Sets this vector to zero size, effectively removing all its elements.
190: */
191: public void clear() {
192: size = 0;
193: }
194:
195: /** Determines whether this vector is empty.
196: * @return <code>true</code> if this vector is empty.
197: */
198: public boolean isEmpty() {
199: return size == 0;
200: }
201:
202: /** Searches for the supplied value within this vector.
203: * @param tofind The value to be searched for.
204: * @return The first index at which this value occurs, or -1 if it does not appear.
205: */
206: public int findInt(int tofind) {
207: for (int i = 0; i < size; ++i) {
208: if (ints[i] == tofind)
209: return i;
210: }
211: return -1;
212: }
213:
214: /** Assigns to this intVector the contents of another, overwriting our contents.
215: * @param other The intVector to assign to us.
216: */
217:
218: public void assign(intVector other) {
219: ints = new int[other.ints.length];
220: System.arraycopy(other.ints, 0, ints, 0, ints.length);
221: size = other.size;
222: }
223:
224: /** Returns the contents of this intVector as an array of ints.
225: * @return A int array with the contents of this intVector.
226: */
227: public int[] asArray() {
228: int[] togo = new int[size];
229: System.arraycopy(ints, 0, togo, 0, size);
230: return togo;
231: }
232:
233: public intVector copy() {
234: intVector togo = new intVector(ints.length);
235: System.arraycopy(ints, 0, togo.ints, 0, ints.length);
236: togo.size = size;
237: return togo;
238: }
239:
240: public void sort() {
241: Arrays.sort(ints, 0, size);
242: // if (size == 0) return;
243: // int last = ints[0];
244: // for (int i = 1; i < size; ++ i) {
245: // int next = ints[i];
246: // Assertions.expect(next >= last, "Sort error!");
247: // last = next;
248: // }
249: }
250:
251: /** Renders this intVector as a String for debugging purposes.
252: * @return the contents of this intVector as a debug string.
253: */
254:
255: public String toString() {
256: StringBuffer togo = new StringBuffer();
257: for (int i = 0; i < size - 1; ++i) {
258: togo.append(ints[i]).append(' ');
259: }
260: if (size > 0) {
261: togo.append(ints[size - 1]);
262: }
263: return togo.toString();
264: }
265:
266: public int compareTo(Object othero) {
267: intVector other = (intVector) othero;
268: return Algorithms.lexicalCompare(ints, size, other.ints,
269: other.size);
270: }
271:
272: public boolean equals(Object othero) {
273: intVector other = (intVector) othero;
274: if (size != other.size)
275: return false;
276: for (int i = 0; i < size; ++i) {
277: if (ints[i] != other.ints[i])
278: return false;
279: }
280: return true;
281: }
282:
283: }
|