Example usage for java.util BitSet and

List of usage examples for java.util BitSet and

Introduction

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

Prototype

public void and(BitSet set) 

Source Link

Document

Performs a logical AND of this target bit set with the argument bit set.

Usage

From source file:org.apache.tez.dag.app.rm.DagAwareYarnTaskScheduler.java

@GuardedBy("this")
@Nullable// w  w  w.j a  v  a  2  s .  c o  m
private Collection<ContainerId> maybePreempt(Resource freeResources) {
    if (preemptionPercentage == 0
            || numHeartbeats - lastPreemptionHeartbeat < numHeartbeatsBetweenPreemptions) {
        return null;
    }
    if (!requestTracker.isPreemptionDeadlineExpired()
            && requestTracker.fitsHighestPriorityRequest(freeResources)) {
        if (numHeartbeats % 50 == 1) {
            LOG.info("Highest priority request fits in free resources {}", freeResources);
        }
        return null;
    }

    int numIdleContainers = idleTracker.getNumContainers();
    if (numIdleContainers > 0) {
        if (numHeartbeats % 50 == 1) {
            LOG.info("Avoiding preemption since there are {} idle containers", numIdleContainers);
        }
        return null;
    }

    BitSet blocked = requestTracker.createVertexBlockedSet();
    if (!blocked.intersects(assignedVertices)) {
        if (numHeartbeats % 50 == 1) {
            LOG.info(
                    "Avoiding preemption since there are no descendants of the highest priority requests running");
        }
        return null;
    }

    Resource preemptLeft = requestTracker.getAmountToPreempt(preemptionPercentage);
    if (!resourceCalculator.anyAvailable(preemptLeft)) {
        if (numHeartbeats % 50 == 1) {
            LOG.info("Avoiding preemption since amount to preempt is {}", preemptLeft);
        }
        return null;
    }

    PriorityQueue<HeldContainer> candidates = new PriorityQueue<>(11, PREEMPT_ORDER_COMPARATOR);
    blocked.and(assignedVertices);
    for (int i = blocked.nextSetBit(0); i >= 0; i = blocked.nextSetBit(i + 1)) {
        Collection<HeldContainer> containers = vertexAssignments.get(i);
        if (containers != null) {
            candidates.addAll(containers);
        } else {
            LOG.error("Vertex {} in assignedVertices but no assignments?", i);
        }
    }

    ArrayList<ContainerId> preemptedContainers = new ArrayList<>();
    HeldContainer hc;
    while ((hc = candidates.poll()) != null) {
        LOG.info("Preempting container {} currently allocated to task {}", hc.getId(), hc.getAssignedTask());
        preemptedContainers.add(hc.getId());
        resourceCalculator.deductFrom(preemptLeft, hc.getCapability());
        if (!resourceCalculator.anyAvailable(preemptLeft)) {
            break;
        }
    }

    return preemptedContainers;
}

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

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

From source file:org.roaringbitmap.TestRoaringBitmap.java

@Test
public void flipTestBig() {
    final int numCases = 1000;
    System.out.println("flipTestBig for " + numCases + " tests");
    final RoaringBitmap rb = new RoaringBitmap();
    final BitSet bs = new BitSet();
    final Random r = new Random(3333);
    int checkTime = 2;

    for (int i = 0; i < numCases; ++i) {
        final int start = r.nextInt(65536 * 20);
        int end = r.nextInt(65536 * 20);
        if (r.nextDouble() < 0.1)
            end = start + r.nextInt(100);
        rb.flip(start, end);/*from w  w w .j  a  v  a2s  . c o m*/
        if (start < end)
            bs.flip(start, end); // throws exception
        // otherwise
        // insert some more ANDs to keep things sparser
        if (r.nextDouble() < 0.2) {
            final RoaringBitmap mask = new RoaringBitmap();
            final BitSet mask1 = new BitSet();
            final int startM = r.nextInt(65536 * 20);
            final int endM = startM + 100000;
            mask.flip(startM, endM);
            mask1.flip(startM, endM);
            mask.flip(0, 65536 * 20 + 100000);
            mask1.flip(0, 65536 * 20 + 100000);
            rb.and(mask);
            bs.and(mask1);
        }
        // see if we can detect incorrectly shared containers
        if (r.nextDouble() < 0.1) {
            final RoaringBitmap irrelevant = RoaringBitmap.flip(rb, 10, 100000);
            irrelevant.flip(5, 200000);
            irrelevant.flip(190000, 260000);
        }
        if (i > checkTime) {
            Assert.assertTrue(equals(bs, rb));
            checkTime *= 1.5;
        }
    }
}

From source file:org.roaringbitmap.TestRoaringBitmap.java

@Test
public void flipTestBigA() {
    final int numCases = 1000;
    final BitSet bs = new BitSet();
    final Random r = new Random(3333);
    int checkTime = 2;
    RoaringBitmap rb1 = new RoaringBitmap(), rb2 = null; // alternate
    // between/*  w  ww.  j  ava2  s  . c o m*/
    // them
    for (int i = 0; i < numCases; ++i) {
        final int start = r.nextInt(65536 * 20);
        int end = r.nextInt(65536 * 20);
        if (r.nextDouble() < 0.1)
            end = start + r.nextInt(100);

        if ((i & 1) == 0) {
            rb2 = RoaringBitmap.flip(rb1, start, end);
            // tweak the other, catch bad sharing
            int r1 = r.nextInt(65536 * 20);
            int r2 = r.nextInt(65536 * 20);
            rb1.flip(r1, r2);
        } else {
            rb1 = RoaringBitmap.flip(rb2, start, end);
            int r1 = r.nextInt(65536 * 20);
            int r2 = r.nextInt(65536 * 20);
            rb2.flip(r1, r2);
        }

        if (start < end) {
            bs.flip(start, end); // throws exception
            // otherwise
        }
        // insert some more ANDs to keep things sparser
        if (r.nextDouble() < 0.2 && (i & 1) == 0) {
            final RoaringBitmap mask = new RoaringBitmap();
            final BitSet mask1 = new BitSet();
            final int startM = r.nextInt(65536 * 20);
            final int endM = startM + 100000;
            mask.flip(startM, endM);
            mask1.flip(startM, endM);
            mask.flip(0, 65536 * 20 + 100000);
            mask1.flip(0, 65536 * 20 + 100000);
            rb2.and(mask);
            bs.and(mask1);
        }
        if (i > checkTime) {
            System.out.println("check after " + i + ", card = " + rb2.getCardinality());
            final RoaringBitmap rb = (i & 1) == 0 ? rb2 : rb1;
            final boolean status = equals(bs, rb);
            Assert.assertTrue(status);
            checkTime *= 1.5;
        }
    }
}

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  .  co  m
            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");
            }
        }
    }
}

From source file:org.wso2.andes.kernel.router.TopicRoutingMatcher.java

/**
 * Get storage queues matching to routing key
 * @param routingKey routing key to match queues
 * @return set of storage queues/*from   ww w. j av  a  2  s.co m*/
 */
public Set<StorageQueue> getMatchingStorageQueues(String routingKey) {
    Set<StorageQueue> matchingQueues = new HashSet<>();

    if (StringUtils.isNotEmpty(routingKey)) {

        // constituentDelimiter is quoted to avoid making the delimiter a regex symbol
        String[] constituents = routingKey.split(Pattern.quote(constituentsDelimiter), -1);

        int noOfCurrentMaxConstituents = constituentTables.size();

        // If given routingKey has more constituents than any subscriber has, then create constituent tables
        // for those before collecting matching subscribers
        if (constituents.length > noOfCurrentMaxConstituents) {
            for (int i = noOfCurrentMaxConstituents; i < constituents.length; i++) {
                addEmptyConstituentTable();
            }
        }

        // Keeps the results of 'AND' operations between each bit sets
        BitSet andBitSet = new BitSet(storageQueueList.size());

        // Since BitSet is initialized with false for each element we need to flip
        andBitSet.flip(0, storageQueueList.size());

        // Get corresponding bit set for each constituent in the routingKey and operate bitwise AND operation
        for (int constituentIndex = 0; constituentIndex < constituents.length; constituentIndex++) {
            String constituent = constituents[constituentIndex];
            Map<String, BitSet> constituentTable = constituentTables.get(constituentIndex);

            BitSet bitSetForAnd = constituentTable.get(constituent);

            if (null == bitSetForAnd) {
                // The constituent is not found in the table, hence matching with 'other' constituent
                bitSetForAnd = constituentTable.get(OTHER_CONSTITUENT);
            }

            andBitSet.and(bitSetForAnd);
        }

        // If there are more constituent tables, get the null constituent in each of them and operate bitwise AND
        for (int constituentIndex = constituents.length; constituentIndex < constituentTables
                .size(); constituentIndex++) {
            Map<String, BitSet> constituentTable = constituentTables.get(constituentIndex);
            andBitSet.and(constituentTable.get(NULL_CONSTITUENT));
        }

        // Valid queues are filtered, need to pick from queue pool
        int nextSetBitIndex = andBitSet.nextSetBit(0);
        while (nextSetBitIndex > -1) {
            matchingQueues.add(storageQueueList.get(nextSetBitIndex));
            nextSetBitIndex = andBitSet.nextSetBit(nextSetBitIndex + 1);
        }

    } else {
        log.warn("Cannot retrieve storage queues via bitmap handler since routingKey to match is empty");
    }

    return matchingQueues;
}

From source file:org.wso2.andes.subscription.ClusterSubscriptionBitMapHandler.java

/**
 * Get matching subscribers for a given non-wildcard destination.
 *
 * @param destination The destination without wildcard
 * @return Set of matching subscriptions
 *//*from w  w  w . j av a  2s.c o m*/
@Override
public Set<AndesSubscription> getMatchingWildCardSubscriptions(String destination) {
    Set<AndesSubscription> subscriptions = new HashSet<AndesSubscription>();

    if (StringUtils.isNotEmpty(destination)) {

        // constituentDelimiter is quoted to avoid making the delimiter a regex symbol
        String[] constituents = destination.split(Pattern.quote(constituentsDelimiter));

        int noOfCurrentMaxConstituents = constituentTables.size();

        // If given destination has more constituents than any subscriber has, then create constituent tables
        // for those before collecting matching subscribers
        if (constituents.length > noOfCurrentMaxConstituents) {
            for (int i = noOfCurrentMaxConstituents; i < constituents.length; i++) {
                addEmptyConstituentTable();
            }
        }

        // Keeps the results of 'AND' operations between each bit sets
        BitSet andBitSet = new BitSet(wildCardSubscriptionList.size());

        // Since BitSet is initialized with false for each element we need to flip
        andBitSet.flip(0, wildCardSubscriptionList.size());

        // Get corresponding bit set for each constituent in the destination and operate bitwise AND operation
        for (int constituentIndex = 0; constituentIndex < constituents.length; constituentIndex++) {
            String constituent = constituents[constituentIndex];
            Map<String, BitSet> constituentTable = constituentTables.get(constituentIndex);

            BitSet bitSetForAnd = constituentTable.get(constituent);

            if (null == bitSetForAnd) {
                // The constituent is not found in the table, hence matching with 'other' constituent
                bitSetForAnd = constituentTable.get(OTHER_CONSTITUENT);
            }

            andBitSet.and(bitSetForAnd);
        }

        // If there are more constituent tables, get the null constituent in each of them and operate bitwise AND
        for (int constituentIndex = constituents.length; constituentIndex < constituentTables
                .size(); constituentIndex++) {
            Map<String, BitSet> constituentTable = constituentTables.get(constituentIndex);
            andBitSet.and(constituentTable.get(NULL_CONSTITUENT));
        }

        // Valid subscriptions are filtered, need to pick from subscription pool
        int nextSetBitIndex = andBitSet.nextSetBit(0);
        while (nextSetBitIndex > -1) {
            subscriptions.add(wildCardSubscriptionList.get(nextSetBitIndex));
            nextSetBitIndex = andBitSet.nextSetBit(nextSetBitIndex + 1);
        }

    } else {
        log.warn("Cannot retrieve subscriptions via bitmap handler since destination to match is empty");
    }

    return subscriptions;
}

From source file:org.wso2.andes.subscription.TopicSubscriptionBitMapStore.java

/**
 * {@inheritDoc}//w w  w.ja va 2  s.com
 */
@Override
public Set<AndesSubscription> getMatchingSubscriptions(String destination, DestinationType destinationType) {
    Set<AndesSubscription> subscriptions = new HashSet<>();

    if (StringUtils.isNotEmpty(destination)) {

        // constituentDelimiter is quoted to avoid making the delimiter a regex symbol
        String[] constituents = destination.split(Pattern.quote(constituentsDelimiter), -1);

        int noOfCurrentMaxConstituents = constituentTables.size();

        // If given destination has more constituents than any subscriber has, then create constituent tables
        // for those before collecting matching subscribers
        if (constituents.length > noOfCurrentMaxConstituents) {
            for (int i = noOfCurrentMaxConstituents; i < constituents.length; i++) {
                addEmptyConstituentTable();
            }
        }

        // Keeps the results of 'AND' operations between each bit sets
        BitSet andBitSet = new BitSet(subscriptionList.size());

        // Since BitSet is initialized with false for each element we need to flip
        andBitSet.flip(0, subscriptionList.size());

        // Get corresponding bit set for each constituent in the destination and operate bitwise AND operation
        for (int constituentIndex = 0; constituentIndex < constituents.length; constituentIndex++) {
            String constituent = constituents[constituentIndex];
            Map<String, BitSet> constituentTable = constituentTables.get(constituentIndex);

            BitSet bitSetForAnd = constituentTable.get(constituent);

            if (null == bitSetForAnd) {
                // The constituent is not found in the table, hence matching with 'other' constituent
                bitSetForAnd = constituentTable.get(OTHER_CONSTITUENT);
            }

            andBitSet.and(bitSetForAnd);
        }

        // If there are more constituent tables, get the null constituent in each of them and operate bitwise AND
        for (int constituentIndex = constituents.length; constituentIndex < constituentTables
                .size(); constituentIndex++) {
            Map<String, BitSet> constituentTable = constituentTables.get(constituentIndex);
            andBitSet.and(constituentTable.get(NULL_CONSTITUENT));
        }

        // Valid subscriptions are filtered, need to pick from subscription pool
        int nextSetBitIndex = andBitSet.nextSetBit(0);
        while (nextSetBitIndex > -1) {
            subscriptions.add(subscriptionList.get(nextSetBitIndex));
            nextSetBitIndex = andBitSet.nextSetBit(nextSetBitIndex + 1);
        }

    } else {
        log.warn("Cannot retrieve subscriptions via bitmap handler since destination to match is empty");
    }

    return subscriptions;
}