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

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

本文整理了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

  1. /**
  2. * This hash is different than the one described in Sun's documentation. The
  3. * described hash uses 64 bit integers and that's not practical in
  4. * JavaScript.
  5. */
  6. @Override
  7. public int hashCode() {
  8. // FNV constants
  9. final int fnvOffset = 0x811c9dc5;
  10. final int fnvPrime = 0x1000193;
  11. // initialize
  12. final int last = trimToSize(array);
  13. int hash = fnvOffset ^ last;
  14. // loop over the data
  15. for (int i = 0; i <= last; i++) {
  16. int value = getWord(array, i);
  17. // hash one byte at a time using FNV1
  18. hash = (hash * fnvPrime) ^ (value & 0xff);
  19. hash = (hash * fnvPrime) ^ ((value >>> 8) & 0xff);
  20. hash = (hash * fnvPrime) ^ ((value >>> 16) & 0xff);
  21. hash = (hash * fnvPrime) ^ (value >>> 24);
  22. }
  23. return hash;
  24. }

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

  1. /**
  2. * Cloning this {@code BitSet} produces a new {@code BitSet}
  3. * that is equal to it.
  4. * The clone of the bit set is another bit set that has exactly the
  5. * same bits set to {@code true} as this bit set.
  6. *
  7. * @return a clone of this bit set
  8. * @see #size()
  9. */
  10. public Object clone() {
  11. if (! sizeIsSticky)
  12. trimToSize();
  13. try {
  14. BitSet result = (BitSet) super.clone();
  15. result.words = words.clone();
  16. result.checkInvariants();
  17. return result;
  18. } catch (CloneNotSupportedException e) {
  19. throw new InternalError();
  20. }
  21. }

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

  1. /**
  2. * Cloning this {@code BitSet} produces a new {@code BitSet}
  3. * that is equal to it.
  4. * The clone of the bit set is another bit set that has exactly the
  5. * same bits set to {@code true} as this bit set.
  6. *
  7. * @return a clone of this bit set
  8. * @see #size()
  9. */
  10. public Object clone() {
  11. if (! sizeIsSticky)
  12. trimToSize();
  13. try {
  14. BitSet result = (BitSet) super.clone();
  15. result.words = words.clone();
  16. result.checkInvariants();
  17. return result;
  18. } catch (CloneNotSupportedException e) {
  19. throw new InternalError();
  20. }
  21. }

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

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

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

  1. @Override
  2. public boolean equals(Object obj) {
  3. if (this != obj) {
  4. if (!ClassReflection.isInstance(BitSet.class, obj)) {
  5. return false;
  6. }
  7. BitSet other = (BitSet) obj;
  8. int last = trimToSize(array);
  9. if (last != trimToSize(other.array)) {
  10. return false;
  11. }
  12. int index = 0;
  13. while ((index = nextSetWord(array, index)) != -1) {
  14. if (getWord(array, index) != getWord(other.array, index)) {
  15. return false;
  16. }
  17. index++;
  18. }
  19. }
  20. return true;
  21. }

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

  1. /**
  2. * Save the state of the {@code BitSet} instance to a stream (i.e.,
  3. * serialize it).
  4. */
  5. private void writeObject(ObjectOutputStream s)
  6. throws IOException {
  7. checkInvariants();
  8. if (! sizeIsSticky)
  9. trimToSize();
  10. ObjectOutputStream.PutField fields = s.putFields();
  11. fields.put("bits", words);
  12. s.writeFields();
  13. }

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

  1. /**
  2. * Save the state of the {@code BitSet} instance to a stream (i.e.,
  3. * serialize it).
  4. */
  5. private void writeObject(ObjectOutputStream s)
  6. throws IOException {
  7. checkInvariants();
  8. if (! sizeIsSticky)
  9. trimToSize();
  10. ObjectOutputStream.PutField fields = s.putFields();
  11. fields.put("bits", words);
  12. s.writeFields();
  13. }

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

  1. public boolean intersects(BitSet set) {
  2. int last = trimToSize(array);
  3. if (this == set) {
  4. // if it has any bits then it intersects itself
  5. return last != -1;
  6. }
  7. int length = set.array.length();
  8. int index = 0;
  9. while ((index = nextSetWord(array, index)) != -1) {
  10. if ((array.get(index) & getWord(set.array, index)) != 0) {
  11. return true;
  12. }
  13. if (++index >= length) {
  14. // nothing further can intersect
  15. break;
  16. }
  17. }
  18. return false;
  19. }

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

  1. public void andNot(BitSet set) {
  2. // a & !a is false
  3. if (this == set) {
  4. // all falses result in an empty BitSet
  5. clear();
  6. return;
  7. }
  8. // trim the second set to avoid extra work
  9. trimToSize(array);
  10. int length = array.length();
  11. // truth table
  12. //
  13. // case | a | b | !b | a & !b | change?
  14. // 1 | false | false | true | false | a is already false
  15. // 2 | false | true | false | false | a is already false
  16. // 3 | true | false | true | true | a is already true
  17. // 4 | true | true | false | false | set a to false
  18. //
  19. // we only need to change something in case 4
  20. // whenever b is true, a should be false, so iterate over set b
  21. int index = 0;
  22. while ((index = nextSetWord(set.array, index)) != -1) {
  23. setWord(array, index, getWord(array, index) & ~set.array.get(index));
  24. if (++index >= length) {
  25. // nothing further will affect anything
  26. break;
  27. }
  28. }
  29. }

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

  1. public int nextSetBit(int fromIndex) {
  2. checkIndex(fromIndex);
  3. int index = wordIndex(fromIndex);
  4. // check the current word
  5. int word = getWord(array, index);
  6. if (word != 0) {
  7. for (int i = bitOffset(fromIndex); i < 32; i++) {
  8. if ((word & (1 << i)) != 0) {
  9. return (bitIndex(index)) + i;
  10. }
  11. }
  12. }
  13. index++;
  14. // find the next set word
  15. trimToSize(array);
  16. index = nextSetWord(array, index);
  17. if (index == -1) {
  18. return -1;
  19. }
  20. // return the next set bit
  21. return (bitIndex(index))
  22. + Integer.numberOfTrailingZeros(array.get(index));
  23. };

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

  1. public void and(BitSet set) {
  2. // a & a is just a
  3. if (this == set) {
  4. return;
  5. }
  6. // trim the second set to avoid extra work
  7. trimToSize(set.array);
  8. // check if the length is longer than otherLength
  9. int otherLength = set.array.length();
  10. if (array.length() > otherLength) {
  11. // shrink the array, effectively ANDing those bits to false
  12. setLengthWords(array, otherLength);
  13. }
  14. // truth table
  15. //
  16. // case | a | b | a & b | change?
  17. // 1 | false | false | false | a is already false
  18. // 2 | false | true | false | a is already false
  19. // 3 | true | false | false | set a to false
  20. // 4 | true | true | true | a is already true
  21. //
  22. // we only need to change something in case 3, so iterate over set a
  23. int index = 0;
  24. while ((index = nextSetWord(array, index)) != -1) {
  25. setWord(array, index, array.get(index) & getWord(set.array, index));
  26. index++;
  27. }
  28. }

相关文章