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

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

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

BitSet.wordIndex介绍

[英]Given a bit index, return word index containing it.
[中]给定位索引,返回包含它的单词索引。

代码示例

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

private void initWords(int nbits) {
  words = new long[wordIndex(nbits-1) + 1];
}

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

private void initWords(int nbits) {
  words = new long[wordIndex(nbits-1) + 1];
}

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

/**
 * Returns the value of the bit with the specified index. The value
 * is {@code true} if the bit with the index {@code bitIndex}
 * is currently set in this {@code BitSet}; otherwise, the result
 * is {@code false}.
 *
 * @param  bitIndex   the bit index
 * @return the value of the bit with the specified index
 * @throws IndexOutOfBoundsException if the specified index is negative
 */
public boolean get(int bitIndex) {
  if (bitIndex < 0)
    throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex);
  checkInvariants();
  int wordIndex = wordIndex(bitIndex);
  return (wordIndex < wordsInUse)
    && ((words[wordIndex] & (1L << bitIndex)) != 0);
}

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

/**
 * Returns the value of the bit with the specified index. The value
 * is {@code true} if the bit with the index {@code bitIndex}
 * is currently set in this {@code BitSet}; otherwise, the result
 * is {@code false}.
 *
 * @param  bitIndex   the bit index
 * @return the value of the bit with the specified index
 * @throws IndexOutOfBoundsException if the specified index is negative
 */
public boolean get(int bitIndex) {
  if (bitIndex < 0)
    throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex);
  checkInvariants();
  int wordIndex = wordIndex(bitIndex);
  return (wordIndex < wordsInUse)
    && ((words[wordIndex] & (1L << bitIndex)) != 0);
}

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

/**
 * Returns the index of the first bit that is set to {@code false}
 * that occurs on or after the specified starting index.
 *
 * @param  fromIndex the index to start checking from (inclusive)
 * @return the index of the next clear bit
 * @throws IndexOutOfBoundsException if the specified index is negative
 * @since  1.4
 */
public int nextClearBit(int fromIndex) {
  // Neither spec nor implementation handle bitsets of maximal length.
  // See 4816253.
  if (fromIndex < 0)
    throw new IndexOutOfBoundsException("fromIndex < 0: " + fromIndex);
  checkInvariants();
  int u = wordIndex(fromIndex);
  if (u >= wordsInUse)
    return fromIndex;
  long word = ~words[u] & (WORD_MASK << fromIndex);
  while (true) {
    if (word != 0)
      return (u * BITS_PER_WORD) + Long.numberOfTrailingZeros(word);
    if (++u == wordsInUse)
      return wordsInUse * BITS_PER_WORD;
    word = ~words[u];
  }
}

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

/**
 * Returns the index of the first bit that is set to {@code false}
 * that occurs on or after the specified starting index.
 *
 * @param  fromIndex the index to start checking from (inclusive)
 * @return the index of the next clear bit
 * @throws IndexOutOfBoundsException if the specified index is negative
 * @since  1.4
 */
public int nextClearBit(int fromIndex) {
  // Neither spec nor implementation handle bitsets of maximal length.
  // See 4816253.
  if (fromIndex < 0)
    throw new IndexOutOfBoundsException("fromIndex < 0: " + fromIndex);
  checkInvariants();
  int u = wordIndex(fromIndex);
  if (u >= wordsInUse)
    return fromIndex;
  long word = ~words[u] & (WORD_MASK << fromIndex);
  while (true) {
    if (word != 0)
      return (u * BITS_PER_WORD) + Long.numberOfTrailingZeros(word);
    if (++u == wordsInUse)
      return wordsInUse * BITS_PER_WORD;
    word = ~words[u];
  }
}

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

private static boolean get(JsArrayInteger array, int bitIndex) {
  // retrieve the bits for the given index
  int word = getWord(array, wordIndex(bitIndex));
  // shift and mask the bit out
  return ((word >>> (bitOffset(bitIndex))) & 1) == 1;
}

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

/**
 * Sets the bit at the specified index to {@code true}.
 *
 * @param  bitIndex a bit index
 * @throws IndexOutOfBoundsException if the specified index is negative
 * @since  JDK1.0
 */
public void set(int bitIndex) {
  if (bitIndex < 0)
    throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex);
  int wordIndex = wordIndex(bitIndex);
  expandTo(wordIndex);
  words[wordIndex] |= (1L << bitIndex); // Restores invariants
  checkInvariants();
}

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

/**
 * Sets the bit at the specified index to {@code true}.
 *
 * @param  bitIndex a bit index
 * @throws IndexOutOfBoundsException if the specified index is negative
 * @since  JDK1.0
 */
public void set(int bitIndex) {
  if (bitIndex < 0)
    throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex);
  int wordIndex = wordIndex(bitIndex);
  expandTo(wordIndex);
  words[wordIndex] |= (1L << bitIndex); // Restores invariants
  checkInvariants();
}

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

/**
 * Sets the bit specified by the index to {@code false}.
 *
 * @param  bitIndex the index of the bit to be cleared
 * @throws IndexOutOfBoundsException if the specified index is negative
 * @since  JDK1.0
 */
public void clear(int bitIndex) {
  if (bitIndex < 0)
    throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex);
  int wordIndex = wordIndex(bitIndex);
  if (wordIndex >= wordsInUse)
    return;
  words[wordIndex] &= ~(1L << bitIndex);
  recalculateWordsInUse();
  checkInvariants();
}

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

/**
 * Sets the bit specified by the index to {@code false}.
 *
 * @param  bitIndex the index of the bit to be cleared
 * @throws IndexOutOfBoundsException if the specified index is negative
 * @since  JDK1.0
 */
public void clear(int bitIndex) {
  if (bitIndex < 0)
    throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex);
  int wordIndex = wordIndex(bitIndex);
  if (wordIndex >= wordsInUse)
    return;
  words[wordIndex] &= ~(1L << bitIndex);
  recalculateWordsInUse();
  checkInvariants();
}

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

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

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

private static void clear(JsArrayInteger array, int bitIndex) {
  int index = wordIndex(bitIndex);
  int word = getWord(array, index);
  if (word != 0) {
    // mask the correct bit out
    setWord(array, index, word & ~(1 << (bitOffset(bitIndex))));
  }
}

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

/**
 * Sets the bit at the specified index to the complement of its
 * current value.
 *
 * @param  bitIndex the index of the bit to flip
 * @throws IndexOutOfBoundsException if the specified index is negative
 * @since  1.4
 */
public void flip(int bitIndex) {
  if (bitIndex < 0)
    throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex);
  int wordIndex = wordIndex(bitIndex);
  expandTo(wordIndex);
  words[wordIndex] ^= (1L << bitIndex);
  recalculateWordsInUse();
  checkInvariants();
}

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

/**
 * Sets the bit at the specified index to the complement of its
 * current value.
 *
 * @param  bitIndex the index of the bit to flip
 * @throws IndexOutOfBoundsException if the specified index is negative
 * @since  1.4
 */
public void flip(int bitIndex) {
  if (bitIndex < 0)
    throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex);
  int wordIndex = wordIndex(bitIndex);
  expandTo(wordIndex);
  words[wordIndex] ^= (1L << bitIndex);
  recalculateWordsInUse();
  checkInvariants();
}

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

private static void set(JsArrayInteger array, int bitIndex) {
  int index = wordIndex(bitIndex);
  array.set(index, getWord(array, index) | (1 << (bitOffset(bitIndex))));
}

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

private static void flip(JsArrayInteger array, int bitIndex) {
  // calculate index and offset
  int index = wordIndex(bitIndex);
  int offset = bitOffset(bitIndex);
  // figure out if the bit is on or off
  int word = getWord(array, index);
  if (((word >>> offset) & 1) == 1) {
    // if on, turn it off
    setWord(array, index, word & ~(1 << offset));
  } else {
    // if off, turn it on
    array.set(index, word | (1 << offset));
  }
};

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

private static void set(JsArrayInteger array, int fromIndex, int toIndex) {
  int first = wordIndex(fromIndex);
  int last = wordIndex(toIndex);
  int startBit = bitOffset(fromIndex);
  int endBit = bitOffset(toIndex);
  if (first == last) {
    // set the bits in between first and last
    maskInWord(array, first, startBit, endBit);
  } else {
    // set the bits from fromIndex to the next 32 bit boundary
    if (startBit != 0) {
      maskInWord(array, first++, startBit, 32);
    }
    // set the bits from the last 32 bit boundary to the toIndex
    if (endBit != 0) {
      maskInWord(array, last, 0, endBit);
    }
    //
    // set everything in between
    //
    for (int i = first; i < last; i++) {
      array.set(i, 0xffffffff);
    }
  }
}

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

public int nextClearBit(int fromIndex) {
  checkIndex(fromIndex);
  int index = wordIndex(fromIndex);
  // special case for first index
  int fromBit = fromIndex - (bitIndex(index));
  int word = getWord(array, index);
  for (int i = fromBit; i < 32; i++) {
    if ((word & (1 << i)) == 0) {
      return (bitIndex(index)) + i;
    }
  }
  // loop through the rest
  while (true) {
    index++;
    word = getWord(array, index);
    if (word != 0xffffffff) {
      return (bitIndex(index)) + Integer.numberOfTrailingZeros(~word);
    }
  }
}

代码示例来源: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));
};

相关文章