/** * Accumulates the given instance to this one via logical OR. * * @param that that should be accumulated * @return this instance */ public Bitmask orInPlace(Bitmask that) { this.ensureCapacity(that.bits.length << WORD_ADDRESS_BITS); for (int i = 0; i < that.bits.length; i++) { this.bits[i] |= that.bits[i]; } this.cardinalityCache = -1; return this; }
/** * Accumulates the given instance to this one via logical AND NOT. * * @param that that should be accumulated * @return this instance */ public Bitmask andNotInPlace(Bitmask that) { this.ensureCapacity(that.bits.length << WORD_ADDRESS_BITS); for (int i = 0; i < that.bits.length; i++) { this.bits[i] &= ~that.bits[i]; } this.cardinalityCache = -1; return this; }
/** * Accumulates the given instance to this one via logical AND. * * @param that that should be accumulated * @return this instance */ public Bitmask andInPlace(Bitmask that) { this.ensureCapacity(that.bits.length << WORD_ADDRESS_BITS); for (int i = 0; i < that.bits.length; i++) { this.bits[i] &= that.bits[i]; } for (int i = that.bits.length; i < this.bits.length; i++) { this.bits[i] = 0; } this.cardinalityCache = -1; return this; }
/** * Sets the bit at the given index. * * @param index where the bit should be set * @return whether this instance was changed */ public boolean set(int index) { if (!this.ensureCapacity(index) && this.get(index)) { return false; } final int longPos = getLongPos(index); final int offset = getOffset(index); this.bits[longPos] = this.bits[longPos] | (1L << offset); if (this.cardinalityCache != -1) this.cardinalityCache++; return true; }
/** * Flips all bits in the given range * * @param fromIndex inclusive start index of the range * @param toIndex exclusive end index of the range * @return this instance */ public Bitmask flip(int fromIndex, int toIndex) { if (fromIndex < toIndex) { this.ensureCapacity(toIndex - 1); int fromLongPos = getLongPos(fromIndex); int untilLongPos = getLongPos(toIndex - 1); for (int longPos = fromLongPos; longPos <= untilLongPos; longPos++) { int fromOffset = (longPos == fromLongPos) ? getOffset(fromIndex) : 0; int untilOffset = (longPos == untilLongPos) ? getOffset(toIndex - 1) + 1 : BITS_PER_WORD; long flipMask = createAllSetBits(untilOffset) ^ createAllSetBits(fromOffset); this.bits[longPos] ^= flipMask; } this.cardinalityCache = -1; } return this; }