Example usage for java.util BitSet BitSet

List of usage examples for java.util BitSet BitSet

Introduction

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

Prototype

private BitSet(long[] words) 

Source Link

Document

Creates a bit set using words as the internal representation.

Usage

From source file:net.minecraftforge.registries.ForgeRegistry.java

ForgeRegistry(Class<V> superType, ResourceLocation defaultKey, int min, int max,
        @Nullable CreateCallback<V> create, @Nullable AddCallback<V> add, @Nullable ClearCallback<V> clear,
        RegistryManager stage, boolean allowOverrides, boolean isModifiable,
        @Nullable DummyFactory<V> dummyFactory) {
    this.stage = stage;
    this.superType = superType;
    this.defaultKey = defaultKey;
    this.min = min;
    this.max = max;
    this.availabilityMap = new BitSet(Math.min(max + 1, 0x0FFF));
    this.create = create;
    this.add = add;
    this.clear = clear;
    this.isDelegated = IForgeRegistryEntry.Impl.class.isAssignableFrom(superType); //TODO: Make this IDelegatedRegistryEntry?
    this.allowOverrides = allowOverrides;
    this.isModifiable = isModifiable;
    this.dummyFactory = dummyFactory;
    if (this.create != null)
        this.create.onCreate(this, stage);
}

From source file:net.solarnetwork.node.control.modbus.heartbeat.ModbusHeartbeatJob.java

private synchronized Boolean setValue(Boolean desiredValue) throws IOException {
    final ModbusNetwork network = (modbusNetwork == null ? null : modbusNetwork.service());
    if (network == null) {
        log.debug("No ModbusNetwork avaialble");
        return Boolean.FALSE;
    }/*from  ww w .j  a  v  a2 s  .c  o  m*/
    final BitSet bits = new BitSet(1);
    bits.set(0, desiredValue);
    log.info("Setting modbus unit {} register {} value to {}", unitId, address, desiredValue);
    final Integer[] addresses = new Integer[] { address };
    return network.performAction(new ModbusConnectionAction<Boolean>() {

        @Override
        public Boolean doWithConnection(ModbusConnection conn) throws IOException {
            return conn.writeDiscreetValues(addresses, bits);
        }
    }, unitId);
}

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  ww  w.  j a va  2  s.co m
 * @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.kylin.cube.cuboid.algorithm.generic.GeneticAlgorithm.java

protected Population initRandomPopulation(BitsChromosomeHelper helper) {
    List<Chromosome> chromosomeList = Lists.newArrayListWithCapacity(populationSize);

    while (chromosomeList.size() < populationSize) {
        BitSet bitSetForSelection = new BitSet(helper.getLength());

        //Initialize selection genes
        double totalSpace = 0;
        while (totalSpace < helper.spaceLimit) {
            int j = org.apache.commons.math3.genetics.GeneticAlgorithm.getRandomGenerator()
                    .nextInt(helper.getLength());
            if (!bitSetForSelection.get(j)) {
                totalSpace += helper.getCuboidSizeByBitIndex(j);
                bitSetForSelection.set(j);
            }/* w  ww . j a  va  2s  . c om*/
        }

        Chromosome chromosome = new BitsChromosome(bitSetForSelection, benefitPolicy.getInstance(), helper);
        chromosomeList.add(chromosome);
    }
    return new ElitisticListPopulation(chromosomeList, maxPopulationSize, 0.8);
}

From source file:de.uniba.wiai.lspi.chord.data.ID.java

private static BitSet hashServiceId(ServiceId svcId) {
    final BitSet bs = new BitSet(kTotalBitLen);
    final Fn3<Integer, Integer, String, Unit> hashInto = (Integer offset, Integer bitLen, String s) -> {
        if (s != null) {
            partHashSet(bs, offset, hashString(s), bitLen);
        }/*from  ww w.j  a  v a 2s  .co  m*/

        return Unit.U;
    };

    // hash semantic 
    for (int i = 0; i < ServiceId.kPartsSemantic; ++i)
        hashInto.apply(i * kSemanticPartBitLen, kSemanticPartBitLen, svcId.parts[i]);

    // hash provider
    hashInto.apply(kSemanticTotalBitLen, kProviderBitLen, svcId.parts[ServiceId.kPartsSemantic]);

    return bs;
}

From source file:org.apache.jackrabbit.core.query.lucene.TermDocsCache.java

/**
 * Returns the {@link TermDocs} for the given term.
 *
 * @param t the term./*from w  w w. ja  va  2s  .co m*/
 * @return the term docs for the given term.
 * @throws IOException if an error occurs while reading from the index.
 */
public TermDocs termDocs(final Term t) throws IOException {
    if (t.field() != field) {
        return reader.termDocs(t);
    }

    String text = t.text();
    if (unknownValues.get(text) != null) {
        log.debug("EmptyTermDocs({},{})", field, text);
        return EmptyTermDocs.INSTANCE;
    }

    // maintain cache
    CacheEntry entry;
    synchronized (cache) {
        entry = (CacheEntry) cache.get(text);
        if (entry == null) {
            // check space
            if (cache.size() >= CACHE_SIZE) {
                // prune half of them and adjust the rest
                CacheEntry[] entries = (CacheEntry[]) cache.values().toArray(new CacheEntry[cache.size()]);
                Arrays.sort(entries);
                int threshold = entries[CACHE_SIZE / 2].numAccessed;
                for (Iterator it = cache.entrySet().iterator(); it.hasNext();) {
                    Map.Entry e = (Map.Entry) it.next();
                    if (((CacheEntry) e.getValue()).numAccessed <= threshold) {
                        // prune
                        it.remove();
                    } else {
                        // adjust
                        CacheEntry ce = (CacheEntry) e.getValue();
                        ce.numAccessed = (int) Math.sqrt(ce.numAccessed);
                    }
                }
            }
            entry = new CacheEntry();
            cache.put(text, entry);
        } else {
            entry.numAccessed++;
        }
    }

    // this is a threshold to prevent caching of TermDocs
    // that are read only irregularly.
    if (entry.numAccessed < 10) {
        if (log.isDebugEnabled()) {
            log.debug("#{} TermDocs({},{})", new Object[] { new Integer(entry.numAccessed), field, text });
        }
        return reader.termDocs(t);
    }

    if (entry.bits == null) {
        // collect bits
        BitSet bits = null;
        TermDocs tDocs = reader.termDocs(t);
        try {
            while (tDocs.next()) {
                if (bits == null) {
                    bits = new BitSet(reader.maxDoc());
                }
                bits.set(tDocs.doc());
            }
        } finally {
            tDocs.close();
        }
        if (bits != null) {
            entry.bits = bits;
        }
    }

    if (entry.bits == null) {
        // none collected
        unknownValues.put(text, text);
        return EmptyTermDocs.INSTANCE;
    } else {
        if (log.isDebugEnabled()) {
            log.debug("CachedTermDocs({},{},{}/{})", new Object[] { field, text,
                    new Integer(entry.bits.cardinality()), new Integer(reader.maxDoc()) });
        }
        return new CachedTermDocs(entry.bits);
    }
}

From source file:org.exoplatform.services.jcr.impl.core.query.lucene.TermDocsCache.java

/**
 * Returns the {@link TermDocs} for the given term.
 *
 * @param t the term./* w w w  . ja va2 s. c  o  m*/
 * @return the term docs for the given term.
 * @throws IOException if an error occurs while reading from the index.
 */
public TermDocs termDocs(final Term t) throws IOException {
    if (t == null || t.field() != field) {
        return reader.termDocs(t);
    }

    String text = t.text();
    if (unknownValues.get(text) != null) {
        log.debug("EmptyTermDocs({},{})", field, text);
        return EmptyTermDocs.INSTANCE;
    }

    // maintain cache
    CacheEntry entry;
    synchronized (cache) {
        entry = (CacheEntry) cache.get(text);
        if (entry == null) {
            // check space
            if (cache.size() >= CACHE_SIZE) {
                // prune half of them and adjust the rest
                CacheEntry[] entries = (CacheEntry[]) cache.values().toArray(new CacheEntry[cache.size()]);
                Arrays.sort(entries);
                int threshold = entries[CACHE_SIZE / 2].numAccessed;
                for (Iterator it = cache.entrySet().iterator(); it.hasNext();) {
                    Map.Entry e = (Map.Entry) it.next();
                    if (((CacheEntry) e.getValue()).numAccessed <= threshold) {
                        // prune
                        it.remove();
                    } else {
                        // adjust
                        CacheEntry ce = (CacheEntry) e.getValue();
                        ce.numAccessed = (int) Math.sqrt(ce.numAccessed);
                    }
                }
            }
            entry = new CacheEntry();
            cache.put(text, entry);
        } else {
            entry.numAccessed++;
        }
    }

    // this is a threshold to prevent caching of TermDocs
    // that are read only irregularly.
    if (entry.numAccessed < 10) {
        if (log.isDebugEnabled()) {
            log.debug("#{} TermDocs({},{})", new Object[] { new Integer(entry.numAccessed), field, text });
        }
        return reader.termDocs(t);
    }

    if (entry.bits == null) {
        // collect bits
        BitSet bits = null;
        TermDocs tDocs = reader.termDocs(t);
        try {
            while (tDocs.next()) {
                if (bits == null) {
                    bits = new BitSet(reader.maxDoc());
                }
                bits.set(tDocs.doc());
            }
        } finally {
            tDocs.close();
        }
        if (bits != null) {
            entry.bits = bits;
        }
    }

    if (entry.bits == null) {
        // none collected
        unknownValues.put(text, text);
        return EmptyTermDocs.INSTANCE;
    } else {
        if (log.isDebugEnabled()) {
            log.debug("CachedTermDocs({},{},{}/{})", new Object[] { field, text,
                    new Integer(entry.bits.cardinality()), new Integer(reader.maxDoc()) });
        }
        return new CachedTermDocs(entry.bits);
    }
}

From source file:com.netspective.commons.security.BasicAuthenticatedUser.java

public BitSet createPermissionsBitSet(AccessControlListsManager aclsManager) {
    return new BitSet(aclsManager.getAccessControlLists().getHighestPermissionId());
}

From source file:org.apache.hadoop.yarn.server.nodemanager.containermanager.linux.resources.TrafficController.java

TrafficController(Configuration conf, PrivilegedOperationExecutor exec) {
    this.conf = conf;
    this.classIdSet = new BitSet(MAX_CONTAINER_CLASSES);
    this.privilegedOperationExecutor = exec;
}

From source file:com.alibaba.android.layoutmanager.layoutmanager.StaggeredGridLayoutHelper.java

private void ensureLanes() {
    if (mSpans == null || mSpans.length != mNumLanes || mRemainingSpans == null) {
        mRemainingSpans = new BitSet(mNumLanes);
        mSpans = new Span[mNumLanes];
        for (int i = 0; i < mNumLanes; i++) {
            mSpans[i] = new Span(i);
        }//from   w  w  w .  ja  v  a2 s  . c  om
    }
}