KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > lists > BitVector


1 // Copyright (c) 2001 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 boolean values. */
8
9 public class BitVector extends SimpleVector implements Externalizable
10 {
11   boolean[] data;
12   protected static boolean[] empty = new boolean[0];
13
14   public BitVector ()
15   {
16     data = empty;
17   }
18
19   public BitVector(int size, boolean value)
20   {
21     boolean[] array = new boolean[size];
22     data = array;
23     this.size = size;
24     if (value)
25       {
26     while (--size >= 0)
27       array[size] = true;
28       }
29   }
30
31   public BitVector(int size)
32   {
33     this.data = new boolean[size];
34     this.size = size;
35   }
36
37   public BitVector (boolean[] data)
38   {
39     this.data = data;
40     size = data.length;
41   }
42
43   public BitVector(Sequence seq)
44   {
45     data = new boolean[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     boolean[] tmp = new boolean[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 boolean booleanAt(int index)
70   {
71     if (index > size)
72       throw new IndexOutOfBoundsException JavaDoc();
73     return data[index];
74   }
75
76   public final boolean booleanAtBuffer(int index)
77   {
78     return data[index];
79   }
80
81   public final Object JavaDoc get(int index)
82   {
83     if (index > size)
84       throw new IndexOutOfBoundsException JavaDoc();
85     return Convert.toObject(data[index]);
86   }
87
88   public final Object JavaDoc getBuffer(int index)
89   {
90     return Convert.toObject(data[index]);
91   }
92
93   public Object JavaDoc setBuffer(int index, Object JavaDoc value)
94   {
95     boolean old = data[index];
96     data[index] = Convert.toBoolean(value);
97     return Convert.toObject(old);
98   }
99
100   public final void setBooleanAt(int index, boolean value)
101   {
102     if (index > size)
103       throw new IndexOutOfBoundsException JavaDoc();
104     data[index] = value;
105   }
106
107   public final void setBooleanAtBuffer(int index, boolean value)
108   {
109     data[index] = value;
110   }
111
112   protected void clearBuffer(int start, int count)
113   {
114     while (--count >= 0)
115       data[start++] = false;
116   }
117
118   public int getElementKind()
119   {
120     return BOOLEAN_VALUE;
121   }
122
123   public String JavaDoc getTag() { return "b"; }
124
125   public boolean consumeNext(int ipos, Consumer out)
126   {
127     int index = ipos >>> 1;
128     if (index >= size)
129       return false;
130     out.writeBoolean(data[index]);
131     return true;
132   }
133
134   public void consumePosRange(int iposStart, int iposEnd, Consumer out)
135   {
136     if (out.ignoring())
137       return;
138     int i = iposStart >>> 1;
139     int end = iposEnd >>> 1;
140     for (; i < end; i++)
141       out.writeBoolean(data[i]);
142   }
143
144   /**
145    * @serialData Write 'size' (using writeInt),
146    * followed by 'size' elements in order (using writeBoolean).
147    */

148   public void writeExternal(ObjectOutput out) throws IOException
149   {
150     int size = this.size;
151     out.writeInt(size);
152     for (int i = 0; i < size; i++)
153       out.writeBoolean(data[i]);
154   }
155
156   public void readExternal(ObjectInput in)
157     throws IOException, ClassNotFoundException JavaDoc
158   {
159     int size = in.readInt();
160     boolean[] data = new boolean[size];
161     for (int i = 0; i < size; i++)
162       data[i] = in.readBoolean();
163     this.data = data;
164     this.size = size;
165   }
166 }
167
Popular Tags