KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > lists > S64Vector


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

161   public void writeExternal(ObjectOutput out) throws IOException
162   {
163     int size = this.size;
164     out.writeInt(size);
165     for (int i = 0; i < size; i++)
166       out.writeLong(data[i]);
167   }
168
169   public void readExternal(ObjectInput in)
170     throws IOException, ClassNotFoundException JavaDoc
171   {
172     int size = in.readInt();
173     long[] data = new long[size];
174     for (int i = 0; i < size; i++)
175       data[i] = in.readLong();
176     this.data = data;
177     this.size = size;
178   }
179 }
180
Popular Tags