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:Main.java

public static byte[] unencodedSeptetsToEncodedSeptets(byte[] septetBytes) {
    byte[] txtBytes;
    byte[] txtSeptets;
    int txtBytesLen;
    BitSet bits;
    int i, j;/*w w w.  j av a  2s  . c  o m*/
    txtBytes = septetBytes;
    txtBytesLen = txtBytes.length;
    bits = new BitSet();
    for (i = 0; i < txtBytesLen; i++)
        for (j = 0; j < 7; j++)
            if ((txtBytes[i] & (1 << j)) != 0)
                bits.set((i * 7) + j);
    // big diff here
    int encodedSeptetByteArrayLength = txtBytesLen * 7 / 8 + ((txtBytesLen * 7 % 8 != 0) ? 1 : 0);
    txtSeptets = new byte[encodedSeptetByteArrayLength];
    for (i = 0; i < encodedSeptetByteArrayLength; i++) {
        for (j = 0; j < 8; j++) {
            txtSeptets[i] |= (byte) ((bits.get((i * 8) + j) ? 1 : 0) << j);
        }
    }
    return txtSeptets;
}

From source file:Main.java

public static byte[] encodedSeptetsToUnencodedSeptets(byte[] octetBytes, boolean discardLast) {
    byte newBytes[];
    BitSet bitSet;
    int i, j, value1, value2;
    bitSet = new BitSet(octetBytes.length * 8);
    value1 = 0;/*from   ww  w.ja v a 2s. c  om*/
    for (i = 0; i < octetBytes.length; i++)
        for (j = 0; j < 8; j++) {
            value1 = (i * 8) + j;
            if ((octetBytes[i] & (1 << j)) != 0)
                bitSet.set(value1);
        }
    value1++;
    value2 = value1 / 7 + ((value1 % 7 != 0) ? 1 : 0); // big diff here
    if (value2 == 0)
        value2++;
    newBytes = new byte[value2];
    for (i = 0; i < value2; i++) {
        for (j = 0; j < 7; j++) {
            if ((value1 + 1) > (i * 7 + j)) {
                if (bitSet.get(i * 7 + j)) {
                    newBytes[i] |= (byte) (1 << j);
                }
            }
        }
    }
    if (discardLast && octetBytes.length * 8 % 7 > 0) {
        // when decoding a 7bit encoded string 
        // the last septet may become 0, this should be discarded
        // since this is an artifact of the encoding not part of the 
        // original string
        // this is only done for decoding 7bit encoded text NOT for
        // reversing octets to septets (e.g. for the encoding the UDH)
        if (newBytes[newBytes.length - 1] == 0) {
            byte[] retVal = new byte[newBytes.length - 1];
            System.arraycopy(newBytes, 0, retVal, 0, retVal.length);
            return retVal;
        }
    }
    return newBytes;
}

From source file:ezbake.deployer.impl.Files.java

public static int convertPosixFilePermissionsToTarArchiveEntryMode(Set<PosixFilePermission> permissions) {
    BitSet mode = new BitSet();

    if (permissions.contains(PosixFilePermission.OTHERS_EXECUTE)) {
        mode.set(PosixFilePermission.OTHERS_EXECUTE.ordinal());
    }/*from  w  w w .j a  v a 2  s.c om*/
    if (permissions.contains(PosixFilePermission.OTHERS_WRITE)) {
        mode.set(PosixFilePermission.OTHERS_WRITE.ordinal());
    }
    if (permissions.contains(PosixFilePermission.OTHERS_READ)) {
        mode.set(PosixFilePermission.OTHERS_READ.ordinal());
    }
    if (permissions.contains(PosixFilePermission.GROUP_EXECUTE)) {
        mode.set(PosixFilePermission.GROUP_EXECUTE.ordinal());
    }
    if (permissions.contains(PosixFilePermission.GROUP_WRITE)) {
        mode.set(PosixFilePermission.GROUP_WRITE.ordinal());
    }
    if (permissions.contains(PosixFilePermission.GROUP_READ)) {
        mode.set(PosixFilePermission.GROUP_READ.ordinal());
    }
    if (permissions.contains(PosixFilePermission.OWNER_EXECUTE)) {
        mode.set(PosixFilePermission.OWNER_EXECUTE.ordinal());
    }
    if (permissions.contains(PosixFilePermission.OWNER_WRITE)) {
        mode.set(PosixFilePermission.OWNER_WRITE.ordinal());
    }
    if (permissions.contains(PosixFilePermission.OWNER_READ)) {
        mode.set(PosixFilePermission.OWNER_READ.ordinal());
    }

    return (int) mode.toLongArray()[0];
}

From source file:com.roche.sequencing.bioinformatics.common.utils.BitSetUtil.java

/**
 * combine a variable number of bitsets of the same length
 * //  w w w . j  a  va  2 s  .  co m
 * @param length
 * @param bitsets
 * @return
 */
public static BitSet combine(int length, BitSet... bitsets) {
    BitSet combinedBitSet = new BitSet();
    int currentBitset = 0;
    for (BitSet bitset : bitsets) {
        for (int i = 0; i < length; i++) {
            if (bitset.get(i)) {
                combinedBitSet.set((currentBitset * length) + i);
            }
        }
        currentBitset++;
    }
    return combinedBitSet;
}

From source file:Main.java

public static byte[] encodedSeptetsToUnencodedSeptets(byte[] octetBytes, boolean discardLast) {
    byte newBytes[];
    BitSet bitSet;
    int i, j, value1, value2;
    bitSet = new BitSet(octetBytes.length * 8);
    value1 = 0;/*from w  ww  .  j  a  v a  2  s  .com*/
    for (i = 0; i < octetBytes.length; i++)
        for (j = 0; j < 8; j++) {
            value1 = (i * 8) + j;
            if ((octetBytes[i] & (1 << j)) != 0)
                bitSet.set(value1);
        }
    value1++;
    // this is a bit count NOT a byte count
    value2 = value1 / 7 + ((value1 % 7 != 0) ? 1 : 0); // big diff here
    //System.out.println(octetBytes.length);
    //System.out.println(value1+" --> "+value2);
    if (value2 == 0)
        value2++;
    newBytes = new byte[value2];
    for (i = 0; i < value2; i++) {
        for (j = 0; j < 7; j++) {
            if ((value1 + 1) > (i * 7 + j)) {
                if (bitSet.get(i * 7 + j)) {
                    newBytes[i] |= (byte) (1 << j);
                }
            }
        }
    }
    if (discardLast && octetBytes.length * 8 % 7 > 0) {
        // when decoding a 7bit encoded string 
        // the last septet may become 0, this should be discarded
        // since this is an artifact of the encoding not part of the 
        // original string
        // this is only done for decoding 7bit encoded text NOT for
        // reversing octets to septets (e.g. for the encoding the UDH)
        if (newBytes[newBytes.length - 1] == 0) {
            byte[] retVal = new byte[newBytes.length - 1];
            System.arraycopy(newBytes, 0, retVal, 0, retVal.length);
            return retVal;
        }
    }
    return newBytes;
}

From source file:org.caleydo.data.importer.tcga.FirehoseProvider.java

/**
 * @param header/*ww  w .  ja  v a2s. c  om*/
 * @param good
 * @return
 */
private static BitSet filterCols(String[] header, Set<String> good) {
    BitSet r = new BitSet(header.length);
    for (int i = 0; i < header.length; ++i)
        if (!good.contains(header[i]))
            r.set(i);
    return r;
}

From source file:org.openecomp.sdnc.sli.aai.AAIRequest.java

public static void setProperties(Properties props, AAIService aaiService) {
    AAIRequest.configProperties = props;
    AAIRequest.aaiService = aaiService;//w w w.  j av  a  2s. c  o m

    try {
        URL url = null;
        Bundle bundle = FrameworkUtil.getBundle(AAIServiceActivator.class);
        if (bundle != null) {
            BundleContext ctx = bundle.getBundleContext();
            if (ctx == null)
                return;

            url = ctx.getBundle().getResource(AAIService.PATH_PROPERTIES);
        } else {
            url = aaiService.getClass().getResource("/aai-path.properties");
        }

        InputStream in = url.openStream();
        Reader reader = new InputStreamReader(in, "UTF-8");

        Properties properties = new Properties();
        properties.load(reader);
        LOG.info("loaded " + properties.size());

        Set<String> keys = properties.stringPropertyNames();

        int index = 0;
        Set<String> resourceNames = new TreeSet<String>();

        for (String key : keys) {
            String[] tags = key.split("\\|");
            for (String tag : tags) {
                if (!resourceNames.contains(tag)) {
                    resourceNames.add(tag);
                    tagValues.put(tag, Integer.toString(++index));
                }
            }
            BitSet bs = new BitSet(256);
            for (String tag : tags) {
                String value = tagValues.get(tag);
                Integer bitIndex = Integer.parseInt(value);
                bs.set(bitIndex);
            }
            String path = properties.getProperty(key);
            LOG.info(String.format("bitset %s\t\t%s", bs.toString(), path));
            bitsetPaths.put(bs, path);
        }
        LOG.info("loaded " + resourceNames.toString());
    } catch (Exception e) {
        LOG.error("Caught exception", e);
    }
}

From source file:org.moeaframework.TestUtils.java

/**
 * Returns {@code true} if the two populations are equal; otherwise
 * {@code false}. Two populations are equal if all solutions contained
 * in one population are contained in the other population.
 * /*from   ww  w. j a  v  a  2s. com*/
 * @param p1 the first population
 * @param p2 the second population
 * @return {@code true} if the two populations are equal; otherwise
 *         {@code false}
 */
public static boolean equals(Population p1, Population p2) {
    if (p1.size() != p2.size()) {
        return false;
    }

    BitSet matched1 = new BitSet(p1.size());
    BitSet matched2 = new BitSet(p2.size());

    for (int i = 0; i < p1.size(); i++) {
        for (int j = 0; j < p2.size(); j++) {
            if (equals(p1.get(i), p2.get(j))) {
                matched1.set(i);
                matched2.set(j);
            }
        }
    }

    return (matched1.cardinality() == p1.size()) && (matched2.cardinality() == p2.size());
}

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

private static void dumpSandbox(Sandbox sb, BitSet seen) {
    int id = sb.getID();
    String result = String.format("    %s %s package=%s", sb, sb.getTaints(), sb.getAssignedPackage());
    if (seen == null) {
        Log.w(TAG, result + " [LEAKED]");
    } else if (seen.get(id)) {
        Log.w(TAG, result + " [DUPLICATE]");
    } else {//w  w w. j a v a  2 s  . com
        Log.v(TAG, result);
        seen.set(id);
    }
}

From source file:gov.nasa.jpf.constraints.solvers.cw.CWSolver.java

static void populateLookupTables(RealVectorSpace space, List<Expression<Boolean>> constraints,
        List<Expression<Boolean>>[] constraintsByVariableIndex,
        Map<Expression<Boolean>, BitSet> variableIndicesByConstraint) {
    int numberOfVariables = space.dimensions().size();
    for (Expression<Boolean> constraint : constraints) {
        BitSet variableIndices = new BitSet(numberOfVariables);
        variableIndicesByConstraint.put(constraint, variableIndices);
        for (Variable<?> variable : ExpressionUtil.freeVariables(constraint)) {
            int varIndex = space.indexOf(variable);
            variableIndices.set(varIndex);
            if (constraintsByVariableIndex[varIndex] == null) {
                constraintsByVariableIndex[varIndex] = new ArrayList<>();
            }//from   w w w .  ja  v a 2s.c o m
            constraintsByVariableIndex[varIndex].add(constraint);
        }
    }
}