org.apache.derby.iapi.services.io
Class FormatableBitSet

java.lang.Object
  extended by org.apache.derby.iapi.services.io.FormatableBitSet
All Implemented Interfaces:
java.io.Externalizable, java.io.Serializable, java.lang.Cloneable, Formatable, TypedFormat

public final class FormatableBitSet
extends java.lang.Object
implements Formatable, java.lang.Cloneable

FormatableBitSet is implemented as a packed array of bytes.

See Also:
Serialized Form

Field Summary
private  byte bitsInLastByte
           
private  int lengthAsBits
           
private  byte[] value
          Bits are stored as an array of bytes.
 
Constructor Summary
FormatableBitSet()
          Niladic Constructor
FormatableBitSet(byte[] newValue)
          Constructs a Bit from an array of bytes.
FormatableBitSet(FormatableBitSet original)
          Copy constructor
FormatableBitSet(int numBits)
          Constructs a Bit with the initial number of bits
 
Method Summary
 void and(FormatableBitSet otherBit)
          Bitwise AND this FormatableBitSet with another FormatableBitSet.
 int anySetBit()
          If any bit is set, return the zero-based bit index of the first bit that is set.
 int anySetBit(int beyondBit)
          Like anySetBit(), but return any set bit whose number is bigger than beyondBit.
private  int calculateLength(int realByteLength)
           
private  void checkPosition(int p)
           
 void clear()
          Clear all the bits in this FormatableBitSet
 void clear(int position)
          Bit clear
 java.lang.Object clone()
           
 int compare(FormatableBitSet other)
          Bit comparison.
 boolean equals(FormatableBitSet other)
          Bit equivalence.
private static byte firstSet(byte v)
          A utility method which treats the byte argument as an 8-bit bitset and finds the first set bit in that byte.
 boolean get(int position)
          Bit get -- alias for isSet()
 byte[] getByteArray()
          Get the value of the byte array
 int getLength()
          Get the length in bits
 int getLengthInBytes()
          Get the length in bytes of a Bit value
 int getNumBitsSet()
          Get a count of the number of bits that are set.
 int getTypeFormatId()
          Get the formatID which corresponds to this class.
 void grow(int n)
          Grow (widen) a FormatableBitSet so that it contains at least N bits.
 int hashCode()
          Produce a hash code by putting the value bytes into an int, exclusive OR'ing if there are more than 4 bytes.
private  void initializeBits(int numBits)
           
 boolean invariantHolds()
          This method returns true if the following conditions hold: 1.
 boolean isSet(int position)
          Bit isSet
static int maxBitsForSpace(int numBytes)
          Statically calculates how many bits can fit into the number of bytes if this Bit object is externalized.
private static byte numBitsInLastByte(int bits)
          Figure out how many bits are in the last byte from the total number of bits.
private static int numBytesFromBits(int bits)
          Figure out how many bytes are needed to store the input number of bits.
 void or(FormatableBitSet otherBit)
          Bitwise OR this FormatableBitSet with another FormatableBitSet.
 void readExternal(java.io.ObjectInput in)
          Note: gracefully handles zero length bits -- will create a zero length array with no bits being used.
 void set(int position)
          Bit set
 void shrink(int n)
          Shrink (narrow) a FormatableBitSet to N bits.
 int size()
          Get the length in bits -- alias for getLength()
 java.lang.String toString()
          Format the string into BitSet format: {0, 2, 4, 8} if bits 0, 2, 4, 8 are set.
private static int udiv8(int i)
           
private static byte umod8(int i)
           
private static int umul8(int i)
           
 void writeExternal(java.io.ObjectOutput out)
          Format: int length in bits byte[]
 void xor(FormatableBitSet otherBit)
          Bitwise XOR this FormatableBitSet with another FormatableBitSet.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

value

private byte[] value
Bits are stored as an array of bytes. Bits are numbered starting at 0. Bits 0..7 go in byte[0], 8..15 in byte[1] and so on. The number of bytes is tracked as part of the byte array. The number of bits being used is derived by the number of bytes being used and the number of bits being used by the last byte. The partially unused byte is always byte[byte.length] with the lowest bits being unused. Zero length bits are stored using a zero length byte array, with all bits marked as unused.


bitsInLastByte

private byte bitsInLastByte

lengthAsBits

private transient int lengthAsBits
Constructor Detail

FormatableBitSet

public FormatableBitSet()
Niladic Constructor


FormatableBitSet

public FormatableBitSet(int numBits)
Constructs a Bit with the initial number of bits


FormatableBitSet

public FormatableBitSet(byte[] newValue)
Constructs a Bit from an array of bytes. Assume bytes are all being used.

Parameters:
newValue - The array of bytes to make up the new Bit

FormatableBitSet

public FormatableBitSet(FormatableBitSet original)
Copy constructor

Parameters:
original - the FormatableBitSet to make a copy from
Method Detail

checkPosition

private final void checkPosition(int p)

udiv8

private static int udiv8(int i)

umod8

private static byte umod8(int i)

umul8

private static int umul8(int i)

initializeBits

private void initializeBits(int numBits)

clone

public java.lang.Object clone()
Overrides:
clone in class java.lang.Object

invariantHolds

public boolean invariantHolds()
This method returns true if the following conditions hold: 1. The number of bits in the bitset will fit into the allocated byte array. 2. 'lengthAsBits' and 'bitsInLastByte' are consistent. 3. All unused bits in the byte array are unset. This represents an invariant for the class, so this method should always return true. The method is public, but is primarily intended for testing and ASSERTS.

Returns:
true if invariant holds, false otherwise

getLengthInBytes

public int getLengthInBytes()
Get the length in bytes of a Bit value

Returns:
The length in bytes of this value

getLength

public int getLength()
Get the length in bits

Returns:
The length in bits for this value NOTE: could possibly be changed to a long. As is we are restricted to 2^(31-3) -> 256meg instead of 2^31 (Integer.MAX_VALUE) like other datatypes (or 2 gig). If it is ever changed to a long be sure to change read/writeExternal which write out the length in bits.

calculateLength

private int calculateLength(int realByteLength)

size

public int size()
Get the length in bits -- alias for getLength()

Returns:
The length in bits for this value

getByteArray

public byte[] getByteArray()
Get the value of the byte array

Returns:
The value of the byte array

grow

public void grow(int n)
Grow (widen) a FormatableBitSet so that it contains at least N bits. If the bitset already has more than n bits, this is a noop. Negative values of n are not allowed. ASSUMPTIONS: that all extra bits in the last byte are zero.

Parameters:
n - The number of bits you want. The bits are always added as 0 and are appended to the least significant end of the bit array.

shrink

public void shrink(int n)
Shrink (narrow) a FormatableBitSet to N bits. N may not be larger than the current bitset size, or negative.

Parameters:
n - The number of bits the caller wants. The bits are always removed from the least significant end of the bit array.

equals

public boolean equals(FormatableBitSet other)
Bit equivalence. Compare this with other. If the length is different, then cannot be equal so short circuit. Otherwise, rely on compare(). Note that two zero length bits are considered equal.

Parameters:
other - the other bit to compare to
Returns:
TRUE|FALSE

compare

public int compare(FormatableBitSet other)
Bit comparison. Compare this with other. Will always do a byte by byte compare. Given 2 similar bits of unequal lengths (x and y), where x.getLength() < y.getLength() but where: x[0..x.getLength()] == y[0..x.getLength()] then x < y.

Parameters:
other - the other bit to compare to
Returns:
-1 - if other < this 0 - if other == this 1 - if other > this

hashCode

public int hashCode()
Produce a hash code by putting the value bytes into an int, exclusive OR'ing if there are more than 4 bytes.

Overrides:
hashCode in class java.lang.Object
Returns:
the hash code

isSet

public final boolean isSet(int position)
Bit isSet

Parameters:
position - the bit to check

get

public final boolean get(int position)
Bit get -- alias for isSet()

Parameters:
position - the bit to check

set

public void set(int position)
Bit set

Parameters:
position - the bit to set

clear

public void clear(int position)
Bit clear

Parameters:
position - the bit to clear

clear

public void clear()
Clear all the bits in this FormatableBitSet


numBytesFromBits

private static int numBytesFromBits(int bits)
Figure out how many bytes are needed to store the input number of bits.

Parameters:
bits - bits
Returns:
the number of bytes

numBitsInLastByte

private static byte numBitsInLastByte(int bits)
Figure out how many bits are in the last byte from the total number of bits.

Parameters:
bits - bits
Returns:
the number of bits

toString

public java.lang.String toString()
Format the string into BitSet format: {0, 2, 4, 8} if bits 0, 2, 4, 8 are set.

Overrides:
toString in class java.lang.Object
Returns:
A new String containing the formatted Bit value

maxBitsForSpace

public static int maxBitsForSpace(int numBytes)
Statically calculates how many bits can fit into the number of bytes if this Bit object is externalized. Only valid for this implementation of Bit.


firstSet

private static byte firstSet(byte v)
A utility method which treats the byte argument as an 8-bit bitset and finds the first set bit in that byte. Assumes that at least one bit in v is set (v!=0).

Parameters:
v - a non-zero byte to check for set bits
Returns:
the zero-based index of the first set bit in the argument byte

anySetBit

public int anySetBit()
If any bit is set, return the zero-based bit index of the first bit that is set. If no bit is set, return -1. By using anySetBit() and anySetBit(beyondBit), one can quickly go thru the entire bit array to return all set bit.

Returns:
the zero-based index of the first bit that is set, or -1 if no bit is set

anySetBit

public int anySetBit(int beyondBit)
Like anySetBit(), but return any set bit whose number is bigger than beyondBit. If no bit is set after beyondBit, -1 is returned. By using anySetBit() and anySetBit(beyondBit), one can quickly go thru the entire bit array to return all set bit.

Parameters:
beyondBit - Only look at bit that is greater than this bit number. Supplying a value of -1 makes the call equivalent to anySetBit().
Returns:
the bit number of a bit that is set, or -1 if no bit after beyondBit is set

or

public void or(FormatableBitSet otherBit)
Bitwise OR this FormatableBitSet with another FormatableBitSet. The result is stored in this bitset. The operand is unaffected. A null operand is treated as an empty bitset (i.e. a noop). A bitset that is smaller than its operand is expanded to the same size.

Parameters:
otherBit - bitset operand

and

public void and(FormatableBitSet otherBit)
Bitwise AND this FormatableBitSet with another FormatableBitSet. The result is stored in this bitset. The operand is unaffected. A null operand is treated as an empty bitset (i.e. clearing this bitset). A bitset that is smaller than its operand is expanded to the same size.

Parameters:
otherBit - bitset operand

xor

public void xor(FormatableBitSet otherBit)
Bitwise XOR this FormatableBitSet with another FormatableBitSet. The result is stored in this bitset. The operand is unaffected. A null operand is treated as an empty bitset (i.e. a noop). A bitset that is smaller than its operand is expanded to the same size.

Parameters:
otherBit - bitset operand

getNumBitsSet

public int getNumBitsSet()
Get a count of the number of bits that are set.

Returns:
The number of bits that are set.

writeExternal

public void writeExternal(java.io.ObjectOutput out)
                   throws java.io.IOException
Format:

Specified by:
writeExternal in interface java.io.Externalizable
Throws:
java.io.IOException
See Also:
Externalizable.writeExternal(java.io.ObjectOutput)

readExternal

public void readExternal(java.io.ObjectInput in)
                  throws java.io.IOException
Note: gracefully handles zero length bits -- will create a zero length array with no bits being used. Fortunately in.read() is ok with a zero length array so no special code.

WARNING: this method cannot be changed w/o changing SQLBit because SQLBit calls this directly w/o calling read/writeObject(), so the format id is not stored in that case.

Specified by:
readExternal in interface java.io.Externalizable
Throws:
java.io.IOException
See Also:
Externalizable.readExternal(java.io.ObjectInput)

getTypeFormatId

public int getTypeFormatId()
Get the formatID which corresponds to this class.

Specified by:
getTypeFormatId in interface TypedFormat
Returns:
the formatID of this class

Built on Thu 2012-03-29 21:53:33+0000, from revision ???

Apache Derby V10.6 Internals - Copyright © 2004,2007 The Apache Software Foundation. All Rights Reserved.