1 | /* |
2 | |
3 | Derby - Class com.ihost.cs.JBitSet |
4 | |
5 | Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.iapi.util; |
22 | |
23 | import org.apache.derby.iapi.services.sanity.SanityManager; |
24 | |
25 | import java.util.BitSet; |
26 | |
27 | /** |
28 | * JBitSet is a wrapper class for BitSet. It is a fixed length implementation |
29 | * which can be extended via the grow() method. It provides additional |
30 | * methods to manipulate BitSets. |
31 | * NOTE: JBitSet was driven by the (current and perceived) needs of the |
32 | * optimizer, but placed in the util package since it is not specific to |
33 | * query trees.. |
34 | * NOTE: java.util.BitSet is final, so we must provide a wrapper class |
35 | * which includes a BitSet member in order to extend the functionality. |
36 | * We want to make it look like JBitSet extends BitSet, so we need to |
37 | * provide wrapper methods for all of BitSet's methods. |
38 | * |
39 | * @author Jerry Brenner |
40 | */ |
41 | public final class JBitSet |
42 | { |
43 | /* The BitSet that we'd like to extend */ |
44 | private final BitSet bitSet; |
45 | /* Cache size() of bitSet, since accessed a lot */ |
46 | private int size; |
47 | |
48 | /** |
49 | * Construct a JBitSet of the specified size. |
50 | * |
51 | * @param size The number of bits in the JBitSet. |
52 | */ |
53 | public JBitSet(int size) |
54 | { |
55 | bitSet = new BitSet(size); |
56 | this.size = size; |
57 | } |
58 | |
59 | /** |
60 | * Construct a JBitSet with the specified bitSet. |
61 | * |
62 | * @param bitSet The BitSet. |
63 | * @param size The size of bitSet. |
64 | * NOTE: We need to specify the size since the size of a |
65 | * BitSet is not guaranteed to be the same as JBitSet.size(). |
66 | */ |
67 | private JBitSet(BitSet bitSet, int size) |
68 | { |
69 | this.bitSet = bitSet; |
70 | this.size = size; |
71 | } |
72 | |
73 | /** |
74 | * Set the BitSet to have the exact same bits set as the parameter's BitSet. |
75 | * |
76 | * @param sourceBitSet The JBitSet to copy. |
77 | */ |
78 | public void setTo(JBitSet sourceBitSet) |
79 | { |
80 | if (SanityManager.DEBUG) |
81 | { |
82 | SanityManager.ASSERT(size == sourceBitSet.size(), |
83 | "JBitSets are expected to be the same size"); |
84 | } |
85 | /* High reuse solution */ |
86 | and(sourceBitSet); |
87 | or(sourceBitSet); |
88 | } |
89 | |
90 | /** |
91 | * Test to see if one JBitSet contains another one of |
92 | * the same size. |
93 | * |
94 | * @param jBitSet JBitSet that we want to know if it is |
95 | * a subset of current JBitSet |
96 | * |
97 | * @return boolean Whether or not jBitSet is a subset. |
98 | */ |
99 | public boolean contains(JBitSet jBitSet) |
100 | { |
101 | if (SanityManager.DEBUG) |
102 | { |
103 | SanityManager.ASSERT(size == jBitSet.size(), |
104 | "JBitSets are expected to be the same size"); |
105 | } |
106 | for (int bitIndex = 0; bitIndex < size; bitIndex++) |
107 | { |
108 | if (jBitSet.bitSet.get(bitIndex) && ! (bitSet.get(bitIndex))) |
109 | { |
110 | return false; |
111 | } |
112 | } |
113 | return true; |
114 | } |
115 | |
116 | /** |
117 | * See of a JBitSet has exactly 1 bit set. |
118 | * |
119 | * @return boolean Whether or not JBitSet has a single bit set. |
120 | */ |
121 | public boolean hasSingleBitSet() |
122 | { |
123 | boolean found = false; |
124 | |
125 | for (int bitIndex = 0; bitIndex < size; bitIndex++) |
126 | { |
127 | if (bitSet.get(bitIndex)) |
128 | { |
129 | if (found) |
130 | { |
131 | return false; |
132 | } |
133 | else |
134 | { |
135 | found = true; |
136 | } |
137 | } |
138 | } |
139 | |
140 | return found; |
141 | } |
142 | |
143 | /** |
144 | * Get the first set bit (starting at index 0) from a JBitSet. |
145 | * |
146 | * @return int Index of first set bit, -1 if none set. |
147 | */ |
148 | public int getFirstSetBit() |
149 | { |
150 | for (int bitIndex = 0; bitIndex < size; bitIndex++) |
151 | { |
152 | if (bitSet.get(bitIndex)) |
153 | { |
154 | return bitIndex; |
155 | } |
156 | } |
157 | |
158 | return -1; |
159 | } |
160 | |
161 | /** |
162 | * Grow an existing JBitSet to the specified size. |
163 | * |
164 | * @param newSize The new size |
165 | * |
166 | */ |
167 | public void grow(int newSize) |
168 | { |
169 | if (SanityManager.DEBUG) |
170 | { |
171 | SanityManager.ASSERT(newSize > size, |
172 | "New size is expected to be larger than current size"); |
173 | } |
174 | |
175 | size = newSize; |
176 | |
177 | } |
178 | |
179 | /** |
180 | * Clear all of the bits in this JBitSet |
181 | */ |
182 | public void clearAll() |
183 | { |
184 | for (int bitIndex = 0; bitIndex < size; bitIndex++) |
185 | { |
186 | if (bitSet.get(bitIndex)) |
187 | { |
188 | bitSet.clear(bitIndex); |
189 | } |
190 | } |
191 | } |
192 | |
193 | /* Wrapper methods for BitSet's methods */ |
194 | public String toString() |
195 | { |
196 | return bitSet.toString(); |
197 | } |
198 | |
199 | public boolean equals(Object obj) |
200 | { |
201 | if (SanityManager.DEBUG) |
202 | { |
203 | SanityManager.ASSERT((obj instanceof JBitSet), |
204 | "obj is expected to be a JBitSet " + obj); |
205 | } |
206 | return bitSet.equals(((JBitSet) obj).bitSet); |
207 | } |
208 | |
209 | public int hashCode() |
210 | { |
211 | return bitSet.hashCode(); |
212 | } |
213 | |
214 | public Object clone() |
215 | { |
216 | return new JBitSet((BitSet) bitSet.clone(), size); |
217 | } |
218 | |
219 | public boolean get(int bitIndex) |
220 | { |
221 | return bitSet.get(bitIndex); |
222 | } |
223 | |
224 | public void set(int bitIndex) |
225 | { |
226 | bitSet.set(bitIndex); |
227 | } |
228 | |
229 | public void clear(int bitIndex) |
230 | { |
231 | bitSet.clear(bitIndex); |
232 | } |
233 | |
234 | public void and(JBitSet set) |
235 | { |
236 | bitSet.and(set.bitSet); |
237 | } |
238 | |
239 | public void or(JBitSet set) |
240 | { |
241 | bitSet.or(set.bitSet); |
242 | } |
243 | |
244 | public void xor(JBitSet set) |
245 | { |
246 | bitSet.xor(set.bitSet); |
247 | } |
248 | |
249 | /** |
250 | * Return the size of bitSet |
251 | * |
252 | * @return int Size of bitSet |
253 | */ |
254 | public int size() |
255 | { |
256 | return size; |
257 | } |
258 | } |
259 | |