uchicago.src.sim.space
Class AbsMulti2DGrid

java.lang.Object
  extended by uchicago.src.sim.space.AbsMulti2DGrid
All Implemented Interfaces:
Discrete2DSpace, IMulti2DGrid
Direct Known Subclasses:
AbsMulti2DHexagonalGrid, AbsMulti2DTorus, Multi2DGrid, OrderedMulti2DGrid

public abstract class AbsMulti2DGrid
extends java.lang.Object
implements IMulti2DGrid

Base class for grids whose cells can hold more that one occupant. The actual object held in the grid cell is a Cell object.

Version:
$Revision: 1.5 $ $Date: 2004/11/03 19:50:58 $

Field Summary
protected  BaseMatrix matrix
           
protected  java.util.Iterator roIter
           
protected  java.util.List roList
           
protected  int xSize
           
protected  int ySize
           
 
Fields inherited from interface uchicago.src.sim.space.Discrete2DSpace
MOORE, VON_NEUMANN
 
Constructor Summary
AbsMulti2DGrid(int xSize, int ySize, boolean sparse)
          Creates this AbsMulti2DGrid with the specified dimensions.
 
Method Summary
 void clear(int x, int y)
          Clears the contents the specified cell.
 Cell getCellAt(int x, int y)
          Gets the Cell object at the specified coordinates.
 int getCellSizeAt(int x, int y)
          Gets the size (number of occupants) of the cell at the specified location.
 java.util.Iterator getIteratorAt(int x, int y)
          Gets the iterator for the collection of objects at the specified coordinates.
 BaseMatrix getMatrix()
          Gets the matrix collection class that contains all the values
 java.util.ArrayList getMooreNeighbors(int x, int y, boolean returnNulls)
          Gets the Moore neighbors of the object(s) at x, y.
 java.util.ArrayList getMooreNeighbors(int x, int y, int xExtent, int yExtent, boolean returnNulls)
          Gets the extended Moore neighbors of the object(s) at x, y.
 java.util.ArrayList getMooreNeighborsLoc(int x, int y, boolean returnNulls)
          Gets the Moore neighbors of the object(s) at x, y.
 java.util.ArrayList getMooreNeighborsLoc(int x, int y, int xExtent, int yExtent, boolean returnNulls)
          Gets the extended Moore neighbors of the object(s) at x, y.
 java.lang.Object getObjectAt(int x, int y)
          Gets the Object at the specified coordinate.
 java.util.List getObjectsAt(int x, int y)
          Gets the List of objects at the specified coordinates.
 java.awt.Dimension getSize()
          Gets the dimension of the space
 int getSizeX()
          Gets the size of the x dimension
 int getSizeY()
          Gets the size of the y dimension
 double getValueAt(int x, int y)
          Gets the value at the specified coordinate if appropriate.
 java.util.ArrayList getVNNeighbors(int x, int y, boolean returnNulls)
          Gets the von Neumann neighbors of the object(s) at x, y.
 java.util.ArrayList getVNNeighbors(int x, int y, int xExtent, int yExtent, boolean returnNulls)
          Gets the extended von Neumann neighbors of the objects(s) at x, y.
 java.util.ArrayList getVNNeighborsLoc(int x, int y, boolean returnNulls)
          Gets the von Neumann neighbors of the object(s) at x, y.
 java.util.ArrayList getVNNeighborsLoc(int x, int y, int xExtent, int yExtent, boolean returnNulls)
          Gets the extended von Neumann neighbors of the objects(s) at x, y.
abstract  void putObjectAt(int x, int y, java.lang.Object object)
          Puts the specified Object at the specified coordinate.
 void putValueAt(int x, int y, double value)
          Puts the specified value at the specified coordinate.
protected  void rangeCheck(int x, int y)
           
 void removeObjectAt(int x, int y, java.lang.Object obj)
          Removes the specified object from the specified location.
 void trim()
          Releases any superfluous memory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

matrix

protected BaseMatrix matrix

xSize

protected int xSize

ySize

protected int ySize

roList

protected java.util.List roList

roIter

protected java.util.Iterator roIter
Constructor Detail

AbsMulti2DGrid

public AbsMulti2DGrid(int xSize,
                      int ySize,
                      boolean sparse)
Creates this AbsMulti2DGrid with the specified dimensions. sparse specifies whether the grid will be sparsely filled or not. Specifying sparse can result in substantial memory savings.

Parameters:
xSize - the number of columns in the grid
ySize - the number of rows in the grid
sparse - whether the grid will be sparsely populated or not
Method Detail

rangeCheck

protected void rangeCheck(int x,
                          int y)

clear

public void clear(int x,
                  int y)
Clears the contents the specified cell.

Parameters:
x - the x coordinate of the cell to clear
y - the y coordinate of the cell to clear

getObjectsAt

public java.util.List getObjectsAt(int x,
                                   int y)
Gets the List of objects at the specified coordinates. An ordered grid will return the first object inserted at the beginning of the list and the last object inserted at the end of the list. The list order is undetermined for an unordered object.

Specified by:
getObjectsAt in interface IMulti2DGrid
Parameters:
x - the x coordinate
y - the y coordinate
Returns:
the list of objects at the specified location. If no objects are at the location the list will be empty (and unmodifable).
Throws:
java.lang.IndexOutOfBoundsException - if the given coordinates are out of range (x < 0 || x >= xSize || y < 0 || y >= ySize)

getIteratorAt

public java.util.Iterator getIteratorAt(int x,
                                        int y)
Gets the iterator for the collection of objects at the specified coordinates. For an ordered grid the order of iteration will be first object inserted, first returned and so on. For an unordered grid, order is undefined.

Specified by:
getIteratorAt in interface IMulti2DGrid
Parameters:
x - the x coordinate
y - the y coordinate
Returns:
an iterator for the objects at the specified location. If no objects are at the location, the iterator will be empty.
Throws:
java.lang.IndexOutOfBoundsException - if the given coordinates are out of range (x < 0 || x >= xSize || y < 0 || y >= ySize)

getCellAt

public Cell getCellAt(int x,
                      int y)
Gets the Cell object at the specified coordinates.

Specified by:
getCellAt in interface IMulti2DGrid
Parameters:
x - the x coordinate
y - the y coordinate
Throws:
java.lang.IndexOutOfBoundsException - if the given coordinates are out of range (x < 0 || x >= xSize || y < 0 || y >= ySize)

getCellSizeAt

public int getCellSizeAt(int x,
                         int y)
Gets the size (number of occupants) of the cell at the specified location.

Specified by:
getCellSizeAt in interface IMulti2DGrid
Parameters:
x - the x coordinate
y - the y coordinate*
Throws:
java.lang.IndexOutOfBoundsException - if the given coordinates are out of range (x < 0 || x >= xSize || y < 0 || y >= ySize)

removeObjectAt

public void removeObjectAt(int x,
                           int y,
                           java.lang.Object obj)
Removes the specified object from the specified location.

Specified by:
removeObjectAt in interface IMulti2DGrid
Parameters:
x - the x coordinate
y - the y coordinate
obj - the object to remove
Throws:
java.lang.IndexOutOfBoundsException - if the given coordinates are out of range (x < 0 || x >= xSize || y < 0 || y >= ySize)

getVNNeighborsLoc

public java.util.ArrayList getVNNeighborsLoc(int x,
                                             int y,
                                             boolean returnNulls)
Gets the von Neumann neighbors of the object(s) at x, y. Objects are returned in west, east, north, south order. The returned ArrayList contains ObjectLocation objects that can be used to determined the exact location of the object returned. The objects at x, y are not returned.

If the neighboring objects contain their location information, getVNNeighbors(...) should be used as it is faster.

Parameters:
x - the x coordinate of the object
y - the y coordinate of the object
returnNulls - whether nulls (nothing at x,y) should be returned
Returns:
an ArrayList of ObjectLocation objects. The object contained by the ObjectLocation object may be null.
Throws:
java.lang.IndexOutOfBoundsException - if the given coordinates are out of range (x < 0 || x >= xSize || y < 0 || y >= ySize)
See Also:
ObjectLocation

getVNNeighborsLoc

public java.util.ArrayList getVNNeighborsLoc(int x,
                                             int y,
                                             int xExtent,
                                             int yExtent,
                                             boolean returnNulls)
Gets the extended von Neumann neighbors of the objects(s) at x, y. The extension in the x and y direction are specified by xExtent and yExtent. Objects are returned in west, east, north, south order. The most distant objects are returned first, that is, all the objects to the west starting with the most distant, then those to the east and so on. The returned ArrayList contains ObjectLocation objects that can be used to determined the exact location of the object returned. The objects at x,y are not returned.

If the neighboring objects contain their location information, getVNNeighbors(...) should be used as it is faster.

Parameters:
x - the x coordinate of the object
y - the y coordinate of the object
xExtent - the extension of the neighborhood in the x direction
yExtent - the extension of the neighborhood in the y direction
returnNulls - whether nulls should be returned
Returns:
an ArrayList of ObjectLocation objects. The object contained by the ObjectLocation object may be null.
Throws:
java.lang.IndexOutOfBoundsException - if the given coordinates are out of range (x < 0 || x >= xSize || y < 0 || y >= ySize)
See Also:
ObjectLocation

getMooreNeighborsLoc

public java.util.ArrayList getMooreNeighborsLoc(int x,
                                                int y,
                                                boolean returnNulls)
Gets the Moore neighbors of the object(s) at x, y. The returned ArrayList contains ObjectLocation objects that can be used to determined the exact location of the object returned. The objects at x, y are not returned. Objects are returned by row starting with the "NW corner" and ending with the "SE corner."

If the neighboring objects contain their location information, getMooreNeighbors(...) should be used as it is faster.

Parameters:
x - the x coordinate of the object
y - the y coordinate of the object
returnNulls - should the returned Vector contain null objects
Returns:
an ArrayList of ObjectLocation objects. The object contained by the ObjectLocation object may be null.
Throws:
java.lang.IndexOutOfBoundsException - if the given coordinates are out of range (x < 0 || x >= xSize || y < 0 || y >= ySize)
See Also:
ObjectLocation

getMooreNeighborsLoc

public java.util.ArrayList getMooreNeighborsLoc(int x,
                                                int y,
                                                int xExtent,
                                                int yExtent,
                                                boolean returnNulls)
Gets the extended Moore neighbors of the object(s) at x, y. The extension in the x and y direction are specified by xExtent and yExtent. The returned ArrayList contains ObjectLocation objects that can be used to determined the exact location of the object returned. The objects at x, y are not returned. Objects are returned by row starting with the "NW corner" and ending with the "SE corner."

If the neighboring objects contain their location information, getMooreNeighbors(...) should be used as it is faster.

Parameters:
x - the x coordinate of the object
y - the y coordinate of the object
xExtent - the extension of the neighborhood in the x direction
yExtent - the extension of the neighborhood in the y direction
returnNulls - should the returned ArrayList contain null objects
Returns:
an ArrayList of ObjectLocation objects. The object contained by the ObjectLocation object may be null.
Throws:
java.lang.IndexOutOfBoundsException - if the given coordinates are out of range (x < 0 || x >= xSize || y < 0 || y >= ySize)
See Also:
ObjectLocation

getVNNeighbors

public java.util.ArrayList getVNNeighbors(int x,
                                          int y,
                                          boolean returnNulls)
Gets the von Neumann neighbors of the object(s) at x, y. Objects are returned in west, east, north, south order. The objects at x, y are not returned.

If the neighboring objects do not contain their location information, getVNNeighborsLoc(...) can be used, although it is slower.

Parameters:
x - the x coordinate of the object
y - the y coordinate of the object
returnNulls - whether nulls (nothing at x,y) should be returned
Returns:
an ArrayList of objects (and possibly nulls).
Throws:
java.lang.IndexOutOfBoundsException - if the given coordinates are out of range (x < 0 || x >= xSize || y < 0 || y >= ySize)

getVNNeighbors

public java.util.ArrayList getVNNeighbors(int x,
                                          int y,
                                          int xExtent,
                                          int yExtent,
                                          boolean returnNulls)
Gets the extended von Neumann neighbors of the objects(s) at x, y. The extension in the x and y direction are specified by xExtent and yExtent. Objects are returned in west, east, north, south order. The most distant objects are returned first, that is, all the objects to the west starting with the most distant, then those to the east and so on. The objects at x,y are not returned.

If the neighboring objects do not contain their location information, getVNNeighborsLoc(...) can be used, although it is slower.

Parameters:
x - the x coordinate of the object
y - the y coordinate of the object
xExtent - the extension of the neighborhood in the x direction
yExtent - the extension of the neighborhood in the y direction
returnNulls - whether nulls should be returned
Returns:
an ArrayList of objects (and possibly nulls).
Throws:
java.lang.IndexOutOfBoundsException - if the given coordinates are out of range (x < 0 || x >= xSize || y < 0 || y >= ySize)

getMooreNeighbors

public java.util.ArrayList getMooreNeighbors(int x,
                                             int y,
                                             boolean returnNulls)
Gets the Moore neighbors of the object(s) at x, y. The objects at x, y are not returned. Objects are returned by row starting with the "NW corner" and ending with the "SE corner."

If the neighboring objects do not contain their location information, getMooreNeighborsLoc(...) can be used although it is slower.

Parameters:
x - the x coordinate of the object
y - the y coordinate of the object
returnNulls - should the returned ArrayList contain null objects
Returns:
an ArrayList of objects (and possibly nulls) ordered by row starting with the "NW corner" and ending with the "SE corner."
Throws:
java.lang.IndexOutOfBoundsException - if the given coordinates are out of range (x < 0 || x >= xSize || y < 0 || y >= ySize)

getMooreNeighbors

public java.util.ArrayList getMooreNeighbors(int x,
                                             int y,
                                             int xExtent,
                                             int yExtent,
                                             boolean returnNulls)
Gets the extended Moore neighbors of the object(s) at x, y. The objects at x, y are not returned. Objects are returned by row starting with the "NW corner" and ending with the "SE corner."

If the neighboring objects do not contain their location information, getMooreNeighborsLoc(...) can be used although it is slower.

Parameters:
x - the x coordinate of the object
y - the y coordinate of the object
xExtent - the extension of the neighborhood in the x direction
yExtent - the extension of the neighborhood in the y direction
returnNulls - should the returned ArrayList contain null objects
Returns:
an ArrayList of objects (and possibly nulls) ordered by row starting with the "NW corner" and ending with the "SE corner."
Throws:
java.lang.IndexOutOfBoundsException - if the given coordinates are out of range (x < 0 || x >= xSize || y < 0 || y >= ySize)
See Also:
ObjectLocation

getSizeX

public int getSizeX()
Gets the size of the x dimension

Specified by:
getSizeX in interface Discrete2DSpace

getSizeY

public int getSizeY()
Gets the size of the y dimension

Specified by:
getSizeY in interface Discrete2DSpace

getSize

public java.awt.Dimension getSize()
Gets the dimension of the space

Specified by:
getSize in interface Discrete2DSpace

getObjectAt

public java.lang.Object getObjectAt(int x,
                                    int y)
Gets the Object at the specified coordinate.

Specified by:
getObjectAt in interface Discrete2DSpace
Parameters:
x - the x coordinate
y - the y coordinate
Returns:
the Cell at x,y
Throws:
java.lang.IndexOutOfBoundsException - if the given coordinates are out of range (x < 0 || x >= xSize || y < 0 || y >= ySize)

getValueAt

public double getValueAt(int x,
                         int y)
Gets the value at the specified coordinate if appropriate.

Specified by:
getValueAt in interface Discrete2DSpace
Parameters:
x - the x coordinate
y - the y coordinate
Returns:
the value at x, y

putObjectAt

public abstract void putObjectAt(int x,
                                 int y,
                                 java.lang.Object object)
Puts the specified Object at the specified coordinate.

Specified by:
putObjectAt in interface Discrete2DSpace
Parameters:
x - the x coordinate
y - the y coordinate
object - the object to put

putValueAt

public void putValueAt(int x,
                       int y,
                       double value)
Puts the specified value at the specified coordinate.

Specified by:
putValueAt in interface Discrete2DSpace
Parameters:
x - the x coordinate
y - the y coordinate
value - the value to put at x,y

getMatrix

public BaseMatrix getMatrix()
Gets the matrix collection class that contains all the values

Specified by:
getMatrix in interface Discrete2DSpace

trim

public void trim()
Releases any superfluous memory. This is only usefull when working with sparse grids.