Class BitString
- java.lang.Object
-
- com.github.javabdd.BitString
-
- All Implemented Interfaces:
Serializable,Cloneable
public final class BitString extends Object implements Cloneable, Serializable
BitStringimplements a vector of bits much likejava.util.BitSet, except that this implementation actually works. Also,BitStringhas some groovy features whichBitSetdoesn't; mostly related to efficient iteration overtrueandfalsecomponents.Each component of the
BitStringhas a boolean value. The bits of aBitStringare indexed by non-negative integers (that means they are zero-based, of course). Individual indexed bits can be examined, set, or cleared. OneBitStringmay be used to modify the contents of anotherBitStringthrough 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 current size, which is the number of bits of space currently in use by the bit set. Note that the size is related to the implementation of a bit set, so it may change with implementation. The length of a bit set related to the logical length of a bit set and is defined independently of implementation.
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classBitString.BackwardBitStringIteratorIterator for iterating through a bit string in backward order.static classBitString.BitStringIteratorAbstract bit string iterator class.classBitString.ForwardBitStringIteratorIterator for iterating through a bit string in forward order.classBitString.ForwardBitStringZeroIteratorIterator for iterating through a bit string in forward order.
-
Constructor Summary
Constructors Constructor Description BitString(int nbits)Creates an empty string with the specified size.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanand(BitString set)Logically ANDs this bit set with the specified set of bits.BitString.BackwardBitStringIteratorbackwardsIterator()Returns an iterator that iterates through the bits in backward order.BitString.BackwardBitStringIteratorbackwardsIterator(int i)Returns an iterator that iterates through the bits in backward order, starting at the given index.static intbsf(int b)Utility function to return the index of the first (lowest-order) one bit in the given integer.static intbsr(int v)Utility function to return the index of the last one bit in the given integer.voidclear(int bit)Clears a bit.voidclearAll()Clears all bits.voidclearUpTo(int bit)Clears all bits up to and including the given bit.Objectclone()Clones the BitString.booleancontains(BitString other)Check if this set contains all bits of the given set.voidcopyBits(BitString set)Copies the values of the bits in the specified set into this set.booleanequals(Object obj)Compares this object against the specified object.intfirstSet()Returns the first index in the bit string which is set, or -1 if there is no such index.intfirstSet(int where)Returns the first index greater thanwherein the bit string which is set, or -1 if there is no such index.booleanget(int bit)Gets a bit.inthashCode()Returns a hash code value for this bit string whose value depends only on which bits have been set within thisBitString.booleanintersectionEmpty(BitString other)Check if the intersection of the two sets is empty.booleanisZero()Returns whether thisBitStringis all zeroes.BitString.ForwardBitStringIteratoriterator()Returns an iterator that iterates through the bits in forward order.intlastSet()Returns the last index in the bit string which is set, or -1 if there is no such index.intlastSet(int where)Returns the last index less thanwherein the bit string which is set, or -1 if there is no such index.intlength()Returns the "logical size" of thisBitString: the index of the highest set bit in theBitStringplus one.booleanminus(BitString set)Logically subtracts this bit set with the specified set of bits.intnumberOfOnes()Returns the number of ones in thisBitString.intnumberOfOnes(int where)Returns the number of ones in thisBitStringup to a given index.booleanor(BitString set)Logically ORs this bit set with the specified set of bits.booleanor_upTo(BitString set, int bit)Logically ORs this bit set with the specified set of bits.static bytepopcount(int b)Utility function to return the number of 1 bits in the given integer value.static bytepopcount(long b)Utility function to return the number of 1 bits in the given long value.voidset(int bit)Sets a bit.voidsetAll()Sets all bits.voidsetRange(int lo, int hi)voidsetUpTo(int bit)Sets all bits up to and including the given bit.voidshl(int amt)Performs a left-shift operation.voidshr(int amt)Performs a right-shift operation.intsize()Returns the number of bits of space actually in use by thisBitStringto represent bit values.StringtoString()Converts the BitString to a String.booleanxor(BitString set)Logically XORs this bit set with the specified set of bits.BitString.ForwardBitStringZeroIteratorzeroIterator()
-
-
-
Method Detail
-
firstSet
public int firstSet()
Returns the first index in the bit string which is set, or -1 if there is no such index.- Returns:
- the first index
-
firstSet
public int firstSet(int where)
Returns the first index greater thanwherein the bit string which is set, or -1 if there is no such index.- Parameters:
where- the starting point for the search. May be negative.- Returns:
- the first index
-
popcount
public static final byte popcount(int b)
Utility function to return the number of 1 bits in the given integer value.- Parameters:
b- value to check- Returns:
- byte number of one bits
-
popcount
public static final byte popcount(long b)
Utility function to return the number of 1 bits in the given long value.- Parameters:
b- value to check- Returns:
- byte number of one bits
-
bsf
public static final int bsf(int b)
Utility function to return the index of the first (lowest-order) one bit in the given integer. Returns zero if the given number is zero.- Parameters:
b- value to check- Returns:
- byte index of first one bit, or zero if the number is zero
-
bsr
public static final int bsr(int v)
Utility function to return the index of the last one bit in the given integer. Returns zero if the given number is zero.- Parameters:
v- value to check- Returns:
- byte index of first one bit, or zero if the number is zero
-
lastSet
public int lastSet(int where)
Returns the last index less thanwherein the bit string which is set, or -1 if there is no such index.- Parameters:
where- the starting point for the search.- Returns:
- last index
-
lastSet
public int lastSet()
Returns the last index in the bit string which is set, or -1 if there is no such index.- Returns:
- last index
-
setAll
public void setAll()
Sets all bits.
-
setUpTo
public void setUpTo(int bit)
Sets all bits up to and including the given bit.- Parameters:
bit- the bit to be set up to (zero-based)
-
setRange
public void setRange(int lo, int hi)
-
set
public void set(int bit)
Sets a bit.- Parameters:
bit- the bit to be set (zero-based)
-
clearAll
public void clearAll()
Clears all bits.
-
clearUpTo
public void clearUpTo(int bit)
Clears all bits up to and including the given bit.- Parameters:
bit- the bit to be set up to (zero-based)
-
clear
public void clear(int bit)
Clears a bit.- Parameters:
bit- the bit to be cleared (zero-based)
-
get
public boolean get(int bit)
Gets a bit.- Parameters:
bit- the bit to be gotten (zero-based)- Returns:
- the bit
-
and
public boolean and(BitString set)
Logically ANDs this bit set with the specified set of bits. Returnstrueifthiswas modified in response to the operation.- Parameters:
set- the bit set to be ANDed with- Returns:
- modified
-
or
public boolean or(BitString set)
Logically ORs this bit set with the specified set of bits. Returnstrueifthiswas modified in response to the operation.- Parameters:
set- the bit set to be ORed with- Returns:
- modified
-
or_upTo
public boolean or_upTo(BitString set, int bit)
Logically ORs this bit set with the specified set of bits. Returnstrueifthiswas modified in response to the operation.- Parameters:
set- the bit set to be ORed withbit- set of bits- Returns:
- modified
-
xor
public boolean xor(BitString set)
Logically XORs this bit set with the specified set of bits. Returnstrueifthiswas modified in response to the operation.- Parameters:
set- the bit set to be XORed with- Returns:
- modified
-
minus
public boolean minus(BitString set)
Logically subtracts this bit set with the specified set of bits. Returnstrueifthiswas modified in response to the operation.- Parameters:
set- the bit set to subtract- Returns:
- modified
-
intersectionEmpty
public boolean intersectionEmpty(BitString other)
Check if the intersection of the two sets is empty.- Parameters:
other- the set to check intersection with- Returns:
- is empty or not
-
contains
public boolean contains(BitString other)
Check if this set contains all bits of the given set.- Parameters:
other- the set to check containment with- Returns:
- contains or not
-
shl
public void shl(int amt)
Performs a left-shift operation.- Parameters:
amt- number of bits to shift, can be negative
-
shr
public void shr(int amt)
Performs a right-shift operation.- Parameters:
amt- number of bits to shift
-
copyBits
public void copyBits(BitString set)
Copies the values of the bits in the specified set into this set.- Parameters:
set- the bit set to copy the bits from
-
hashCode
public int hashCode()
Returns a hash code value for this bit string whose value depends only on which bits have been set within thisBitString.
-
length
public int length()
Returns the "logical size" of thisBitString: the index of the highest set bit in theBitStringplus one. Returns zero if theBitStringcontains no set bits.- Returns:
- logical size
-
size
public int size()
Returns the number of bits of space actually in use by thisBitStringto represent bit values. The maximum element in the set is the size - 1st element. The minimum element in the set is the zero'th element.- Returns:
- number of bits
-
equals
public boolean equals(Object obj)
Compares this object against the specified object.
-
isZero
public boolean isZero()
Returns whether thisBitStringis all zeroes.- Returns:
- true if it is all zeroes.
-
numberOfOnes
public int numberOfOnes()
Returns the number of ones in thisBitString.- Returns:
- number of bits set.
-
numberOfOnes
public int numberOfOnes(int where)
Returns the number of ones in thisBitStringup to a given index.- Parameters:
where- given index- Returns:
- number of bits set.
-
toString
public String toString()
Converts the BitString to a String.
-
iterator
public BitString.ForwardBitStringIterator iterator()
Returns an iterator that iterates through the bits in forward order.- Returns:
- an iterator
-
zeroIterator
public BitString.ForwardBitStringZeroIterator zeroIterator()
-
backwardsIterator
public BitString.BackwardBitStringIterator backwardsIterator()
Returns an iterator that iterates through the bits in backward order.- Returns:
- an iterator
-
backwardsIterator
public BitString.BackwardBitStringIterator backwardsIterator(int i)
Returns an iterator that iterates through the bits in backward order, starting at the given index.- Parameters:
i- given index- Returns:
- an iterator
-
-