Package microsim.space
Class SparseObjectSpace
- All Implemented Interfaces:
ObjectSpace
A sparse bi-dimensional grid containing one object per cell.
-
Field Summary
FieldsModifier and TypeFieldDescriptionFields inherited from class microsim.space.AbstractSpace
modCount, xSize, ySize -
Constructor Summary
ConstructorsConstructorDescriptionSparseObjectSpace(int xSize, int ySize) Create a grid with given size.SparseObjectSpace(@NonNull AbstractSpace<Object> grid) Create a copy of the given grid. -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdd an object implementingSpacePositioninterface to the grid.booleanaddGridPosition(@Nullable SpacePosition object) Add an object implementingSpacePositioninterface to the grid.voidclear()Set all cells to the null value.booleanTest if given object is contained into the grid.intcountObjectsAt(int x, int y) Return the number of objects currently on the given position.booleanTest if o is the same of this.@Nullable Objectget(int x, int y) Return the object stored at the given position.@NonNull ObjectSpaceIterator<Object>Return aObjectSpaceIteratorstoring the position of the read value.iterator()booleanmoveGridPosition(@Nullable SpacePosition position, int destinationX, int destinationY) Move aSpacePositionobject from its current position to the specified destination, only if the destination cell is empty.booleanRemove the given object from the grid.booleanremoveGridPosition(@Nullable SpacePosition object) Remove theSpacePositionobject 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.@NonNull Object[]toArray()Return an array of objects stored into the grid.Methods inherited from class microsim.space.AbstractSpace
boundX, boundY, getMooreNeighborsPositions, getVonNeumannNeighborsPositions, reflectX, reflectY, torusX, torusY, toString
-
Field Details
-
m
-
-
Constructor Details
-
SparseObjectSpace
Create a copy of the given grid.- Parameters:
grid- The source grid.
-
SparseObjectSpace
public SparseObjectSpace(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- Thexcoordinate.y- Theycoordinate.- Returns:
- The requested object.
-
set
Put the given object at the given position.- Specified by:
setin interfaceObjectSpace- Specified by:
setin classAbstractSpace<Object>- Parameters:
x- Thexcoordinate.y- Theycoordinate.obj- The object to be stored at the(x, y)cell.
-
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- Thexcoordinate for the first cell.y1- Theycoordinate for the first cell.x2- Thexcoordinate for the second cell.y2- Theycoordinate for the second cell.
-
countObjectsAt
public int countObjectsAt(int x, int y) Return the number of objects currently on the given position.- Specified by:
countObjectsAtin interfaceObjectSpace- Specified by:
countObjectsAtin classAbstractSpace<Object>- Parameters:
x- Thexcoordinate.y- Theycoordinate.- Returns:
- the number of object in the cell
(x,y). Can be only 0 or 1.
-
moveGridPosition
public boolean moveGridPosition(@Nullable @Nullable SpacePosition position, int destinationX, int destinationY) Move aSpacePositionobject from its current position to the specified destination, only if the destination cell is empty.- Specified by:
moveGridPositionin interfaceObjectSpace- Parameters:
destinationX- Thexdestination coordinate.destinationY- Theydestination coordinate.position- An object implementingSpacePositioninterface.- Returns:
- true if object has been moved, false otherwise.
-
gridIterator
Return aObjectSpaceIteratorstoring the position of the read value.- Returns:
- An iterator that scrolls grid from the top-left corner to the bottom-right, reading each line from left to right. It remembers the position of the last read value.
-
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.
-
contains
Test if given object is contained into the grid.- Parameters:
o- The object to be tested.- Returns:
- True if object is present into the grid.
-
iterator
- Returns:
- An iterator that scrolls grid from the top-left corner to the bottom-right, reading each line from left to right.
-
toArray
Return an array of objects stored into the grid.- Returns:
- A vector containing only the objects stored into the grid. The empty cells are ignored.
-
add
Add an object implementingSpacePositioninterface to the grid. If object implementsSpacePositionit stored in the right position of the grid.- Parameters:
o- TheSpacePositionobject to be added.- Returns:
- True if object was added. If o does not implement
SpacePositioninterface it will not be added and method will return false.
-
remove
Remove the given object from the grid.- Parameters:
o- The object to be removed.- Returns:
- True if object was found and removed, false otherwise.
- Throws:
ClassCastException- If o does not implementSpacePositioninterface.
-
clear
public void clear()Set all cells to the null value.- Specified by:
clearin classAbstractSpace<Object>
-
equals
Test if o is the same of this. There is no content comparing.- Overrides:
equalsin classAbstractSpace<Object>- Parameters:
o- The object to be compared.- Returns:
- True o is this object.
-
addGridPosition
Add an object implementingSpacePositioninterface to the grid. If the destination cell is already occupied the method returns false and the object is not added.- Specified by:
addGridPositionin interfaceObjectSpace- Parameters:
object- TheSpacePositionobject to be added.- Returns:
- True if object has been added. False if destination cell is already occupied or if argument object is null.
-
removeGridPosition
Remove theSpacePositionobject from the grid.- Specified by:
removeGridPositionin interfaceObjectSpace- Parameters:
object- TheSpacePositionobject to be removed.- Returns:
- true if object has been removed. False if object is null or is not present on the grid.
-