Example usage for java.util BitSet andNot

List of usage examples for java.util BitSet andNot

Introduction

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

Prototype

public void andNot(BitSet set) 

Source Link

Document

Clears all of the bits in this BitSet whose corresponding bit is set in the specified BitSet .

Usage

From source file:de.blizzy.documentr.search.PageIndex.java

Bits getVisibleDocIds(IndexSearcher searcher, Authentication authentication)
        throws IOException, TimeoutException {
    Future<BitSet> branchPagesFuture = taskExecutor
            .submit(new GetVisibleBranchDocIdsTask(searcher, authentication, permissionEvaluator));
    Future<BitSet> inaccessibleDocsFuture = taskExecutor.submit(new GetInaccessibleDocIdsTask(searcher,
            Permission.VIEW, authentication, userStore, permissionEvaluator));
    try {/*from w  w w . j  a  v  a 2 s.  c  o  m*/
        BitSet docIds = branchPagesFuture.get(INTERACTIVE_TIMEOUT, TimeUnit.SECONDS);
        docIds.andNot(inaccessibleDocsFuture.get(INTERACTIVE_TIMEOUT, TimeUnit.SECONDS));
        return new DocIdBitSet(docIds);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    } catch (ExecutionException e) {
        Throwable cause = e.getCause();
        if (cause instanceof IOException) {
            throw (IOException) cause;
        } else {
            throw Util.toRuntimeException(cause);
        }
    } finally {
        branchPagesFuture.cancel(false);
        inaccessibleDocsFuture.cancel(false);
    }
}

From source file:com.bittorrent.mpetazzoni.client.SharedTorrent.java

/**
 * Bit field availability handler./*  w w  w . j  a va2 s .co  m*/
 *
 * <p>
 * Handle updates in piece availability from a peer's BITFIELD message.
 * When this happens, we need to mark in all the pieces the peer has that
 * they can be reached through this peer, thus augmenting the global
 * availability of pieces.
 * </p>
 *
 * @param peer The peer we got the update from.
 * @param availablePieces The pieces availability bit field of the peer.
 */
@Override
public synchronized void handleBitfieldAvailability(SharingPeer peer, BitSet availablePieces) {
    // Determine if the peer is interesting for us or not, and notify it.
    BitSet interesting = (BitSet) availablePieces.clone();
    interesting.andNot(this.completedPieces);
    interesting.andNot(this.requestedPieces);

    if (interesting.cardinality() == 0) {
        peer.notInteresting();
    } else {
        peer.interesting();
    }

    // Record that the peer has all the pieces it told us it had.
    for (int i = availablePieces.nextSetBit(0); i >= 0; i = availablePieces.nextSetBit(i + 1)) {
        this.rarest.remove(this.pieces[i]);
        this.pieces[i].seenAt(peer);
        this.rarest.add(this.pieces[i]);
    }

    logger.trace("Peer {} contributes {} piece(s) ({} interesting) " + "[completed={}; available={}/{}].",
            new Object[] { peer, availablePieces.cardinality(), interesting.cardinality(),
                    this.completedPieces.cardinality(), this.getAvailablePieces().cardinality(),
                    this.pieces.length });
}

From source file:com.bittorrent.mpetazzoni.client.SharedTorrent.java

/**
 * Peer ready handler./*from  w  ww  .ja va2  s . co m*/
 *
 * <p>
 * When a peer becomes ready to accept piece block requests, select a piece
 * to download and go for it.
 * </p>
 *
 * @param peer The peer that became ready.
 */
@Override
public synchronized void handlePeerReady(SharingPeer peer) {
    BitSet interesting = peer.getAvailablePieces();
    interesting.andNot(this.completedPieces);
    interesting.andNot(this.requestedPieces);

    logger.trace("Peer {} is ready and has {} interesting piece(s).", peer, interesting.cardinality());

    // If we didn't find interesting pieces, we need to check if we're in
    // an end-game situation. If yes, we request an already requested piece
    // to try to speed up the end.
    if (interesting.cardinality() == 0) {
        interesting = peer.getAvailablePieces();
        interesting.andNot(this.completedPieces);
        if (interesting.cardinality() == 0) {
            logger.trace("No interesting piece from {}!", peer);
            return;
        }

        if (this.completedPieces.cardinality() < ENG_GAME_COMPLETION_RATIO * this.pieces.length) {
            logger.trace("Not far along enough to warrant end-game mode.");
            return;
        }

        logger.trace("Possible end-game, we're about to request a piece "
                + "that was already requested from another peer.");
    }

    // Extract the RAREST_PIECE_JITTER rarest pieces from the interesting
    // pieces of this peer.
    ArrayList<Piece> choice = new ArrayList<Piece>(RAREST_PIECE_JITTER);
    synchronized (this.rarest) {
        for (Piece piece : this.rarest) {
            if (interesting.get(piece.getIndex())) {
                choice.add(piece);
                if (choice.size() >= RAREST_PIECE_JITTER) {
                    break;
                }
            }
        }
    }

    Piece chosen = choice.get(this.random.nextInt(Math.min(choice.size(), RAREST_PIECE_JITTER)));
    this.requestedPieces.set(chosen.getIndex());

    logger.trace("Requesting {} from {}, we now have {} " + "outstanding request(s): {}",
            new Object[] { chosen, peer, this.requestedPieces.cardinality(), this.requestedPieces });

    peer.downloadPiece(chosen);
}

From source file:com.turn.ttorrent.client.SharedTorrent.java

/**
 * Peer ready handler./*from   w  w  w.j a  v a 2s.  c  o  m*/
 *
 * <p>
 * When a peer becomes ready to accept piece block requests, select a piece
 * to download and go for it.
 * </p>
 *
 * @param peer The peer that became ready.
 */
@Override
public synchronized void handlePeerReady(SharingPeer peer) {
    BitSet interesting = peer.getAvailablePieces();
    interesting.andNot(this.completedPieces);
    interesting.andNot(this.requestedPieces);

    logger.trace("Peer {} is ready and has {} interesting piece(s).", peer, interesting.cardinality());

    // If we didn't find interesting pieces, we need to check if we're in
    // an end-game situation. If yes, we request an already requested piece
    // to try to speed up the end.
    if (interesting.cardinality() == 0) {
        interesting = peer.getAvailablePieces();
        interesting.andNot(this.completedPieces);
        if (interesting.cardinality() == 0) {
            logger.trace("No interesting piece from {}!", peer);
            return;
        }

        if (this.completedPieces.cardinality() < ENG_GAME_COMPLETION_RATIO * this.pieces.length) {
            logger.trace("Not far along enough to warrant end-game mode.");
            return;
        }

        logger.trace("Possible end-game, we're about to request a piece "
                + "that was already requested from another peer.");
    }

    Piece chosen = requestStrategy.choosePiece(rarest, interesting, pieces);
    this.requestedPieces.set(chosen.getIndex());

    logger.trace("Requesting {} from {}, we now have {} " + "outstanding request(s): {}",
            new Object[] { chosen, peer, this.requestedPieces.cardinality(), this.requestedPieces });

    peer.downloadPiece(chosen);
}

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

protected final BitString standardAndNot(final BitString other) {
    final BitSet bitSetCopy = this.bitSet();
    bitSetCopy.andNot(other.bitSet());
    return BitString.create(bitSetCopy, Math.max(size(), other.size()));
}

From source file:org.roaringbitmap.TestRoaringBitmap.java

public void rTest(final int N) {
    System.out.println("rtest N=" + N);
    for (int gap = 1; gap <= 65536; gap *= 2) {
        final BitSet bs1 = new BitSet();
        final RoaringBitmap rb1 = new RoaringBitmap();
        for (int x = 0; x <= N; x += gap) {
            bs1.set(x);// w  ww .j a va  2s  . com
            rb1.add(x);
        }
        if (bs1.cardinality() != rb1.getCardinality())
            throw new RuntimeException("different card");
        if (!equals(bs1, rb1))
            throw new RuntimeException("basic  bug");
        for (int offset = 1; offset <= gap; offset *= 2) {
            final BitSet bs2 = new BitSet();
            final RoaringBitmap rb2 = new RoaringBitmap();
            for (int x = 0; x <= N; x += gap) {
                bs2.set(x + offset);
                rb2.add(x + offset);
            }
            if (bs2.cardinality() != rb2.getCardinality())
                throw new RuntimeException("different card");
            if (!equals(bs2, rb2))
                throw new RuntimeException("basic  bug");

            BitSet clonebs1;
            // testing AND
            clonebs1 = (BitSet) bs1.clone();
            clonebs1.and(bs2);
            if (!equals(clonebs1, RoaringBitmap.and(rb1, rb2)))
                throw new RuntimeException("bug and");
            {
                final RoaringBitmap t = rb1.clone();
                t.and(rb2);
                if (!equals(clonebs1, t))
                    throw new RuntimeException("bug inplace and");
                if (!t.equals(RoaringBitmap.and(rb1, rb2))) {
                    System.out.println(t.highLowContainer.getContainerAtIndex(0).getClass().getCanonicalName());
                    System.out.println(RoaringBitmap.and(rb1, rb2).highLowContainer.getContainerAtIndex(0)
                            .getClass().getCanonicalName());

                    throw new RuntimeException("bug inplace and");
                }
            }

            // testing OR
            clonebs1 = (BitSet) bs1.clone();
            clonebs1.or(bs2);

            if (!equals(clonebs1, RoaringBitmap.or(rb1, rb2)))
                throw new RuntimeException("bug or");
            {
                final RoaringBitmap t = rb1.clone();
                t.or(rb2);
                if (!equals(clonebs1, t))
                    throw new RuntimeException("bug or");
                if (!t.equals(RoaringBitmap.or(rb1, rb2)))
                    throw new RuntimeException("bug or");
                if (!t.toString().equals(RoaringBitmap.or(rb1, rb2).toString()))
                    throw new RuntimeException("bug or");

            }
            // testing XOR
            clonebs1 = (BitSet) bs1.clone();
            clonebs1.xor(bs2);
            if (!equals(clonebs1, RoaringBitmap.xor(rb1, rb2))) {
                throw new RuntimeException("bug xor");
            }
            {
                final RoaringBitmap t = rb1.clone();
                t.xor(rb2);
                if (!equals(clonebs1, t))
                    throw new RuntimeException("bug xor");
                if (!t.equals(RoaringBitmap.xor(rb1, rb2)))
                    throw new RuntimeException("bug xor");
            }
            // testing NOTAND
            clonebs1 = (BitSet) bs1.clone();
            clonebs1.andNot(bs2);
            if (!equals(clonebs1, RoaringBitmap.andNot(rb1, rb2))) {
                throw new RuntimeException("bug andnot");
            }
            clonebs1 = (BitSet) bs2.clone();
            clonebs1.andNot(bs1);
            if (!equals(clonebs1, RoaringBitmap.andNot(rb2, rb1))) {
                throw new RuntimeException("bug andnot");
            }
            {
                final RoaringBitmap t = rb2.clone();
                t.andNot(rb1);
                if (!equals(clonebs1, t)) {
                    throw new RuntimeException("bug inplace andnot");
                }
                final RoaringBitmap g = RoaringBitmap.andNot(rb2, rb1);
                if (!equals(clonebs1, g)) {
                    throw new RuntimeException("bug andnot");
                }
                if (!t.equals(g))
                    throw new RuntimeException("bug");
            }
            clonebs1 = (BitSet) bs1.clone();
            clonebs1.andNot(bs2);
            if (!equals(clonebs1, RoaringBitmap.andNot(rb1, rb2))) {
                throw new RuntimeException("bug andnot");
            }
            {
                final RoaringBitmap t = rb1.clone();
                t.andNot(rb2);
                if (!equals(clonebs1, t)) {
                    throw new RuntimeException("bug andnot");
                }
                final RoaringBitmap g = RoaringBitmap.andNot(rb1, rb2);
                if (!equals(clonebs1, g)) {
                    throw new RuntimeException("bug andnot");
                }
                if (!t.equals(g))
                    throw new RuntimeException("bug");
            }
        }
    }
}