KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cojen > classfile > BitList


1 /*
2  * Copyright 2004 Brian S O'Neill
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.cojen.classfile;
18
19 /**
20  * A specialized, faster BitSet used by InstructionList.
21  *
22  * @author Brian S O'Neill
23  */

24 final class BitList implements Cloneable JavaDoc {
25     // Bits are stored little endian.
26
private int[] mData;
27
28     /**
29      * @param capacity initial amount of bits to store
30      */

31     public BitList(int capacity) {
32         mData = new int[(capacity + 31) >> 5];
33     }
34
35     public boolean get(int index) {
36         return (mData[index >> 5] & (0x80000000 >>> index)) != 0;
37     }
38
39     /**
40      * @return true if any change made
41      */

42     public boolean set(int index) {
43         int i = index >> 5;
44         int v = mData[i];
45         return v != (mData[i] = v | (0x80000000 >>> index));
46     }
47
48     /**
49      * @return true if any changes made
50      */

51     public boolean or(BitList list) {
52         boolean changes = ensureCapacity(list.capacity());
53         for (int i=list.mData.length; --i >= 0; ) {
54             int v = mData[i];
55             changes |= (v != (mData[i] = v | list.mData[i]));
56         }
57         return changes;
58     }
59
60     public boolean isAllClear() {
61         for (int i=mData.length; --i >= 0; ) {
62             if (mData[i] != 0) {
63                 return false;
64             }
65         }
66         return true;
67     }
68
69     public boolean isAllSet() {
70         for (int i=mData.length; --i >= 0; ) {
71             if (mData[i] != 0xffffffff) {
72                 return false;
73             }
74         }
75         return true;
76     }
77
78     /**
79      * @return true if the bitwise or of the two lists is different than the
80      * bitwise xor.
81      */

82     public boolean intersects(BitList list) {
83         if (list != null) {
84             for (int i=Math.min(mData.length, list.mData.length); --i >= 0; ) {
85                 int v1 = mData[i];
86                 int v2 = list.mData[i];
87                 if ((v1 | v2) != (v1 ^ v2)) {
88                     return true;
89                 }
90             }
91         }
92         return false;
93     }
94
95     public int hashCode() {
96         int hash = 0;
97         for (int i=mData.length; --i >= 0; ) {
98             hash = hash * 31 + mData[i];
99         }
100         return hash;
101     }
102
103     public int capacity() {
104         return mData.length << 5;
105     }
106
107     public boolean equals(Object JavaDoc obj) {
108         if (obj instanceof BitList) {
109             return java.util.Arrays.equals(mData, ((BitList)obj).mData);
110         }
111         return false;
112     }
113
114     public BitList copy() {
115         return (BitList)clone();
116     }
117
118     public Object JavaDoc clone() {
119         try {
120             return super.clone();
121         }
122         catch (CloneNotSupportedException JavaDoc e) {
123             throw new InternalError JavaDoc();
124         }
125     }
126
127     public String JavaDoc toString() {
128         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(mData.length + 2);
129         buf.append('[');
130         for (int i=0; i<mData.length; i++) {
131             String JavaDoc binary = Integer.toBinaryString(mData[i]);
132             for (int j=binary.length(); j<32; j++) {
133                 buf.append('0');
134             }
135             buf.append(binary);
136         }
137         buf.append(']');
138         return buf.toString();
139     }
140
141     private boolean ensureCapacity(int capacity) {
142         int len = (capacity + 31) >> 5;
143         if (len > mData.length) {
144             int[] newData = new int[len];
145             System.arraycopy(mData, 0, newData, 0, mData.length);
146             mData = newData;
147             return true;
148         }
149         return false;
150     }
151 }
152
Popular Tags