Example usage for java.util BitSet set

List of usage examples for java.util BitSet set

Introduction

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

Prototype

public void set(int bitIndex) 

Source Link

Document

Sets the bit at the specified index to true .

Usage

From source file:org.roaringbitmap.TestRoaringBitmap.java

@Test
public void flipTest6A() {
    System.out.println("FlipTest6A");
    final RoaringBitmap rb = new RoaringBitmap();
    final RoaringBitmap rb1 = RoaringBitmap.flip(rb, 100000, 132000);
    final RoaringBitmap rb2 = RoaringBitmap.flip(rb1, 99000, 2 * 65536);
    final int rbcard = rb2.getCardinality();

    Assert.assertEquals(1928, rbcard);//from  w ww . j  a va2s.  co m

    final BitSet bs = new BitSet();
    for (int i = 99000; i < 100000; ++i)
        bs.set(i);
    for (int i = 2 * 65536; i < 132000; ++i)
        bs.set(i);
    Assert.assertTrue(equals(bs, rb2));
}

From source file:org.roaringbitmap.TestRoaringBitmap.java

@Test
public void flipTest4A() {
    System.out.println("FlipTest4A");
    final RoaringBitmap rb = new RoaringBitmap();
    final RoaringBitmap rb1 = RoaringBitmap.flip(rb, 100000, 200000);
    final RoaringBitmap rb2 = RoaringBitmap.flip(rb1, 65536, 4 * 65536);
    final int rbcard = rb2.getCardinality();

    Assert.assertEquals(96608, rbcard);/*from w  ww .  j a  v  a2  s . c  o m*/

    final BitSet bs = new BitSet();
    for (int i = 65536; i < 100000; ++i)
        bs.set(i);
    for (int i = 200000; i < 262144; ++i)
        bs.set(i);

    Assert.assertTrue(equals(bs, rb2));
}

From source file:org.roaringbitmap.TestRoaringBitmap.java

@Test
public void flipTest6() { // fits evenly on big end, multiple containers
    System.out.println("FlipTest6");
    final RoaringBitmap rb = new RoaringBitmap();
    rb.flip(100000, 132000);//from w w w .j  av a 2 s  . c o  m
    rb.flip(99000, 2 * 65536);
    final int rbcard = rb.getCardinality();

    // 99000 to 99999 are 1000 1s
    // 131072 to 131999 are 928 1s

    Assert.assertEquals(1928, rbcard);

    final BitSet bs = new BitSet();
    for (int i = 99000; i < 100000; ++i)
        bs.set(i);
    for (int i = 2 * 65536; i < 132000; ++i)
        bs.set(i);
    Assert.assertTrue(equals(bs, rb));
}

From source file:org.roaringbitmap.TestRoaringBitmap.java

@Test
public void flipTest4() { // fits evenly on both ends
    System.out.println("FlipTest4");
    final RoaringBitmap rb = new RoaringBitmap();
    rb.flip(100000, 200000); // got 100k-199999
    rb.flip(65536, 4 * 65536);// ww  w  . j  a  v a  2 s.c om
    final int rbcard = rb.getCardinality();

    // 65536 to 99999 are 1s
    // 200000 to 262143 are 1s: total card

    Assert.assertEquals(96608, rbcard);

    final BitSet bs = new BitSet();
    for (int i = 65536; i < 100000; ++i)
        bs.set(i);
    for (int i = 200000; i < 262144; ++i)
        bs.set(i);

    Assert.assertTrue(equals(bs, rb));
}

From source file:org.roaringbitmap.TestRoaringBitmap.java

@Test
public void flipTest5() { // fits evenly on small end, multiple
    // containers
    System.out.println("FlipTest5");
    final RoaringBitmap rb = new RoaringBitmap();
    rb.flip(100000, 132000);//from  ww w  .java2s  .c o m
    rb.flip(65536, 120000);
    final int rbcard = rb.getCardinality();

    // 65536 to 99999 are 1s
    // 120000 to 131999

    Assert.assertEquals(46464, rbcard);

    final BitSet bs = new BitSet();
    for (int i = 65536; i < 100000; ++i)
        bs.set(i);
    for (int i = 120000; i < 132000; ++i)
        bs.set(i);
    Assert.assertTrue(equals(bs, rb));
}

From source file:org.roaringbitmap.TestRoaringBitmap.java

@Test
public void flipTest1A() {
    final RoaringBitmap rb = new RoaringBitmap();

    final RoaringBitmap rb1 = RoaringBitmap.flip(rb, 100000, 200000);
    final int rbcard = rb1.getCardinality();
    Assert.assertEquals(100000, rbcard);
    Assert.assertEquals(0, rb.getCardinality());

    final BitSet bs = new BitSet();
    Assert.assertTrue(equals(bs, rb)); // still empty?
    for (int i = 100000; i < 200000; ++i)
        bs.set(i);
    Assert.assertTrue(equals(bs, rb1));//from  ww  w . java 2 s  .  com
}

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);
            rb1.add(x);/*from   w  ww  .  j a v a  2s.  c om*/
        }
        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.apache.openjpa.kernel.StateManagerImpl.java

/**
 * Loads a delayed access field./*from ww  w .  j  av a2 s.  c o m*/
 * @param field
 */
public void loadDelayedField(int field) {
    if (!isDelayed(field)) {
        return;
    }

    try {
        beforeRead(field);
    } catch (RuntimeException re) {
        throw translate(re);
    }
    lock();
    try {
        boolean active = _broker.isActive();
        int lockLevel = calculateLockLevel(active, false, null);
        BitSet fields = new BitSet();
        fields.set(field);
        if (!_broker.getStoreManager().load(this, fields, _broker.getFetchConfiguration(), lockLevel, null)) {
            throw new ObjectNotFoundException(_loc.get("del-instance", _meta.getDescribedType(), _oid))
                    .setFailedObject(getManagedInstance());
        }
        // Cleared the delayed bit
        _delayed.clear(field);
        obtainLocks(active, false, lockLevel, null, null);
    } catch (RuntimeException re) {
        throw translate(re);
    } finally {
        unlock();
    }
}

From source file:edu.iu.subgraph.colorcount_HJ.java

/**
 * @brief compute color counting in N iterations 
 *
 * @param template/*  www.  jav  a  2s  . co  m*/
 * @param N
 *
 * @return 
 */
public double do_full_count(Graph template, int N) {

    this.t = template;
    this.num_iter = N;
    this.labels_t = t.labels;

    // --------------------------- creating subtemplates and comb number index system --------------------------- 

    if (this.verbose) {
        LOG.info("Begining partition...");
    }

    //partition the template into subtemplates 
    this.part = new partitioner(this.t, this.labeled, this.labels_t);
    this.part.sort_subtemplates();

    if (this.verbose) {
        LOG.info("done partitioning");
    }

    //colors equals the num of vertices
    this.num_colors = this.t.num_vertices();
    //get array of subtemplates
    this.subtemplates = this.part.get_subtemplates();
    //subtemplates num
    this.subtemplate_count = this.part.get_subtemplate_count();

    //obtain the hash values table for each subtemplate and a combination of color sets
    create_tables();

    //initialize dynamic prog table, with subtemplate-vertices-color array
    this.dt.init(this.subtemplates, this.subtemplate_count, this.num_verts_graph, this.num_colors,
            this.max_abs_id);

    //vertice num of the full graph, huge
    this.chunks = divide_chunks(this.num_verts_graph, this.thread_num);
    // in pipeline regroup-update, thread 0 is doing communication
    this.chunks_pipeline = divide_chunks(this.num_verts_graph, this.thread_num - 1);

    //triggering vtune profiling add command option for vtune 
    // java.nio.file.Path vtune_file = java.nio.file.Paths.get("vtune-flag.txt");
    // String flag_trigger = "Start training process and trigger vtune profiling.";
    // try{
    //     java.nio.file.Files.write(vtune_file, flag_trigger.getBytes());
    // }catch (IOException e)
    // {
    //    LOG.info("Failed to create vtune trigger flag");
    // }

    if (this.verbose) {
        LOG.info("Starting Multi-threading Counting Iterations");
    }

    launchHabaneroApp(() -> forallChunked(0, this.thread_num - 1, (threadIdx) -> {

        //set Java threads affinity
        BitSet bitSet = new BitSet(this.core_num);
        int thread_mask = 0;

        if (this.verbose && threadIdx == 0) {
            LOG.info("Set up threads affinity: Core Num: " + this.core_num + "; Total Threads: "
                    + this.thread_num + "; thd per core: " + this.tpc + "; affinity: " + this.affinity);
        }

        if (this.affinity == "scatter") {
            //implement threads bind by core round-robin
            thread_mask = threadIdx % this.core_num;

        } else {
            //default affinity compact
            //implement a compact bind, 2 threads a core
            int tpn = this.tpc * this.core_num;
            thread_mask = threadIdx % tpn;
            thread_mask /= this.tpc;
        }

        bitSet.set(thread_mask);
        Affinity.setAffinity(bitSet);

        try {

            // start the main loop of iterations
            for (int cur_itr = 0; cur_itr < this.num_iter; cur_itr++) {
                if (threadIdx == 0)
                    this.cur_iter = cur_itr;

                //start sampling colors
                this.barrier.await();

                if (verbose && threadIdx == 0) {
                    LOG.info("Start Sampling Graph for Itr: " + cur_itr);
                    // this.start_comp = System.currentTimeMillis();
                    this.start_misc = System.currentTimeMillis();
                }

                Random rand = new Random(System.currentTimeMillis());
                //sampling the vertices of full graph g
                for (int i = this.chunks[threadIdx]; i < this.chunks[threadIdx + 1]; ++i) {
                    this.colors_g[i] = rand.nextInt(this.num_colors);
                }

                this.barrier.await();
                if (this.verbose && threadIdx == 0) {
                    LOG.info("Finish Sampling Graph for Itr: " + cur_itr + "; use time: "
                            + (System.currentTimeMillis() - this.start_misc) + "ms");
                }

                // start doing counting
                for (int s = this.subtemplate_count - 1; s > 0; --s) {

                    if (threadIdx == 0) {

                        //get num_vert of subtemplate s
                        this.num_verts_sub_ato = this.num_verts_table[s];

                        if (this.verbose)
                            LOG.info("Initing Subtemplate " + s + ", t verts: " + num_verts_sub_ato);

                        int a = this.part.get_active_index(s);
                        int p = this.part.get_passive_index(s);

                        if (this.verbose)
                            LOG.info("Subtemplate: " + s + "; active_idx: " + a + "; passive_idx: " + p);

                        this.dt.init_sub(s, a, p);
                    }

                    this.barrier.await();

                    if (this.verbose && threadIdx == 0)
                        LOG.info("Start Counting Local Graph Subtemplate " + s);

                    //hit the bottom of subtemplate chain, dangling template node
                    if (this.num_verts_sub_ato == 1) {

                        if (s == this.subtemplate_count - 1) {
                            init_table_node_HJ(s, threadIdx, this.chunks);
                        } else {
                            if (threadIdx == 0)
                                dt.set_to_table(this.subtemplate_count - 1, s);
                        }

                    } else {
                        colorful_count_HJ(s, threadIdx, this.chunks);
                    }

                    this.barrier.await();

                    if (this.verbose && threadIdx == 0)
                        LOG.info("Finish Counting Local Graph Subtemplate " + s);

                    //start communication part single thread 
                    // only for subtemplates size > 1, having neighbours on other mappers
                    // only if more than one mapper, otherwise all g verts are local
                    if (this.mapper_num > 1 && this.num_verts_sub_ato > 1) {
                        if (this.rotation_pipeline)
                            regroup_update_pipeline(s, threadIdx);
                        else
                            regroup_update_all(s, threadIdx, this.chunks);
                    }

                    // printout results for sub s
                    this.barrier.await();

                    if (threadIdx == 0) {

                        if (this.verbose)
                            LOG.info("JVM Memory Used in subtemplate: " + s + " is: "
                                    + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()));

                        int a = this.part.get_active_index(s);
                        int p = this.part.get_passive_index(s);

                        //release the template with size > 1
                        if (a != SCConstants.NULL_VAL) {
                            // do not free the dangling template node
                            if (this.num_verts_table[a] > 1)
                                this.dt.clear_sub(a);
                        }
                        if (p != SCConstants.NULL_VAL) {
                            if (this.num_verts_table[p] > 1)
                                this.dt.clear_sub(p);
                        }

                    }

                    this.barrier.await();

                    //print out counts for this subtemplate 
                    if (this.verbose) {
                        print_counts(s, threadIdx, this.chunks);
                        this.barrier.await();
                        if (threadIdx == 0) {
                            double sub_total_counts = 0;
                            for (int x = 0; x < this.thread_num; x++)
                                sub_total_counts += cc_ato[x];

                            LOG.info("Total counts for sub-temp: " + s + " is: " + sub_total_counts);
                        }

                        this.barrier.await();
                    }

                    if (threadIdx == 0)
                        this.context.progress();

                } // end of a subtemplate

                if (verbose && threadIdx == 0)
                    LOG.info("Done with initialization. Doing full count");

                // do the count for the full template
                if (threadIdx == 0) {
                    this.num_verts_sub_ato = this.num_verts_table[0];
                    int a = this.part.get_active_index(0);
                    int p = this.part.get_passive_index(0);

                    if (this.verbose)
                        LOG.info("Subtemplate 0 ; active_idx: " + a + "; passive_idx: " + p);

                    dt.init_sub(0, a, p);

                }

                this.barrier.await();
                colorful_count_HJ(0, threadIdx, chunks);
                this.barrier.await();

                //comm and add the communicated counts to full_count_ato
                // only for subtemplates size > 1, having neighbours on other mappers
                // only if more than one mapper, otherwise all g verts are local
                if (this.num_verts_sub_ato > 1 && this.mapper_num > 1) {

                    if (this.rotation_pipeline)
                        regroup_update_pipeline(0, threadIdx);
                    else
                        regroup_update_all(0, threadIdx, this.chunks);
                }

                this.barrier.await();
                // printout results for last sub 
                if (threadIdx == 0) {
                    double sum_count = 0.0;
                    for (int k = 0; k < this.thread_num; k++) {
                        sum_count += this.count_local_root[k];
                        sum_count += this.count_comm_root[k];
                    }

                    this.full_count_ato = sum_count;
                    LOG.info("Finish update comm counts for last subtemplate: " + "; total counts: "
                            + sum_count);
                }

                this.barrier.await();

                if (threadIdx == 0) {
                    if (this.verbose)
                        LOG.info("JVM Memory Used in Last subtemplate is: "
                                + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()));

                    int a = this.part.get_active_index(0);
                    int p = this.part.get_passive_index(0);

                    if (a != SCConstants.NULL_VAL)
                        this.dt.clear_sub(a);
                    if (p != SCConstants.NULL_VAL)
                        this.dt.clear_sub(p);

                    //free the first dangling template node
                    this.dt.clear_sub(subtemplate_count - 1);
                }

                //add counts from every iteration
                if (threadIdx == 0) {
                    this.cumulate_count_ato += this.full_count_ato;
                }

                // free comm data
                if (threadIdx == 0) {
                    if (this.mapper_num > 1) {
                        ResourcePool.get().clean();
                        ConnPool.get().clean();
                    }
                    System.gc();
                    this.context.progress();
                }

            } // end of an iteration

            this.barrier.await();

        } catch (InterruptedException | BrokenBarrierException e) {
            LOG.info("Catch barrier exception in itr: " + this.cur_iter);
            e.printStackTrace();
        }

    }));

    //----------------------- end of color_counting -----------------

    double final_count = cumulate_count_ato / (double) this.num_iter;

    //free memory
    this.send_vertex_table = null;
    this.comm_vertex_table = null;
    this.update_map = null;
    this.colors_g = null;

    this.compress_cache_array = null;
    this.compress_cache_index = null;

    this.map_ids_cache_pip = null;
    this.chunk_ids_cache_pip = null;
    this.chunk_internal_offsets_cache_pip = null;

    delete_tables();
    this.part.clear_temparrays();

    return final_count;

}

From source file:org.apache.openjpa.kernel.StateManagerImpl.java

/**
 * Load the given field's fetch group; the field itself may already be
 * loaded if it is being set by the user.
 *//*www .  ja v  a2s .  c  o m*/
protected void loadField(int field, int lockLevel, boolean forWrite, boolean fgs) {
    FetchConfiguration fetch = _broker.getFetchConfiguration();
    FieldMetaData fmd = _meta.getField(field);
    BitSet fields = null;

    // if this is a dfg field or we need to load our dfg, do so
    if (fgs && (_flags & FLAG_LOADED) == 0)
        fields = getUnloadedInternal(fetch, LOAD_FGS, null);

    // check for load fetch group
    String lfg = fmd.getLoadFetchGroup();
    boolean lfgAdded = false;
    if (lfg != null) {
        FieldMetaData[] fmds = _meta.getFields();
        for (int i = 0; i < fmds.length; i++) {
            if (!_loaded.get(i) && (i == field || fmds[i].isInFetchGroup(lfg))) {
                if (fields == null)
                    fields = new BitSet(fmds.length);
                fields.set(i);
            }
        }

        // relation field is loaded with the load-fetch-group
        // but this addition must be reverted once the load is over
        if (!fetch.hasFetchGroup(lfg)) {
            fetch.addFetchGroup(lfg);
            lfgAdded = true;
        }
    } else if (fmd.isInDefaultFetchGroup() && fields == null) {
        // no load group but dfg: add dfg fields if we haven't already
        fields = getUnloadedInternal(fetch, LOAD_FGS, null);
    } else if (!_loaded.get(fmd.getIndex())) {
        // no load group or dfg: load individual field
        if (fields == null)
            fields = new BitSet();
        fields.set(fmd.getIndex());
    }

    // call this method even if there are no unloaded fields; loadFields
    // takes care of things like loading version info and setting PC flags
    try {
        loadFields(fields, fetch, lockLevel, null);
    } finally {
        if (lfgAdded)
            fetch.removeFetchGroup(lfg);
    }
}