Bit set based implementation of java.util.Set. To store objects in bit set, the subclasses need to take care of the mappings between objects and indexes by implementing #getIndex and #getElement. The objects stored in the same bit set s should preserve the invaria
| 53 | * @param <E> type of elements |
| 54 | */ |
| 55 | public abstract class GenericBitSet<E> extends AbstractSetEx<E> |
| 56 | implements Serializable { |
| 57 | |
| 58 | protected IBitSet bitSet; |
| 59 | |
| 60 | protected GenericBitSet(boolean isSparse) { |
| 61 | bitSet = IBitSet.newBitSet(isSparse); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public boolean contains(Object o) { |
| 66 | checkInvariant(o); |
| 67 | return bitSet.get(getIndex((E) o)); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public boolean add(E e) { |
| 72 | checkInvariant(e); |
| 73 | return bitSet.set(getIndex(e)); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public boolean remove(Object o) { |
| 78 | checkInvariant(o); |
| 79 | return bitSet.clear(getIndex((E) o)); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * The objects passed to this set should preserve this invariant. |
| 84 | */ |
| 85 | private void checkInvariant(Object o) { |
| 86 | assert o.equals(getElement(getIndex((E) o))); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public boolean containsAll(@Nonnull Collection<?> c) { |
| 91 | if (c instanceof GenericBitSet s) { |
| 92 | checkContext(s); |
| 93 | return bitSet.contains(s.bitSet); |
| 94 | } else { |
| 95 | return super.containsAll(c); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public boolean addAll(@Nonnull Collection<? extends E> c) { |
| 101 | if (c instanceof GenericBitSet s) { |
| 102 | checkContext(s); |
| 103 | return bitSet.or(s.bitSet); |
| 104 | } else { |
| 105 | return super.addAll(c); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public boolean removeAll(Collection<?> c) { |
| 111 | if (c instanceof GenericBitSet s) { |
| 112 | checkContext(s); |
nothing calls this directly
no outgoing calls
no test coverage detected