Name: rlT66838 Date: 08/09/99
The java.util.BitSet class does not provide a reasonable method for determining if a set is empty, or one for determining if one set intersects another. BitSet should provide the following two methods:
/**
* Returns true if the BitSet has no bits set.
*/
boolean isEmpty();
/**
* Returns true if the given set has a 'set' bit that is
* set, and the same bit is 'set' in the current set.
*/
boolean intersects( BitSet set );
These are intrinsic set operations, so it is surprising that they are not implemented in the BitSet class. But even worse, trying to provide the needed functionality using the existing BitSet class has proven to be both difficult and inefficient.
Without the isEmpty() method, the best alternative available would seem to be to test whether the length() of the set is 0, using something like this:
static boolean isEmpty( BitSet set ) {
return ( set.length() == 0 );
}
Unfortunately, the length() method does not work correctly (I have submitted a separate bug report on the length() method), so this does not work.
Without the intersects() method, the best alternative available would seem to be to perform an and() of the two sets, and test if the result is empty, using something like this:
static boolean intersects( BitSet s1, BitSet s2 ) {
BitSet s = new BitSet();
s.or( s1 );
s.and( s2 );
return s.isEmpty(); // No such method.
}
If the above workaround attempts did actually work, they would still be rather inefficient.
The static isEmpty() method above uses the length() method, which must loop through all of the bits in the last unit, testing each bit to see if it is set, rather than perform a simple boolean test.
The static intersects() method above must allocate a new BitSet, and then compute the and() of all units of the BitSets, rather than just scan until one intersecting unit was found.
The BitSet members that must be accessed in order to implement these functions are all private, so subclassing BitSet is not a viable solution.
(Review ID: 93706)
======================================================================