java.util.BitSet.trimToSize()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(5.7k)|赞(0)|评价(0)|浏览(115)

本文整理了Java中java.util.BitSet.trimToSize()方法的一些代码示例,展示了BitSet.trimToSize()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BitSet.trimToSize()方法的具体详情如下:
包路径:java.util.BitSet
类名称:BitSet
方法名:trimToSize

BitSet.trimToSize介绍

[英]Attempts to reduce internal storage used for the bits in this bit set. Calling this method may, but is not required to, affect the value returned by a subsequent call to the #size() method.
[中]尝试减少用于此位集中位的内部存储。调用此方法可能会(但不是必需的)影响后续调用#size()方法返回的值。

代码示例

代码示例来源:origin: junkdog/artemis-odb

/**
 * This hash is different than the one described in Sun's documentation. The
 * described hash uses 64 bit integers and that's not practical in
 * JavaScript.
 */
@Override
public int hashCode() {
  // FNV constants
  final int fnvOffset = 0x811c9dc5;
  final int fnvPrime = 0x1000193;
  // initialize
  final int last = trimToSize(array);
  int hash = fnvOffset ^ last;
  // loop over the data
  for (int i = 0; i <= last; i++) {
    int value = getWord(array, i);
    // hash one byte at a time using FNV1
    hash = (hash * fnvPrime) ^ (value & 0xff);
    hash = (hash * fnvPrime) ^ ((value >>> 8) & 0xff);
    hash = (hash * fnvPrime) ^ ((value >>> 16) & 0xff);
    hash = (hash * fnvPrime) ^ (value >>> 24);
  }
  return hash;
}

代码示例来源:origin: jtulach/bck2brwsr

/**
 * Cloning this {@code BitSet} produces a new {@code BitSet}
 * that is equal to it.
 * The clone of the bit set is another bit set that has exactly the
 * same bits set to {@code true} as this bit set.
 *
 * @return a clone of this bit set
 * @see    #size()
 */
public Object clone() {
  if (! sizeIsSticky)
    trimToSize();
  try {
    BitSet result = (BitSet) super.clone();
    result.words = words.clone();
    result.checkInvariants();
    return result;
  } catch (CloneNotSupportedException e) {
    throw new InternalError();
  }
}

代码示例来源:origin: org.apidesign.bck2brwsr/emul

/**
 * Cloning this {@code BitSet} produces a new {@code BitSet}
 * that is equal to it.
 * The clone of the bit set is another bit set that has exactly the
 * same bits set to {@code true} as this bit set.
 *
 * @return a clone of this bit set
 * @see    #size()
 */
public Object clone() {
  if (! sizeIsSticky)
    trimToSize();
  try {
    BitSet result = (BitSet) super.clone();
    result.words = words.clone();
    result.checkInvariants();
    return result;
  } catch (CloneNotSupportedException e) {
    throw new InternalError();
  }
}

代码示例来源:origin: junkdog/artemis-odb

public int length() {
  int last = trimToSize(array);
  if (last == -1) {
    return 0;
  }
  // compute the position of the leftmost bit's index
  int offsets[] = { 16, 8, 4, 2, 1 };
  int bitMasks[] = { 0xffff0000, 0xff00, 0xf0, 0xc, 0x2 };
  int position = bitIndex(last) + 1;
  int word = getWord(array, last);
  for (int i = 0; i < offsets.length; i++) {
    if ((word & bitMasks[i]) != 0) {
      word >>>= offsets[i];
      position += offsets[i];
    }
  }
  return position;
}

代码示例来源:origin: junkdog/artemis-odb

@Override
public boolean equals(Object obj) {
  if (this != obj) {
    if (!ClassReflection.isInstance(BitSet.class, obj)) {
      return false;
    }
    BitSet other = (BitSet) obj;
    int last = trimToSize(array);
    if (last != trimToSize(other.array)) {
      return false;
    }
    int index = 0;
    while ((index = nextSetWord(array, index)) != -1) {
      if (getWord(array, index) != getWord(other.array, index)) {
        return false;
      }
      index++;
    }
  }
  return true;
}

代码示例来源:origin: jtulach/bck2brwsr

/**
 * Save the state of the {@code BitSet} instance to a stream (i.e.,
 * serialize it).
 */
private void writeObject(ObjectOutputStream s)
  throws IOException {
  checkInvariants();
  if (! sizeIsSticky)
    trimToSize();
  ObjectOutputStream.PutField fields = s.putFields();
  fields.put("bits", words);
  s.writeFields();
}

代码示例来源:origin: org.apidesign.bck2brwsr/emul

/**
 * Save the state of the {@code BitSet} instance to a stream (i.e.,
 * serialize it).
 */
private void writeObject(ObjectOutputStream s)
  throws IOException {
  checkInvariants();
  if (! sizeIsSticky)
    trimToSize();
  ObjectOutputStream.PutField fields = s.putFields();
  fields.put("bits", words);
  s.writeFields();
}

代码示例来源:origin: junkdog/artemis-odb

public boolean intersects(BitSet set) {
  int last = trimToSize(array);
  if (this == set) {
    // if it has any bits then it intersects itself
    return last != -1;
  }
  int length = set.array.length();
  int index = 0;
  while ((index = nextSetWord(array, index)) != -1) {
    if ((array.get(index) & getWord(set.array, index)) != 0) {
      return true;
    }
    if (++index >= length) {
      // nothing further can intersect
      break;
    }
  }
  return false;
}

代码示例来源:origin: junkdog/artemis-odb

public void andNot(BitSet set) {
  // a & !a is false
  if (this == set) {
    // all falses result in an empty BitSet
    clear();
    return;
  }
  // trim the second set to avoid extra work
  trimToSize(array);
  int length = array.length();
  // truth table
  //
  // case | a | b | !b | a & !b | change?
  // 1 | false | false | true | false | a is already false
  // 2 | false | true | false | false | a is already false
  // 3 | true | false | true | true | a is already true
  // 4 | true | true | false | false | set a to false
  //
  // we only need to change something in case 4
  // whenever b is true, a should be false, so iterate over set b
  int index = 0;
  while ((index = nextSetWord(set.array, index)) != -1) {
    setWord(array, index, getWord(array, index) & ~set.array.get(index));
    if (++index >= length) {
      // nothing further will affect anything
      break;
    }
  }
}

代码示例来源:origin: junkdog/artemis-odb

public int nextSetBit(int fromIndex) {
  checkIndex(fromIndex);
  int index = wordIndex(fromIndex);
  // check the current word
  int word = getWord(array, index);
  if (word != 0) {
    for (int i = bitOffset(fromIndex); i < 32; i++) {
      if ((word & (1 << i)) != 0) {
        return (bitIndex(index)) + i;
      }
    }
  }
  index++;
  // find the next set word
  trimToSize(array);
  index = nextSetWord(array, index);
  if (index == -1) {
    return -1;
  }
  // return the next set bit
  return (bitIndex(index))
      + Integer.numberOfTrailingZeros(array.get(index));
};

代码示例来源:origin: junkdog/artemis-odb

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++;
  }
}

相关文章