A set of integers, represented by a bit set
| 22 | * A set of integers, represented by a bit set |
| 23 | */ |
| 24 | public class BitIntSet implements IntSet { |
| 25 | |
| 26 | /** also accessed in ListIntSet */ |
| 27 | int[] bits; |
| 28 | |
| 29 | /** |
| 30 | * Constructs an instance. |
| 31 | * |
| 32 | * @param max the maximum value of ints in this set. |
| 33 | */ |
| 34 | public BitIntSet(int max) { |
| 35 | bits = Bits.makeBitSet(max); |
| 36 | } |
| 37 | |
| 38 | /** {@inheritDoc} */ |
| 39 | @Override |
| 40 | public void add(int value) { |
| 41 | ensureCapacity(value); |
| 42 | Bits.set(bits, value, true); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Ensures that the bit set has the capacity to represent the given value. |
| 47 | * |
| 48 | * @param value {@code >= 0;} value to represent |
| 49 | */ |
| 50 | private void ensureCapacity(int value) { |
| 51 | if (value >= Bits.getMax(bits)) { |
| 52 | int[] newBits = Bits.makeBitSet( |
| 53 | Math.max(value + 1, 2 * Bits.getMax(bits))); |
| 54 | System.arraycopy(bits, 0, newBits, 0, bits.length); |
| 55 | bits = newBits; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** {@inheritDoc} */ |
| 60 | @Override |
| 61 | public void remove(int value) { |
| 62 | if (value < Bits.getMax(bits)) { |
| 63 | Bits.set(bits, value, false); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** {@inheritDoc} */ |
| 68 | @Override |
| 69 | public boolean has(int value) { |
| 70 | return (value < Bits.getMax(bits)) && Bits.get(bits, value); |
| 71 | } |
| 72 | |
| 73 | /** {@inheritDoc} */ |
| 74 | @Override |
| 75 | public void merge(IntSet other) { |
| 76 | if (other instanceof BitIntSet) { |
| 77 | BitIntSet o = (BitIntSet) other; |
| 78 | ensureCapacity(Bits.getMax(o.bits) + 1); |
| 79 | Bits.or(bits, o.bits); |
| 80 | } else if (other instanceof ListIntSet) { |
| 81 | ListIntSet o = (ListIntSet) other; |
nothing calls this directly
no outgoing calls
no test coverage detected