

PREV CLASS NEXT CLASS  FRAMES NO FRAMES  
SUMMARY: NESTED  FIELD  CONSTR  METHOD  DETAIL: FIELD  CONSTR  METHOD 
java.lang.Object org.jscience.mathematics.algebraic.AbstractHypermatrix org.jscience.mathematics.algebraic.AbstractMatrix org.jscience.mathematics.algebraic.AbstractVector org.jscience.mathematics.algebraic.matrices.AbstractBooleanVector org.jscience.mathematics.algebraic.matrices.BooleanVector
public class BooleanVector
Fixed sized (non resizable) BooleanVector. Upon instance construction a BooleanVector is told to hold a fixed number of Booleans  it's size. The size can be any number (need not be a power of 2 or so). The Booleans of a BooleanVector are indexed by nonnegative integers. Any attempt to access a Boolean at an index<0  index>=size() will throw an IndexOutOfBoundsException.
Individual indexed Booleans can be examined, set, or cleared.
Subranges can quickly be extracted, copied and replaced.
Quick iteration over subranges is provided by optimized internal iterators (forEach() methods).
One BooleanVector
may be used to modify the contents of another
BooleanVector
through logical AND, OR, XOR and other similar operations.
All operations consider the Booleans 0..size()1 and nothing else. Operations involving two BooleanVectors (like AND, OR, XOR, etc.) will throw an IllegalArgumentException if the secondary Boolean vector has a size smaller than the receiver.
A BooleanVector is never automatically resized, but it can manually be grown or shrinked via setSize(...).
For use cases that need to store several Booleans per information entity, quick accessors are provided that interpret subranges as 64 bit long integers.
Why this class? Fist, boolean[] take one byte per stored bit. This class takes one bit per stored bit. Second, many applications find the semantics of java.util.BitSet not particularly helpful for their needs. Third, operations working on all bits of a BooleanVector are extremely quick. For example, on NT, Pentium Pro 200 Mhz, SunJDK1.2.2, java classic, for two BooleanVectors A,B (both much larger than processor cache), the following results are obtained.
Note that this implementation is not synchronized.
BooleanMatrix
,
BitSet
,
Serialized FormConstructor Summary  


BooleanVector(AbstractBooleanVector vec)
Constructs a vector by copying a vector. 

BooleanVector(boolean[] array)
Constructs a vector by wrapping an array. 

BooleanVector(int size)
Constructs a Boolean vector that holds size Booleans. 
protected 
BooleanVector(long[] bits,
int size)
You normally need not use this method. 
Method Summary  

AbelianGroup.Member 
add(AbelianGroup.Member v)
Returns the addition of this vector and another. 
AbstractBooleanVector 
add(AbstractBooleanVector v)
Returns the addition (or) of this vector and another. 
BooleanVector 
add(BooleanVector v)
Returns the addition (or) of this vector and another. 
BooleanVector 
and(AbstractBooleanVector other)
Performs a logical AND of the receiver with another Boolean vector (A = A & B). 
BooleanVector 
and(BooleanVector other)

BooleanVector 
andNot(AbstractBooleanVector other)
Clears all of the Boolean in receiver whose corresponding bit is set in the other BooleanVector (A = A \ B). 
BooleanVector 
andNot(BooleanVector other)
Clears all of the Boolean in receiver whose corresponding bit is set in the other BooleanVector (A = A \ B). 
protected void 
checkSize(BooleanVector other)
Sanity check for operations requiring another BooleanVector with at least the same size. 
BooleanVector 
clear()
Clears all Boolean of the receiver. 
void 
clear(int bitIndex)
Changes the Boolean with index bitIndex to the "clear" (false) state. 
java.lang.Object 
clone()
Clone vector into a new vector. 
protected long[] 
elements()
You normally need not use this method. 
boolean 
equals(java.lang.Object obj)
Compares this object against the specified object. 
long 
getLongFromTo(int from,
int to)
Returns a long value representing Booleans of the receiver from index from to index to. 
boolean 
getPrimitiveElement(int bitIndex)
Returns from the BooleanVector the value of the Boolean with the specified index. 
int 
hashCode()
Returns a hash code value for the NON EMPTY receiver. 
int 
indexOfFromTo(int from,
int to,
boolean state)
Returns the index of the first occurrence of the specified state. 
AbstractBooleanVector 
mapElements(PrimitiveMapping f)
Applies a function on all the vector components. 
Ring.Member 
multiply(Ring.Member r)
The multiplication law. 
AbelianGroup.Member 
negate()
Returns the negative (not) of this vector. 
BooleanVector 
not()
Performs a logical NOT on the Boolean of the receiver (A = ~A). 
protected int 
numberOfBooleansInPartialUnit()
Returns the number of Booleans used in the trailing PARTIAL unit. 
protected int 
numberOfFullUnits()
Returns the number of units that are FULL (not PARTIAL). 
BooleanVector 
or(AbstractBooleanVector other)
Performs a logical OR of the receiver with another Boolean vector (A = A  B). 
BooleanVector 
or(BooleanVector other)

BooleanVector 
partFromTo(int from,
int to)
Constructs and returns a new Boolean vector which is a copy of the given range. 
void 
putLongFromTo(long value,
int from,
int to)
Sets Booleans of the receiver from index from to index to to the Booleans of value . 
void 
replaceFromToWith(int from,
int to,
boolean value)
Sets the Booleans in the given range to the state specified by value. 
void 
replaceFromToWith(int from,
int to,
BooleanVector source,
int sourceFrom)
Replaces the Booleans of the receiver in the given range with the Booleans of another Boolean vector. 
AbstractBooleanVector 
scalarMultiply(boolean x)
Returns the multiplication (and) of this vector by a scalar. 
Module.Member 
scalarMultiply(Ring.Member x)
Returns the multiplication (and) of this vector by a scalar. 
int 
scalarProduct(AbstractBooleanVector v)
Returns the scalar product of this vector and another. 
int 
scalarProduct(BooleanVector v)
Returns the scalar product of this vector and another. 
void 
setDimension(int newSize)
Shrinks or expands the receiver so that it holds newSize Booleans. 
void 
setElement(int bitIndex)
Changes the Boolean with index bitIndex to the "set" (true) state. 
void 
setElement(int bitIndex,
boolean value)
Sets the Boolean with index bitIndex to the state specified by value. 
AbelianGroup.Member 
subtract(AbelianGroup.Member v)
Returns the subtraction (or not) of this vector by another. 
AbstractBooleanVector 
subtract(AbstractBooleanVector v)
Returns the subtraction (or not) of this vector by another. 
BooleanVector 
subtract(BooleanVector v)
Returns the subtraction (or not) of this vector by another. 
java.lang.String 
toString()
Returns a string representation of the receiver. 
int 
trueBooleans()
Returns the number of Booleans currently in the true state. 
BooleanVector 
xor(AbstractBooleanVector other)
Performs a logical XOR of the receiver with another Boolean vector (A = A ^ B). 
BooleanVector 
xor(BooleanVector other)
Performs a logical XOR of the receiver with another Boolean vector (A = A ^ B). 
Methods inherited from class org.jscience.mathematics.algebraic.matrices.AbstractBooleanVector 

getColumn, getElement, getRow, getSubVector, read, reverse, scalarDivide, setAllElements, setColumn, setElement, setRow, setSubVector, toComplexVector, toDoubleVector, toIntegerVector, toMatrix, toPrimitiveArray, transpose 
Methods inherited from class org.jscience.mathematics.algebraic.AbstractVector 

getDimension, getElement, getInvalidElementMsg, toArray, toArray 
Methods inherited from class org.jscience.mathematics.algebraic.AbstractMatrix 

getElement, getInvalidElementMsg, numColumns, numRows, print, print, print, print, toArray 
Methods inherited from class org.jscience.mathematics.algebraic.AbstractHypermatrix 

getDimensions, numDimensions, numElements, numElements, toArray 
Methods inherited from class java.lang.Object 

finalize, getClass, notify, notifyAll, wait, wait, wait 
Methods inherited from interface org.jscience.mathematics.algebraic.Matrix 

numColumns, numRows, toArray 
Methods inherited from interface org.jscience.mathematics.algebraic.Hypermatrix 

getDimensions, getElement, numDimensions, numElements, numElements, toArray 
Constructor Detail 

protected BooleanVector(long[] bits, int size)
A BooleanVector is modelled as a long array, i.e. long[] bits holds Booleans of a BooleanVector. Each long value holds 64 bits. The ith bit is stored in bits[i/64] at bit position i % 64 (where bit position 0 refers to the least significant bit and 63 refers to the most significant bit).
java.lang.IllegalArgumentException
 if size < 0  size > bits.length*64.public BooleanVector(int size)
size
 the number of Booleans the Boolean vector shall have.
java.lang.IllegalArgumentException
 if size < 0.public BooleanVector(boolean[] array)
array
 an assigned valuepublic BooleanVector(AbstractBooleanVector vec)
vec
 an assigned valueMethod Detail 

public int trueBooleans()
protected void checkSize(BooleanVector other)
protected long[] elements()
A BooleanVector is modelled as a long array, i.e. long[] bits holds bits of a BooleanVector. Each long value holds 64 bits. The ith bit is stored in bits[i/64] at bit position i % 64 (where bit position 0 refers to the least significant bit and 63 refers to the most significant bit).
public boolean getPrimitiveElement(int bitIndex)
getPrimitiveElement
in class AbstractBooleanVector
bitIndex
 the bit index.
java.lang.IndexOutOfBoundsException
 if bitIndex<0  bitIndex>=size()public long getLongFromTo(int from, int to)
from
, ..., bit tofrom
set to bit to
.
All other bits of the return value are set to 0.
If tofrom+1==0 then returns zero (0L).
from
 index of start bit (inclusive).to
 index of end bit (inclusive).
java.lang.IndexOutOfBoundsException
 if from<0  from>=size()  to<0  to>=size()  tofrom+1<0  tofrom+1>64public int indexOfFromTo(int from, int to, boolean state)
1
if the receiver does not contain this state.
Searches between from
, inclusive and to
, inclusive.
Optimized for speed. Preliminary performance (200Mhz Pentium Pro, JDK 1.2, NT): size=10^6, from=0, to=size1, receiver contains matching state in the very end > 0.002 seconds elapsed time.
state
 state to search for.from
 the leftmost search position, inclusive.to
 the rightmost search position, inclusive.
1
if the element is not found.
java.lang.IndexOutOfBoundsException
 if (size()>0 && (from<0  from>to  to>=size())).protected int numberOfBooleansInPartialUnit()
protected int numberOfFullUnits()
public BooleanVector partFromTo(int from, int to)
from
 the start index within the receiver, inclusive.to
 the end index within the receiver, inclusive.
java.lang.IndexOutOfBoundsException
 if size()>0 && (from<0  from>to  to>=size())).public void setElement(int bitIndex, boolean value)
setElement
in class AbstractBooleanVector
bitIndex
 the index of the Boolean to be changed.value
 the value to be stored in the Boolean.
java.lang.IndexOutOfBoundsException
 if bitIndex<0  bitIndex>=size()public void putLongFromTo(long value, int from, int to)
from
to index to
to the Booleans of value
.
Boolean from
is set to bit 0 of value
, ..., bit to
is set to bit tofrom
of value
.
All other bits stay unaffected.
If tofrom+1==0 then does nothing.
value
 the value to be copied into the receiver.from
 index of start bit (inclusive).to
 index of end bit (inclusive).
java.lang.IndexOutOfBoundsException
 if from<0  from>=size()  to<0  to>=size()  tofrom+1<0  tofrom+1>64.public void replaceFromToWith(int from, int to, BooleanVector source, int sourceFrom)
Optimized for speed. Preliminary performance (200Mhz Pentium Pro, JDK 1.2, NT): replace 10^6 ill aligned Booleans > 0.02 seconds elapsed time.
from
 the start index within the receiver, inclusive.to
 the end index within the receiver, inclusive.source
 the source BooleanVector to copy from.sourceFrom
 the start index within source, inclusive.
java.lang.IndexOutOfBoundsException
 if size()>0 && (from<0  from>to  to>=size()  sourceFrom<0  sourceFrom+tofrom+1>source.size())).public void replaceFromToWith(int from, int to, boolean value)
Optimized for speed. Preliminary performance (200Mhz Pentium Pro, JDK 1.2, NT): replace 10^6 ill aligned Booleans > 0.002 seconds elapsed time.
from
 the start index, inclusive.to
 the end index, inclusive.value
 the value to be stored in the Booleans of the range.
java.lang.IndexOutOfBoundsException
 if size()>0 && (from<0  from>to  to>=size()).public void setElement(int bitIndex)
bitIndex
 the index of the Boolean to be set.
java.lang.IndexOutOfBoundsException
 if bitIndex<0  bitIndex>=size()public void setDimension(int newSize)
newSize
 the number of Booleans the Boolean vector shall have.
java.lang.IllegalArgumentException
 if size < 0.public int hashCode()
Suppose the Booleans in the receiver were to be stored
in an array of long
integers called, say,
Booleans
, in such a manner that Boolean k
is
set in the receiver (for nonnegative values of
k
) if and only if the expression
((k>>6) < bits.length) && ((bits[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 int hashCode() { long h = 1234; for (int i = bits.length; i >= 0; ) { h ^= bits[i] * (i + 1); } return (int)((h >> 32) ^ h); }Note that the hash code values change if the set of bits is altered.
hashCode
in class AbstractBooleanVector
public boolean equals(java.lang.Object obj)
true
if and only if the argument is
not null
and is a BooleanVector
object
that has the same size as the receiver and
the same Booleans set to true
as the receiver.
That is, for every nonnegative int
index k
,
((BooleanVector)obj).get(k) == this.get(k)must be true.
equals
in class AbstractBooleanVector
obj
 the object to compare with.
true
if the objects are the same;
false
otherwise.public java.lang.String toString()
toString
in class AbstractBooleanVector
public BooleanVector clear()
public void clear(int bitIndex)
bitIndex
 the index of the Boolean to be cleared.
java.lang.IndexOutOfBoundsException
 if bitIndex<0  bitIndex>=size()public BooleanVector and(AbstractBooleanVector other)
true
if and only if it already had the
value true
and the corresponding Boolean in the other Boolean vector
argument has the value true
.
and
in class AbstractBooleanVector
other
 a Boolean vector.
java.lang.IllegalArgumentException
 if size() > other.size().public BooleanVector and(BooleanVector other)
public BooleanVector andNot(AbstractBooleanVector other)
other
 a BooleanVector with which to mask the receiver.
java.lang.IllegalArgumentException
 if size() > other.size().public BooleanVector andNot(BooleanVector other)
andNot
in class AbstractBooleanVector
other
 a BooleanVector with which to mask the receiver.
java.lang.IllegalArgumentException
 if size() > other.size().public BooleanVector not()
not
in class AbstractBooleanVector
public BooleanVector or(AbstractBooleanVector other)
true
if and only if it either already had the
value true
or the corresponding Boolean in the other Boolean vector
argument has the value true
.
or
in class AbstractBooleanVector
other
 a Boolean vector.
java.lang.IllegalArgumentException
 if size() > other.size().public BooleanVector or(BooleanVector other)
public BooleanVector xor(AbstractBooleanVector other)
true
if and only if one of the following statements holds:
true
, and the
corresponding bit in the argument has the value false
.
false
, and the
corresponding Boolean in the argument has the value true
.
xor
in class AbstractBooleanVector
other
 a Boolean vector.
java.lang.IllegalArgumentException
 if size() > other.size().public BooleanVector xor(BooleanVector other)
true
if and only if one of the following statements holds:
true
, and the
corresponding bit in the argument has the value false
.
false
, and the
corresponding Boolean in the argument has the value true
.
other
 a Boolean vector.
java.lang.IllegalArgumentException
 if size() > other.size().public AbelianGroup.Member negate()
negate
in interface AbelianGroup.Member
negate
in class AbstractBooleanVector
public AbelianGroup.Member add(AbelianGroup.Member v)
add
in interface AbelianGroup.Member
v
 a group member
public AbstractBooleanVector add(AbstractBooleanVector v)
add
in class AbstractBooleanVector
v
 an bit vector
IllegalDimensionException
 If the vectors are different sizes.public BooleanVector add(BooleanVector v)
v
 an bit vector
IllegalDimensionException
 If the vectors are different sizes.public AbelianGroup.Member subtract(AbelianGroup.Member v)
subtract
in interface AbelianGroup.Member
v
 a group member
public AbstractBooleanVector subtract(AbstractBooleanVector v)
subtract
in class AbstractBooleanVector
v
 an bit vector
IllegalDimensionException
 If the vectors are different sizes.public BooleanVector subtract(BooleanVector v)
v
 an bit vector
IllegalDimensionException
 If the vectors are different sizes.public Ring.Member multiply(Ring.Member r)
Ring.Member
multiply
in interface Ring.Member
r
 a ring member
public Module.Member scalarMultiply(Ring.Member x)
scalarMultiply
in interface Module.Member
x
 a ring member
public AbstractBooleanVector scalarMultiply(boolean x)
scalarMultiply
in class AbstractBooleanVector
x
 an bit
public int scalarProduct(AbstractBooleanVector v)
scalarProduct
in class AbstractBooleanVector
v
 an bit vector
IllegalDimensionException
 If the vectors are different sizes.public int scalarProduct(BooleanVector v)
v
 an bit vector
IllegalDimensionException
 If the vectors are different sizes.public AbstractBooleanVector mapElements(PrimitiveMapping f)
mapElements
in class AbstractBooleanVector
f
 a userdefined function.
public java.lang.Object clone()
clone
in class java.lang.Object


PREV CLASS NEXT CLASS  FRAMES NO FRAMES  
SUMMARY: NESTED  FIELD  CONSTR  METHOD  DETAIL: FIELD  CONSTR  METHOD 