Provides common functionality for IBitSet implementations. Especially, based on IBitSet.Action, it implements some operations on IBitSet without the need to knowing its concrete type, so that it support operations between bit sets of different types.
| 30 | * it support operations between bit sets of different types. |
| 31 | */ |
| 32 | public abstract class AbstractBitSet implements IBitSet { |
| 33 | |
| 34 | /* |
| 35 | * BitSets are packed into arrays of "words." Currently, a word is |
| 36 | * a long, which consists of 64 bits, requiring 6 address bits. |
| 37 | * The choice of word size is determined purely by performance concerns. |
| 38 | */ |
| 39 | protected static final int ADDRESS_BITS_PER_WORD = 6; |
| 40 | |
| 41 | protected static final int BITS_PER_WORD = 1 << ADDRESS_BITS_PER_WORD; |
| 42 | |
| 43 | /** |
| 44 | * Given a bit index, return word index containing it. |
| 45 | */ |
| 46 | protected static int wordIndex(int bitIndex) { |
| 47 | return bitIndex >> ADDRESS_BITS_PER_WORD; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public boolean set(int bitIndex, boolean value) { |
| 52 | return value ? set(bitIndex) : clear(bitIndex); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public boolean intersects(IBitSet set) { |
| 57 | return set.iterateBits(new IntersectsAction()); |
| 58 | } |
| 59 | |
| 60 | private class IntersectsAction implements Action<Boolean> { |
| 61 | |
| 62 | private boolean intersects = false; |
| 63 | |
| 64 | @Override |
| 65 | public boolean accept(int bitIndex) { |
| 66 | if (get(bitIndex)) { |
| 67 | intersects = true; |
| 68 | return false; |
| 69 | } |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public Boolean getResult() { |
| 75 | return intersects; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public boolean disjoints(IBitSet set) { |
| 81 | return !intersects(set); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public boolean contains(IBitSet set) { |
| 86 | return set.iterateBits(new ContainsAction()); |
| 87 | } |
| 88 | |
| 89 | private class ContainsAction implements Action<Boolean> { |
nothing calls this directly
no outgoing calls
no test coverage detected