@author zsombor
| 17 | * |
| 18 | */ |
| 19 | public class BitSet implements Serializable, Cloneable { |
| 20 | |
| 21 | final static int BITS_PER_LONG = 64; |
| 22 | final static int BITS_PER_LONG_SHIFT = 6; |
| 23 | final static long MASK = 0xFFFFFFFFFFFFFFFFL; |
| 24 | |
| 25 | private long[] bits; |
| 26 | |
| 27 | private static int longPosition(int index) { |
| 28 | return index >> BITS_PER_LONG_SHIFT; |
| 29 | } |
| 30 | |
| 31 | private static long bitPosition(int index) { |
| 32 | return 1L << (index % BITS_PER_LONG); |
| 33 | } |
| 34 | |
| 35 | private static long getTrueMask(int fromIndex, int toIndex) { |
| 36 | int currentRange = toIndex - fromIndex; |
| 37 | return (MASK >>> (BITS_PER_LONG - currentRange)) << (fromIndex % BITS_PER_LONG); |
| 38 | } |
| 39 | |
| 40 | public BitSet(int bitLength) { |
| 41 | if (bitLength % BITS_PER_LONG == 0) { |
| 42 | enlarge(longPosition(bitLength)); |
| 43 | } else { |
| 44 | enlarge(longPosition(bitLength) + 1); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | public BitSet() { |
| 49 | enlarge(1); |
| 50 | } |
| 51 | |
| 52 | public void and(BitSet otherBits) { |
| 53 | int min = Math.min(bits.length, otherBits.bits.length); |
| 54 | for (int i = 0; i < min; i++) { |
| 55 | bits[i] &= otherBits.bits[i]; |
| 56 | } |
| 57 | for (int i = min; i < bits.length; i++) { |
| 58 | bits[i] = 0; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | public void andNot(BitSet otherBits) { |
| 63 | int max = Math.max(bits.length, otherBits.bits.length); |
| 64 | enlarge(max); |
| 65 | int min = Math.min(bits.length, otherBits.bits.length); |
| 66 | for (int i = 0; i < min; i++) { |
| 67 | bits[i] &= ~otherBits.bits[i]; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | public void or(BitSet otherBits) { |
| 72 | int max = Math.max(bits.length, otherBits.bits.length); |
| 73 | enlarge(max); |
| 74 | int min = Math.min(bits.length, otherBits.bits.length); |
| 75 | for (int i = 0; i < min; i++) { |
| 76 | bits[i] |= otherBits.bits[i]; |
nothing calls this directly
no outgoing calls
no test coverage detected