001: // Copyright (c) 2001, 2002 Per M.A. Bothner and Brainfood Inc.
002: // This is free software; for terms and warranty disclaimer see ./COPYING.
003:
004: package gnu.lists;
005:
006: import java.io.*;
007:
008: /** Simple adjustable-length vector of signed 32-bit integers (ints). */
009:
010: public class S32Vector extends SimpleVector implements Externalizable
011: /* #ifdef JAVA2 */
012: , Comparable
013: /* #endif */
014: {
015: int[] data;
016: protected static int[] empty = new int[0];
017:
018: public S32Vector() {
019: data = empty;
020: }
021:
022: public S32Vector(int size, int value) {
023: int[] array = new int[size];
024: data = array;
025: this .size = size;
026: while (--size >= 0)
027: array[size] = value;
028: }
029:
030: public S32Vector(int size) {
031: this .data = new int[size];
032: this .size = size;
033: }
034:
035: public S32Vector(int[] data) {
036: this .data = data;
037: size = data.length;
038: }
039:
040: public S32Vector(Sequence seq) {
041: data = new int[seq.size()];
042: addAll(seq);
043: }
044:
045: /** Get the allocated length of the data buffer. */
046: public int getBufferLength() {
047: return data.length;
048: }
049:
050: public void setBufferLength(int length) {
051: int oldLength = data.length;
052: if (oldLength != length) {
053: int[] tmp = new int[length];
054: System.arraycopy(data, 0, tmp, 0,
055: oldLength < length ? oldLength : length);
056: data = tmp;
057: }
058: }
059:
060: protected Object getBuffer() {
061: return data;
062: }
063:
064: public final int intAt(int index) {
065: if (index > size)
066: throw new IndexOutOfBoundsException();
067: return data[index];
068: }
069:
070: public final int intAtBuffer(int index) {
071: return data[index];
072: }
073:
074: public final Object get(int index) {
075: if (index > size)
076: throw new IndexOutOfBoundsException();
077: return Convert.toObject(data[index]);
078: }
079:
080: public final Object getBuffer(int index) {
081: return Convert.toObject(data[index]);
082: }
083:
084: public Object setBuffer(int index, Object value) {
085: int old = data[index];
086: data[index] = Convert.toInt(value);
087: return Convert.toObject(old);
088: }
089:
090: public final void setIntAt(int index, int value) {
091: if (index > size)
092: throw new IndexOutOfBoundsException();
093: data[index] = value;
094: }
095:
096: public final void setIntAtBuffer(int index, int value) {
097: data[index] = value;
098: }
099:
100: protected void clearBuffer(int start, int count) {
101: while (--count >= 0)
102: data[start++] = 0;
103: }
104:
105: public int getElementKind() {
106: return INT_S32_VALUE;
107: }
108:
109: public String getTag() {
110: return "s32";
111: }
112:
113: public boolean consumeNext(int ipos, Consumer out) {
114: int index = ipos >>> 1;
115: if (index >= size)
116: return false;
117: out.writeInt(data[index]);
118: return true;
119: }
120:
121: public void consumePosRange(int iposStart, int iposEnd, Consumer out) {
122: if (out.ignoring())
123: return;
124: int i = iposStart >>> 1;
125: int end = iposEnd >>> 1;
126: if (end > size)
127: end = size;
128: for (; i < end; i++)
129: out.writeInt(data[i]);
130: }
131:
132: public int compareTo(Object obj) {
133: return compareToInt(this , (S32Vector) obj);
134: }
135:
136: /**
137: * @serialData Write 'size' (using writeInt),
138: * followed by 'size' elements in order (using writeInt).
139: */
140: public void writeExternal(ObjectOutput out) throws IOException {
141: int size = this .size;
142: out.writeInt(size);
143: for (int i = 0; i < size; i++)
144: out.writeInt(data[i]);
145: }
146:
147: public void readExternal(ObjectInput in) throws IOException,
148: ClassNotFoundException {
149: int size = in.readInt();
150: int[] data = new int[size];
151: for (int i = 0; i < size; i++)
152: data[i] = in.readInt();
153: this.data = data;
154: this.size = size;
155: }
156: }
|