Example usage for java.util BitSet set

List of usage examples for java.util BitSet set

Introduction

In this page you can find the example usage for java.util BitSet set.

Prototype

public void set(int fromIndex, int toIndex) 

Source Link

Document

Sets the bits from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to true .

Usage

From source file:com.roche.sequencing.bioinformatics.common.utils.BitSetUtil.java

/**
 * /* ww w . j a v  a  2s .c o  m*/
 * @param fromBytes
 * @param toBitSet
 * @param bitsetStartInBits
 * @return the number of bits written
 */
public static int copy(byte[] fromBytes, int bitsToUse, BitSet toBitSet, int bitsetStartInBits) {
    for (int i = 0; i < bitsToUse; i++) {
        int byteIndex = i / ByteUtil.BITS_PER_BYTE;
        boolean isBitOn = false;
        if (fromBytes.length > byteIndex) {
            Byte theByte = fromBytes[byteIndex];
            int bitIndexInByte = i % ByteUtil.BITS_PER_BYTE;
            isBitOn = ByteUtil.isBitOn(theByte, bitIndexInByte);
        }
        int indexInBitSet = bitsetStartInBits + i;
        toBitSet.set(indexInBitSet, isBitOn);
    }
    return bitsToUse;
}

From source file:com.roche.sequencing.bioinformatics.common.utils.BitSetUtil.java

/**
 * /*from  ww  w . j a  va  2s.  c o  m*/
 * @param fromBytes
 * @param toBitSet
 * @param bitsetStartInBits
 * @return the number of bits written
 */
public static int copy(byte[] fromBytes, BitSet toBitSet, int bitsetStartInBits) {
    int totalBits = fromBytes.length * ByteUtil.BITS_PER_BYTE;
    for (int i = 0; i < totalBits; i++) {
        boolean isBitOn = false;
        int byteIndex = i / ByteUtil.BITS_PER_BYTE;
        if (byteIndex < fromBytes.length) {
            Byte theByte = fromBytes[byteIndex];
            int bitIndexInByte = i % ByteUtil.BITS_PER_BYTE;
            isBitOn = ByteUtil.isBitOn(theByte, bitIndexInByte);
        }
        int indexInBitSet = bitsetStartInBits + i;
        toBitSet.set(indexInBitSet, isBitOn);
    }
    return totalBits;
}

From source file:org.lwes.serializer.Deserializer.java

public static BitSet deserializeBitSet(DeserializerState myState, byte[] bytes) {

    int size = deserializeUINT16(myState, bytes);
    int numBytes = (int) Math.ceil(size / 8.0);
    BitSet bitSet = new BitSet(size);
    int offset = myState.currentIndex();
    int index = 0;
    for (int i = 0; i < numBytes; i++) {
        int val = bytes[offset + i];
        for (int j = 0; j < 8; j++) {
            bitSet.set(index++, ((val & (1 << j)) != 0));
        }/*w w  w .  java 2s.com*/
    }
    myState.incr(numBytes);
    return bitSet;
}

From source file:com.almende.test.dht.TestScale.java

/**
 * Test a large nof nodes./* w w  w .  j  ava2  s .co  m*/
 * 
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 * @throws URISyntaxException
 *             the URI syntax exception
 * @throws InterruptedException
 *             the interrupted exception
 */
@Test
public void testScale() throws IOException, URISyntaxException, InterruptedException {
    DHTAgent[] agents = new DHTAgent[NOFNODES];

    DHTAgent agent = new DHTAgent("agent_0");
    agents[0] = agent;
    for (int i = 1; i < NOFNODES; i++) {
        System.out.print("Created node:agent_" + i + "\r");
        DHTAgent next = new DHTAgent("agent_" + i);
        try {
            next.getDht().join(agent.asNode());
        } catch (NullPointerException e) {
            System.err.println("NPE at:" + i);
            throw e;
        }
        agent = next;
        agents[i] = agent;
    }
    final Key key = Key.fromString("Hello world");
    final ObjectNode value = JOM.createObjectNode();
    value.put("Hello:", "world!");
    agents[0].getDht().iterative_store_value(key, value);

    JsonNode result = agents[(int) Math.floor(Math.random() * NOFNODES)].getDht()
            .iterative_find_value(Key.fromString("Hello world"), false);

    assertEquals(result, value);

    final int otherIdx = (int) Math.floor(Math.random() * NOFNODES);
    JsonNode result2 = agents[otherIdx].getDht().iterative_find_value(Key.fromString("Hello world"), false);
    assertEquals(result2, value);

    final Key key2 = Key.fromString("Some other key");
    final ObjectNode value2 = JOM.createObjectNode();
    value2.put("Hello:", "world2!");
    agents[0].getDht().iterative_store_value(key2, value2);

    JsonNode result3 = agents[(int) Math.floor(Math.random() * NOFNODES)].getDht()
            .iterative_find_value(Key.fromString("Some other key"), false);

    assertEquals(result3, value2);

    JsonNode result4 = agents[otherIdx].getDht().iterative_find_value(Key.fromString("Hello world"), false);
    assertNotSame(result4, value2);
    assertEquals(result4, value);

    JsonNode result5 = agents[otherIdx].getDht().iterative_find_value(Key.fromString("Hello world"), false);
    assertEquals(result5, value);
    JsonNode result6 = agents[otherIdx].getDht().iterative_find_value(Key.fromString("Hello world!"), false);
    assertNotSame(result6, value);
    assertEquals(result6, JOM.createNullNode());

    final BitSet set = key2.getVal();
    set.set(10, !set.get(10));
    final Key key3 = new Key(set);
    final ObjectNode value3 = JOM.createObjectNode();
    value3.put("Hello:", "world3!");
    agents[0].getDht().iterative_store_value(key3, value3);

    JsonNode result7 = agents[otherIdx + 2].getDht().iterative_find_value(key3, false);
    assertEquals(result7, value3);

    int count = 0;
    for (final DHTAgent a : agents) {
        if (a.getDht().hasValues()) {
            count++;
        }
    }
    System.out.println(count + " agents have some value stored.");
}

From source file:org.kuali.student.enrollment.class2.scheduleofclasses.sort.KSComparatorChain.java

protected void initializeComparatorChain() {
    BitSet bitSet = new BitSet(comparators.size());
    int index = 0;
    for (KSComparator comparator : comparators) {
        bitSet.set(index++, comparator.isReverseSort());
    }//from ww w. java 2 s .c  o m
    comparatorChain = new ComparatorChain(comparators, bitSet);
}

From source file:net.solarnetwork.node.control.modbus.heartbeat.ModbusHeartbeatJob.java

private synchronized Boolean setValue(Boolean desiredValue) throws IOException {
    final ModbusNetwork network = (modbusNetwork == null ? null : modbusNetwork.service());
    if (network == null) {
        log.debug("No ModbusNetwork avaialble");
        return Boolean.FALSE;
    }/*from  www .jav a2s .c  o  m*/
    final BitSet bits = new BitSet(1);
    bits.set(0, desiredValue);
    log.info("Setting modbus unit {} register {} value to {}", unitId, address, desiredValue);
    final Integer[] addresses = new Integer[] { address };
    return network.performAction(new ModbusConnectionAction<Boolean>() {

        @Override
        public Boolean doWithConnection(ModbusConnection conn) throws IOException {
            return conn.writeDiscreetValues(addresses, bits);
        }
    }, unitId);
}

From source file:org.apache.kylin.cube.cuboid.algorithm.generic.BitsMutation.java

/**
 * Mutate the given chromosome. Randomly changes one gene.
 *
 * @param original the original chromosome.
 * @return the mutated chromosome./*from w ww  . j  a v  a 2  s.c o m*/
 * @throws IllegalArgumentException if <code>original</code> is not an instance of {@link BitsChromosome}.
 */
public Chromosome mutate(Chromosome original) throws IllegalArgumentException {
    if (!(original instanceof BitsChromosome)) {
        throw new MathIllegalArgumentException(
                new DummyLocalizable("bits mutation only works on BitsChromosome"));
    }

    BitsChromosome origChrom = (BitsChromosome) original;
    BitSet newNey = (BitSet) origChrom.getRepresentation().clone();

    // randomly select a gene
    int geneIndex = GeneticAlgorithm.getRandomGenerator().nextInt(origChrom.getLength());
    // change it
    newNey.set(geneIndex, !newNey.get(geneIndex));

    Chromosome newChrom = origChrom.newBitsChromosome(newNey);
    return newChrom;
}

From source file:org.apache.eagle.alert.engine.serialization.PartitionedEventSerializerTest.java

@Test
public void testBitSet() {
    BitSet bitSet = new BitSet();
    bitSet.set(0, true); // 1
    bitSet.set(1, false); // 0
    bitSet.set(2, true); // 1
    LOG.info("Bit Set Size: {}", bitSet.size());
    LOG.info("Bit Set Byte[]: {}", bitSet.toByteArray());
    LOG.info("Bit Set Byte[]: {}", bitSet.toLongArray());
    LOG.info("BitSet[0]: {}", bitSet.get(0));
    LOG.info("BitSet[1]: {}", bitSet.get(1));
    LOG.info("BitSet[1]: {}", bitSet.get(2));

    byte[] bytes = bitSet.toByteArray();

    BitSet bitSet2 = BitSet.valueOf(bytes);

    LOG.info("Bit Set Size: {}", bitSet2.size());
    LOG.info("Bit Set Byte[]: {}", bitSet2.toByteArray());
    LOG.info("Bit Set Byte[]: {}", bitSet2.toLongArray());
    LOG.info("BitSet[0]: {}", bitSet2.get(0));
    LOG.info("BitSet[1]: {}", bitSet2.get(1));
    LOG.info("BitSet[1]: {}", bitSet2.get(2));

    BitSet bitSet3 = new BitSet();
    bitSet3.set(0, true);/*w  ww .  j  a  v  a2s. c o  m*/
    Assert.assertEquals(1, bitSet3.length());

    BitSet bitSet4 = new BitSet();
    bitSet4.set(0, false);
    Assert.assertEquals(0, bitSet4.length());
    Assert.assertFalse(bitSet4.get(1));
    Assert.assertFalse(bitSet4.get(2));
}

From source file:net.solarnetwork.node.control.modbus.toggle.ModbusToggler.java

private synchronized Boolean setValue(Boolean desiredValue) throws IOException {
    final BitSet bits = new BitSet(1);
    bits.set(0, desiredValue);
    log.info("Setting {} value to {}", controlId, desiredValue);
    final Integer[] addresses = new Integer[] { address };
    return performAction(new ModbusConnectionAction<Boolean>() {

        @Override//from  w  w w. j  av  a  2  s.  c o m
        public Boolean doWithConnection(ModbusConnection conn) throws IOException {
            return conn.writeDiscreetValues(addresses, bits);
        }
    });
}

From source file:org.asoem.greyfish.utils.collect.BitString.java

public static BitString create(final Iterable<Boolean> val) {
    checkNotNull(val);
    final BitSet bs = new BitSet();
    int idx = 0;// w w  w.  j  av a  2 s .co  m
    int cardinality = 0;
    for (final boolean b : val) {
        bs.set(idx++, b);
        if (b) {
            ++cardinality;
        }
    }
    return create(bs, idx, cardinality);
}