Example usage for java.util BitSet get

List of usage examples for java.util BitSet get

Introduction

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

Prototype

public boolean get(int bitIndex) 

Source Link

Document

Returns the value of the bit with the specified index.

Usage

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

/**
 * Test a large nof nodes./*from  www.j av  a  2  s.c  o  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:net.bioclipse.cdk.ui.sdfeditor.CDKFingerPrintPropertyCalculator.java

public String toString(Object value) {
    // TODO check if this is right
    BitSet val = (BitSet) value;
    byte[] bytes = new byte[val.length() / 8 + 1];
    for (int i = 0; i < val.length(); i++) {
        if (val.get(i)) {
            bytes[bytes.length - i / 8 - 1] |= 1 << (i % 8);
        }/*from   ww w .  j a v  a  2 s .co m*/
    }
    return new String(new Base64().encode(bytes));
}

From source file:be.error.rpi.adc.ObjectStatusReader.java

@Override
public Pair<AdcChannel, ObjectStatusType> apply(final AdcChannel adcChannel) {
    try {/*from   ww  w .j  ava 2  s .  c om*/
        byte readResult[] = adcChannel.getAdc().read(adcChannel);
        BitSet conversion = valueOf(new byte[] { readResult[1], readResult[0] });

        ObjectStatusType objectStatusType = READ_ERROR;

        if (!conversion.get(15)) {
            long[] l = conversion.toLongArray();
            if (l != null && l.length == 1) {
                BigDecimal value = new BigDecimal(l[0]);
                BigDecimal voltage = value.multiply(step).setScale(2, HALF_UP);
                objectStatusType = objectStatusTypeMapper.map(voltage);
            }
        }
        return of(adcChannel, objectStatusType);
    } catch (IOException ioException) {
        logger.error("Could not read from channel", ioException);
        throw new RuntimeException(ioException);
    }
}

From source file:TypeConversionHelper.java

/**
 * Convert an instance of our value class into a boolean[].
 *
 * @param value Object to be converted//w  ww. j  a v a  2  s  .  co m
 *
 * @return converted boolean array
 */
public static boolean[] getBooleanArrayFromBitSet(BitSet value) {
    if (value == null) {
        return null;
    }

    boolean[] a = new boolean[value.length()];
    for (int i = 0; i < a.length; i++) {
        a[i] = value.get(i);
    }
    return a;
}

From source file:org.apache.tez.runtime.library.common.shuffle.orderedgrouped.ShuffleInputEventHandlerOrderedGrouped.java

private void processDataMovementEvent(DataMovementEvent dmEvent) throws IOException {
    DataMovementEventPayloadProto shufflePayload;
    try {//from  w w  w .j  a  v a2s  . com
        shufflePayload = DataMovementEventPayloadProto.parseFrom(ByteString.copyFrom(dmEvent.getUserPayload()));
    } catch (InvalidProtocolBufferException e) {
        throw new TezUncheckedException("Unable to parse DataMovementEvent payload", e);
    }
    int partitionId = dmEvent.getSourceIndex();
    LOG.info("DME srcIdx: " + partitionId + ", targetIdx: " + dmEvent.getTargetIndex() + ", attemptNum: "
            + dmEvent.getVersion() + ", payload: " + ShuffleUtils.stringify(shufflePayload));
    // TODO NEWTEZ See if this duration hack can be removed.
    int duration = shufflePayload.getRunDuration();
    if (duration > maxMapRuntime) {
        maxMapRuntime = duration;
        scheduler.informMaxMapRunTime(maxMapRuntime);
    }
    if (shufflePayload.hasEmptyPartitions()) {
        try {
            byte[] emptyPartitions = TezCommonUtils
                    .decompressByteStringToByteArray(shufflePayload.getEmptyPartitions());
            BitSet emptyPartitionsBitSet = TezUtilsInternal.fromByteArray(emptyPartitions);
            if (emptyPartitionsBitSet.get(partitionId)) {
                InputAttemptIdentifier srcAttemptIdentifier = new InputAttemptIdentifier(
                        dmEvent.getTargetIndex(), dmEvent.getVersion());
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Source partition: " + partitionId + " did not generate any data. SrcAttempt: ["
                            + srcAttemptIdentifier + "]. Not fetching.");
                }
                scheduler.copySucceeded(srcAttemptIdentifier, null, 0, 0, 0, null);
                return;
            }
        } catch (IOException e) {
            throw new TezUncheckedException("Unable to set " + "the empty partition to succeeded", e);
        }
    }

    InputAttemptIdentifier srcAttemptIdentifier = new InputAttemptIdentifier(dmEvent.getTargetIndex(),
            dmEvent.getVersion(), shufflePayload.getPathComponent());

    URI baseUri = getBaseURI(shufflePayload.getHost(), shufflePayload.getPort(), partitionId);
    scheduler.addKnownMapOutput(shufflePayload.getHost(), shufflePayload.getPort(), partitionId,
            baseUri.toString(), srcAttemptIdentifier);
}

From source file:uniol.apt.extension.ExtendStateFile.java

private String codeToCodeString(BitSet code) {
    StringBuilder builder = new StringBuilder();

    for (int i = 0; i < codeLength; i++) {
        if (code.get(i)) {
            builder.append('1');
        } else {//w ww.  j a  v  a 2 s.  c o m
            builder.append('0');
        }
    }

    return builder.toString();
}

From source file:org.apache.tez.runtime.library.shuffle.common.impl.ShuffleInputEventHandlerImpl.java

private void processDataMovementEvent(DataMovementEvent dme) throws IOException {
    DataMovementEventPayloadProto shufflePayload;
    try {//  ww  w  .ja  v a  2s .  c  o  m
        shufflePayload = DataMovementEventPayloadProto.parseFrom(ByteString.copyFrom(dme.getUserPayload()));
    } catch (InvalidProtocolBufferException e) {
        throw new TezUncheckedException("Unable to parse DataMovementEvent payload", e);
    }
    int srcIndex = dme.getSourceIndex();
    String hostIdentifier = shufflePayload.getHost() + ":" + shufflePayload.getPort();
    LOG.info("Processing DataMovementEvent with srcIndex: " + srcIndex + ", targetIndex: "
            + dme.getTargetIndex() + ", attemptNum: " + dme.getVersion() + ", payload: "
            + ShuffleUtils.stringify(shufflePayload));

    if (shufflePayload.hasEmptyPartitions()) {
        byte[] emptyPartitions = TezCommonUtils
                .decompressByteStringToByteArray(shufflePayload.getEmptyPartitions());
        BitSet emptyPartionsBitSet = TezUtilsInternal.fromByteArray(emptyPartitions);
        if (emptyPartionsBitSet.get(srcIndex)) {
            InputAttemptIdentifier srcAttemptIdentifier = new InputAttemptIdentifier(dme.getTargetIndex(),
                    dme.getVersion());
            LOG.info("Source partition: " + srcIndex + " did not generate any data. SrcAttempt: ["
                    + srcAttemptIdentifier + "]. Not fetching.");
            shuffleManager.addCompletedInputWithNoData(srcAttemptIdentifier);
            return;
        }
    }

    InputAttemptIdentifier srcAttemptIdentifier = new InputAttemptIdentifier(dme.getTargetIndex(),
            dme.getVersion(), shufflePayload.getPathComponent(), (useSharedInputs && srcIndex == 0));

    if (shufflePayload.hasData()) {
        DataProto dataProto = shufflePayload.getData();
        FetchedInput fetchedInput = inputAllocator.allocate(dataProto.getRawLength(),
                dataProto.getCompressedLength(), srcAttemptIdentifier);
        moveDataToFetchedInput(dataProto, fetchedInput, hostIdentifier);
        shuffleManager.addCompletedInputWithData(srcAttemptIdentifier, fetchedInput);
    } else {
        shuffleManager.addKnownInput(shufflePayload.getHost(), shufflePayload.getPort(), srcAttemptIdentifier,
                srcIndex);
    }

}

From source file:org.apache.tez.runtime.library.common.shuffle.impl.ShuffleInputEventHandlerImpl.java

private void processDataMovementEvent(DataMovementEvent dme) throws IOException {
    DataMovementEventPayloadProto shufflePayload;
    try {//ww  w.ja  va2 s  .  c om
        shufflePayload = DataMovementEventPayloadProto.parseFrom(ByteString.copyFrom(dme.getUserPayload()));
    } catch (InvalidProtocolBufferException e) {
        throw new TezUncheckedException("Unable to parse DataMovementEvent payload", e);
    }
    int srcIndex = dme.getSourceIndex();
    String hostIdentifier = shufflePayload.getHost() + ":" + shufflePayload.getPort();
    LOG.info("DME srcIdx: " + srcIndex + ", targetIndex: " + dme.getTargetIndex() + ", attemptNum: "
            + dme.getVersion() + ", payload: " + ShuffleUtils.stringify(shufflePayload));

    if (shufflePayload.hasEmptyPartitions()) {
        byte[] emptyPartitions = TezCommonUtils
                .decompressByteStringToByteArray(shufflePayload.getEmptyPartitions());
        BitSet emptyPartionsBitSet = TezUtilsInternal.fromByteArray(emptyPartitions);
        if (emptyPartionsBitSet.get(srcIndex)) {
            InputAttemptIdentifier srcAttemptIdentifier = new InputAttemptIdentifier(dme.getTargetIndex(),
                    dme.getVersion());
            if (LOG.isDebugEnabled()) {
                LOG.debug("Source partition: " + srcIndex + " did not generate any data. SrcAttempt: ["
                        + srcAttemptIdentifier + "]. Not fetching.");
            }
            shuffleManager.addCompletedInputWithNoData(srcAttemptIdentifier);
            return;
        }
    }

    InputAttemptIdentifier srcAttemptIdentifier = new InputAttemptIdentifier(dme.getTargetIndex(),
            dme.getVersion(), shufflePayload.getPathComponent(), (useSharedInputs && srcIndex == 0));

    if (shufflePayload.hasData()) {
        DataProto dataProto = shufflePayload.getData();
        FetchedInput fetchedInput = inputAllocator.allocate(dataProto.getRawLength(),
                dataProto.getCompressedLength(), srcAttemptIdentifier);
        moveDataToFetchedInput(dataProto, fetchedInput, hostIdentifier);
        shuffleManager.addCompletedInputWithData(srcAttemptIdentifier, fetchedInput);
    } else {
        shuffleManager.addKnownInput(shufflePayload.getHost(), shufflePayload.getPort(), srcAttemptIdentifier,
                srcIndex);
    }

}

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.//ww w.j  a  v  a2s. 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.hadoop.hive.ql.optimizer.SharedWorkOptimizer.java

private static boolean areMergeable(ParseContext pctx, SharedWorkOptimizerCache optimizerCache,
        TableScanOperator tsOp1, TableScanOperator tsOp2) throws SemanticException {
    // First we check if the two table scan operators can actually be merged
    // If schemas do not match, we currently do not merge
    List<String> prevTsOpNeededColumns = tsOp1.getNeededColumns();
    List<String> tsOpNeededColumns = tsOp2.getNeededColumns();
    if (prevTsOpNeededColumns.size() != tsOpNeededColumns.size()) {
        return false;
    }//from  ww  w . j  a v a 2 s. c  o  m
    boolean notEqual = false;
    for (int i = 0; i < prevTsOpNeededColumns.size(); i++) {
        if (!prevTsOpNeededColumns.get(i).equals(tsOpNeededColumns.get(i))) {
            notEqual = true;
            break;
        }
    }
    if (notEqual) {
        return false;
    }
    // If row limit does not match, we currently do not merge
    if (tsOp1.getConf().getRowLimit() != tsOp2.getConf().getRowLimit()) {
        return false;
    }
    // If partitions do not match, we currently do not merge
    PrunedPartitionList prevTsOpPPList = pctx.getPrunedPartitions(tsOp1);
    PrunedPartitionList tsOpPPList = pctx.getPrunedPartitions(tsOp2);
    if (!prevTsOpPPList.getPartitions().equals(tsOpPPList.getPartitions())) {
        return false;
    }
    // If is a DPP, check if actually it refers to same target, column, etc.
    // Further, the DPP value needs to be generated from same subtree
    List<Operator<?>> dppsOp1 = new ArrayList<>(optimizerCache.tableScanToDPPSource.get(tsOp1));
    List<Operator<?>> dppsOp2 = new ArrayList<>(optimizerCache.tableScanToDPPSource.get(tsOp2));
    if (dppsOp1.isEmpty() && dppsOp2.isEmpty()) {
        return true;
    }
    for (int i = 0; i < dppsOp1.size(); i++) {
        Operator<?> op = dppsOp1.get(i);
        if (op instanceof ReduceSinkOperator) {
            Set<Operator<?>> ascendants = findAscendantWorkOperators(pctx, optimizerCache, op);
            if (ascendants.contains(tsOp2)) {
                dppsOp1.remove(i);
                i--;
            }
        }
    }
    for (int i = 0; i < dppsOp2.size(); i++) {
        Operator<?> op = dppsOp2.get(i);
        if (op instanceof ReduceSinkOperator) {
            Set<Operator<?>> ascendants = findAscendantWorkOperators(pctx, optimizerCache, op);
            if (ascendants.contains(tsOp1)) {
                dppsOp2.remove(i);
                i--;
            }
        }
    }
    if (dppsOp1.size() != dppsOp2.size()) {
        // Only first or second operator contains DPP pruning
        return false;
    }
    // Check if DPP branches are equal
    for (int i = 0; i < dppsOp1.size(); i++) {
        Operator<?> dppOp1 = dppsOp1.get(i);
        BitSet bs = new BitSet();
        for (int j = 0; j < dppsOp2.size(); j++) {
            if (!bs.get(j)) {
                // If not visited yet
                Operator<?> dppOp2 = dppsOp2.get(j);
                if (compareAndGatherOps(pctx, dppOp1, dppOp2) != null) {
                    // The DPP operator/branch are equal
                    bs.set(j);
                    break;
                }
            }
        }
        if (bs.cardinality() == i) {
            return false;
        }
    }
    return true;
}