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 unsigned 64-bit integers (longs). */
009:
010: public class U64Vector extends SimpleVector implements Externalizable
011: /* #ifdef JAVA2 */
012: , Comparable
013: /* #endif */
014: {
015: long[] data;
016:
017: public U64Vector() {
018: data = S64Vector.empty;
019: }
020:
021: public U64Vector(int size, long value) {
022: long[] array = new long[size];
023: data = array;
024: this .size = size;
025: while (--size >= 0)
026: array[size] = value;
027: }
028:
029: public U64Vector(int size) {
030: this .data = new long[size];
031: this .size = size;
032: }
033:
034: public U64Vector(long[] data) {
035: this .data = data;
036: size = data.length;
037: }
038:
039: public U64Vector(Sequence seq) {
040: data = new long[seq.size()];
041: addAll(seq);
042: }
043:
044: /** Get the allocated length of the data buffer. */
045: public int getBufferLength() {
046: return data.length;
047: }
048:
049: public void setBufferLength(int length) {
050: int oldLength = data.length;
051: if (oldLength != length) {
052: long[] tmp = new long[length];
053: System.arraycopy(data, 0, tmp, 0,
054: oldLength < length ? oldLength : length);
055: data = tmp;
056: }
057: }
058:
059: protected Object getBuffer() {
060: return data;
061: }
062:
063: public final int intAtBuffer(int index) {
064: return (int) data[index];
065: }
066:
067: public final long longAt(int index) {
068: if (index > size)
069: throw new IndexOutOfBoundsException();
070: return data[index];
071: }
072:
073: public final long longAtBuffer(int index) {
074: return data[index];
075: }
076:
077: public final Object get(int index) {
078: if (index > size)
079: throw new IndexOutOfBoundsException();
080: return Convert.toObjectUnsigned(data[index]);
081: }
082:
083: public final Object getBuffer(int index) {
084: return Convert.toObjectUnsigned(data[index]);
085: }
086:
087: public Object setBuffer(int index, Object value) {
088: long old = data[index];
089: data[index] = Convert.toLongUnsigned(value);
090: return Convert.toObjectUnsigned(old);
091: }
092:
093: public final void setLongAt(int index, long value) {
094: if (index > size)
095: throw new IndexOutOfBoundsException();
096: data[index] = value;
097: }
098:
099: public final void setLongAtBuffer(int index, long value) {
100: data[index] = value;
101: }
102:
103: protected void clearBuffer(int start, int count) {
104: while (--count >= 0)
105: data[start++] = 0;
106: }
107:
108: public int getElementKind() {
109: return INT_U64_VALUE;
110: }
111:
112: public String getTag() {
113: return "u64";
114: }
115:
116: public boolean consumeNext(int ipos, Consumer out) {
117: int index = ipos >>> 1;
118: if (index >= size)
119: return false;
120: out.writeLong(data[index]);
121: return true;
122: }
123:
124: public void consumePosRange(int iposStart, int iposEnd, Consumer out) {
125: if (out.ignoring())
126: return;
127: int i = iposStart >>> 1;
128: int end = iposEnd >>> 1;
129: if (end > size)
130: end = size;
131: for (; i < end; i++)
132: out.writeLong(data[i]);
133: }
134:
135: public int compareTo(Object obj) {
136: U64Vector vec2 = (U64Vector) obj;
137: long[] arr1 = data;
138: long[] arr2 = vec2.data;
139: int n1 = size;
140: int n2 = vec2.size;
141: int n = n1 > n2 ? n2 : n1;
142: for (int i = 0; i < n; i++) {
143: long v1 = arr1[i];
144: long v2 = arr2[i];
145: if (v1 != v2)
146: return (v1 ^ 0x8000000000000000L) > (v2 ^ 0x8000000000000000L) ? 1
147: : -1;
148: }
149: return n1 - n2;
150: }
151:
152: /**
153: * @serialData Write 'size' (using writeInt),
154: * followed by 'size' elements in order (using writeLong).
155: */
156: public void writeExternal(ObjectOutput out) throws IOException {
157: int size = this .size;
158: out.writeInt(size);
159: for (int i = 0; i < size; i++)
160: out.writeLong(data[i]);
161: }
162:
163: public void readExternal(ObjectInput in) throws IOException,
164: ClassNotFoundException {
165: int size = in.readInt();
166: long[] data = new long[size];
167: for (int i = 0; i < size; i++)
168: data[i] = in.readLong();
169: this.data = data;
170: this.size = size;
171: }
172: }
|