KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Util > DFSRChecker > state > BitList


1 /*
2  * $Id: BitList.java,v 1.2 2005/07/08 12:04:12 kofron Exp $
3  *
4  * Copyright 2004
5  * Distributed Systems Research Group
6  * Department of Software Engineering
7  * Faculty of Mathematics and Physics
8  * Charles University, Prague
9  *
10  * Copyright 2005
11  * Formal Methods In Software Engineering Group
12  * Institute of Computer Science
13  * Academy of Sciences of the Czech Republic
14  *
15  * This code was developed by Jan Kofron <kofron@nenya.ms.mff.cuni.cz>
16  */

17
18
19 package SOFA.SOFAnode.Util.DFSRChecker.state;
20
21 /**
22  * This class implements a simple bit list.
23  *
24  */

25 public class BitList implements Comparable JavaDoc {
26     /**
27      * Creates a new instance of BitList.
28      * @param size size of the bitlist
29      */

30     public BitList(int size) {
31         buffer = new int[1];
32         max = size;
33         length = 0;
34     }
35     
36     /**
37      * Sets a bit on position to the specified value.
38      * @param position position of the bit to be set
39      * @param bit value (true = 1, false = 0);
40      */

41     public void set(int position, boolean bit) {
42         int idx = position >> 5;
43         int off = position & 0x1F;
44         
45         while (idx >= buffer.length) {
46             // need for reallocation
47
int size = buffer.length * 2;
48             
49             /*
50             if (size > max)
51                 size = max;
52             */

53             int[] newbuffer = new int[size];
54             
55             for (int i = 0; i < buffer.length; ++i)
56                 newbuffer[i] = buffer[i];
57             
58             buffer = newbuffer;
59         }
60         
61         if (bit)
62             buffer[idx] = buffer[idx] | (1 << (31 - off));
63         else
64             buffer[idx] = buffer[idx] & ~(1 << (31 - off));
65         
66         if (position >= length)
67             length = position + 1;
68     }
69     
70     /**
71      * Gets the value of the bit at specified position.
72      * @param position position of the bit
73      * @return the value of the bit at the specified position
74      */

75     public boolean get(int position) {
76         int idx = position >> 5;
77         int off = position & 0x1F;
78         
79         return ((buffer[idx] & (1 << off)) == 0) ? true : false;
80     }
81     
82     /**
83      * Compare to other BitList.
84      * @param o the other bitlist
85      * @return @see java.lang.Object.compareTo(Object)
86      */

87     public int compareTo(Object JavaDoc o) {
88         if (!(o instanceof BitList))
89             return -1;
90         
91         BitList other = (BitList)o;
92         
93         int min = this.buffer.length < other.buffer.length ? this.buffer.length : other.buffer.length;
94         
95         for (int i = 0; i < min; ++i) {
96             if (this.buffer[i] < other.buffer[i])
97                return -1;
98             if (this.buffer[i] > other.buffer[i])
99                 return 1;
100         }
101         
102         if (this.length < other.length)
103             return -1;
104
105         if (this.length > other.length)
106             return 1;
107         
108         return 0;
109     }
110     
111     
112     /**
113      * @see java.lang.Object#equals(Object)
114      */

115     public boolean equals(Object JavaDoc o) {
116         if (!(o instanceof BitList))
117             return false;
118         
119         BitList other = (BitList)o;
120         
121         if (this.length != other.length)
122             return false;
123
124         int min = this.buffer.length;
125         
126         for (int i = 0; i < min; ++i)
127             if (this.buffer[i] != other.buffer[i])
128                return false;
129
130         return true;
131     }
132     
133     /**
134      * @see java.lang.Object#hashCode()
135      */

136     public int hashCode() {
137         int result = 0;
138         
139         for (int i = 0; i < buffer.length; ++i)
140             result = result ^ (buffer[i] << i);
141         
142         return result;
143     }
144     
145     /**
146      * Adds the given signature to this.
147      * @param second the signature to be concatenated.
148      */

149     public void concat(BitList second) {
150         int thisidx = this.length >> 5;
151         int thisoff = this.length & 0x1F;
152         int secondleft = second.length;
153         
154         
155         int needed = ((this.length + second.length) >> 5) + ((((this.length + second.length) & 0x1F) != 0) ? 1 : 0);
156         
157
158         while (needed >= buffer.length) {
159             // need for reallocation
160
int size = (buffer.length * 2) > needed ? (buffer.length * 2) : needed + 1;
161             /*
162             if (size > max)
163                 size = max;
164             */

165             int[] newbuffer = new int[size];
166             
167             for (int i = 0; i < buffer.length; ++i)
168                 newbuffer[i] = buffer[i];
169             
170             buffer = newbuffer;
171         }
172         
173         
174         int i = 0;
175         
176         while (secondleft > 0) {
177             this.buffer[thisidx + i] |= (second.buffer[i] >>> thisoff);
178             if ((secondleft - 32 + thisoff > 0) && (thisoff > 0))
179                 this.buffer[thisidx + i + 1] |= (second.buffer[i] << (32 - thisoff));
180             
181             ++i;
182             secondleft -= 32;
183         }
184         
185         this.length += second.length;
186     }
187
188     /**
189      * @see java.lang.Object#toString()
190      */

191     public String JavaDoc toString() {
192         String JavaDoc s = new String JavaDoc();
193         
194         for (int i = 0; i < this.length; ++i)
195             s += ((buffer[i >> 5] & (1 << (31 - i))) != 0) ? "1" : "0";
196         
197         return s;
198     }
199     
200
201     /**
202      * Gets the buffer of the bitlist
203      * @return the buffer of the bitlist
204      */

205     public int[] getBuffer() {
206         return buffer;
207     }
208     
209     //-----------------------------------------------------------------------------
210
/**
211      * the buffer with the bitlist
212      */

213     private int[] buffer;
214     
215     /**
216      * max size of the buffer
217      */

218     private int max;
219     
220     /**
221      * index of the highest valid bit
222      */

223     private int length;
224     
225 }
Popular Tags