Package microsim.space
Class SparseObjectSpace
- All Implemented Interfaces:
ObjectSpace
A sparse bi-dimensional grid containing one object per cell.
-
Field Summary
Modifier and TypeFieldDescriptionFields inherited from class microsim.space.AbstractSpace
modCount, xSize, ySize
-
Constructor Summary
ConstructorDescriptionSparseObjectSpace
(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 TypeMethodDescriptionboolean
Add an object implementingSpacePosition
interface to the grid.boolean
addGridPosition
(@Nullable SpacePosition object) Add an object implementingSpacePosition
interface to the grid.void
clear()
Set all cells to the null value.boolean
Test if given object is contained into the grid.int
countObjectsAt
(int x, int y) Return the number of objects currently on the given position.boolean
Test if o is the same of this.@Nullable Object
get
(int x, int y) Return the object stored at the given position.@NonNull ObjectSpaceIterator<Object>
Return aObjectSpaceIterator
storing the position of the read value.iterator()
boolean
moveGridPosition
(@Nullable SpacePosition position, int destinationX, int destinationY) Move aSpacePosition
object from its current position to the specified destination, only if the destination cell is empty.boolean
Remove the given object from the grid.boolean
removeGridPosition
(@Nullable SpacePosition object) Remove theSpacePosition
object from the grid.void
Put the given object at the given position.int
size()
Return the number of objects stored into the grid.void
swapPositions
(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:
get
in interfaceObjectSpace
- Specified by:
get
in classAbstractSpace<Object>
- Parameters:
x
- Thex
coordinate.y
- They
coordinate.- Returns:
- The requested object.
-
set
Put the given object at the given position.- Specified by:
set
in interfaceObjectSpace
- Specified by:
set
in classAbstractSpace<Object>
- Parameters:
x
- Thex
coordinate.y
- They
coordinate.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:
swapPositions
in classAbstractSpace<Object>
- Parameters:
x1
- Thex
coordinate for the first cell.y1
- They
coordinate for the first cell.x2
- Thex
coordinate for the second cell.y2
- They
coordinate for the second cell.
-
countObjectsAt
public int countObjectsAt(int x, int y) Return the number of objects currently on the given position.- Specified by:
countObjectsAt
in interfaceObjectSpace
- Specified by:
countObjectsAt
in classAbstractSpace<Object>
- Parameters:
x
- Thex
coordinate.y
- They
coordinate.- 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 aSpacePosition
object from its current position to the specified destination, only if the destination cell is empty.- Specified by:
moveGridPosition
in interfaceObjectSpace
- Parameters:
destinationX
- Thex
destination coordinate.destinationY
- They
destination coordinate.position
- An object implementingSpacePosition
interface.- Returns:
- true if object has been moved, false otherwise.
-
gridIterator
Return aObjectSpaceIterator
storing 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:
size
in 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 implementingSpacePosition
interface to the grid. If object implementsSpacePosition
it stored in the right position of the grid.- Parameters:
o
- TheSpacePosition
object to be added.- Returns:
- True if object was added. If o does not implement
SpacePosition
interface 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 implementSpacePosition
interface.
-
clear
public void clear()Set all cells to the null value.- Specified by:
clear
in classAbstractSpace<Object>
-
equals
Test if o is the same of this. There is no content comparing.- Overrides:
equals
in classAbstractSpace<Object>
- Parameters:
o
- The object to be compared.- Returns:
- True o is this object.
-
addGridPosition
Add an object implementingSpacePosition
interface to the grid. If the destination cell is already occupied the method returns false and the object is not added.- Specified by:
addGridPosition
in interfaceObjectSpace
- Parameters:
object
- TheSpacePosition
object 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 theSpacePosition
object from the grid.- Specified by:
removeGridPosition
in interfaceObjectSpace
- Parameters:
object
- TheSpacePosition
object to be removed.- Returns:
- true if object has been removed. False if object is null or is not present on the grid.
-