Package microsim.space
Class DenseObjectSpace
- All Implemented Interfaces:
ObjectSpace
- Direct Known Subclasses:
MultiObjectSpace
A bidimensional grid containing one object per cell.
Title: JAS
Description: Java Agent-based Simulation library
Copyright (C) 2002 Michele Sonnessa
This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.- Author:
- Michele Sonnessa
-
Field Summary
FieldsFields inherited from class microsim.space.AbstractSpace
modCount, xSize, ySize -
Constructor Summary
ConstructorsConstructorDescriptionDenseObjectSpace(int xSize, int ySize) Create a grid with given size.DenseObjectSpace(AbstractSpace<Object> grid) Create a copy of the given grid. -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdd an object implementing IGridPosition interface to the grid.booleanaddGridPosition(SpacePosition position) Add an object implementing IGridPosition interface to the grid.voidclear()Set all cells to the null value.intcountObjectsAt(int x, int y) Returns the number of objects allocated in cell (x,y).get(int x, int y) Return the object stored at the given position.Object[][]Return the matrix of objects representing the grid.Object[]getMooreNeighbors(int x, int y) Gets the Moore neighbors of the specified coordinate. doubles are returned by row starting with the "NW corner" and ending with the "SE corner."Object[]getMooreNeighbors(int x, int y, int xExtent, int yExtent) Gets the extended Moore neighbors of the specified coordinate.Object[]getVonNeumannNeighbors(int x, int y) Gets the von Neumann neighbors of the specified coordinate.Object[]getVonNeumannNeighbors(int x, int y, int xExtent, int yExtent) Gets the extended von Neumann neighbors of the specified coordinate.booleanmoveGridPosition(SpacePosition position, int destinationX, int destinationY) Move a IGridPosition object from its current position to the specified destination, only if the destination cell is empty.booleanremoveGridPosition(SpacePosition position) Remove the IGridPosition object from the grid.voidPut the given object at the given position.intsize()Return the number of objects stored into the grid.voidswapPositions(int x1, int y1, int x2, int y2) Swap the content of the (x1, y1) and (x2, y2) cells of the grid.Methods inherited from class microsim.space.AbstractSpace
boundX, boundY, equals, getMooreNeighborsPositions, getVonNeumannNeighborsPositions, getXSize, getYSize, reflectX, reflectY, torusX, torusY, toString
-
Field Details
-
m
-
objects
protected int objects
-
-
Constructor Details
-
DenseObjectSpace
Create a copy of the given grid.- Parameters:
grid- The source grid.
-
DenseObjectSpace
public DenseObjectSpace(int xSize, int ySize) Create a grid with given size.- Parameters:
xSize- The width of the grid.ySize- The height of the grid.
-
-
Method Details
-
get
Return the object stored at the given position.- Specified by:
getin interfaceObjectSpace- Specified by:
getin classAbstractSpace<Object>- Parameters:
x- The x coordinate.y- The y coordinate.- Returns:
- The requested object.
-
add
Add an object implementing IGridPosition interface to the grid. If object implements IGridPosition it stored in the right position of the grid.- Parameters:
o- The IGridPosition object to be added.- Returns:
- True if object was added. If o does not implement IGridPosition interface it will not be added and method will return false.
-
swapPositions
public void swapPositions(int x1, int y1, int x2, int y2) Swap the content of the (x1, y1) and (x2, y2) cells of the grid.- Specified by:
swapPositionsin classAbstractSpace<Object>- Parameters:
x1- The x coordinate for the first cell.y1- The y coordinate for the first cell.x2- The x coordinate for the second cell.y2- The y coordinate for the second cell.
-
set
Put the given object at the given position.- Specified by:
setin interfaceObjectSpace- Specified by:
setin classAbstractSpace<Object>- Parameters:
x- The x coordinate.y- The y coordinate.obj- The object to be stored at the (x, y) cell.
-
getMooreNeighbors
Gets the Moore neighbors of the specified coordinate. doubles are returned by row starting with the "NW corner" and ending with the "SE corner." The double at x, y is not returned.- Parameters:
x- the x coordinate of the objecty- the y coordinate of the object- Returns:
- an array of doubles ordered by row starting with the "NW corner" and ending with the "SE corner."
-
getMooreNeighbors
Gets the extended Moore neighbors of the specified coordinate. The extension in the x and y direction are specified by xExtent and yExtent. doubles are returned by row starting with the "NW corner" and ending with the "SE corner." The double at x,y is not returned.- Parameters:
x- the x coordinate of the objecty- the y coordinate of the objectxExtent- the extension of the neighborhood in the x directionyExtent- the extension of the neighborhood in the y direction- Returns:
- an array of doubles ordered by row starting with the "NW corner" and ending with the "SE corner."
-
getVonNeumannNeighbors
Gets the von Neumann neighbors of the specified coordinate. doubles are returned in west, east, north, south order. The double at x, y is not returned.- Parameters:
x- the x coordinate of the objecty- the y coordinate of the object- Returns:
- an array of doubles in west, east, north, south order
-
getVonNeumannNeighbors
Gets the extended von Neumann neighbors of the specified coordinate. The extension in the x and y direction are specified by xExtent and yExtent. doubles are returned in west, east, north, south order with the most distant object first. The double at x,y is not returned.- Parameters:
x- the x coordinate of the objecty- the y coordinate of the objectxExtent- the extension of the neighborhood in the x directionyExtent- the extension of the neighborhood in the y direction- Returns:
- an array of doubles in west, east, north, south order with the most distant object first.
-
moveGridPosition
Move a IGridPosition object from its current position to the specified destination, only if the destination cell is empty.- Specified by:
moveGridPositionin interfaceObjectSpace- Parameters:
destinationX- The x destination coordinate.destinationY- The y destination coordinate.- Returns:
- true if object has been moved, false otherwise.
-
getMatrix
Return the matrix of objects representing the grid.- Returns:
- A matrix of Object with the same dimensions of the grid.
-
size
public int size()Return the number of objects stored into the grid.- Specified by:
sizein classAbstractSpace<Object>- Returns:
- The sum of cells containing an object.
-
clear
public void clear()Set all cells to the null value.- Specified by:
clearin classAbstractSpace<Object>
-
addGridPosition
Add an object implementing IGridPosition interface to the grid. If the destination cell is already occupied the method return false and the object is not added.- Specified by:
addGridPositionin interfaceObjectSpace- Returns:
- True if object has been added. False if destination cell is already occupied or if argument object is null.
-
removeGridPosition
Remove the IGridPosition object from the grid.- Specified by:
removeGridPositionin interfaceObjectSpace- Returns:
- true if object has been removed. False if object is null or is not present on the grid.
-
countObjectsAt
public int countObjectsAt(int x, int y) Description copied from class:AbstractSpaceReturns the number of objects allocated in cell (x,y).- Specified by:
countObjectsAtin interfaceObjectSpace- Specified by:
countObjectsAtin classAbstractSpace<Object>- Parameters:
x- The x coordinate.y- The y coordinate.- Returns:
- the number of "entities" contained in the specified cell. See specific semantic in API of each extending class.
-