KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > lists > U32Vector


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 32-bit integers (ints). */
8
9 public class U32Vector extends SimpleVector
10   implements Externalizable
11   /* #ifdef JAVA2 */
12   , Comparable JavaDoc
13   /* #endif */
14 {
15   int[] data;
16
17   public U32Vector ()
18   {
19     data = S32Vector.empty;
20   }
21
22   public U32Vector(int size, int value)
23   {
24     int[] array = new int[size];
25     data = array;
26     this.size = size;
27     while (--size >= 0)
28       array[size] = value;
29   }
30
31   public U32Vector(int size)
32   {
33     this.data = new int[size];
34     this.size = size;
35   }
36
37   public U32Vector (int[] data)
38   {
39     this.data = data;
40     size = data.length;
41   }
42
43   public U32Vector(Sequence seq)
44   {
45     data = new int[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     int[] tmp = new int[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 data[index];
72   }
73
74   public final long longAtBuffer(int index)
75   {
76     return (long) data[index] & 0xffffffffL;
77   }
78
79   public final long longAt(int index)
80   {
81     if (index > size)
82       throw new IndexOutOfBoundsException JavaDoc();
83     return longAtBuffer(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     int old = data[index];
101     data[index] = Convert.toIntUnsigned(value);
102     return Convert.toObjectUnsigned(old);
103   }
104
105   public final void setIntAt(int index, int value)
106   {
107     if (index > size)
108       throw new IndexOutOfBoundsException JavaDoc();
109     data[index] = value;
110   }
111
112   public final void setIntAtBuffer(int index, int 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_U16_VALUE;
126   }
127
128   public String JavaDoc getTag() { return "u32"; }
129
130   public boolean consumeNext (int ipos, Consumer out)
131   {
132     int index = ipos >>> 1;
133     if (index >= size)
134       return false;
135     out.writeInt(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.writeInt(data[i]);
149   }
150
151   public int compareTo(Object JavaDoc obj)
152   {
153     return compareToLong(this, (U32Vector) obj);
154   }
155
156   /**
157    * @serialData Write 'size' (using writeInt),
158    * followed by 'size' elements in order (using writeInt).
159    */

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