public void test2_bitCount() { for (int i = 1; i < 1000; i += 13) { int[] bits = Bits.makeBitSet(i); int count = 0; for (int j = 0; j < i; j += 20) { Bits.set(bits, j); count++; } for (int j = 7; j < i; j += 11) { if (!Bits.get(bits, j)) { Bits.set(bits, j); count++; } } for (int j = 3; j < i; j += 17) { if (!Bits.get(bits, j)) { Bits.set(bits, j); count++; } } assertEquals(label(i), count, Bits.bitCount(bits)); } }
public void test_clear() { int[] bits = Bits.makeBitSet(100); for (int i = 0; i < bits.length; i++) { bits[i] = -1; } for (int i = 0; i < 100; i++) { if ((i % 5) == 0) { Bits.clear(bits, i); } } for (int i = 0; i < 100; i++) { boolean expect = (i % 5) != 0; assertTrue(label(i), Bits.get(bits, i) == expect); } }
/** * Ensures that the bit set has the capacity to represent the given value. * * @param value {@code >= 0;} value to represent */ private void ensureCapacity(int value) { if (value >= Bits.getMax(bits)) { int[] newBits = Bits.makeBitSet( Math.max(value + 1, 2 * Bits.getMax(bits))); System.arraycopy(bits, 0, newBits, 0, bits.length); bits = newBits; } }
/** {@inheritDoc} */ public void merge(IntSet other) { if (other instanceof BitIntSet) { BitIntSet o = (BitIntSet) other; ensureCapacity(Bits.getMax(o.bits) + 1); Bits.or(bits, o.bits); } else if (other instanceof ListIntSet) { ListIntSet o = (ListIntSet) other; int sz = o.ints.size(); if (sz > 0) { ensureCapacity(o.ints.get(sz - 1)); } for (int i = 0; i < o.ints.size(); i++) { Bits.set(bits, o.ints.get(i), true); } } else { IntIterator iter = other.iterator(); while (iter.hasNext()) { add(iter.next()); } } }
/** * Processes the given "work set" by repeatedly finding the lowest bit * in the set, clearing it, and parsing and visiting the instruction at * the indicated offset (that is, the bit index), repeating until the * work set is empty. It is expected that the visitor will regularly * set new bits in the work set during the process. * * @param workSet {@code non-null;} the work set to process * @param visitor {@code non-null;} visitor to call back to for * each instruction */ public void processWorkSet(int[] workSet, Visitor visitor) { if (visitor == null) { throw new NullPointerException("visitor == null"); } for (;;) { int offset = Bits.findFirst(workSet, 0); if (offset < 0) { break; } Bits.clear(workSet, offset); parseInstruction(offset, visitor); visitor.setPreviousOffset(offset); } }
Bits.set(workSet, 0); Bits.set(blockSet, 0); while (!Bits.isEmpty(workSet)) { try { bytes.processWorkSet(workSet, this); int start = item.getStartPc(); int end = item.getEndPc(); if (Bits.anyInRange(liveSet, start, end)) { Bits.set(blockSet, start); Bits.set(blockSet, end); addWorkIfNecessary(item.getHandlerPc(), true);
public void test3_get() { int[] bits = Bits.makeBitSet(100); for (int i = 0; i < 100; i++) { Bits.set(bits, i, (i % 5) == 0); } for (int i = 0; i < 100; i++) { boolean expect = (i % 5) == 0; assertTrue(label(i), Bits.get(bits, i) == expect); } }
public void test1_get() { int[] bits = Bits.makeBitSet(100); for (int i = 0; i < 100; i++) { assertFalse(label(i), Bits.get(bits, i)); } }
public void test2_isEmpty() { for (int i = 1; i < 1000; i += 11) { int[] bits = Bits.makeBitSet(i); for (int j = i % 11; j >= 0; j--) { int x = i - 1 - (j * 13); if (x >= 0) { Bits.set(bits, x); } } assertFalse(label(i), Bits.isEmpty(bits)); } }
/** {@inheritDoc} */ @Override public int next() { if (!hasNext()) { throw new NoSuchElementException(); } int ret = idx; idx = Bits.findFirst(bits, idx+1); return ret; } };
/** {@inheritDoc} */ @Override public int elements() { return Bits.bitCount(bits); }
public void test4_anyInRange() { int[] bits = new int[100]; for (int i = 0; i < bits.length; i++) { bits[i] = -1; } for (int i = 1; i < 100; i += 11) { assertTrue(label(i), Bits.anyInRange(bits, 0, i)); } }
/** * Constructs an instance. * * @param max the maximum value of ints in this set. */ public BitIntSet(int max) { bits = Bits.makeBitSet(max); }
/** {@inheritDoc} */ @Override public void add(int value) { ensureCapacity(value); Bits.set(bits, value, true); }
public void test_getMax() { for (int i = 0; i < 4000; i += 59) { int expect = i >> 5; if ((expect * 32) < i) { expect++; } assertEquals(label(i), expect * 32, Bits.getMax(new int[expect])); } }
public void test1_isEmpty() { for (int i = 0; i < 10; i++) { assertTrue(label(i), Bits.isEmpty(new int[i])); } }
/** @inheritDoc */ public void merge(IntSet other) { if (other instanceof BitIntSet) { BitIntSet o = (BitIntSet) other; ensureCapacity(Bits.getMax(o.bits) + 1); Bits.or(bits, o.bits); } else if (other instanceof ListIntSet) { ListIntSet o = (ListIntSet) other; int sz = o.ints.size(); if (sz > 0) { ensureCapacity(o.ints.get(sz - 1)); } for (int i = 0; i < o.ints.size(); i++) { Bits.set(bits, o.ints.get(i), true); } } else { IntIterator iter = other.iterator(); while (iter.hasNext()) { add(iter.next()); } } }
/** * Processes the given "work set" by repeatedly finding the lowest bit * in the set, clearing it, and parsing and visiting the instruction at * the indicated offset (that is, the bit index), repeating until the * work set is empty. It is expected that the visitor will regularly * set new bits in the work set during the process. * * @param workSet {@code non-null;} the work set to process * @param visitor {@code non-null;} visitor to call back to for * each instruction */ public void processWorkSet(int[] workSet, Visitor visitor) { if (visitor == null) { throw new NullPointerException("visitor == null"); } for (;;) { int offset = Bits.findFirst(workSet, 0); if (offset < 0) { break; } Bits.clear(workSet, offset); parseInstruction(offset, visitor); visitor.setPreviousOffset(offset); } }