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 64-bit integers (longs). */
009:
010: public class S64Vector extends SimpleVector implements Externalizable
011: /* #ifdef JAVA2 */
012: , Comparable
013: /* #endif */
014: {
015: long[] data;
016: protected static long[] empty = new long[0];
017:
018: public S64Vector() {
019: data = empty;
020: }
021:
022: public S64Vector(int size, long value) {
023: long[] array = new long[size];
024: data = array;
025: this .size = size;
026: while (--size >= 0)
027: array[size] = value;
028: }
029:
030: public S64Vector(int size) {
031: this .data = new long[size];
032: this .size = size;
033: }
034:
035: public S64Vector(long[] data) {
036: this .data = data;
037: size = data.length;
038: }
039:
040: public S64Vector(Sequence seq) {
041: data = new long[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: long[] tmp = new long[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 long longAt(int index) {
065: if (index > size)
066: throw new IndexOutOfBoundsException();
067: return data[index];
068: }
069:
070: public final long longAtBuffer(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 final int intAtBuffer(int index) {
085: return (int) data[index];
086: }
087:
088: public Object setBuffer(int index, Object value) {
089: long old = data[index];
090: data[index] = Convert.toLong(value);
091: return Convert.toObject(old);
092: }
093:
094: public final void setLongAt(int index, long value) {
095: if (index > size)
096: throw new IndexOutOfBoundsException();
097: data[index] = value;
098: }
099:
100: public final void setLongAtBuffer(int index, long value) {
101: data[index] = value;
102: }
103:
104: protected void clearBuffer(int start, int count) {
105: while (--count >= 0)
106: data[start++] = 0;
107: }
108:
109: public int getElementKind() {
110: return INT_S64_VALUE;
111: }
112:
113: public String getTag() {
114: return "s64";
115: }
116:
117: public boolean consumeNext(int ipos, Consumer out) {
118: int index = ipos >>> 1;
119: if (index >= size)
120: return false;
121: out.writeLong(data[index]);
122: return true;
123: }
124:
125: public void consumePosRange(int iposStart, int iposEnd, Consumer out) {
126: if (out.ignoring())
127: return;
128: int i = iposStart >>> 1;
129: int end = iposEnd >>> 1;
130: if (end > size)
131: end = size;
132: for (; i < end; i++)
133: out.writeLong(data[i]);
134: }
135:
136: public int compareTo(Object obj) {
137: return compareToLong(this , (S64Vector) obj);
138: }
139:
140: /**
141: * @serialData Write 'size' (using writeInt),
142: * followed by 'size' elements in order (using writeLong).
143: */
144: public void writeExternal(ObjectOutput out) throws IOException {
145: int size = this .size;
146: out.writeInt(size);
147: for (int i = 0; i < size; i++)
148: out.writeLong(data[i]);
149: }
150:
151: public void readExternal(ObjectInput in) throws IOException,
152: ClassNotFoundException {
153: int size = in.readInt();
154: long[] data = new long[size];
155: for (int i = 0; i < size; i++)
156: data[i] = in.readLong();
157: this.data = data;
158: this.size = size;
159: }
160: }
|