Example usage for java.util BitSet cardinality

List of usage examples for java.util BitSet cardinality

Introduction

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

Prototype

public int cardinality() 

Source Link

Document

Returns the number of bits set to true in this BitSet .

Usage

From source file:org.apache.pig.backend.local.executionengine.LocalPigLauncher.java

private int runPipeline(POStore[] leaves, PigContext pc) throws IOException, ExecException {
    BitSet bs = new BitSet(leaves.length);
    int failed = 0;
    while (true) {
        if (bs.cardinality() == leaves.length) {
            break;
        }/*from  w  w w .j a  va  2s  .co m*/
        for (int i = bs.nextClearBit(0); i < leaves.length; i = bs.nextClearBit(i + 1)) {
            Result res = leaves[i].getNext(DUMMYTUPLE);
            switch (res.returnStatus) {
            case POStatus.STATUS_NULL:
                // good null from store means keep at it.
                continue;
            case POStatus.STATUS_OK:
                // ok shouldn't happen store should have consumed it.
                // fallthrough
            case POStatus.STATUS_ERR:
                leaves[i].cleanUp();
                leaves[i].tearDown();
                failed++;
                failedStores.add(leaves[i].getSFile());
                if ("true".equalsIgnoreCase(pc.getProperties().getProperty("stop.on.failure", "false"))) {
                    int errCode = 6017;
                    String msg = "Execution failed, while processing " + leaves[i].getSFile().getFileName();

                    throw new ExecException(msg, errCode, PigException.REMOTE_ENVIRONMENT);
                }
                bs.set(i);
                break;
            case POStatus.STATUS_EOP:
                leaves[i].tearDown();
                succeededStores.add(leaves[i].getSFile());
                // fallthrough
            default:
                bs.set(i);
                break;
            }
        }
    }
    return failed;
}

From source file:org.jax.haplotype.analysis.StrainBinaryPartitionSignificanceTester.java

/**
 * Test the significance of the given responses
 * @param strainPartitions/* w w w.  jav a 2  s  . co m*/
 *          the partitions to test
 * @param strainResponses
 *          the responses
 * @return
 *          test p-values. this will be an array as long as the
 *          input partitions
 */
public double[] tTestSingleResponseSignificance(List<? extends BinaryStrainPartition> strainPartitions,
        double[] strainResponses) {
    double[] significanceValues = new double[strainPartitions.size()];
    for (int currPartitionIndex = 0; currPartitionIndex < strainPartitions.size(); currPartitionIndex++) {
        BinaryStrainPartition currPartition = strainPartitions.get(currPartitionIndex);

        // segregate the responses
        BitSet currStrainBitSet = currPartition.getStrainBitSet();
        int currStrainCount = currStrainBitSet.cardinality();
        double[] insidePartitionResponses = new double[currStrainCount];
        double[] outsidePartitionResponses = new double[strainResponses.length - currStrainCount];

        int currInsidePartitionCursor = 0;
        int currOutsidePartitionCursor = 0;
        for (int responseIndex = 0; responseIndex < strainResponses.length; responseIndex++) {
            if (currStrainBitSet.get(responseIndex)) {
                // this response is in the partition
                insidePartitionResponses[currInsidePartitionCursor] = strainResponses[responseIndex];
                currInsidePartitionCursor++;
            } else {
                // this response is outside the partition
                outsidePartitionResponses[currOutsidePartitionCursor] = strainResponses[responseIndex];
                currOutsidePartitionCursor++;
            }
        }

        assert currInsidePartitionCursor == insidePartitionResponses.length;
        assert currOutsidePartitionCursor == outsidePartitionResponses.length;

        // perform t-test on segregated responses
        if (insidePartitionResponses.length <= 2 || outsidePartitionResponses.length <= 2) {
            significanceValues[currPartitionIndex] = 1.0;
        } else {
            DescriptiveStatistics insidePartitionResponseSummary = new DescriptiveStatistics();
            for (double currInsideResponseValue : insidePartitionResponses) {
                insidePartitionResponseSummary.addValue(currInsideResponseValue);
            }

            DescriptiveStatistics outsidePartitionResponseSummary = new DescriptiveStatistics();
            for (double currOutsideResponseValue : outsidePartitionResponses) {
                outsidePartitionResponseSummary.addValue(currOutsideResponseValue);
            }

            try {
                double pValue = this.tTester.tTest(insidePartitionResponseSummary,
                        outsidePartitionResponseSummary);

                significanceValues[currPartitionIndex] = pValue;
            } catch (MathException ex) {
                throw new IllegalStateException(ex);
            }
        }
    }

    return significanceValues;
}

From source file:fingerprints.helper.BloomFilter.java

/**
 *
 * @param other//from w  ww  .  j  a va 2  s  .  c o  m
 * @return
 */
public int intersect(BloomFilter<T> other) {
    BitSet intersection = (BitSet) this.bitSet.clone();
    intersection.and(other.bitSet);
    return (intersection.cardinality() / bitSetSize);
}

From source file:org.apache.tez.dag.app.dag.impl.DAGSchedulerNaturalOrderControlled.java

private boolean scheduledTasksForwarded(Vertex vertex) {
    boolean canSchedule = false;
    BitSet scheduledTasks = vertexScheduledTasks.get(vertex.getName());
    if (scheduledTasks != null) {
        if (scheduledTasks.cardinality() >= vertex.getTotalTasks()) {
            canSchedule = true;//  w w  w .  jav a  2s  .  c  o m
        }
    }
    return canSchedule;
}

From source file:org.jax.haplotype.analysis.StrainBinaryPartitionSignificanceTester.java

/**
 * Test the significance of the given responses
 * @param genomicPartitions/* w w w . j  a va  2  s  . c o m*/
 *          the partitions to test
 * @param strainResponses
 *          the responses
 * @return
 *          significance values. this will be an array as long as the
 *          input partitions
 */
public double[] normalizedTestSingleResponseSignificance(
        List<? extends PartitionedIntervalSet> genomicPartitions, double[] strainResponses) {
    double[] sinificanceValues = new double[genomicPartitions.size()];
    for (int currPartitionIndex = 0; currPartitionIndex < genomicPartitions.size(); currPartitionIndex++) {
        BinaryStrainPartition currPartition = genomicPartitions.get(currPartitionIndex);

        // segregate the responses
        BitSet currPartitionStrainBitSet = currPartition.getStrainBitSet();
        int currPartitionChromoCount = currPartitionStrainBitSet.cardinality();
        double[] insidePartitionResponses = new double[currPartitionChromoCount];
        double[] outsidePartitionResponses = new double[strainResponses.length - currPartitionChromoCount];

        int currInsidePartitionCursor = 0;
        int currOutsidePartitionCursor = 0;
        for (int responseIndex = 0; responseIndex < strainResponses.length; responseIndex++) {
            if (currPartitionStrainBitSet.get(responseIndex)) {
                // this response is in the partition
                insidePartitionResponses[currInsidePartitionCursor] = strainResponses[responseIndex];
                currInsidePartitionCursor++;
            } else {
                // this response is outside the partition
                outsidePartitionResponses[currOutsidePartitionCursor] = strainResponses[responseIndex];
                currOutsidePartitionCursor++;
            }
        }

        assert currInsidePartitionCursor == insidePartitionResponses.length;
        assert currOutsidePartitionCursor == outsidePartitionResponses.length;

        // perform t-test on segregated responses
        if (insidePartitionResponses.length <= 2 || outsidePartitionResponses.length <= 2) {
            sinificanceValues[currPartitionIndex] = 1.0;
        } else {
            DescriptiveStatistics insidePartitionResponseSummary = new DescriptiveStatistics();
            for (double currInsideResponseValue : insidePartitionResponses) {
                insidePartitionResponseSummary.addValue(currInsideResponseValue);
            }

            DescriptiveStatistics outsidePartitionResponseSummary = new DescriptiveStatistics();
            for (double currOutsideResponseValue : outsidePartitionResponses) {
                outsidePartitionResponseSummary.addValue(currOutsideResponseValue);
            }

            try {
                double pValue = this.tTester.tTest(insidePartitionResponseSummary,
                        outsidePartitionResponseSummary);

                // reduce the pValue relative to cumulative extent
                pValue /= genomicPartitions.get(currPartitionIndex).getCumulativeExtentInBasePairs();

                sinificanceValues[currPartitionIndex] = pValue;
            } catch (MathException ex) {
                throw new IllegalStateException(ex);
            }
        }
    }

    return sinificanceValues;
}

From source file:com.bittorrent.mpetazzoni.client.peer.PeerExchange.java

/**
 * Initialize and start a new peer exchange.
 *
 * @param peer The remote peer to communicate with.
 * @param torrent The torrent we're exchanging on with the peer.
 * @param channel A channel on the connected socket to the peer.
 *//*from   www  .ja v a2  s . c  om*/
public PeerExchange(SharingPeer peer, SharedTorrent torrent, SocketChannel channel) throws SocketException {
    this.peer = peer;
    this.torrent = torrent;
    this.channel = channel;

    this.listeners = new HashSet<MessageListener>();
    this.sendQueue = new LinkedBlockingQueue<PeerMessage>();

    if (!this.peer.hasPeerId()) {
        throw new IllegalStateException("Peer does not have a " + "peer ID. Was the handshake made properly?");
    }

    this.in = new IncomingThread();
    this.in.setName("bt-peer(" + this.peer.getShortHexPeerId() + ")-recv");

    this.out = new OutgoingThread();
    this.out.setName("bt-peer(" + this.peer.getShortHexPeerId() + ")-send");
    this.out.setDaemon(true);

    // Automatically start the exchange activity loops
    this.stop = false;
    this.in.start();
    this.out.start();

    logger.debug("Started peer exchange with {} for {}.", this.peer, this.torrent);

    // If we have pieces, start by sending a BITFIELD message to the peer.
    BitSet pieces = this.torrent.getCompletedPieces();
    if (pieces.cardinality() > 0) {
        this.send(PeerMessage.BitfieldMessage.craft(pieces));
    }
}

From source file:com.turn.ttorrent.client.peer.PeerExchange.java

/**
 * Initialize and start a new peer exchange.
 *
 * @param peer The remote peer to communicate with.
 * @param torrent The torrent we're exchanging on with the peer.
 * @param channel A channel on the connected socket to the peer.
 *//* ww  w  .  j  a va2  s.co  m*/
public PeerExchange(SharingPeer peer, SharedTorrent torrent, SocketChannel channel) throws SocketException {
    this.peer = peer;
    this.torrent = torrent;
    this.channel = channel;

    this.listeners = new HashSet<MessageListener>();
    this.sendQueue = new LinkedBlockingQueue<PeerMessage>();

    if (!this.peer.hasPeerId()) {
        throw new IllegalStateException("Peer does not have a " + "peer ID. Was the handshake made properly?");
    }

    this.in = new IncomingThread();
    this.in.setName("bt-peer(" + this.peer.getShortHexPeerId() + ")-recv");

    this.out = new OutgoingThread();
    this.out.setName("bt-peer(" + this.peer.getShortHexPeerId() + ")-send");
    this.out.setDaemon(true);

    this.stop = false;

    logger.debug("Started peer exchange with {} for {}.", this.peer, this.torrent);

    // If we have pieces, start by sending a BITFIELD message to the peer.
    BitSet pieces = this.torrent.getCompletedPieces();
    if (pieces.cardinality() > 0) {
        this.send(PeerMessage.BitfieldMessage.craft(pieces, torrent.getPieceCount()));
    }
}

From source file:com.joliciel.jochre.graphics.VectorizerImpl.java

/**
 * Find n longest lines within shape which connect two points in the outline
 * @param outline//from ww  w  . j a va 2  s.c  om
 * @param maxLines
 * @return
 */
List<LineSegment> getLongestLines(Shape shape, BitSet outline, int maxLines, int threshold) {
    TreeSet<LineSegment> lineSegmentSet = new TreeSet<LineSegment>();

    int outlineCardinality = outline.cardinality();
    int samplingInterval = outlineCardinality / 100;
    if (samplingInterval == 0)
        samplingInterval = 1;

    int samplingIndex = 0;
    for (int y = 0; y < shape.getHeight(); y++) {
        for (int x = 0; x < shape.getWidth(); x++) {
            if (outline.get(y * shape.getWidth() + x)) {
                // this pixel is part of the outline
                if (samplingIndex == 0) {
                    lineSegmentSet.addAll(this.getLinesToEdge(shape, x, y, threshold));
                }
                samplingIndex++;
                if (samplingIndex == samplingInterval)
                    samplingIndex = 0;
            }
        }
    }

    int i = 0;
    List<LineSegment> lineSegments = new ArrayList<LineSegment>();
    for (LineSegment lineSegment : lineSegmentSet) {
        lineSegments.add(lineSegment);
        i++;
        if (i >= maxLines)
            break;
    }

    if (LOG.isDebugEnabled()) {
        i = 0;
        for (LineSegment lineSegment : lineSegments) {
            double slope = (double) (lineSegment.getEndY() - lineSegment.getStartY())
                    / (double) (lineSegment.getEndX() - lineSegment.getStartX());
            LOG.debug("Line " + i++ + "(" + lineSegment.getStartX() + "," + lineSegment.getStartY() + ") " + "("
                    + lineSegment.getEndX() + "," + lineSegment.getEndY() + "). Length = "
                    + lineSegment.getLength() + ", Slope = " + slope);
        }
    }
    return lineSegments;
}

From source file:hivemall.ftvec.ranking.ItemPairsSamplingUDTF.java

@Override
public void process(Object[] args) throws HiveException {
    final int numPosItems;
    final BitSet bits;
    if (bitsetInput) {
        if (_rand == null) {
            this._rand = new Random(43);
        }/*  w ww  .j  ava2s .  c o  m*/
        long[] longs = HiveUtils.asLongArray(args[0], listOI, listElemOI);
        bits = BitSet.valueOf(longs);
        numPosItems = bits.cardinality();
    } else {
        if (_bitset == null) {
            bits = new BitSet();
            this._bitset = bits;
            this._rand = new Random(43);
        } else {
            bits = _bitset;
            bits.clear();
        }
        numPosItems = HiveUtils.setBits(args[0], listOI, listElemOI, bits);
    }

    if (numPosItems == 0) {
        return;
    }
    final int numNegItems = maxItemId + 1 - numPosItems;
    if (numNegItems == 0) {
        return;
    } else if (numNegItems < 0) {
        throw new UDFArgumentException(
                "maxItemId + 1 - numPosItems = " + maxItemId + " + 1 - " + numPosItems + " = " + numNegItems);
    }

    if (withReplacement) {
        sampleWithReplacement(numPosItems, numNegItems, bits);
    } else {
        sampleWithoutReplacement(numPosItems, numNegItems, bits);
    }
}

From source file:org.omnaest.utils.table.impl.TableDataAccessor.java

public void setRow(final int rowIndex, final E... elements) {
    OperationUtils.executeWithLocks(new OperationIntrinsic() {
        @Override//w w w  .j a  va 2 s  .c  om
        public void execute() {
            E[] previousElements = TableDataAccessor.this.tableDataCore.setRow(rowIndex, elements);
            TableDataAccessor.this.modificationCounter.incrementAndGet();

            final BitSet modifiedIndices = ArrayUtils.differenceBitSet(elements, previousElements);
            if (modifiedIndices.cardinality() > 0) {
                TableDataAccessor.this.tableEventDispatcher.handleUpdatedRow(rowIndex, elements,
                        previousElements, modifiedIndices);
            }
        }
    }, this.tableLock.writeLock());
}