Example usage for java.util BitSet flip

List of usage examples for java.util BitSet flip

Introduction

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

Prototype

public void flip(int fromIndex, int toIndex) 

Source Link

Document

Sets each bit from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to the complement of its current value.

Usage

From source file:Main.java

public static void main(String[] args) {

    BitSet bitset1 = new BitSet(8);
    BitSet bitset2 = new BitSet(8);

    // assign values to bitset1
    bitset1.set(0);/*from   w w  w  . j  av a 2  s . c om*/
    bitset1.set(1);
    bitset1.set(2);

    // assign values to bitset2
    bitset2.set(2);
    bitset2.set(4);

    // print the sets
    System.out.println("Bitset1:" + bitset1);
    System.out.println("Bitset2:" + bitset2);

    // flip from index 2 to index 5 of bitset1 and print it
    bitset1.flip(2, 5);
    System.out.println(bitset1);

    // flip from index 1 to index 5 of bitset2 and print it
    bitset2.flip(1, 5);
    System.out.println(bitset2);

}

From source file:edu.umich.flowfence.service.SandboxManager.java

private void dumpSandboxes() {
    if (localLOGV) {
        BitSet seenSandboxes = new BitSet(SANDBOX_COUNT);
        Log.v(TAG, ">>> Dumping current sandbox state:");
        Log.v(TAG, "Running: " + mRunningSandboxes.size() + " sandboxes");
        for (Sandbox sb : mRunningSandboxes) {
            dumpSandbox(sb, seenSandboxes);
        }//from  ww w  . j  ava  2s .  c  o m
        Log.v(TAG, "Idle: " + mIdleSandboxes.size() + " sandboxes (LRU order)");
        for (Sandbox sb : mIdleSandboxes.keySet()) {
            dumpSandbox(sb, seenSandboxes);
        }
        Log.v(TAG, "Stopped: " + mStoppedSandboxes.size() + " sandboxes");
        for (Sandbox sb : mStoppedSandboxes) {
            dumpSandbox(sb, seenSandboxes);
        }
        Log.v(TAG, "Hot spares: " + mHotSpares.size() + " sandboxes");
        for (Sandbox sb : mHotSpares) {
            dumpSandbox(sb, seenSandboxes);
        }
        seenSandboxes.flip(0, SANDBOX_COUNT); // true = unseen
        if (!seenSandboxes.isEmpty()) {
            Log.w(TAG, "WARNING: leaked " + seenSandboxes.cardinality() + " sandboxes");
            int leaked = -1;
            while ((leaked = seenSandboxes.nextSetBit(leaked + 1)) >= 0) {
                dumpSandbox(Sandbox.get(leaked), null);
            }
        } else {
            Log.v(TAG, "No leaks detected");
        }
        Log.v(TAG, "<<< End of state dump");
    }
}

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 ww  . j  a  v a 2 s  .  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//from  ww w .  j  a  va2 s  .c om
    // 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.wso2.andes.kernel.router.TopicRoutingMatcher.java

/**
 * This methods adds a constituent table with only null and other constituents.
 * This is required when a message comes with more than the available number of constituents. If wildcard
 * queues are available for those, they should match. Hence need to create these empty constituent tables.
 *//*www. j av  a2s .  com*/
private void addEmptyConstituentTable() {
    int noOfqueues = storageQueueList.size();
    Map<String, BitSet> constituentTable = new HashMap<>();

    BitSet nullBitSet = new BitSet(noOfqueues);
    BitSet otherBitSet = new BitSet(noOfqueues);

    if (noOfqueues > 0) {

        // Null constituent will always be true for empty constituents, hence need to flip
        nullBitSet.flip(0, noOfqueues - 1);

        for (int queueIndex = 0; queueIndex < noOfqueues; queueIndex++) {
            // For 'other', if subscribers last constituent is multi level wild card then matching
            String[] allConstituent = queueConstituents.get(queueIndex);
            String lastConstituent = allConstituent[allConstituent.length - 1];

            if (multiLevelWildCard.equals(lastConstituent)) {
                otherBitSet.set(queueIndex);
            } else {
                otherBitSet.set(queueIndex, false);
            }
        }
    }

    constituentTable.put(NULL_CONSTITUENT, nullBitSet);
    constituentTable.put(OTHER_CONSTITUENT, otherBitSet);

    constituentTables.add(constituentTable);
}

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   w w  w. j  a v a2s  .c  o 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

/**
 * This methods adds a constituent table with only null and other constituents.
 * This is required when a message comes with more than the available number of constituents. If wildcard
 * subscriptions are available for those, they should match. Hence need to create these empty constituent tables.
 *//*  ww w .j a va2 s.c  om*/
private void addEmptyConstituentTable() {
    int noOfSubscriptions = wildCardSubscriptionList.size();
    Map<String, BitSet> constituentTable = new HashMap<String, BitSet>();

    BitSet nullBitSet = new BitSet(noOfSubscriptions);
    BitSet otherBitSet = new BitSet(noOfSubscriptions);

    if (noOfSubscriptions > 0) {

        // Null constituent will always be true for empty constituents, hence need to flip
        nullBitSet.flip(0, noOfSubscriptions - 1);

        for (int subscriptionIndex = 0; subscriptionIndex < noOfSubscriptions; subscriptionIndex++) {
            // For 'other', if subscribers last constituent is multi level wild card then matching
            String[] allConstituent = subscriptionConstituents.get(subscriptionIndex);
            String lastConstituent = allConstituent[allConstituent.length - 1];

            if (multiLevelWildCard.equals(lastConstituent)) {
                otherBitSet.set(subscriptionIndex);
            } else {
                otherBitSet.set(subscriptionIndex, false);
            }
        }
    }

    constituentTable.put(NULL_CONSTITUENT, nullBitSet);
    constituentTable.put(OTHER_CONSTITUENT, otherBitSet);

    constituentTables.add(constituentTable);
}

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
 */// ww  w.  j  a va  2  s .  c  om
@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

/**
 * This methods adds a constituent table with only null and other constituents.
 * This is required when a message comes with more than the available number of constituents. If wildcard
 * subscriptions are available for those, they should match. Hence need to create these empty constituent tables.
 *//*from  w  w  w  .  jav  a  2  s  .  com*/
private void addEmptyConstituentTable() {
    int noOfSubscriptions = subscriptionList.size();
    Map<String, BitSet> constituentTable = new HashMap<String, BitSet>();

    BitSet nullBitSet = new BitSet(noOfSubscriptions);
    BitSet otherBitSet = new BitSet(noOfSubscriptions);

    if (noOfSubscriptions > 0) {

        // Null constituent will always be true for empty constituents, hence need to flip
        nullBitSet.flip(0, noOfSubscriptions - 1);

        for (int subscriptionIndex = 0; subscriptionIndex < noOfSubscriptions; subscriptionIndex++) {
            // For 'other', if subscribers last constituent is multi level wild card then matching
            String[] allConstituent = subscriptionConstituents.get(subscriptionIndex);
            String lastConstituent = allConstituent[allConstituent.length - 1];

            if (multiLevelWildCard.equals(lastConstituent)) {
                otherBitSet.set(subscriptionIndex);
            } else {
                otherBitSet.set(subscriptionIndex, false);
            }
        }
    }

    constituentTable.put(NULL_CONSTITUENT, nullBitSet);
    constituentTable.put(OTHER_CONSTITUENT, otherBitSet);

    constituentTables.add(constituentTable);
}

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

/**
 * {@inheritDoc}// ww w.  j  a va 2  s.c  o m
 */
@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;
}