wyvern.util
Class BitMatrix

java.lang.Object
  extended bywyvern.util.BitMatrix

public final class BitMatrix
extends java.lang.Object

A 2D array of boolean flags, implemented as a BitSet for compact storage and efficient access.

Version:
1.0, Nov 17, 2002
Author:
Steve Yegge

Constructor Summary
BitMatrix(int width, int height)
          Constructs a new BitMatrix
 
Method Summary
 void clear()
          Resets all the values to zero (false).
 boolean getEntry(int x, int y)
          Returns the flag at the specified (x, y) matrix position.
 int getHeight()
          Returns the matrix height.
 int getWidth()
          Returns the matrix width.
 boolean isEmpty()
          Returns true if this BitMatrix contains no bits that are set to true.
 boolean isEmpty(int x, int y, int w, int h)
          Returns true if the specified rectangle is empty.
static void main(java.lang.String[] args)
          Tests that it works.
 BitMatrix makeClone()
          Clones this BitMatrix.
 void setEntries(int x, int y, int w, int h)
          Sets a rectangular area of bits in the matrix.
 void setEntry(int x, int y, boolean value)
          Sets an entry at the specified (x, y) location.
 java.lang.String toString()
          Prints out the matrix for debugging.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BitMatrix

public BitMatrix(int width,
                 int height)
Constructs a new BitMatrix

Method Detail

setEntry

public void setEntry(int x,
                     int y,
                     boolean value)
Sets an entry at the specified (x, y) location.

Parameters:
x - 0-indexed x value
y - 0-indexed y value

getEntry

public boolean getEntry(int x,
                        int y)
Returns the flag at the specified (x, y) matrix position.


clear

public void clear()
Resets all the values to zero (false).


getWidth

public int getWidth()
Returns the matrix width.


getHeight

public int getHeight()
Returns the matrix height.


isEmpty

public boolean isEmpty()
Returns true if this BitMatrix contains no bits that are set to true.

Returns:
true if all bits are false

setEntries

public void setEntries(int x,
                       int y,
                       int w,
                       int h)
Sets a rectangular area of bits in the matrix. Faster than using a nested for-loop to do it yourself.

Parameters:
x - left
y - top
w - width
h - height
Throws:
java.lang.ArrayIndexOutOfBoundsException - if any values are out of bounds.

isEmpty

public boolean isEmpty(int x,
                       int y,
                       int w,
                       int h)
Returns true if the specified rectangle is empty.

Parameters:
x - left coordinate in bit-matrix space
y - top coordinate in bit-matrix space
w - width of sub-rectangle to examine
h - height of sub-rectangle to examine
Throws:
java.lang.ArrayIndexOutOfBoundsException - if any values are out of bounds (but not guaranteed to be thrown, e.g. if the entire matrix is empty.) You should do the bounds-check up front before calling this method.

makeClone

public BitMatrix makeClone()
Clones this BitMatrix.

Returns:
a new BitMatrix of the same dimensions, with the same bits set as this one.

main

public static void main(java.lang.String[] args)
Tests that it works.


toString

public java.lang.String toString()
Prints out the matrix for debugging.