Esempio n. 1
0
  public void and(BitSet set) {
    // a & a is just a
    if (this == set) {
      return;
    }

    // trim the second set to avoid extra work
    trimToSize(set.array);

    // check if the length is longer than otherLength
    int otherLength = set.array.length();
    if (array.length() > otherLength) {
      // shrink the array, effectively ANDing those bits to false
      setLengthWords(array, otherLength);
    }

    // truth table
    //
    // case | a | b | a & b | change?
    // 1 | false | false | false | a is already false
    // 2 | false | true | false | a is already false
    // 3 | true | false | false | set a to false
    // 4 | true | true | true | a is already true
    //
    // we only need to change something in case 3, so iterate over set a
    int index = 0;
    while ((index = nextSetWord(array, index)) != -1) {
      setWord(array, index, array.get(index) & getWord(set.array, index));
      index++;
    }
  }
Esempio n. 2
0
  public void clear(int fromIndex, int toIndex) {
    checkRange(fromIndex, toIndex);

    int length = length();
    if (fromIndex >= length) {
      // nothing to do
      return;
    }

    // check to see if toIndex is greater than our array length
    if (toIndex >= length) {
      // truncate the array by setting it's length
      int newLength = wordIndex(fromIndex + 31);
      setLengthWords(array, newLength);

      // remove the extra bits off the end
      if ((bitIndex(newLength)) - fromIndex != 0) {
        maskOutWord(array, newLength - 1, bitOffset(fromIndex), 32);
      }

    } else {
      int first = wordIndex(fromIndex);
      int last = wordIndex(toIndex);
      int startBit = bitOffset(fromIndex);
      int endBit = bitOffset(toIndex);

      if (first == last) {
        // clear the bits in between first and last
        maskOutWord(array, first, startBit, endBit);

      } else {
        // clear the bits from fromIndex to the next 32 bit boundary
        if (startBit != 0) {
          maskOutWord(array, first++, startBit, 32);
        }

        // clear the bits from the last 32 bit boundary to the toIndex
        if (endBit != 0) {
          maskOutWord(array, last, 0, endBit);
        }

        //
        // delete everything in between
        //
        for (int i = first; i < last; i++) {
          deleteWord(array, i);
        }
      }
    }
  }
Esempio n. 3
0
  public BitSet(int nbits) {
    this();

    // throw an exception to be consistent
    // but (do we want to be consistent?)
    if (nbits < 0) {
      throw new NegativeArraySizeException("nbits < 0: " + nbits);
    }

    // even though the array's length is loosely kept to that of Sun's
    // "logical
    // length," this might help in some cases where code uses size() to fill
    // in
    // bits after constructing a BitSet, or after having one passed in as a
    // parameter.
    setLengthWords(array, wordIndex(nbits + 31));
  }