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:org.alfresco.module.org_alfresco_module_rm.capability.RMAfterInvocationProvider.java

private ResultSet decide(Authentication authentication, Object object, ConfigAttributeDefinition config,
        ResultSet returnedObject) {
    if (returnedObject == null) {
        return null;
    }/*from  w  w  w.  j a v a2s.  c o  m*/

    BitSet inclusionMask = new BitSet(returnedObject.length());
    FilteringResultSet filteringResultSet = new FilteringResultSet(returnedObject, inclusionMask);

    List<ConfigAttributeDefintion> supportedDefinitions = extractSupportedDefinitions(config);

    Integer maxSize = null;
    if (returnedObject.getResultSetMetaData().getSearchParameters().getMaxItems() >= 0) {
        maxSize = Integer.valueOf(returnedObject.getResultSetMetaData().getSearchParameters().getMaxItems());
    }
    if ((maxSize == null) && (returnedObject.getResultSetMetaData().getSearchParameters()
            .getLimitBy() == LimitBy.FINAL_SIZE)) {
        maxSize = Integer.valueOf(returnedObject.getResultSetMetaData().getSearchParameters().getLimit());
    }
    // Allow for skip
    if ((maxSize != null)
            && (returnedObject.getResultSetMetaData().getSearchParameters().getSkipCount() >= 0)) {
        maxSize = Integer
                .valueOf(maxSize + returnedObject.getResultSetMetaData().getSearchParameters().getSkipCount());
    }

    if (supportedDefinitions.size() == 0) {
        if (maxSize == null) {
            return returnedObject;
        } else if (returnedObject.length() > maxSize.intValue()) {
            for (int i = 0; i < maxSize.intValue(); i++) {
                inclusionMask.set(i, true);
            }
            filteringResultSet.setResultSetMetaData(new SimpleResultSetMetaData(
                    returnedObject.getResultSetMetaData().getLimitedBy(), PermissionEvaluationMode.EAGER,
                    returnedObject.getResultSetMetaData().getSearchParameters()));
            return filteringResultSet;
        } else {
            for (int i = 0; i < returnedObject.length(); i++) {
                inclusionMask.set(i, true);
            }
            filteringResultSet.setResultSetMetaData(new SimpleResultSetMetaData(
                    returnedObject.getResultSetMetaData().getLimitedBy(), PermissionEvaluationMode.EAGER,
                    returnedObject.getResultSetMetaData().getSearchParameters()));
            return filteringResultSet;
        }
    }

    // record the start time
    long startTimeMillis = System.currentTimeMillis();

    // set the default, unlimited resultset type
    filteringResultSet.setResultSetMetaData(new SimpleResultSetMetaData(
            returnedObject.getResultSetMetaData().getLimitedBy(), PermissionEvaluationMode.EAGER,
            returnedObject.getResultSetMetaData().getSearchParameters()));

    for (int i = 0; i < returnedObject.length(); i++) {
        long currentTimeMillis = System.currentTimeMillis();

        // All permission checks must pass
        inclusionMask.set(i, true);

        if (!nodeService.exists(returnedObject.getNodeRef(i))) {
            inclusionMask.set(i, false);
        } else {
            int parentCheckRead = checkRead(returnedObject.getChildAssocRef(i).getParentRef());
            int childCheckRead = checkRead(returnedObject.getNodeRef(i));

            for (ConfigAttributeDefintion cad : supportedDefinitions) {
                NodeRef testNodeRef = returnedObject.getNodeRef(i);
                int checkRead = childCheckRead;
                if (cad.parent) {
                    testNodeRef = returnedObject.getChildAssocRef(i).getParentRef();
                    checkRead = parentCheckRead;
                }

                if (isUnfiltered(testNodeRef)) {
                    continue;
                }

                if (inclusionMask.get(i) && (testNodeRef != null)
                        && (checkRead != AccessDecisionVoter.ACCESS_GRANTED)) {
                    inclusionMask.set(i, false);
                }
            }
        }

        // Bug out if we are limiting by size
        if ((maxSize != null) && (filteringResultSet.length() > maxSize.intValue())) {
            // Remove the last match to fix the correct size
            inclusionMask.set(i, false);
            filteringResultSet.setResultSetMetaData(
                    new SimpleResultSetMetaData(LimitBy.FINAL_SIZE, PermissionEvaluationMode.EAGER,
                            returnedObject.getResultSetMetaData().getSearchParameters()));
            break;
        }
    }
    return filteringResultSet;
}

From source file:org.apache.nutch.tools.PruneIndexTool.java

/**
 * For each query, find all matching documents and delete them from all input
 * indexes. Optionally, an additional check can be performed by using {@link PruneChecker}
 * implementations./*from  w ww  . j a  v  a 2 s . c  o  m*/
 */
public void run() {
    BitSet bits = new BitSet(reader.maxDoc());
    AllHitsCollector ahc = new AllHitsCollector(bits);
    boolean doDelete = false;
    for (int i = 0; i < queries.length; i++) {
        if (LOG.isInfoEnabled()) {
            LOG.info(dr + "Processing query: " + queries[i].toString());
        }
        bits.clear();
        try {
            searcher.search(queries[i], ahc);
        } catch (IOException e) {
            if (LOG.isWarnEnabled()) {
                LOG.warn(dr + " - failed: " + e.getMessage());
            }
            continue;
        }
        if (bits.cardinality() == 0) {
            if (LOG.isInfoEnabled()) {
                LOG.info(dr + " - no matching documents.");
            }
            continue;
        }
        if (LOG.isInfoEnabled()) {
            LOG.info(dr + " - found " + bits.cardinality() + " document(s).");
        }
        // Now delete all matching documents
        int docNum = -1, start = 0, cnt = 0;
        // probably faster than looping sequentially through all index values?
        while ((docNum = bits.nextSetBit(start)) != -1) {
            // don't delete the same document multiple times
            if (reader.isDeleted(docNum))
                continue;
            try {
                if (checkers != null && checkers.length > 0) {
                    boolean check = true;
                    for (int k = 0; k < checkers.length; k++) {
                        // fail if any checker returns false
                        check &= checkers[k].isPrunable(queries[i], reader, docNum);
                    }
                    doDelete = check;
                } else
                    doDelete = true;
                if (doDelete) {
                    if (!dryrun)
                        reader.deleteDocument(docNum);
                    cnt++;
                }
            } catch (Exception e) {
                if (LOG.isWarnEnabled()) {
                    LOG.warn(dr + " - failed to delete doc #" + docNum);
                }
            }
            start = docNum + 1;
        }
        if (LOG.isInfoEnabled()) {
            LOG.info(dr + " - deleted " + cnt + " document(s).");
        }
    }
    // close checkers
    if (checkers != null) {
        for (int i = 0; i < checkers.length; i++) {
            checkers[i].close();
        }
    }
    try {
        reader.close();
    } catch (IOException e) {
        if (LOG.isWarnEnabled()) {
            LOG.warn(dr + "Exception when closing reader(s): " + e.getMessage());
        }
    }
}

From source file:de.javakaffee.kryoserializers.KryoTest.java

@Test(enabled = true)
public void testBitSet() throws Exception {
    final BitSet bitSet = new BitSet(10);
    bitSet.flip(2);//from ww  w  .ja va 2 s  . c  om
    bitSet.flip(4);
    final Holder<BitSet> holder = new Holder<BitSet>(bitSet);
    @SuppressWarnings("unchecked")
    final Holder<BitSet> deserialized = deserialize(serialize(holder), Holder.class);
    assertDeepEquals(deserialized, holder);
}

From source file:backtype.storm.generated.ExecutorInfo.java

private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException {
    try {//w  ww .j  a va 2  s. co  m
        // it doesn't seem like you should have to do this, but java serialization is wacky, and doesn't call the default constructor.
        __isset_bit_vector = new BitSet(1);
        read(new org.apache.thrift7.protocol.TCompactProtocol(
                new org.apache.thrift7.transport.TIOStreamTransport(in)));
    } catch (org.apache.thrift7.TException te) {
        throw new java.io.IOException(te);
    }
}

From source file:hivemall.smile.classification.GradientTreeBoostingClassifierUDTF.java

private void train2(@Nonnull final double[][] x, @Nonnull final int[] y) throws HiveException {
    final int numVars = SmileExtUtils.computeNumInputVars(_numVars, x);
    if (logger.isInfoEnabled()) {
        logger.info("k: " + 2 + ", numTrees: " + _numTrees + ", shirinkage: " + _eta + ", subsample: "
                + _subsample + ", numVars: " + numVars + ", maxDepth: " + _maxDepth + ", minSamplesSplit: "
                + _minSamplesSplit + ", maxLeafs: " + _maxLeafNodes + ", seed: " + _seed);
    }/*w  w  w  .  j  a v a  2  s .  c  o m*/

    final int numInstances = x.length;
    final int numSamples = (int) Math.round(numInstances * _subsample);

    final double[] h = new double[numInstances]; // current F(x_i)
    final double[] response = new double[numInstances]; // response variable for regression tree.

    final double mu = smile.math.Math.mean(y);
    final double intercept = 0.5d * Math.log((1.d + mu) / (1.d - mu));

    for (int i = 0; i < numInstances; i++) {
        h[i] = intercept;
    }

    final int[][] order = SmileExtUtils.sort(_attributes, x);
    final RegressionTree.NodeOutput output = new L2NodeOutput(response);

    final BitSet sampled = new BitSet(numInstances);
    final int[] bag = new int[numSamples];
    final int[] perm = new int[numSamples];
    for (int i = 0; i < numSamples; i++) {
        perm[i] = i;
    }

    long s = (this._seed == -1L) ? SmileExtUtils.generateSeed() : new smile.math.Random(_seed).nextLong();
    final smile.math.Random rnd1 = new smile.math.Random(s);
    final smile.math.Random rnd2 = new smile.math.Random(rnd1.nextLong());

    for (int m = 0; m < _numTrees; m++) {
        reportProgress(_progressReporter);

        SmileExtUtils.shuffle(perm, rnd1);
        for (int i = 0; i < numSamples; i++) {
            int index = perm[i];
            bag[i] = index;
            sampled.set(index);
        }

        for (int i = 0; i < numInstances; i++) {
            response[i] = 2.0d * y[i] / (1.d + Math.exp(2.d * y[i] * h[i]));
        }

        RegressionTree tree = new RegressionTree(_attributes, x, response, numVars, _maxDepth, _maxLeafNodes,
                _minSamplesSplit, _minSamplesLeaf, order, bag, output, rnd2);

        for (int i = 0; i < numInstances; i++) {
            h[i] += _eta * tree.predict(x[i]);
        }

        // out-of-bag error estimate
        int oobTests = 0, oobErrors = 0;
        for (int i = sampled.nextClearBit(0); i < numInstances; i = sampled.nextClearBit(i + 1)) {
            oobTests++;
            final int pred = (h[i] > 0.d) ? 1 : 0;
            if (pred != y[i]) {
                oobErrors++;
            }
        }
        float oobErrorRate = 0.f;
        if (oobTests > 0) {
            oobErrorRate = ((float) oobErrors) / oobTests;
        }

        forward(m + 1, intercept, _eta, oobErrorRate, tree);
        sampled.clear();
    }
}

From source file:org.simplx.args.MainArgs.java

/**
 * Creates a new {@link MainArgs} object that will return specified options,
 * arguments, and operands. The {@code prog} parameter is the program name.
 *
 * @param programName The name to use for the program.
 * @param args        The command line arguments.
 *//*ww w . j  a v a  2s  . c  om*/
public MainArgs(String programName, String... args) {
    if (logger.isLoggable(Level.FINE)) {
        SimplxLogging.logFormat(logger, Level.FINE, "MainArgs(%s)", programName);
        for (int i = 0; i < args.length; i++) {
            String arg = args[i];
            SimplxLogging.logFormat(logger, Level.FINE, "    %d: %s", i, arg);
        }
    }

    this.programName = programName;
    this.args = args.clone();
    used = new BitSet(args.length);
    options = new ArrayList<Opt>();
    curGroup = 0;
    groups = new ArrayList<String>();
    groups.add("");
}

From source file:de.javakaffee.kryoserializers.KryoTest.java

@Test(enabled = true)
public void testCopyBitSet() throws Exception {
    final BitSet bitSet = new BitSet(10);
    bitSet.flip(2);/*from  w  ww. jav a 2  s . com*/
    bitSet.flip(4);
    final BitSet copy = _kryo.copy(bitSet);
    assertDeepEquals(copy, bitSet);
}

From source file:juicebox.data.MatrixZoomData.java

/**
 * Computes eigenvector from Pearson's./*w w w.  ja va 2 s. c o  m*/
 *
 * @param df    Expected values, needed to get Pearson's
 * @param which Which eigenvector; 0 is principal.
 * @return Eigenvector
 */
public double[] computeEigenvector(ExpectedValueFunction df, int which) {
    BasicMatrix pearsons = getPearsons(df);
    if (pearsons == null) {
        return null;
    }

    int dim = pearsons.getRowDimension();
    double[][] data = new double[dim][dim];
    BitSet bitSet = new BitSet(dim);
    for (int i = 0; i < dim; i++) {
        for (int j = 0; j < dim; j++) {
            float tmp = pearsons.getEntry(i, j);
            data[i][j] = tmp;
            if (data[i][j] != 0 && !Float.isNaN(tmp)) {
                bitSet.set(i);
            }
        }
    }

    int[] nonCentromereColumns = new int[bitSet.cardinality()];
    int count = 0;
    for (int i = 0; i < dim; i++) {
        if (bitSet.get(i))
            nonCentromereColumns[count++] = i;
    }

    RealMatrix subMatrix = new Array2DRowRealMatrix(data).getSubMatrix(nonCentromereColumns,
            nonCentromereColumns);
    RealVector rv = (new EigenDecompositionImpl(subMatrix, 0)).getEigenvector(which);

    double[] ev = rv.toArray();

    int size = pearsons.getColumnDimension();
    double[] eigenvector = new double[size];
    int num = 0;
    for (int i = 0; i < size; i++) {
        if (num < nonCentromereColumns.length && i == nonCentromereColumns[num]) {
            eigenvector[i] = ev[num];
            num++;
        } else {
            eigenvector[i] = Double.NaN;
        }
    }
    return eigenvector;

}

From source file:org.apache.hadoop.hdfs.shortcircuit.ShortCircuitShm.java

/**
 * Create the ShortCircuitShm.//from   w  ww. jav  a2 s  .  co  m
 * 
 * @param shmId       The ID to use.
 * @param stream      The stream that we're going to use to create this 
 *                    shared memory segment.
 *                    
 *                    Although this is a FileInputStream, we are going to
 *                    assume that the underlying file descriptor is writable
 *                    as well as readable. It would be more appropriate to use
 *                    a RandomAccessFile here, but that class does not have
 *                    any public accessor which returns a FileDescriptor,
 *                    unlike FileInputStream.
 */
public ShortCircuitShm(ShmId shmId, FileInputStream stream) throws IOException {
    if (!NativeIO.isAvailable()) {
        throw new UnsupportedOperationException("NativeIO is not available.");
    }
    if (Shell.WINDOWS) {
        throw new UnsupportedOperationException("DfsClientShm is not yet implemented for Windows.");
    }
    if (unsafe == null) {
        throw new UnsupportedOperationException(
                "can't use DfsClientShm because we failed to " + "load misc.Unsafe.");
    }
    this.shmId = shmId;
    this.mmappedLength = getUsableLength(stream);
    this.baseAddress = POSIX.mmap(stream.getFD(), POSIX.MMAP_PROT_READ | POSIX.MMAP_PROT_WRITE, true,
            mmappedLength);
    this.slots = new Slot[mmappedLength / BYTES_PER_SLOT];
    this.allocatedSlots = new BitSet(slots.length);
    if (LOG.isTraceEnabled()) {
        LOG.trace("creating " + this.getClass().getSimpleName() + "(shmId=" + shmId + ", mmappedLength="
                + mmappedLength + ", baseAddress=" + String.format("%x", baseAddress) + ", slots.length="
                + slots.length + ")");
    }
}

From source file:org.apache.mahout.classifier.bayes.SplitBayesInput.java

/** Perform a split on the specified input file. Results will be written to files of the same name in the specified 
 *  training and test output directories. The {@link #validate()} method is called prior to executing the split.
 *///from ww  w  .java 2  s . c o  m
public void splitFile(Path inputFile) throws IOException {
    if (fs.getFileStatus(inputFile) == null) {
        throw new IOException(inputFile + " does not exist");
    } else if (fs.getFileStatus(inputFile).isDir()) {
        throw new IOException(inputFile + " is a directory");
    }

    validate();

    Path testOutputFile = new Path(testOutputDirectory, inputFile.getName());
    Path trainingOutputFile = new Path(trainingOutputDirectory, inputFile.getName());

    int lineCount = countLines(fs, inputFile, charset);

    log.info("{} has {} lines", inputFile.getName(), lineCount);

    int testSplitStart = 0;
    int testSplitSize = this.testSplitSize; // don't modify state
    BitSet randomSel = null;

    if (testRandomSelectionPct > 0 || testRandomSelectionSize > 0) {
        testSplitSize = this.testRandomSelectionSize;

        if (testRandomSelectionPct > 0) {
            testSplitSize = Math.round(lineCount * testRandomSelectionPct / 100.0f);
        }
        log.info("{} test split size is {} based on random selection percentage {}",
                new Object[] { inputFile.getName(), testSplitSize, testRandomSelectionPct });
        long[] ridx = new long[testSplitSize];
        RandomSampler.sample(testSplitSize, lineCount - 1, testSplitSize, 0, ridx, 0, RandomUtils.getRandom());
        randomSel = new BitSet(lineCount);
        for (long idx : ridx) {
            randomSel.set((int) idx + 1);
        }
    } else {
        if (testSplitPct > 0) { // calculate split size based on percentage
            testSplitSize = Math.round(lineCount * testSplitPct / 100.0f);
            log.info("{} test split size is {} based on percentage {}",
                    new Object[] { inputFile.getName(), testSplitSize, testSplitPct });
        } else {
            log.info("{} test split size is {}", inputFile.getName(), testSplitSize);
        }

        if (splitLocation > 0) { // calculate start of split based on percentage
            testSplitStart = Math.round(lineCount * splitLocation / 100.0f);
            if (lineCount - testSplitStart < testSplitSize) {
                // adjust split start downwards based on split size.
                testSplitStart = lineCount - testSplitSize;
            }
            log.info("{} test split start is {} based on split location {}",
                    new Object[] { inputFile.getName(), testSplitStart, splitLocation });
        }

        if (testSplitStart < 0) {
            throw new IllegalArgumentException(
                    "test split size for " + inputFile + " is too large, it would produce an "
                            + "empty training set from the initial set of " + lineCount + " examples");
        } else if (lineCount - testSplitSize < testSplitSize) {
            log.warn(
                    "Test set size for {} may be too large, {} is larger than the number of "
                            + "lines remaining in the training set: {}",
                    new Object[] { inputFile, testSplitSize, lineCount - testSplitSize });
        }
    }

    BufferedReader reader = new BufferedReader(new InputStreamReader(fs.open(inputFile), charset));
    Writer trainingWriter = new OutputStreamWriter(fs.create(trainingOutputFile), charset);
    Writer testWriter = new OutputStreamWriter(fs.create(testOutputFile), charset);

    int trainCount = 0;
    int testCount = 0;

    try {

        String line;
        int pos = 0;
        while ((line = reader.readLine()) != null) {
            pos++;

            Writer writer;
            if (testRandomSelectionPct > 0) { // Randomly choose
                writer = randomSel.get(pos) ? testWriter : trainingWriter;
            } else { // Choose based on location
                writer = pos > testSplitStart ? testWriter : trainingWriter;
            }

            if (writer == testWriter) {
                if (testCount >= testSplitSize) {
                    writer = trainingWriter;
                } else {
                    testCount++;
                }
            }

            if (writer == trainingWriter) {
                trainCount++;
            }

            writer.write(line);
            writer.write('\n');
        }

    } finally {
        IOUtils.quietClose(reader);
        IOUtils.quietClose(trainingWriter);
        IOUtils.quietClose(testWriter);
    }

    log.info("file: {}, input: {} train: {}, test: {} starting at {}",
            new Object[] { inputFile.getName(), lineCount, trainCount, testCount, testSplitStart });

    // testing;
    if (callback != null) {
        callback.splitComplete(inputFile, lineCount, trainCount, testCount, testSplitStart);
    }
}