KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > lists > U64Vector


1 // Copyright (c) 2001, 2002 Per M.A. Bothner and Brainfood Inc.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.lists;
5 import java.io.*;
6
7 /** Simple adjustable-length vector of unsigned 64-bit integers (longs). */
8
9 public class U64Vector extends SimpleVector
10   implements Externalizable
11   /* #ifdef JAVA2 */
12   , Comparable JavaDoc
13   /* #endif */
14 {
15   long[] data;
16
17   public U64Vector ()
18   {
19     data = S64Vector.empty;
20   }
21
22   public U64Vector(int size, long value)
23   {
24     long[] array = new long[size];
25     data = array;
26     this.size = size;
27     while (--size >= 0)
28       array[size] = value;
29   }
30
31   public U64Vector(int size)
32   {
33     this.data = new long[size];
34     this.size = size;
35   }
36
37   public U64Vector (long[] data)
38   {
39     this.data = data;
40     size = data.length;
41   }
42
43   public U64Vector(Sequence seq)
44   {
45     data = new long[seq.size()];
46     addAll(seq);
47   }
48
49   /** Get the allocated length of the data buffer. */
50   public int getBufferLength()
51   {
52     return data.length;
53   }
54
55   public void setBufferLength(int length)
56   {
57     int oldLength = data.length;
58     if (oldLength != length)
59       {
60     long[] tmp = new long[length];
61     System.arraycopy(data, 0, tmp, 0,
62              oldLength < length ? oldLength : length);
63     data = tmp;
64       }
65   }
66
67   protected Object JavaDoc getBuffer() { return data; }
68
69   public final int intAtBuffer(int index)
70   {
71     return (int) data[index];
72   }
73
74   public final long longAt(int index)
75   {
76     if (index > size)
77       throw new IndexOutOfBoundsException JavaDoc();
78     return data[index];
79   }
80
81   public final long longAtBuffer(int index)
82   {
83     return data[index];
84   }
85
86   public final Object JavaDoc get(int index)
87   {
88     if (index > size)
89       throw new IndexOutOfBoundsException JavaDoc();
90     return Convert.toObjectUnsigned(data[index]);
91   }
92
93   public final Object JavaDoc getBuffer(int index)
94   {
95     return Convert.toObjectUnsigned(data[index]);
96   }
97
98   public Object JavaDoc setBuffer(int index, Object JavaDoc value)
99   {
100     long old = data[index];
101     data[index] = Convert.toLongUnsigned(value);
102     return Convert.toObjectUnsigned(old);
103   }
104
105   public final void setLongAt(int index, long value)
106   {
107     if (index > size)
108       throw new IndexOutOfBoundsException JavaDoc();
109     data[index] = value;
110   }
111
112   public final void setLongAtBuffer(int index, long value)
113   {
114     data[index] = value;
115   }
116
117   protected void clearBuffer(int start, int count)
118   {
119     while (--count >= 0)
120       data[start++] = 0;
121   }
122
123   public int getElementKind()
124   {
125     return INT_U64_VALUE;
126   }
127
128   public String JavaDoc getTag() { return "u64"; }
129
130   public boolean consumeNext (int ipos, Consumer out)
131   {
132     int index = ipos >>> 1;
133     if (index >= size)
134       return false;
135     out.writeLong(data[index]);
136     return true;
137   }
138
139   public void consumePosRange (int iposStart, int iposEnd, Consumer out)
140   {
141     if (out.ignoring())
142       return;
143     int i = iposStart >>> 1;
144     int end = iposEnd >>> 1;
145     if (end > size)
146       end = size;
147     for (; i < end; i++)
148       out.writeLong(data[i]);
149   }
150
151   public int compareTo(Object JavaDoc obj)
152   {
153     U64Vector vec2 = (U64Vector) obj;
154     long[] arr1 = data;
155     long[] arr2 = vec2.data;
156     int n1 = size;
157     int n2 = vec2.size;
158     int n = n1 > n2 ? n2 : n1;
159     for (int i = 0; i < n; i++)
160       {
161     long v1 = arr1[i];
162     long v2 = arr2[i];
163     if (v1 != v2)
164       return (v1^0x8000000000000000L) > (v2^0x8000000000000000L) ? 1 : -1;
165       }
166     return n1 - n2;
167   }
168
169   /**
170    * @serialData Write 'size' (using writeInt),
171    * followed by 'size' elements in order (using writeLong).
172    */

173   public void writeExternal(ObjectOutput out) throws IOException
174   {
175     int size = this.size;
176     out.writeInt(size);
177     for (int i = 0; i < size; i++)
178       out.writeLong(data[i]);
179   }
180
181   public void readExternal(ObjectInput in)
182     throws IOException, ClassNotFoundException JavaDoc
183   {
184     int size = in.readInt();
185     long[] data = new long[size];
186     for (int i = 0; i < size; i++)
187       data[i] = in.readLong();
188     this.data = data;
189     this.size = size;
190   }
191 }
192
Popular Tags