public class BitMatrix extends PersistentObject
Individual bits can be examined, set, or cleared.
Rectangular parts (boxes) can quickly be extracted, copied and replaced.
Quick iteration over boxes is provided by optimized internal iterators (forEach() methods).
One BitMatrix
may be used to modify the contents of another
BitMatrix
through logical AND, OR, XOR and other similar operations.
Legal coordinates range from [0,0] to [columns()-1,rows()-1]. Any attempt to access a bit at a coordinate column<0 || column>=columns() || row<0 || row>=rows() will throw an IndexOutOfBoundsException. Operations involving two bit matrices (like AND, OR, XOR, etc.) will throw an IllegalArgumentException if both bit matrices do not have the same number of columns and rows.
If you need extremely quick access to individual bits: Although getting and setting individual bits with methods get(...) and put(...) is quick, it is even quicker (but not safe) to use getQuick(...) and putQuick(...).
Note that this implementation is not synchronized.
BitVector
,
QuickBitVector
,
BitSet
,
Serialized FormModifier and Type | Field and Description |
---|---|
protected long[] |
bits |
protected int |
columns |
protected int |
rows |
serialVersionUID
Constructor and Description |
---|
BitMatrix(int columns,
int rows)
Constructs a bit matrix with a given number of columns and rows.
|
Modifier and Type | Method and Description |
---|---|
void |
and(BitMatrix other)
Performs a logical AND of the receiver with another bit matrix.
|
void |
andNot(BitMatrix other)
Clears all of the bits in receiver whose corresponding
bit is set in the other bit matrix.
|
int |
cardinality()
Returns the number of bits currently in the true state.
|
protected void |
checkDimensionCompatibility(BitMatrix other)
Sanity check for operations requiring matrices with the same number of columns and rows.
|
void |
clear()
Clears all bits of the receiver.
|
Object |
clone()
Cloning this
BitMatrix produces a new BitMatrix
that is equal to it. |
int |
columns()
Returns the number of columns of the receiver.
|
protected void |
containsBox(int column,
int row,
int width,
int height)
Checks whether the receiver contains the given box.
|
BitMatrix |
copy()
Returns a shallow clone of the receiver; calls
clone() and casts the result. |
protected long[] |
elements() |
protected void |
elements(long[] bits,
int columns,
int rows)
You normally need not use this method.
|
boolean |
equals(Object obj)
Compares this object against the specified object.
|
boolean |
forEachCoordinateInState(boolean state,
IntIntProcedure procedure)
Applies a procedure to each coordinate that holds a bit in the given state.
|
boolean |
get(int column,
int row)
Returns from the receiver the value of the bit at the specified coordinate.
|
boolean |
getQuick(int column,
int row)
Returns from the receiver the value of the bit at the specified coordinate; WARNING: Does not check preconditions.
|
int |
hashCode()
Returns a hash code value for the receiver.
|
void |
not()
Performs a logical NOT on the bits of the receiver.
|
void |
or(BitMatrix other)
Performs a logical OR of the receiver with another bit matrix.
|
BitMatrix |
part(int column,
int row,
int width,
int height)
Constructs and returns a new matrix with width columns and height rows which is a copy of the contents of the given box.
|
void |
put(int column,
int row,
boolean value)
Sets the bit at the specified coordinate to the state specified by value.
|
void |
putQuick(int column,
int row,
boolean value)
Sets the bit at the specified coordinate to the state specified by value; WARNING: Does not check preconditions.
|
void |
replaceBoxWith(int column,
int row,
int width,
int height,
BitMatrix source,
int sourceColumn,
int sourceRow)
Replaces a box of the receiver with the contents of another matrix's box.
|
void |
replaceBoxWith(int column,
int row,
int width,
int height,
boolean value)
Sets the bits in the given box to the state specified by value.
|
int |
rows()
Returns the number of rows of the receiver.
|
int |
size()
Returns the size of the receiver which is columns()*rows().
|
BitVector |
toBitVector()
Converts the receiver to a bitvector.
|
String |
toString()
Returns a (very crude) string representation of the receiver.
|
void |
xor(BitMatrix other)
Performs a logical XOR of the receiver with another bit matrix.
|
public BitMatrix(int columns, int rows)
columns
- the number of columns the matrix shall have.rows
- the number of rows the matrix shall have.IllegalArgumentException
- if columns < 0 || rows < 0.public void and(BitMatrix other)
true
if and only if it already had the
value true
and the corresponding bit in the other bit matrix
argument has the value true
.other
- a bit matrix.IllegalArgumentException
- if columns() != other.columns() || rows() != other.rows().public void andNot(BitMatrix other)
other
- a bit matrix with which to mask the receiver.IllegalArgumentException
- if columns() != other.columns() || rows() != other.rows().public int cardinality()
protected void checkDimensionCompatibility(BitMatrix other)
public void clear()
public Object clone()
BitMatrix
produces a new BitMatrix
that is equal to it.
The clone of the bit matrix is another bit matrix that has exactly the
same bits set to true
as this bit matrix and the same
number of columns and rows.clone
in class PersistentObject
public int columns()
protected void containsBox(int column, int row, int width, int height)
public BitMatrix copy()
clone()
and casts the result.protected long[] elements()
protected void elements(long[] bits, int columns, int rows)
IllegalArgumentException
- if columns < 0 || rows < 0 || columns*rows > bits.length*64public boolean equals(Object obj)
true
if and only if the argument is
not null
and is a BitMatrix
object
that has the same number of columns and rows as the receiver and
that has exactly the same bits set to true
as the receiver.public boolean forEachCoordinateInState(boolean state, IntIntProcedure procedure)
state
- element to search for.procedure
- a procedure object taking as first argument the current column and as second argument the current row. Stops iteration if the procedure returns false, otherwise continues.public boolean get(int column, int row)
column
- the index of the column-coordinate.row
- the index of the row-coordinate.IndexOutOfBoundsException
- if column<0 || column>=columns() || row<0 || row>=rows()public boolean getQuick(int column, int row)
Provided with invalid parameters this method may return invalid values without throwing any exception. You should only use this method when you are absolutely sure that the coordinate is within bounds. Precondition (unchecked): column>=0 && column<columns() && row>=0 && row<rows().
column
- the index of the column-coordinate.row
- the index of the row-coordinate.public int hashCode()
public void not()
public void or(BitMatrix other)
true
if and only if it either already had the
value true
or the corresponding bit in the other bit matrix
argument has the value true
.other
- a bit matrix.IllegalArgumentException
- if columns() != other.columns() || rows() != other.rows().public BitMatrix part(int column, int row, int width, int height)
column
- the index of the column-coordinate.row
- the index of the row-coordinate.width
- the width of the box.height
- the height of the box.IndexOutOfBoundsException
- if column<0 || column+width>columns() || row<0 || row+height>rows()public void put(int column, int row, boolean value)
column
- the index of the column-coordinate.row
- the index of the row-coordinate.value
- the value of the bit to be copied into the specified coordinate.IndexOutOfBoundsException
- if column<0 || column>=columns() || row<0 || row>=rows()public void putQuick(int column, int row, boolean value)
Provided with invalid parameters this method may return invalid values without throwing any exception. You should only use this method when you are absolutely sure that the coordinate is within bounds. Precondition (unchecked): column>=0 && column<columns() && row>=0 && row<rows().
column
- the index of the column-coordinate.row
- the index of the row-coordinate.value
- the value of the bit to be copied into the specified coordinate.public void replaceBoxWith(int column, int row, int width, int height, BitMatrix source, int sourceColumn, int sourceRow)
column
- the index of the column-coordinate.row
- the index of the row-coordinate.width
- the width of the box.height
- the height of the box.source
- the source matrix to copy from(may be identical to the receiver).sourceColumn
- the index of the source column-coordinate.sourceRow
- the index of the source row-coordinate.IndexOutOfBoundsException
- if column<0 || column+width>columns() || row<0 || row+height>rows()IndexOutOfBoundsException
- if sourceColumn<0 || sourceColumn+width>source.columns() || sourceRow<0 || sourceRow+height>source.rows()public void replaceBoxWith(int column, int row, int width, int height, boolean value)
column
- the index of the column-coordinate.row
- the index of the row-coordinate.width
- the width of the box.height
- the height of the box.value
- the value of the bit to be copied into the bits of the specified box.IndexOutOfBoundsException
- if column<0 || column+width>columns() || row<0 || row+height>rows()public int rows()
public int size()
public BitVector toBitVector()
public String toString()
public void xor(BitMatrix 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 bit in the argument has the value true
.
other
- a bit matrix.IllegalArgumentException
- if columns() != other.columns() || rows() != other.rows().Copyright © 2006–2019 SYSTAP, LLC DBA Blazegraph. All rights reserved.