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.util.Arrays;
21 import java.io.Serializable;
23 import org.apache.lucene.search.DocIdSet;
24 import org.apache.lucene.search.DocIdSetIterator;
26 /** An "open" BitSet implementation that allows direct access to the array of words
29 * Unlike java.util.bitset, the fact that bits are packed into an array of longs
30 * is part of the interface. This allows efficient implementation of other algorithms
31 * by someone other than the author. It also allows one to efficiently implement
32 * alternate serialization or interchange formats.
34 * <code>OpenBitSet</code> is faster than <code>java.util.BitSet</code> in most operations
35 * and *much* faster at calculating cardinality of sets and results of set operations.
36 * It can also handle sets of larger cardinality (up to 64 * 2**32-1)
38 * The goals of <code>OpenBitSet</code> are the fastest implementation possible, and
39 * maximum code reuse. Extra safety and encapsulation
40 * may always be built on top, but if that's built in, the cost can never be removed (and
41 * hence people re-implement their own version in order to get better performance).
42 * If you want a "safe", totally encapsulated (and slower and limited) BitSet
43 * class, use <code>java.util.BitSet</code>.
45 * <h3>Performance Results</h3>
47 Test system: Pentium 4, Sun Java 1.5_06 -server -Xbatch -Xmx64M
48 <br/>BitSet size = 1,000,000
49 <br/>Results are java.util.BitSet time divided by OpenBitSet time.
52 <th></th> <th>cardinality</th> <th>intersect_count</th> <th>union</th> <th>nextSetBit</th> <th>get</th> <th>iterator</th>
55 <th>50% full</th> <td>3.36</td> <td>3.96</td> <td>1.44</td> <td>1.46</td> <td>1.99</td> <td>1.58</td>
58 <th>1% full</th> <td>3.31</td> <td>3.90</td> <td> </td> <td>1.04</td> <td> </td> <td>0.99</td>
62 Test system: AMD Opteron, 64 bit linux, Sun Java 1.5_06 -server -Xbatch -Xmx64M
63 <br/>BitSet size = 1,000,000
64 <br/>Results are java.util.BitSet time divided by OpenBitSet time.
67 <th></th> <th>cardinality</th> <th>intersect_count</th> <th>union</th> <th>nextSetBit</th> <th>get</th> <th>iterator</th>
70 <th>50% full</th> <td>2.50</td> <td>3.50</td> <td>1.00</td> <td>1.03</td> <td>1.12</td> <td>1.25</td>
73 <th>1% full</th> <td>2.51</td> <td>3.49</td> <td> </td> <td>1.00</td> <td> </td> <td>1.02</td>
78 public class OpenBitSet extends DocIdSet implements Cloneable, Serializable {
79 protected long[] bits;
80 protected int wlen; // number of words (elements) used in the array
82 // Used only for assert:
85 /** Constructs an OpenBitSet large enough to hold numBits.
89 public OpenBitSet(long numBits) {
90 this.numBits = numBits;
91 bits = new long[bits2words(numBits)];
99 /** Constructs an OpenBitSet from an existing long[].
101 * The first 64 bits are in long[0],
102 * with bit index 0 at the least significant bit, and bit index 63 at the most significant.
104 * the word containing it is long[index/64], and it is at bit number index%64 within that word.
106 * numWords are the number of elements in the array that contain
107 * set bits (non-zero longs).
108 * numWords should be <= bits.length, and
109 * any existing words in the array at position >= numWords should be zero.
112 public OpenBitSet(long[] bits, int numWords) {
114 this.wlen = numWords;
115 this.numBits = wlen * 64;
119 public DocIdSetIterator iterator() {
120 return new OpenBitSetIterator(bits, wlen);
123 /** This DocIdSet implementation is cacheable. */
125 public boolean isCacheable() {
129 /** Returns the current capacity in bits (1 greater than the index of the last bit) */
130 public long capacity() { return bits.length << 6; }
133 * Returns the current capacity of this set. Included for
134 * compatibility. This is *not* equal to {@link #cardinality}
140 /** Returns true if there are no set bits */
141 public boolean isEmpty() { return cardinality()==0; }
143 /** Expert: returns the long[] storing the bits */
144 public long[] getBits() { return bits; }
146 /** Expert: sets a new long[] to use as the bit storage */
147 public void setBits(long[] bits) { this.bits = bits; }
149 /** Expert: gets the number of longs in the array that are in use */
150 public int getNumWords() { return wlen; }
152 /** Expert: sets the number of longs in the array that are in use */
153 public void setNumWords(int nWords) { this.wlen=nWords; }
157 /** Returns true or false for the specified bit index. */
158 public boolean get(int index) {
159 int i = index >> 6; // div 64
160 // signed shift will keep a negative index and force an
161 // array-index-out-of-bounds-exception, removing the need for an explicit check.
162 if (i>=bits.length) return false;
164 int bit = index & 0x3f; // mod 64
165 long bitmask = 1L << bit;
166 return (bits[i] & bitmask) != 0;
170 /** Returns true or false for the specified bit index.
171 * The index should be less than the OpenBitSet size
173 public boolean fastGet(int index) {
174 assert index >= 0 && index < numBits;
175 int i = index >> 6; // div 64
176 // signed shift will keep a negative index and force an
177 // array-index-out-of-bounds-exception, removing the need for an explicit check.
178 int bit = index & 0x3f; // mod 64
179 long bitmask = 1L << bit;
180 return (bits[i] & bitmask) != 0;
185 /** Returns true or false for the specified bit index
187 public boolean get(long index) {
188 int i = (int)(index >> 6); // div 64
189 if (i>=bits.length) return false;
190 int bit = (int)index & 0x3f; // mod 64
191 long bitmask = 1L << bit;
192 return (bits[i] & bitmask) != 0;
195 /** Returns true or false for the specified bit index.
196 * The index should be less than the OpenBitSet size.
198 public boolean fastGet(long index) {
199 assert index >= 0 && index < numBits;
200 int i = (int)(index >> 6); // div 64
201 int bit = (int)index & 0x3f; // mod 64
202 long bitmask = 1L << bit;
203 return (bits[i] & bitmask) != 0;
207 // alternate implementation of get()
208 public boolean get1(int index) {
209 int i = index >> 6; // div 64
210 int bit = index & 0x3f; // mod 64
211 return ((bits[i]>>>bit) & 0x01) != 0;
212 // this does a long shift and a bittest (on x86) vs
213 // a long shift, and a long AND, (the test for zero is prob a no-op)
214 // testing on a P4 indicates this is slower than (bits[i] & bitmask) != 0;
219 /** returns 1 if the bit is set, 0 if not.
220 * The index should be less than the OpenBitSet size
222 public int getBit(int index) {
223 assert index >= 0 && index < numBits;
224 int i = index >> 6; // div 64
225 int bit = index & 0x3f; // mod 64
226 return ((int)(bits[i]>>>bit)) & 0x01;
231 public boolean get2(int index) {
232 int word = index >> 6; // div 64
233 int bit = index & 0x0000003f; // mod 64
234 return (bits[word] << bit) < 0; // hmmm, this would work if bit order were reversed
235 // we could right shift and check for parity bit, if it was available to us.
239 /** sets a bit, expanding the set size if necessary */
240 public void set(long index) {
241 int wordNum = expandingWordNum(index);
242 int bit = (int)index & 0x3f;
243 long bitmask = 1L << bit;
244 bits[wordNum] |= bitmask;
248 /** Sets the bit at the specified index.
249 * The index should be less than the OpenBitSet size.
251 public void fastSet(int index) {
252 assert index >= 0 && index < numBits;
253 int wordNum = index >> 6; // div 64
254 int bit = index & 0x3f; // mod 64
255 long bitmask = 1L << bit;
256 bits[wordNum] |= bitmask;
259 /** Sets the bit at the specified index.
260 * The index should be less than the OpenBitSet size.
262 public void fastSet(long index) {
263 assert index >= 0 && index < numBits;
264 int wordNum = (int)(index >> 6);
265 int bit = (int)index & 0x3f;
266 long bitmask = 1L << bit;
267 bits[wordNum] |= bitmask;
270 /** Sets a range of bits, expanding the set size if necessary
272 * @param startIndex lower index
273 * @param endIndex one-past the last bit to set
275 public void set(long startIndex, long endIndex) {
276 if (endIndex <= startIndex) return;
278 int startWord = (int)(startIndex>>6);
280 // since endIndex is one past the end, this is index of the last
281 // word to be changed.
282 int endWord = expandingWordNum(endIndex-1);
284 long startmask = -1L << startIndex;
285 long endmask = -1L >>> -endIndex; // 64-(endIndex&0x3f) is the same as -endIndex due to wrap
287 if (startWord == endWord) {
288 bits[startWord] |= (startmask & endmask);
292 bits[startWord] |= startmask;
293 Arrays.fill(bits, startWord+1, endWord, -1L);
294 bits[endWord] |= endmask;
299 protected int expandingWordNum(long index) {
300 int wordNum = (int)(index >> 6);
302 ensureCapacity(index+1);
305 assert (numBits = Math.max(numBits, index+1)) >= 0;
311 * The index should be less than the OpenBitSet size.
313 public void fastClear(int index) {
314 assert index >= 0 && index < numBits;
315 int wordNum = index >> 6;
316 int bit = index & 0x03f;
317 long bitmask = 1L << bit;
318 bits[wordNum] &= ~bitmask;
319 // hmmm, it takes one more instruction to clear than it does to set... any
320 // way to work around this? If there were only 63 bits per word, we could
321 // use a right shift of 10111111...111 in binary to position the 0 in the
322 // correct place (using sign extension).
323 // Could also use Long.rotateRight() or rotateLeft() *if* they were converted
324 // by the JVM into a native instruction.
325 // bits[word] &= Long.rotateLeft(0xfffffffe,bit);
329 * The index should be less than the OpenBitSet size.
331 public void fastClear(long index) {
332 assert index >= 0 && index < numBits;
333 int wordNum = (int)(index >> 6); // div 64
334 int bit = (int)index & 0x3f; // mod 64
335 long bitmask = 1L << bit;
336 bits[wordNum] &= ~bitmask;
339 /** clears a bit, allowing access beyond the current set size without changing the size.*/
340 public void clear(long index) {
341 int wordNum = (int)(index >> 6); // div 64
342 if (wordNum>=wlen) return;
343 int bit = (int)index & 0x3f; // mod 64
344 long bitmask = 1L << bit;
345 bits[wordNum] &= ~bitmask;
348 /** Clears a range of bits. Clearing past the end does not change the size of the set.
350 * @param startIndex lower index
351 * @param endIndex one-past the last bit to clear
353 public void clear(int startIndex, int endIndex) {
354 if (endIndex <= startIndex) return;
356 int startWord = (startIndex>>6);
357 if (startWord >= wlen) return;
359 // since endIndex is one past the end, this is index of the last
360 // word to be changed.
361 int endWord = ((endIndex-1)>>6);
363 long startmask = -1L << startIndex;
364 long endmask = -1L >>> -endIndex; // 64-(endIndex&0x3f) is the same as -endIndex due to wrap
366 // invert masks since we are clearing
367 startmask = ~startmask;
370 if (startWord == endWord) {
371 bits[startWord] &= (startmask | endmask);
375 bits[startWord] &= startmask;
377 int middle = Math.min(wlen, endWord);
378 Arrays.fill(bits, startWord+1, middle, 0L);
379 if (endWord < wlen) {
380 bits[endWord] &= endmask;
385 /** Clears a range of bits. Clearing past the end does not change the size of the set.
387 * @param startIndex lower index
388 * @param endIndex one-past the last bit to clear
390 public void clear(long startIndex, long endIndex) {
391 if (endIndex <= startIndex) return;
393 int startWord = (int)(startIndex>>6);
394 if (startWord >= wlen) return;
396 // since endIndex is one past the end, this is index of the last
397 // word to be changed.
398 int endWord = (int)((endIndex-1)>>6);
400 long startmask = -1L << startIndex;
401 long endmask = -1L >>> -endIndex; // 64-(endIndex&0x3f) is the same as -endIndex due to wrap
403 // invert masks since we are clearing
404 startmask = ~startmask;
407 if (startWord == endWord) {
408 bits[startWord] &= (startmask | endmask);
412 bits[startWord] &= startmask;
414 int middle = Math.min(wlen, endWord);
415 Arrays.fill(bits, startWord+1, middle, 0L);
416 if (endWord < wlen) {
417 bits[endWord] &= endmask;
423 /** Sets a bit and returns the previous value.
424 * The index should be less than the OpenBitSet size.
426 public boolean getAndSet(int index) {
427 assert index >= 0 && index < numBits;
428 int wordNum = index >> 6; // div 64
429 int bit = index & 0x3f; // mod 64
430 long bitmask = 1L << bit;
431 boolean val = (bits[wordNum] & bitmask) != 0;
432 bits[wordNum] |= bitmask;
436 /** Sets a bit and returns the previous value.
437 * The index should be less than the OpenBitSet size.
439 public boolean getAndSet(long index) {
440 assert index >= 0 && index < numBits;
441 int wordNum = (int)(index >> 6); // div 64
442 int bit = (int)index & 0x3f; // mod 64
443 long bitmask = 1L << bit;
444 boolean val = (bits[wordNum] & bitmask) != 0;
445 bits[wordNum] |= bitmask;
450 * The index should be less than the OpenBitSet size.
452 public void fastFlip(int index) {
453 assert index >= 0 && index < numBits;
454 int wordNum = index >> 6; // div 64
455 int bit = index & 0x3f; // mod 64
456 long bitmask = 1L << bit;
457 bits[wordNum] ^= bitmask;
461 * The index should be less than the OpenBitSet size.
463 public void fastFlip(long index) {
464 assert index >= 0 && index < numBits;
465 int wordNum = (int)(index >> 6); // div 64
466 int bit = (int)index & 0x3f; // mod 64
467 long bitmask = 1L << bit;
468 bits[wordNum] ^= bitmask;
471 /** flips a bit, expanding the set size if necessary */
472 public void flip(long index) {
473 int wordNum = expandingWordNum(index);
474 int bit = (int)index & 0x3f; // mod 64
475 long bitmask = 1L << bit;
476 bits[wordNum] ^= bitmask;
479 /** flips a bit and returns the resulting bit value.
480 * The index should be less than the OpenBitSet size.
482 public boolean flipAndGet(int index) {
483 assert index >= 0 && index < numBits;
484 int wordNum = index >> 6; // div 64
485 int bit = index & 0x3f; // mod 64
486 long bitmask = 1L << bit;
487 bits[wordNum] ^= bitmask;
488 return (bits[wordNum] & bitmask) != 0;
491 /** flips a bit and returns the resulting bit value.
492 * The index should be less than the OpenBitSet size.
494 public boolean flipAndGet(long index) {
495 assert index >= 0 && index < numBits;
496 int wordNum = (int)(index >> 6); // div 64
497 int bit = (int)index & 0x3f; // mod 64
498 long bitmask = 1L << bit;
499 bits[wordNum] ^= bitmask;
500 return (bits[wordNum] & bitmask) != 0;
503 /** Flips a range of bits, expanding the set size if necessary
505 * @param startIndex lower index
506 * @param endIndex one-past the last bit to flip
508 public void flip(long startIndex, long endIndex) {
509 if (endIndex <= startIndex) return;
510 int startWord = (int)(startIndex>>6);
512 // since endIndex is one past the end, this is index of the last
513 // word to be changed.
514 int endWord = expandingWordNum(endIndex-1);
516 /*** Grrr, java shifting wraps around so -1L>>>64 == -1
517 * for that reason, make sure not to use endmask if the bits to flip will
518 * be zero in the last word (redefine endWord to be the last changed...)
519 long startmask = -1L << (startIndex & 0x3f); // example: 11111...111000
520 long endmask = -1L >>> (64-(endIndex & 0x3f)); // example: 00111...111111
523 long startmask = -1L << startIndex;
524 long endmask = -1L >>> -endIndex; // 64-(endIndex&0x3f) is the same as -endIndex due to wrap
526 if (startWord == endWord) {
527 bits[startWord] ^= (startmask & endmask);
531 bits[startWord] ^= startmask;
533 for (int i=startWord+1; i<endWord; i++) {
537 bits[endWord] ^= endmask;
542 public static int pop(long v0, long v1, long v2, long v3) {
543 // derived from pop_array by setting last four elems to 0.
544 // exchanges one pop() call for 10 elementary operations
545 // saving about 7 instructions... is there a better way?
550 long twosB =(ones&v2)|(u2&v3);
553 long fours=(twosA&twosB);
554 long twos=twosA^twosB;
556 return (pop(fours)<<2)
564 /** @return the number of set bits */
565 public long cardinality() {
566 return BitUtil.pop_array(bits,0,wlen);
569 /** Returns the popcount or cardinality of the intersection of the two sets.
570 * Neither set is modified.
572 public static long intersectionCount(OpenBitSet a, OpenBitSet b) {
573 return BitUtil.pop_intersect(a.bits, b.bits, 0, Math.min(a.wlen, b.wlen));
576 /** Returns the popcount or cardinality of the union of the two sets.
577 * Neither set is modified.
579 public static long unionCount(OpenBitSet a, OpenBitSet b) {
580 long tot = BitUtil.pop_union(a.bits, b.bits, 0, Math.min(a.wlen, b.wlen));
581 if (a.wlen < b.wlen) {
582 tot += BitUtil.pop_array(b.bits, a.wlen, b.wlen-a.wlen);
583 } else if (a.wlen > b.wlen) {
584 tot += BitUtil.pop_array(a.bits, b.wlen, a.wlen-b.wlen);
589 /** Returns the popcount or cardinality of "a and not b"
590 * or "intersection(a, not(b))".
591 * Neither set is modified.
593 public static long andNotCount(OpenBitSet a, OpenBitSet b) {
594 long tot = BitUtil.pop_andnot(a.bits, b.bits, 0, Math.min(a.wlen, b.wlen));
595 if (a.wlen > b.wlen) {
596 tot += BitUtil.pop_array(a.bits, b.wlen, a.wlen-b.wlen);
601 /** Returns the popcount or cardinality of the exclusive-or of the two sets.
602 * Neither set is modified.
604 public static long xorCount(OpenBitSet a, OpenBitSet b) {
605 long tot = BitUtil.pop_xor(a.bits, b.bits, 0, Math.min(a.wlen, b.wlen));
606 if (a.wlen < b.wlen) {
607 tot += BitUtil.pop_array(b.bits, a.wlen, b.wlen-a.wlen);
608 } else if (a.wlen > b.wlen) {
609 tot += BitUtil.pop_array(a.bits, b.wlen, a.wlen-b.wlen);
615 /** Returns the index of the first set bit starting at the index specified.
616 * -1 is returned if there are no more set bits.
618 public int nextSetBit(int index) {
620 if (i>=wlen) return -1;
621 int subIndex = index & 0x3f; // index within the word
622 long word = bits[i] >> subIndex; // skip all the bits to the right of index
625 return (i<<6) + subIndex + BitUtil.ntz(word);
630 if (word!=0) return (i<<6) + BitUtil.ntz(word);
636 /** Returns the index of the first set bit starting at the index specified.
637 * -1 is returned if there are no more set bits.
639 public long nextSetBit(long index) {
640 int i = (int)(index>>>6);
641 if (i>=wlen) return -1;
642 int subIndex = (int)index & 0x3f; // index within the word
643 long word = bits[i] >>> subIndex; // skip all the bits to the right of index
646 return (((long)i)<<6) + (subIndex + BitUtil.ntz(word));
651 if (word!=0) return (((long)i)<<6) + BitUtil.ntz(word);
658 /** Returns the index of the first set bit starting downwards at
659 * the index specified.
660 * -1 is returned if there are no more set bits.
662 public int prevSetBit(int index) {
668 if (i < 0) return -1;
669 subIndex = 63; // last possible bit
672 if (i < 0) return -1;
673 subIndex = index & 0x3f; // index within the word
674 word = (bits[i] << (63-subIndex)); // skip all the bits to the left of index
678 return (i << 6) + subIndex - Long.numberOfLeadingZeros(word); // See LUCENE-3197
684 return (i << 6) + 63 - Long.numberOfLeadingZeros(word);
691 /** Returns the index of the first set bit starting downwards at
692 * the index specified.
693 * -1 is returned if there are no more set bits.
695 public long prevSetBit(long index) {
696 int i = (int) (index >> 6);
701 if (i < 0) return -1;
702 subIndex = 63; // last possible bit
705 if (i < 0) return -1;
706 subIndex = (int)index & 0x3f; // index within the word
707 word = (bits[i] << (63-subIndex)); // skip all the bits to the left of index
711 return (((long)i)<<6) + subIndex - Long.numberOfLeadingZeros(word); // See LUCENE-3197
717 return (((long)i)<<6) + 63 - Long.numberOfLeadingZeros(word);
725 public Object clone() {
727 OpenBitSet obs = (OpenBitSet)super.clone();
728 obs.bits = obs.bits.clone(); // hopefully an array clone is as fast(er) than arraycopy
730 } catch (CloneNotSupportedException e) {
731 throw new RuntimeException(e);
735 /** this = this AND other */
736 public void intersect(OpenBitSet other) {
737 int newLen= Math.min(this.wlen,other.wlen);
738 long[] thisArr = this.bits;
739 long[] otherArr = other.bits;
740 // testing against zero can be more efficient
743 thisArr[pos] &= otherArr[pos];
745 if (this.wlen > newLen) {
746 // fill zeros from the new shorter length to the old length
747 Arrays.fill(bits,newLen,this.wlen,0);
752 /** this = this OR other */
753 public void union(OpenBitSet other) {
754 int newLen = Math.max(wlen,other.wlen);
755 ensureCapacityWords(newLen);
756 assert (numBits = Math.max(other.numBits, numBits)) >= 0;
758 long[] thisArr = this.bits;
759 long[] otherArr = other.bits;
760 int pos=Math.min(wlen,other.wlen);
762 thisArr[pos] |= otherArr[pos];
764 if (this.wlen < newLen) {
765 System.arraycopy(otherArr, this.wlen, thisArr, this.wlen, newLen-this.wlen);
771 /** Remove all elements set in other. this = this AND_NOT other */
772 public void remove(OpenBitSet other) {
773 int idx = Math.min(wlen,other.wlen);
774 long[] thisArr = this.bits;
775 long[] otherArr = other.bits;
777 thisArr[idx] &= ~otherArr[idx];
781 /** this = this XOR other */
782 public void xor(OpenBitSet other) {
783 int newLen = Math.max(wlen,other.wlen);
784 ensureCapacityWords(newLen);
785 assert (numBits = Math.max(other.numBits, numBits)) >= 0;
787 long[] thisArr = this.bits;
788 long[] otherArr = other.bits;
789 int pos=Math.min(wlen,other.wlen);
791 thisArr[pos] ^= otherArr[pos];
793 if (this.wlen < newLen) {
794 System.arraycopy(otherArr, this.wlen, thisArr, this.wlen, newLen-this.wlen);
800 // some BitSet compatability methods
802 //** see {@link intersect} */
803 public void and(OpenBitSet other) {
807 //** see {@link union} */
808 public void or(OpenBitSet other) {
812 //** see {@link andNot} */
813 public void andNot(OpenBitSet other) {
817 /** returns true if the sets have any elements in common */
818 public boolean intersects(OpenBitSet other) {
819 int pos = Math.min(this.wlen, other.wlen);
820 long[] thisArr = this.bits;
821 long[] otherArr = other.bits;
823 if ((thisArr[pos] & otherArr[pos])!=0) return true;
830 /** Expand the long[] with the size given as a number of words (64 bit longs).
831 * getNumWords() is unchanged by this call.
833 public void ensureCapacityWords(int numWords) {
834 if (bits.length < numWords) {
835 bits = ArrayUtil.grow(bits, numWords);
839 /** Ensure that the long[] is big enough to hold numBits, expanding it if necessary.
840 * getNumWords() is unchanged by this call.
842 public void ensureCapacity(long numBits) {
843 ensureCapacityWords(bits2words(numBits));
846 /** Lowers numWords, the number of words in use,
847 * by checking for trailing zero words.
849 public void trimTrailingZeros() {
851 while (idx>=0 && bits[idx]==0) idx--;
855 /** returns the number of 64 bit words it would take to hold numBits */
856 public static int bits2words(long numBits) {
857 return (int)(((numBits-1)>>>6)+1);
861 /** returns true if both sets have the same bits set */
863 public boolean equals(Object o) {
864 if (this == o) return true;
865 if (!(o instanceof OpenBitSet)) return false;
867 OpenBitSet b = (OpenBitSet)o;
868 // make a the larger set.
869 if (b.wlen > this.wlen) {
875 // check for any set bits out of the range of b
876 for (int i=a.wlen-1; i>=b.wlen; i--) {
877 if (a.bits[i]!=0) return false;
880 for (int i=b.wlen-1; i>=0; i--) {
881 if (a.bits[i] != b.bits[i]) return false;
889 public int hashCode() {
890 // Start with a zero hash and use a mix that results in zero if the input is zero.
891 // This effectively truncates trailing zeros without an explicit check.
893 for (int i = bits.length; --i>=0;) {
895 h = (h << 1) | (h >>> 63); // rotate left
897 // fold leftmost bits into right and add a constant to prevent
898 // empty sets from returning 0, which is too common.
899 return (int)((h>>32) ^ h) + 0x98761234;