2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.apache.lucene.util;
20 import java.io.IOException;
21 import java.util.Arrays;
23 import org.apache.lucene.search.DocIdSet;
24 import org.apache.lucene.search.DocIdSetIterator;
26 // TODO: maybe merge with BitVector? Problem is BitVector
27 // caches its cardinality...
29 /** BitSet of fixed length (numBits), backed by accessible
30 * ({@link #getBits}) long[], accessed with an int index,
31 * implementing Bits and DocIdSet. Unlike {@link
32 * OpenBitSet} this bit set does not auto-expand, cannot
33 * handle long index, and does not have fastXX/XX variants
39 public final class FixedBitSet extends DocIdSet implements Bits {
40 private final long[] bits;
43 /** returns the number of 64 bit words it would take to hold numBits */
44 public static int bits2words(int numBits) {
45 int numLong = numBits >>> 6;
46 if ((numBits & 63) != 0) {
52 public FixedBitSet(int numBits) {
53 this.numBits = numBits;
54 bits = new long[bits2words(numBits)];
57 /** Makes full copy. */
58 public FixedBitSet(FixedBitSet other) {
59 bits = new long[other.bits.length];
60 System.arraycopy(other.bits, 0, bits, 0, bits.length);
61 numBits = other.numBits;
65 public DocIdSetIterator iterator() {
66 return new OpenBitSetIterator(bits, bits.length);
73 /** This DocIdSet implementation is cacheable. */
75 public boolean isCacheable() {
80 public long[] getBits() {
84 /** Returns number of set bits. NOTE: this visits every
85 * long in the backing bits array, and the result is not
86 * internally cached! */
87 public int cardinality() {
88 return (int) BitUtil.pop_array(bits, 0, bits.length);
91 public boolean get(int index) {
92 assert index >= 0 && index < numBits;
93 int i = index >> 6; // div 64
94 // signed shift will keep a negative index and force an
95 // array-index-out-of-bounds-exception, removing the need for an explicit check.
96 int bit = index & 0x3f; // mod 64
97 long bitmask = 1L << bit;
98 return (bits[i] & bitmask) != 0;
101 public void set(int index) {
102 assert index >= 0 && index < numBits;
103 int wordNum = index >> 6; // div 64
104 int bit = index & 0x3f; // mod 64
105 long bitmask = 1L << bit;
106 bits[wordNum] |= bitmask;
109 public boolean getAndSet(int index) {
110 assert index >= 0 && index < numBits;
111 int wordNum = index >> 6; // div 64
112 int bit = index & 0x3f; // mod 64
113 long bitmask = 1L << bit;
114 boolean val = (bits[wordNum] & bitmask) != 0;
115 bits[wordNum] |= bitmask;
119 public void clear(int index) {
120 assert index >= 0 && index < numBits;
121 int wordNum = index >> 6;
122 int bit = index & 0x03f;
123 long bitmask = 1L << bit;
124 bits[wordNum] &= ~bitmask;
127 public boolean getAndClear(int index) {
128 assert index >= 0 && index < numBits;
129 int wordNum = index >> 6; // div 64
130 int bit = index & 0x3f; // mod 64
131 long bitmask = 1L << bit;
132 boolean val = (bits[wordNum] & bitmask) != 0;
133 bits[wordNum] &= ~bitmask;
137 /** Returns the index of the first set bit starting at the index specified.
138 * -1 is returned if there are no more set bits.
140 public int nextSetBit(int index) {
141 assert index >= 0 && index < numBits;
143 final int subIndex = index & 0x3f; // index within the word
144 long word = bits[i] >> subIndex; // skip all the bits to the right of index
147 return (i<<6) + subIndex + BitUtil.ntz(word);
150 while(++i < bits.length) {
153 return (i<<6) + BitUtil.ntz(word);
160 /** Returns the index of the last set bit before or on the index specified.
161 * -1 is returned if there are no more set bits.
163 public int prevSetBit(int index) {
164 assert index >= 0 && index < numBits: "index=" + index + " numBits=" + numBits;
166 final int subIndex = index & 0x3f; // index within the word
167 long word = (bits[i] << (63-subIndex)); // skip all the bits to the left of index
170 return (i << 6) + subIndex - Long.numberOfLeadingZeros(word); // See LUCENE-3197
176 return (i << 6) + 63 - Long.numberOfLeadingZeros(word);
183 /** Does in-place OR of the bits provided by the
185 public void or(DocIdSetIterator iter) throws IOException {
186 if (iter instanceof OpenBitSetIterator && iter.docID() == -1) {
187 final OpenBitSetIterator obs = (OpenBitSetIterator) iter;
188 or(obs.arr, obs.words);
189 // advance after last doc that would be accepted if standard
190 // iteration is used (to exhaust it):
191 obs.advance(numBits);
194 while ((doc = iter.nextDoc()) < numBits) {
200 /** this = this OR other */
201 public void or(FixedBitSet other) {
202 or(other.bits, other.bits.length);
205 private void or(final long[] otherArr, final int otherLen) {
206 final long[] thisArr = this.bits;
207 int pos = Math.min(thisArr.length, otherLen);
209 thisArr[pos] |= otherArr[pos];
213 /** Does in-place AND of the bits provided by the
215 public void and(DocIdSetIterator iter) throws IOException {
216 if (iter instanceof OpenBitSetIterator && iter.docID() == -1) {
217 final OpenBitSetIterator obs = (OpenBitSetIterator) iter;
218 and(obs.arr, obs.words);
219 // advance after last doc that would be accepted if standard
220 // iteration is used (to exhaust it):
221 obs.advance(numBits);
223 if (numBits == 0) return;
224 int disiDoc, bitSetDoc = nextSetBit(0);
225 while (bitSetDoc != -1 && (disiDoc = iter.advance(bitSetDoc)) < numBits) {
226 clear(bitSetDoc, disiDoc);
228 bitSetDoc = (disiDoc < numBits) ? nextSetBit(disiDoc) : -1;
230 if (bitSetDoc != -1) {
231 clear(bitSetDoc, numBits);
236 /** this = this AND other */
237 public void and(FixedBitSet other) {
238 and(other.bits, other.bits.length);
241 private void and(final long[] otherArr, final int otherLen) {
242 final long[] thisArr = this.bits;
243 int pos = Math.min(thisArr.length, otherLen);
245 thisArr[pos] &= otherArr[pos];
247 if (thisArr.length > otherLen) {
248 Arrays.fill(thisArr, otherLen, thisArr.length, 0L);
252 /** Does in-place AND NOT of the bits provided by the
254 public void andNot(DocIdSetIterator iter) throws IOException {
255 if (iter instanceof OpenBitSetIterator && iter.docID() == -1) {
256 final OpenBitSetIterator obs = (OpenBitSetIterator) iter;
257 andNot(obs.arr, obs.words);
258 // advance after last doc that would be accepted if standard
259 // iteration is used (to exhaust it):
260 obs.advance(numBits);
263 while ((doc = iter.nextDoc()) < numBits) {
269 /** this = this AND NOT other */
270 public void andNot(FixedBitSet other) {
271 andNot(other.bits, other.bits.length);
274 private void andNot(final long[] otherArr, final int otherLen) {
275 final long[] thisArr = this.bits;
276 int pos = Math.min(thisArr.length, otherLen);
278 thisArr[pos] &= ~otherArr[pos];
282 // NOTE: no .isEmpty() here because that's trappy (ie,
283 // typically isEmpty is low cost, but this one wouldn't
286 /** Flips a range of bits
288 * @param startIndex lower index
289 * @param endIndex one-past the last bit to flip
291 public void flip(int startIndex, int endIndex) {
292 assert startIndex >= 0 && startIndex < numBits;
293 assert endIndex >= 0 && endIndex <= numBits;
294 if (endIndex <= startIndex) {
298 int startWord = startIndex >> 6;
299 int endWord = (endIndex-1) >> 6;
301 /*** Grrr, java shifting wraps around so -1L>>>64 == -1
302 * for that reason, make sure not to use endmask if the bits to flip will
303 * be zero in the last word (redefine endWord to be the last changed...)
304 long startmask = -1L << (startIndex & 0x3f); // example: 11111...111000
305 long endmask = -1L >>> (64-(endIndex & 0x3f)); // example: 00111...111111
308 long startmask = -1L << startIndex;
309 long endmask = -1L >>> -endIndex; // 64-(endIndex&0x3f) is the same as -endIndex due to wrap
311 if (startWord == endWord) {
312 bits[startWord] ^= (startmask & endmask);
316 bits[startWord] ^= startmask;
318 for (int i=startWord+1; i<endWord; i++) {
322 bits[endWord] ^= endmask;
325 /** Sets a range of bits
327 * @param startIndex lower index
328 * @param endIndex one-past the last bit to set
330 public void set(int startIndex, int endIndex) {
331 assert startIndex >= 0 && startIndex < numBits;
332 assert endIndex >= 0 && endIndex <= numBits;
333 if (endIndex <= startIndex) {
337 int startWord = startIndex >> 6;
338 int endWord = (endIndex-1) >> 6;
340 long startmask = -1L << startIndex;
341 long endmask = -1L >>> -endIndex; // 64-(endIndex&0x3f) is the same as -endIndex due to wrap
343 if (startWord == endWord) {
344 bits[startWord] |= (startmask & endmask);
348 bits[startWord] |= startmask;
349 Arrays.fill(bits, startWord+1, endWord, -1L);
350 bits[endWord] |= endmask;
353 /** Clears a range of bits.
355 * @param startIndex lower index
356 * @param endIndex one-past the last bit to clear
358 public void clear(int startIndex, int endIndex) {
359 assert startIndex >= 0 && startIndex < numBits;
360 assert endIndex >= 0 && endIndex <= numBits;
361 if (endIndex <= startIndex) {
365 int startWord = startIndex >> 6;
366 int endWord = (endIndex-1) >> 6;
368 long startmask = -1L << startIndex;
369 long endmask = -1L >>> -endIndex; // 64-(endIndex&0x3f) is the same as -endIndex due to wrap
371 // invert masks since we are clearing
372 startmask = ~startmask;
375 if (startWord == endWord) {
376 bits[startWord] &= (startmask | endmask);
380 bits[startWord] &= startmask;
381 Arrays.fill(bits, startWord+1, endWord, 0L);
382 bits[endWord] &= endmask;
386 public Object clone() {
387 return new FixedBitSet(this);
390 /** returns true if both sets have the same bits set */
392 public boolean equals(Object o) {
396 if (!(o instanceof FixedBitSet)) {
399 FixedBitSet other = (FixedBitSet) o;
400 if (numBits != other.length()) {
403 return Arrays.equals(bits, other.bits);
407 public int hashCode() {
409 for (int i = bits.length; --i>=0;) {
411 h = (h << 1) | (h >>> 63); // rotate left
413 // fold leftmost bits into right and add a constant to prevent
414 // empty sets from returning 0, which is too common.
415 return (int) ((h>>32) ^ h) + 0x98761234;