Refine search
/** helper for gap encoding. */ private static byte[] bitSetToByteArray(BitSet bitSet) { while (bitSet.length() % 8 != 0) { bitSet.set(bitSet.length(), true); } byte[] array = new byte[bitSet.length()/8]; for (int i = 0; i < array.length; i++) { int offset = i * 8; int index = 0; for (int j = 0; j < 8; j++) { index <<= 1; if (bitSet.get(offset+j)) { index++; } } array[i] = (byte)(index - 128); } return array; }
/** * Get the fill rate of the space in percent. The value 0 means the space is * completely free, and 100 means it is completely full. * * @return the fill rate (0 - 100) */ public int getFillRate() { int total = set.length(), count = 0; for (int i = 0; i < total; i++) { if (set.get(i)) { count++; } } if (count == 0) { return 0; } return Math.max(1, (int) (100L * count / total)); }
/** * from bit set to byte * @param bs * @return byte represent the bit set */ public static byte toByte(BitSet bs) { int value = 0; for (int i = 0; i < bs.length(); ++i) { if (bs.get(i)) { value += 1 << i; } } if (bs.length() > 7) { throw new IllegalArgumentException("The byte value " + value + " generated according to bit set " + bs + " is out of range, should be limited between [" + Byte.MIN_VALUE + "] to [" + Byte.MAX_VALUE + "]"); } return (byte) value; }
public class Bits { public static BitSet convert(long value) { BitSet bits = new BitSet(); int index = 0; while (value != 0L) { if (value % 2L != 0) { bits.set(index); } ++index; value = value >>> 1; } return bits; } public static long convert(BitSet bits) { long value = 0L; for (int i = 0; i < bits.length(); ++i) { value += bits.get(i) ? (1L << i) : 0L; } return value; } }
private int findNewSectorStart(int sectorsNeeded) { int start = -1; int runLength = 0; for (int i = sectorsUsed.nextClearBit(0); i < sectorsUsed.length(); i++) { if (sectorsUsed.get(i)) { // must reset start = -1; runLength = 0; } else { if (start == -1) { start = i; } runLength++; if (runLength >= sectorsNeeded) { return start; } } } // reached the end, append to the end of the region instead return -1; }
@Override public void serialize(BitSet bitSet, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { jsonGenerator.writeStartArray(); for (int i = 0; i < bitSet.length(); i++) { jsonGenerator.writeBoolean(bitSet.get(i)); } jsonGenerator.writeEndArray(); } }
/** * Support the subscript operator for a Bitset * * @param self a BitSet * @param index index to retrieve * @return value of the bit at the given index * @see java.util.BitSet * @since 1.5.0 */ public static boolean getAt(BitSet self, int index) { int i = normaliseIndex(index, self.length()); return self.get(i); }
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { BitSet bitSet = (BitSet) source; StringBuffer buffer = new StringBuffer(); boolean seenFirst = false; for (int i = 0; i < bitSet.length(); i++) { if (bitSet.get(i)) { if (seenFirst) { buffer.append(','); } else { seenFirst = true; } buffer.append(i); } } writer.setValue(buffer.toString()); }
/** * Checks that given {@code nodesSet} forms fully-connected component. * * @param nodesSet Set of cluster nodes. * @return {@code True} if all given cluster nodes are able to connect to each other. */ public boolean checkFullyConnected(BitSet nodesSet) { int maxIdx = nodesSet.length(); Iterator<Integer> it = new BitSetIterator(nodesSet); while (it.hasNext()) { int idx = it.next(); for (int i = 0; i < maxIdx; i++) { if (i == idx) continue; if (nodesSet.get(i) && !connections[idx].get(i)) return false; } } return true; } }
public void printRule(PrintStream out) { out.print(" Select " + selectValue + " if "); for(int k = 0; k < filterTypes.length(); k++) { if(filterTypes.get(k)) { String key = universalRulesById.get(k); out.print(key + " "); if(filterNotTypes.length() > 0) { out.print(" ifnot "); for(int k = 0; k < filterNotTypes.length(); k++) { if(filterNotTypes.get(k)) { String key = universalRulesById.get(k); out.print(key + " ");