FileDocCategorySizeDatePackage
BitArray.javaAPI DocJava SE 6 API7385Tue Jun 10 00:22:32 BST 2008com.sun.org.apache.xalan.internal.xsltc.dom

BitArray

public class BitArray extends Object implements Externalizable
author
Morten Jorgensen

Fields Summary
static final long
serialVersionUID
private int[]
_bits
private int
_bitSize
private int
_intSize
private int
_mask
private static final int[]
_masks
private static final boolean
DEBUG_ASSERTIONS
private int
_pos
This method returns the Nth bit that is set in the bit array. The current position is cached in the following 4 variables and will help speed up a sequence of next() call in an index iterator. This method is a mess, but it is fast and it works, so don't fuck with it.
private int
_node
private int
_int
private int
_bit
int
_first
int
_last
Constructors Summary
public BitArray()
Constructor. Defines the initial size of the bit array (in bits).

    
                    
      
	this(32);
    
public BitArray(int size)

        
        if (size < 32) size = 32;
        _bitSize = size;
        _intSize = (_bitSize >>> 5) + 1;
        _bits = new int[_intSize + 1];
    
public BitArray(int size, int[] bits)

	if (size < 32) size = 32;
	_bitSize = size;
	_intSize = (_bitSize >>> 5) + 1;
	_bits = bits;
    
Methods Summary
public com.sun.org.apache.xalan.internal.xsltc.dom.BitArraycloneArray()

	return(new BitArray(_intSize, _bits));
    
public final int[]data()
Returns the integer array in which the bit array is contained

	return(_bits);
    
public final booleangetBit(int bit)
Returns true if the given bit is set

        if (DEBUG_ASSERTIONS) {
            if (bit >= _bitSize) {
                throw new Error(
                             "Programmer's assertion in  BitArray.getBit");
            }
        }

        return((_bits[bit>>>5] & _masks[bit%32]) != 0);
    
public final intgetBitNumber(int pos)


         

	// Return last node if position we're looking for is the same
	if (pos == _pos) return(_node);
	
	// Start from beginning of position we're looking for is before
	// the point where we left off the last time.
	if (pos < _pos) {
	    _int = _bit = _pos = 0;
	}

	// Scan through the bit array - skip integers that have no bits set
	for ( ; _int <= _intSize; _int++) {
	    int bits = _bits[_int];
	    if (bits != 0) { // Any bits set?
		for ( ; _bit < 32; _bit++) {
		    if ((bits & _masks[_bit]) != 0) {
			if (++_pos == pos) {
			    _node = ((_int << 5) + _bit) - 1;
			    return (_node);
			}
		    }
		}
		_bit = 0;
	    }
	}
	return(0);
    
public intgetMask()
See setMask()

	return(_mask);
    
public final intgetNextBit(int startBit)
Returns the next set bit from a given position

        for (int i = (startBit >>> 5) ; i<=_intSize; i++) {
            int bits = _bits[i];
            if (bits != 0) {
                for (int b = (startBit % 32); b<32; b++) {
                    if ((bits & _masks[b]) != 0) {
                        return((i << 5) + b);
                    }
                }
            }
            startBit = 0;
        }
        return(DTMAxisIterator.END);
    
public final com.sun.org.apache.xalan.internal.xsltc.dom.BitArraymerge(com.sun.org.apache.xalan.internal.xsltc.dom.BitArray other)
Merge two bit arrays. This currently only works for nodes from a single DOM (because there is only one _mask per array).

	// Take other array's bits if we have node set
	if (_last == -1) {
	    _bits = other._bits;
	}
	// Only merge if other array has any bits set
	else if (other._last != -1) {
	    int start = (_first < other._first) ? _first : other._first;
	    int stop  = (_last > other._last) ? _last : other._last;

	    // Merge these bits into other array if other array is larger
	    if (other._intSize > _intSize) {
		if (stop > _intSize) stop = _intSize;
		for (int i=start; i<=stop; i++)
		    other._bits[i] |= _bits[i];
		_bits = other._bits;
	    }
	    // Merge other bits into this array if this arrai is large/equal.
	    else {
		if (stop > other._intSize) stop = other._intSize;
		for (int i=start; i<=stop; i++)
		    _bits[i] |= other._bits[i];
	    }
	}
	return(this);
    
public voidreadExternal(java.io.ObjectInput in)
Read the whole tree from a file (serialized)

	_bitSize = in.readInt();
	_intSize = (_bitSize >>> 5) + 1;
	_mask    = in.readInt();
	_bits    = (int[])in.readObject();
    
public final voidresize(int newSize)
Resizes the bit array - try to avoid using this method!!!

	if (newSize > _bitSize) {
	    _intSize = (newSize >>> 5) + 1;
	    final int[] newBits = new int[_intSize + 1];
	    System.arraycopy(_bits, 0, newBits, 0, (_bitSize>>>5) + 1);
	    _bits = newBits;
	    _bitSize = newSize;
	}
    
public final voidsetBit(int bit)
Sets a given bit

 // The _INTEGER INDEX_ where last set bit is

             
         
        if (DEBUG_ASSERTIONS) {
            if (bit >= _bitSize) {
                throw new Error(
                             "Programmer's assertion in  BitArray.getBit");
            }
        }

        if (bit >= _bitSize) return;
        final int i = (bit >>> 5);
        if (i < _first) _first = i;
        if (i > _last) _last = i;
        _bits[i] |= _masks[bit % 32];
    
public voidsetMask(int mask)
Set the mask for this bit array. The upper 8 bits of this mask indicate the DOM in which the nodes in this array belong.

	_mask = mask;
    
public final intsize()
Returns the size of this bit array (in bits).

	return(_bitSize);
    
public voidwriteExternal(java.io.ObjectOutput out)

	out.writeInt(_bitSize);
	out.writeInt(_mask);
	out.writeObject(_bits);
	out.flush();