Example usage for java.util BitSet clone

List of usage examples for java.util BitSet clone

Introduction

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

Prototype

public Object clone() 

Source Link

Document

Cloning this BitSet produces a new BitSet that is equal to it.

Usage

From source file:org.apache.kylin.cube.cuboid.algorithm.generic.BitsOnePointCrossover.java

/**
 * Helper for {@link #crossover(Chromosome, Chromosome)}. Performs the actual crossover.
 *
 * @param first  the first chromosome./*from  w w w . j av a 2 s  . com*/
 * @param second the second chromosome.
 * @return the pair of new chromosomes that resulted from the crossover.
 * @throws DimensionMismatchException if the length of the two chromosomes is different
 */
private ChromosomePair crossover(final BitsChromosome first, final BitsChromosome second)
        throws DimensionMismatchException {
    final int length = first.getLength();
    if (length != second.getLength()) {
        throw new DimensionMismatchException(second.getLength(), length);
    }

    final BitSet parent1Key = first.getRepresentation();
    final BitSet parent2Key = second.getRepresentation();

    final BitSet child1Key = new BitSet(length);
    final BitSet child2Key = new BitSet(length);

    // select a crossover point at random (0 and length makes no sense)
    final int crossoverIndex = 1 + (GeneticAlgorithm.getRandomGenerator().nextInt(length - 2));

    BitSet a = (BitSet) parent1Key.clone();
    a.clear(crossoverIndex, length);
    BitSet b = (BitSet) parent2Key.clone();
    b.clear(0, crossoverIndex);

    BitSet c = (BitSet) parent1Key.clone();
    c.clear(crossoverIndex, length);
    BitSet d = (BitSet) parent2Key.clone();
    d.clear(0, crossoverIndex);

    child1Key.or(a);
    child1Key.or(d);

    child2Key.or(c);
    child2Key.or(b);
    return new ChromosomePair(first.newBitsChromosome(child1Key), second.newBitsChromosome(child2Key));
}

From source file:org.apache.openjpa.util.ArrayStateImage.java

/**
 * Clone a state array./*from w w w  .  ja  v  a2 s  .  c om*/
 */
public static Object[] clone(Object[] state) {
    Object[] copy = new Object[state.length];
    System.arraycopy(state, 0, copy, 0, state.length - 1);
    BitSet loaded = (BitSet) state[state.length - 1];
    copy[copy.length - 1] = loaded.clone();
    return copy;
}

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

private static BitString create(final BitSet bitSet, final int length, final int cardinality) {
    if (length == 0) {
        return emptyBitSequence();
    } else if (cardinality == 0) {
        return zeros(length);
    } else if (cardinality == length) {
        return ones(length);
    } else {//from  w  w  w.  j  a va  2  s . c o m
        return new BitSetString((BitSet) bitSet.clone(), length, cardinality);
    }
}

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);//from   w  ww  .  j  a  v a 2s.  c o  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");
            }
        }
    }
}