Methods Summary |
---|
public static boolean | anyInRange(int[] bits, int start, int end)Returns whether any bits are set to {@code true} in the
specified range.
int idx = findFirst(bits, start);
return (idx >= 0) && (idx < end);
|
public static int | bitCount(int[] bits)Gets the number of bits set to {@code true} in the given bit set.
int len = bits.length;
int count = 0;
for (int i = 0; i < len; i++) {
count += Integer.bitCount(bits[i]);
}
return count;
|
public static void | clear(int[] bits, int idx)Sets the given bit to {@code false}.
int arrayIdx = idx >> 5;
int bit = 1 << (idx & 0x1f);
bits[arrayIdx] &= ~bit;
|
public static int | findFirst(int[] bits, int idx)Finds the lowest-order bit set at or after the given index in the
given bit set.
int len = bits.length;
int minBit = idx & 0x1f;
for (int arrayIdx = idx >> 5; arrayIdx < len; arrayIdx++) {
int word = bits[arrayIdx];
if (word != 0) {
int bitIdx = findFirst(word, minBit);
if (bitIdx >= 0) {
return (arrayIdx << 5) + bitIdx;
}
}
minBit = 0;
}
return -1;
|
public static int | findFirst(int value, int idx)Finds the lowest-order bit set at or after the given index in the
given {@code int}.
value &= ~((1 << idx) - 1); // Mask off too-low bits.
int result = Integer.numberOfTrailingZeros(value);
return (result == 32) ? -1 : result;
|
public static boolean | get(int[] bits, int idx)Gets the value of the bit at the given index.
int arrayIdx = idx >> 5;
int bit = 1 << (idx & 0x1f);
return (bits[arrayIdx] & bit) != 0;
|
public static int | getMax(int[] bits)Gets the maximum index (exclusive) for the given bit set.
return bits.length * 0x20;
|
public static boolean | isEmpty(int[] bits)Returns whether or not the given bit set is empty, that is, whether
no bit is set to {@code true}.
int len = bits.length;
for (int i = 0; i < len; i++) {
if (bits[i] != 0) {
return false;
}
}
return true;
|
public static int[] | makeBitSet(int max)Constructs a bit set to contain bits up to the given index (exclusive).
int size = (max + 0x1f) >> 5;
return new int[size];
|
public static void | or(int[] a, int[] b)Ors bit array {@code b} into bit array {@code a}.
{@code a.length} must be greater than or equal to
{@code b.length}.
for (int i = 0; i < b.length; i++) {
a[i] |= b[i];
}
|
public static void | set(int[] bits, int idx, boolean value)Sets the given bit to the given value.
int arrayIdx = idx >> 5;
int bit = 1 << (idx & 0x1f);
if (value) {
bits[arrayIdx] |= bit;
} else {
bits[arrayIdx] &= ~bit;
}
|
public static void | set(int[] bits, int idx)Sets the given bit to {@code true}.
int arrayIdx = idx >> 5;
int bit = 1 << (idx & 0x1f);
bits[arrayIdx] |= bit;
|
public static java.lang.String | toHuman(int[] bits)
StringBuilder sb = new StringBuilder();
boolean needsComma = false;
sb.append('{");
int bitsLength = 32 * bits.length;
for (int i = 0; i < bitsLength; i++) {
if (Bits.get(bits, i)) {
if (needsComma) {
sb.append(',");
}
needsComma = true;
sb.append(i);
}
}
sb.append('}");
return sb.toString();
|