Java Code Examples for java.util.BitSet#length()

The following examples show how to use java.util.BitSet#length() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BitSetConverter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
    BitSet bitSet = (BitSet) source;
    StringBuffer buffer = new StringBuffer();
    boolean seenFirst = false;
    for (int i = 0; i < bitSet.length(); i++) {
        if (bitSet.get(i)) {
            if (seenFirst) {
                buffer.append(',');
            } else {
                seenFirst = true;
            }
            buffer.append(i);
        }
    }
    writer.setValue(buffer.toString());
}
 
Example 2
Source File: Cidr4Trie.java    From cidr-ip-trie with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public final Cidr4 recreateKey(final BitSet bits, final int numElements) {

  if (bits.length() == 0) {
    return new Cidr4(0, numElements);
  }

  // Maximum of 32 bits in a Cidr4 (IPv4 based)
  int binary = (int) bits.toLongArray()[0];

  // Shift our bits over if we are not 32 bits long
  final int move = 32 - numElements;
  if (move > 0) {
    binary = binary << move;
  }

  return new Cidr4(binary, numElements);
}
 
Example 3
Source File: BitSetSerializer.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public BitSet copy(final Kryo kryo, final BitSet original) {
    final BitSet result = new BitSet();
    final int length = original.length();
    for (int i = 0; i < length; i++) {
        result.set(i, original.get(i));
    }
    return result;
}
 
Example 4
Source File: Phrase.java    From modernmt with Apache License 2.0 5 votes vote down vote up
private static NumericPlaceholder[] extractPlaceholders(NumericSequence sequence, BitSet map) {
    int[] indexes = new int[map.cardinality()];
    int size = 0;

    for (int i = 0; i < map.length(); i++) {
        if (map.get(i) && sequence.hasIndex(i))
            indexes[size++] = i;
    }

    NumericPlaceholder[] result = new NumericPlaceholder[size];
    for (int i = 0; i < size; i++)
        result[i] = sequence.getByIndex(indexes[i]);

    return result;
}
 
Example 5
Source File: Policies.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/** If any errors were found, throw INVALID_POLICY with the smallest
 * index of any offending policy.
 */
private void checkForPolicyError( BitSet errorSet ) throws InvalidPolicy
{
    for (short ctr=0; ctr<errorSet.length(); ctr++ )
        if (errorSet.get(ctr))
            throw new InvalidPolicy(ctr);
}
 
Example 6
Source File: Policies.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/** If any errors were found, throw INVALID_POLICY with the smallest
 * index of any offending policy.
 */
private void checkForPolicyError( BitSet errorSet ) throws InvalidPolicy
{
    for (short ctr=0; ctr<errorSet.length(); ctr++ )
        if (errorSet.get(ctr))
            throw new InvalidPolicy(ctr);
}
 
Example 7
Source File: HierarchyEncoderImpl.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
BitSet singleBitDiff( BitSet x, BitSet y ) {
    BitSet t = new BitSet( x.length() );
    t.or( x );
    t.flip(0, t.size());
    t.and( y );

    switch ( t.cardinality() ) {
        case 0 : return t;
        case 1 : return t;
        default: return new BitSet();
    }
}
 
Example 8
Source File: And.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    BitSet a = new BitSet();
    BitSet b = new BitSet();

    a.set(0);
    a.set(70);
    b.set(40);
    a.and(b);
    if (a.length() != 0)
        throw new RuntimeException("Incorrect length after and().");
}
 
Example 9
Source File: ZOrderUtils.java    From geowave with Apache License 2.0 5 votes vote down vote up
private static long decodeIndex(final BitSet bs, final long rangePerDimension) {
  long floor = 0;
  long ceiling = rangePerDimension;
  long mid = 0;
  for (int i = 0; i < bs.length(); i++) {
    mid = (floor + ceiling) / 2;
    if (bs.get(i)) {
      floor = mid;
    } else {
      ceiling = mid;
    }
  }
  return mid;
}
 
Example 10
Source File: BitSetSerializer.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public void write(final Kryo kryo, final Output output, final BitSet bitSet) {
    final int len = bitSet.length();

    output.writeInt(len, true);

    for (int i = 0; i < len; i++) {
        output.writeBoolean(bitSet.get(i));
    }
}
 
Example 11
Source File: And.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    BitSet a = new BitSet();
    BitSet b = new BitSet();

    a.set(0);
    a.set(70);
    b.set(40);
    a.and(b);
    if (a.length() != 0)
        throw new RuntimeException("Incorrect length after and().");
}
 
Example 12
Source File: DataConvert.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static byte[] toByteArray(BitSet bits) {
    byte[] bytes = new byte[bits.length() / 8 + 1];
    for (int i = 0; i < bits.length(); i++) {
        if (bits.get(i)) {
            bytes[i / 8] |= 1 << (i % 8);
        }
    }
    return bytes;
}
 
Example 13
Source File: BitSetSerializer.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
public void write(final Kryo kryo, final Output output, final BitSet bitSet) {
    final int len = bitSet.length();

    output.writeInt(len, true);

    for (int i = 0; i < len; i++) {
        output.writeBoolean(bitSet.get(i));
    }
}
 
Example 14
Source File: Util.java    From smartcard-reader with GNU General Public License v3.0 5 votes vote down vote up
public static byte[] bitSet2ByteArray(BitSet bits) {
    byte[] bytes = new byte[bits.length() / 8 + 1];
    for (int i = 0; i < bits.length(); i++) {
        if (bits.get(i)) {
            bytes[bytes.length - i / 8 - 1] |= 1 << (i % 8);
        }
    }
    return bytes;
}
 
Example 15
Source File: LongBitSet.java    From metanome-algorithms with Apache License 2.0 5 votes vote down vote up
public LongBitSet(BitSet bitSet) {
	this(bitSet.length());
	for (int bit = bitSet.nextSetBit(0); bit >= 0; bit = bitSet
			.nextSetBit(bit + 1)) {
		set(bit);
	}
}
 
Example 16
Source File: And.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    BitSet a = new BitSet();
    BitSet b = new BitSet();

    a.set(0);
    a.set(70);
    b.set(40);
    a.and(b);
    if (a.length() != 0)
        throw new RuntimeException("Incorrect length after and().");
}
 
Example 17
Source File: TimetableGridHelper.java    From unitime with Apache License 2.0 4 votes vote down vote up
public static String pattern2string(BitSet pattern) {
	StringBuffer ret = new StringBuffer();
	for (int i = 0; i < pattern.length(); i++)
		ret.append(pattern.get(i) ? '1' : '0');
	return ret.toString();
}
 
Example 18
Source File: HierarchyEncoderImpl.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
BitSet decrement( BitSet id, int d ) {
    BitSet x = new BitSet( id.length() );
    x.or( id );
    x.clear(d);
    return x;
}
 
Example 19
Source File: JavaBitSet.java    From Hacktoberfest with MIT License 4 votes vote down vote up
private static BitSet initBitSet(BitSet bitSet) {
	for (int i = 0; i < bitSet.length(); i++) {
		bitSet.set(0);
	}
	return bitSet;
}
 
Example 20
Source File: VerySimpleSelect.java    From minperf with Apache License 2.0 4 votes vote down vote up
public static int getSize(BitSet set) {
    int result = 0;
    int size = set.length() + 1;
    result += BitBuffer.getEliasDeltaSize(size + 1);
    int cardinality = set.cardinality();
    result += BitBuffer.getEliasDeltaSize(cardinality + 1);
    int blockCount = (cardinality + BITS_PER_BLOCK - 1) / BITS_PER_BLOCK;
    ArrayList<Integer> list = new ArrayList<Integer>();
    int pos = set.nextSetBit(0);
    for (int i = 0; i < blockCount; i++) {
        list.add(pos);
        for (int j = 0; j < BITS_PER_BLOCK; j++) {
            pos = set.nextSetBit(pos + 1);
        }
    }
    long blockCountScale = getScaleFactor(size, blockCount);
    int minDiff = Integer.MAX_VALUE;
    for (int i = 0; i < list.size(); i++) {
        // int expected = (int) ((long) size * i / blockCount);
        int expected = (int) ((i * blockCountScale) >>> 32);
        int got = list.get(i);
        int diff = got - expected;
        list.set(i, diff);
        minDiff = Math.min(minDiff, diff);
    }
    if (list.size() == 0) {
        minDiff = 0;
    }
    result += BitBuffer.getEliasDeltaSize(BitBuffer.foldSigned(-minDiff) + 1);
    int max = 0;
    for (int i = 0; i < list.size(); i++) {
        int x = list.get(i) - minDiff;
        max = Math.max(max, x);
        list.set(i, x);
    }
    int bitCount = 32 - Integer.numberOfLeadingZeros(max);
    result += BitBuffer.getEliasDeltaSize(bitCount + 1);
    result += bitCount * list.size();
    result += size;
    return result;
}