FileDocCategorySizeDatePackage
Support_BitSet.javaAPI DocAndroid 1.5 API5868Wed May 06 22:41:06 BST 2009tests.support

Support_BitSet

public class Support_BitSet extends Object

Fields Summary
private long[]
bits
private static final int
ELM_SIZE
Constructors Summary
public Support_BitSet()
Create a new BitSet with size equal to 64 bits

return
The number of bits contained in this BitSet.
see
#clear
see
#set

 // Size in bits of the data type

    // being used in the bits array

                                  
      
        this(64);
    
public Support_BitSet(int nbits)
Create a new BitSet with size equal to nbits. If nbits is not a multiple of 64, then create a BitSet with size nbits rounded to the next closest multiple of 64.

exception
NegativeArraySizeException if nbits < 0.
see
#clear
see
#set

        if (nbits >= 0) {
            bits = new long[(nbits / ELM_SIZE) + (nbits % ELM_SIZE > 0 ? 1 : 0)];
        } else {
            throw new NegativeArraySizeException();
        }
    
Methods Summary
public voidclear(int pos)
Clears the bit at index pos. Grows the BitSet if pos > size.

param
pos int
exception
IndexOutOfBoundsException when pos < 0
see
#set

        if (pos >= 0) {
            if (pos < bits.length * ELM_SIZE) {
                bits[pos / ELM_SIZE] &= ~(1L << (pos % ELM_SIZE));
            } else {
                growBits(pos); // Bit is cleared for free if we have to grow
            }
        } else {
            throw new IndexOutOfBoundsException("Negative index specified");
        }
    
public booleanget(int pos)
Retrieve the bit at index pos. Grows the BitSet if pos > size.

param
pos int
return
A boolean value indicating whether the bit at pos has been set. Answers false if pos > size().
exception
IndexOutOfBoundsException when pos < 0
see
#set
see
#clear

        if (pos >= 0) {
            if (pos < bits.length * ELM_SIZE) {
                return (bits[pos / ELM_SIZE] & (1L << (pos % ELM_SIZE))) != 0;
            }
            return false;
        }
        throw new IndexOutOfBoundsException("Negative index specified");
    
private voidgrowBits(int pos)
Increase the size of the internal array to accomodate pos bits. The new array max index will be a multiple of 64

param
pos int The index the new array needs to be able to access

        pos++; // Inc to get correct bit count
        long[] tempBits = new long[(pos / ELM_SIZE)
                + (pos % ELM_SIZE > 0 ? 1 : 0)];
        System.arraycopy(bits, 0, tempBits, 0, bits.length);
        bits = tempBits;
    
public intlength()
Returns the number of bits up to and including the highest bit set.

        int idx = bits.length - 1;
        while (idx >= 0 && bits[idx] == 0) {
            --idx;
        }
        if (idx == -1) {
            return 0;
        }
        int i = ELM_SIZE - 1;
        long val = bits[idx];
        while ((val & (1L << i)) == 0 && i > 0) {
            i--;
        }
        return idx * ELM_SIZE + i + 1;
    
public voidset(int pos)
Sets the bit at index pos to 1. Grows the BitSet if pos > size.

param
pos int
exception
IndexOutOfBoundsException when pos < 0
see
#clear

        if (pos >= 0) {
            if (pos >= bits.length * ELM_SIZE) {
                growBits(pos);
            }
            bits[pos / ELM_SIZE] |= 1L << (pos % ELM_SIZE);
        } else {
            throw new IndexOutOfBoundsException("Negative index specified");
        }
    
public intsize()
Clears the bit at index pos.

return
The number of bits contained in this BitSet.
see
#BitSet
see
#clear
see
#set

        return bits.length * ELM_SIZE;
    
public java.lang.StringtoString()
Answers a string containing a concise, human-readable description of the receiver.

return
A comma delimited list of the indices of all bits that are set.

        StringBuffer sb = new StringBuffer(bits.length / 2);
        int bitCount = 0;
        sb.append('{");
        boolean comma = false;
        for (long element : bits) {
            if (element == 0) {
                bitCount += ELM_SIZE;
                continue;
            }
            for (int j = 0; j < ELM_SIZE; j++) {
                if (((element & (1L << j)) != 0)) {
                    if (comma) {
                        sb.append(", ");
                    }
                    sb.append(bitCount);
                    comma = true;
                }
                bitCount++;
            }
        }
        sb.append('}");
        return sb.toString();