DRCL J-Sim API

drcl.data
Class BitSet

java.lang.Object
  extended bydrcl.DrclObj
      extended bydrcl.data.BitSet
All Implemented Interfaces:
java.lang.Cloneable, ObjectCloneable, ObjectDuplicable, java.io.Serializable

public class BitSet
extends DrclObj

This class implements a set of bits that grows as needed. Each bit of the set has a boolean value. The bits of a BitSet are indexed by nonnegative integers. Individual indexed bit can be examined, set, or cleared. One BitSet may be used to modify the contents of another BitSet through logical AND, logical inclusive OR, and logical exclusive OR operations.

By default, all bits in the set initially have the value false.

Every bit set has a size, which is the number of bits in use by the bit set. Note that the actual size is the actual number of bits that the bit set is able to represent. The size as well as the actual size is changed when the bit index of an operation exceeds the sizes. Also the size can be set explicitly with setSize(int). The most significant bits are truncated when a smaller size is set. Bits are packed into arrays of subsets(longs). Each subset is indexed with nonnegative integers. Index 0 refers to the subset containing the first 64 least significant bits. BitSet operations also apply to subsets.

See Also:
Serialized Form

Constructor Summary
BitSet()
          Creates a new bit set.
BitSet(int nbits_)
          Creates a bit set whose initial size is large enough to explicitly represent subset with indices in the range 0 through nbits-1.
BitSet(int[] set_)
          Creates and returns a bit set in which the bits of the positions specified in set_ are set to 1's.
BitSet(int size_, long value_)
           
BitSet(int size_, long[] values_)
          Creates and returns a bit set of the specified size given an array of long integers.
 
Method Summary
 void and(BitSet set_)
          Performs a logical AND of this target bit set with the argument bit set.
 void and(int subsetIndex_, long subset_)
          Performs a logical AND of the target subset with the argument subset.
 java.lang.String binaryRepresentation()
          Returns the binary representation of this bit set.
 java.lang.String binaryRepresentation(boolean skipLeadingZeros_)
          Returns the binary representation of this bit set.
 java.lang.String binaryRepresentation(int length_)
          Returns the binary representation of the bit set in the specified number of binary characters.
 void clear()
          Clears all the bits.
 void clear(int bitIndex_)
          Sets the bit specified by the index to false.
 void clear(int[] bitIndices_)
          Sets the bits specified by the indices to false.
 void clearBy(BitSet set_)
          Clears all of the subset in this BitSet whose corresponding bit is set in the specified BitSet.
 void clearBy(int subsetIndex_, long subset_)
          Clears the subset in this BitSet whose corresponding bit is set in the specified subset.
 java.lang.Object clone()
          Returns a clone of this object.
 void duplicate(java.lang.Object source_)
          opies the content of the source_ object to this object.
 boolean equals(java.lang.Object that_)
          Compares this object against the specified object.
 boolean get(int bitIndex_)
          Returns the value of the bit with the specified index.
 int getActualSize()
          Returns the number of bits actually used by this BitSet to represent bit values.
 int getNumSetBits()
          Returns the number of bits that are set true in this bit set.
 int[] getSetBitIndices()
          Returns the array of indices of the bits that are set true in this bit set.
 int getSize()
          Returns the "logical size" of this BitSet.
 long getSubset(int subsetIndex_)
          Returns the subset specified by the index to the argument subset.
 int[] getUnsetBitIndices()
          Returns the array of indices of the bits that are set false in this bit set.
 int hashCode()
          Returns a hash code value for this bit set.
 java.lang.String hexRepresentation()
          Returns the hex representation of this bit set.
 java.lang.String hexRepresentation(boolean skipLeadingZeros_)
          Returns the hex representation of this bit set.
 java.lang.String hexRepresentation(int length_)
          Returns the hex representation of the bit set in the specified number of hex characters.
 void not()
          Performs a logical NOT of the this bitset.
 java.lang.String numberRepresentation()
          Returns the long integer representation of this bit set.
 void or(BitSet set_)
          Performs a logical OR of this bit set with the bit set argument.
 void or(int subsetIndex_, long subset_)
          Performs a logical OR of the target subset with the argument subset.
 void set()
          Sets all the bits.
 void set(int bitIndex_)
          Sets the bit specified by the index to true.
 void set(int[] bitIndices_)
          Sets the bits specified by the indices to true.
 void set(int subsetIndex_, long subset_)
          Sets the subset specified by the index to the argument subset.
 void setSize(int size_)
          If size_ is larger than the current size, the bit set expands from the most significant bit and the new bits are cleared (set to false).
 java.lang.String toString()
          Prints out this bit set by the set of indices of 1's in this bit set.
 void xor(BitSet set_)
          Performs a logical XOR of this bit set with the bit set argument.
 void xor(int subsetIndex_, long subset_)
          Performs a logical XOR of the target subset with the argument subset.
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BitSet

public BitSet(int size_,
              long value_)

BitSet

public BitSet(int size_,
              long[] values_)
Creates and returns a bit set of the specified size given an array of long integers. Bit 0 of values_[0] is stored as the least significant bit, and bit 63 of values_[size-1] as the most significant bit.


BitSet

public BitSet(int[] set_)
Creates and returns a bit set in which the bits of the positions specified in set_ are set to 1's. The length of the bit set is set to the maximum value in set_.


BitSet

public BitSet()
Creates a new bit set. All subset are initially false.


BitSet

public BitSet(int nbits_)
Creates a bit set whose initial size is large enough to explicitly represent subset with indices in the range 0 through nbits-1. All subset are initially false.

Throws:
java.lang.NegativeArraySizeException - if the specified initial size is negative.
Method Detail

getSize

public int getSize()
Returns the "logical size" of this BitSet.

Returns:
the logical size of this BitSet.

setSize

public void setSize(int size_)
If size_ is larger than the current size, the bit set expands from the most significant bit and the new bits are cleared (set to false).


getNumSetBits

public int getNumSetBits()
Returns the number of bits that are set true in this bit set.


getSetBitIndices

public int[] getSetBitIndices()
Returns the array of indices of the bits that are set true in this bit set.

Returns:
the array of indices of the set bits.

getUnsetBitIndices

public int[] getUnsetBitIndices()
Returns the array of indices of the bits that are set false in this bit set.

Returns:
the array of indices of the unset bits.

getActualSize

public int getActualSize()
Returns the number of bits actually used by this BitSet to represent bit values. The maximum element in the set is the size - 1st element.

Returns:
the number of bits currently in this BitSet.

set

public void set()
Sets all the bits.


set

public void set(int bitIndex_)
Sets the bit specified by the index to true.

Parameters:
bitIndex_ - a bit index.
Throws:
java.lang.IndexOutOfBoundsException - if the specified index is negative.

set

public void set(int[] bitIndices_)
Sets the bits specified by the indices to true.

Parameters:
bitIndices_ - array of bit indices.
Throws:
java.lang.IndexOutOfBoundsException - if the specified index is negative.

set

public void set(int subsetIndex_,
                long subset_)
Sets the subset specified by the index to the argument subset. This operation does not change the size of the bit set.

Parameters:
subsetIndex_ - the subset index.
subset_ - the subset with which to mask the corresponding subset in this BitSet.

clear

public void clear()
Clears all the bits.


clear

public void clear(int bitIndex_)
Sets the bit specified by the index to false.

Parameters:
bitIndex_ - the index of the bit to be cleared.
Throws:
java.lang.IndexOutOfBoundsException - if the specified index is negative.

clear

public void clear(int[] bitIndices_)
Sets the bits specified by the indices to false.

Parameters:
bitIndices_ - array of bit indices.
Throws:
java.lang.IndexOutOfBoundsException - if the specified index is negative.

clearBy

public void clearBy(BitSet set_)
Clears all of the subset in this BitSet whose corresponding bit is set in the specified BitSet.

Parameters:
set_ - the BitSet with which to mask this BitSet.

clearBy

public void clearBy(int subsetIndex_,
                    long subset_)
Clears the subset in this BitSet whose corresponding bit is set in the specified subset. This operation does not change the size of the bit set.

Parameters:
subsetIndex_ - the subset index.
subset_ - the subset with which to mask the corresponding subset in this BitSet.

get

public boolean get(int bitIndex_)
Returns the value of the bit with the specified index. The value is true if the bit with the index bitIndex_ is currently set in this BitSet; otherwise, the result is false.

Parameters:
bitIndex_ - the bit index.
Returns:
the value of the bit with the specified index.
Throws:
java.lang.IndexOutOfBoundsException - if the specified index is negative.

getSubset

public long getSubset(int subsetIndex_)
Returns the subset specified by the index to the argument subset.

Parameters:
subsetIndex_ - the subset index.
Returns:
the subset withe the specified index.
Throws:
java.lang.IndexOutOfBoundsException - if the specified index is negative.

and

public void and(BitSet set_)
Performs a logical AND of this target bit set with the argument bit set. This bit set is modified so that each bit in it has the value true if and only if it both initially had the value true and the corresponding bit in the bit set argument also had the value true.

Parameters:
set_ - a bit set.

and

public void and(int subsetIndex_,
                long subset_)
Performs a logical AND of the target subset with the argument subset. This subset is modified so that each bit in it has the value true if and only if it both initially had the value true and the corresponding bit in the subset argument also had the value true. This operation does not change the size of the bit set.

Parameters:
subsetIndex_ - the subset index.
subset_ - the subset with which to mask the corresponding subset in this BitSet.

or

public void or(BitSet set_)
Performs a logical OR of this bit set with the bit set argument. This bit set is modified so that a bit in it has the value true if and only if it either already had the value true or the corresponding bit in the bit set argument has the value true. This operation does not change the size of the bit set.

Parameters:
set_ - a bit set.

or

public void or(int subsetIndex_,
               long subset_)
Performs a logical OR of the target subset with the argument subset. This subset is modified so that each bit in it has the value true if and only if it both initially had the value true or the corresponding bit in the subset argument also had the value true.

Parameters:
subsetIndex_ - the subset index.
subset_ - the subset with which to mask the corresponding subset in this BitSet.

xor

public void xor(BitSet set_)
Performs a logical XOR of this bit set with the bit set argument. This bit set is modified so that a bit in it has the value true if and only if one of the following statements holds:

Parameters:
set_ - a bit set.

xor

public void xor(int subsetIndex_,
                long subset_)
Performs a logical XOR of the target subset with the argument subset. This subset is modified so that a bit in it has the value true if and only if one of the following statements holds: This operation does not change the size of the bit set.

Parameters:
subsetIndex_ - the subset index.
subset_ - the subset with which to mask the corresponding subset in this BitSet.

not

public void not()
Performs a logical NOT of the this bitset. This operation does not change the size of the bit set.


hashCode

public int hashCode()
Returns a hash code value for this bit set. The has code depends only on which subset have been set within this BitSet. The algorithm used to compute it may be described as follows.

Suppose the subset in the BitSet were to be stored in an array of long integers called, say, subset, in such a manner that bit k is set in the BitSet (for nonnegative values of k) if and only if the expression

((k>>6) < subset.length) && ((subset[k>>6] & (1L << (bit & 0x3F))) != 0)
is true. Then the following definition of the hashCode method would be a correct implementation of the actual algorithm:
 public synchronized int hashCode() {
      long h = 1234;
      for (int i = subset.length; --i >= 0; ) {
           h ^= subset[i] * (i + 1);
      }
      return (int)((h >> 32) ^ h);
 }
Note that the hash code values change if the set of subset is altered.

Overrides the hashCode method of Object.

Returns:
a hash code value for this bit set.

equals

public boolean equals(java.lang.Object that_)
Compares this object against the specified object. The result is true if and only if the argument is not null and is a Bitset object that has exactly the same set of subset set to true as this bit set. That is, for every nonnegative int index k,
((BitSet)obj).get(k) == this.get(k)
must be true. The current sizes of the two bit sets are not compared.

Overrides the equals method of Object.

Parameters:
that_ - the object to compare with.
Returns:
true if the objects are the same; false otherwise.

duplicate

public void duplicate(java.lang.Object source_)
Description copied from class: DrclObj
opies the content of the source_ object to this object. he subclass must implement this method to realize DrclObj.clone().

Specified by:
duplicate in interface ObjectDuplicable
Overrides:
duplicate in class DrclObj

clone

public java.lang.Object clone()
Description copied from class: DrclObj
Returns a clone of this object. By default, this method creates an object of the same class and calls DrclObj.duplicate(Object) to duplicate the content of this object to the newly-created one.

This method uses getClass().newInstance() to create new instance. Hence, subclasses need to override this method only if the subclass is not declared as public or does not have explicit no-argument constructor. A subclass may override this method for performance reason.

Specified by:
clone in interface ObjectCloneable
Overrides:
clone in class DrclObj

toString

public java.lang.String toString()
Prints out this bit set by the set of indices of 1's in this bit set. Overrides the toString method of Object.

Example:

 BitSet bs_ = new BitSet();
Now bs_.toString() returns "-0-" (empty set).

 bs_.set(2);
Now bs_.toString() returns "2".

 bs_.set(4);
 bs_.set(10);
Now bs_.toString() returns "2,4,10".

Overrides:
toString in class DrclObj
Returns:
a string representation of this bit set.

numberRepresentation

public java.lang.String numberRepresentation()
Returns the long integer representation of this bit set.


binaryRepresentation

public java.lang.String binaryRepresentation()
Returns the binary representation of this bit set.


binaryRepresentation

public java.lang.String binaryRepresentation(boolean skipLeadingZeros_)
Returns the binary representation of this bit set.

Parameters:
skipLeadingZeros_ - if true, the leading zeros in the resulting binary represenation is not printed.

binaryRepresentation

public java.lang.String binaryRepresentation(int length_)
Returns the binary representation of the bit set in the specified number of binary characters. It stuffs zeros if the bit set is not large enough. The length of the result may exceed the specified length if the bit set is too large.

Parameters:
length_ - expected number of binary characters.

hexRepresentation

public java.lang.String hexRepresentation()
Returns the hex representation of this bit set.


hexRepresentation

public java.lang.String hexRepresentation(boolean skipLeadingZeros_)
Returns the hex representation of this bit set.

Parameters:
skipLeadingZeros_ - if true, the leading zeros in the resulting binary represenation is not printed.

hexRepresentation

public java.lang.String hexRepresentation(int length_)
Returns the hex representation of the bit set in the specified number of hex characters. It stuffs zeros if the bit set is not large enough. The length of the result may exceed the specified length if the bit set is too large.

Parameters:
length_ - expected number of hex characters.

DRCL J-Sim API

Copyright © 1998-2003 Distributed Real-time Computing Lab (DRCL). All Rights Reserved.     ~ To J-Sim Home ~