Package microsim.space
Class MultiObjectSpace
java.lang.Object
microsim.space.AbstractSpace<Object>
microsim.space.DenseObjectSpace
microsim.space.MultiObjectSpace
- All Implemented Interfaces:
ObjectSpace
A bi-dimensional grid capable of containing many objects into each cell.
-
Field Summary
Fields inherited from class microsim.space.DenseObjectSpace
m, objects
Fields inherited from class microsim.space.AbstractSpace
modCount, xSize, ySize
-
Constructor Summary
ConstructorDescriptionMultiObjectSpace
(int xSize, int ySize) Create a grid with given size and a default initial capacity.MultiObjectSpace
(int xSize, int ySize, int cellCapacity) Create a grid with given size and a given initial capacity.MultiObjectSpace
(@NonNull AbstractSpace<Object> grid) Create a copy of the given grid with a default initial capacity.MultiObjectSpace
(@NonNull AbstractSpace<Object> grid, int cellCapacity) Create a copy of the given grid with a given initial capacity. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Add an object implementingSpacePosition
interface to the grid.boolean
addGridPosition
(@Nullable SpacePosition position) Add an object implementingSpacePosition
interface to the grid.int
countObjectsAt
(int x, int y) Return the number of objects stored at the given position.get
(int x, int y, int z) Return the object stored at the given tri-dimensional position.boolean
moveGridPosition
(@Nullable SpacePosition position, int destinationX, int destinationY) Move aSpacePosition
object from its current position to the specified destination.boolean
Remove the given object from the grid.boolean
Remove the given object from the grid at given position.boolean
removeGridPosition
(@Nullable SpacePosition position) Remove theSpacePosition
object from the grid.void
Put the given object at the given tri-dimensional position.void
Enqueue the given object at the given position.Methods inherited from class microsim.space.DenseObjectSpace
clear, get, getMatrix, getMooreNeighbors, getMooreNeighbors, getVonNeumannNeighbors, getVonNeumannNeighbors, size, swapPositions
Methods inherited from class microsim.space.AbstractSpace
boundX, boundY, equals, getMooreNeighborsPositions, getVonNeumannNeighborsPositions, reflectX, reflectY, torusX, torusY, toString
-
Constructor Details
-
MultiObjectSpace
Create a copy of the given grid with a given initial capacity.- Parameters:
grid
- The source grid.cellCapacity
- The initial vector size allocated for each cell. It grows automatically.
-
MultiObjectSpace
public MultiObjectSpace(int xSize, int ySize, int cellCapacity) Create a grid with given size and a given initial capacity.- Parameters:
xSize
- The width of the grid.ySize
- The height of the grid.cellCapacity
- The initial vector size allocated for each cell. It grows automatically.
-
MultiObjectSpace
Create a copy of the given grid with a default initial capacity.- Parameters:
grid
- The source grid.
-
MultiObjectSpace
public MultiObjectSpace(int xSize, int ySize) Create a grid with given size and a default initial capacity.- Parameters:
xSize
- The width of the grid.ySize
- The height of the grid.
-
-
Method Details
-
countObjectsAt
public int countObjectsAt(int x, int y) Return the number of objects stored at the given position.- Specified by:
countObjectsAt
in interfaceObjectSpace
- Overrides:
countObjectsAt
in classDenseObjectSpace
- Parameters:
x
- Thex
coordinate.y
- They
coordinate.- Returns:
- The number of objects at
(x, y)
.
-
get
Return the object stored at the given tri-dimensional position.- Parameters:
x
- Thex
coordinate.y
- They
coordinate.z
- The index of the vector of object stored at the(x, y)
cell.- Returns:
- The requested object.
-
set
Enqueue the given object at the given position.- Specified by:
set
in interfaceObjectSpace
- Overrides:
set
in classDenseObjectSpace
- Parameters:
x
- Thex
coordinate.y
- They
coordinate.obj
- The object to be stored into the vector at the(x, y)
cell.
-
moveGridPosition
public boolean moveGridPosition(@Nullable @Nullable SpacePosition position, int destinationX, int destinationY) Move aSpacePosition
object from its current position to the specified destination.- Specified by:
moveGridPosition
in interfaceObjectSpace
- Overrides:
moveGridPosition
in classDenseObjectSpace
- Parameters:
destinationX
- Thex
destination coordinate.destinationY
- They
destination coordinate.position
- An object implementingSpacePosition
interface.- Returns:
- true. Only if the argument object is null the return value will be false.
-
set
Put the given object at the given tri-dimensional position.- Parameters:
x
- Thex
coordinate.y
- They
coordinate.z
- The index of the vector of object stored at the(x, y)
cell. Ifz
is out of vector bounds this will be automatically grown.obj
- The object to be stored into the vector at the(x, y)
cell.
-
add
Add an object implementingSpacePosition
interface to the grid. If object implementsSpacePosition
it stored in the right position of the grid.- Overrides:
add
in classDenseObjectSpace
- 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.
-
addGridPosition
Add an object implementingSpacePosition
interface to the grid. If the destination cell is already occupied the method return false and the object is not added.- Specified by:
addGridPosition
in interfaceObjectSpace
- Overrides:
addGridPosition
in classDenseObjectSpace
- Parameters:
position
- 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.
-
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.
-
removeGridPosition
Remove theSpacePosition
object from the grid.- Specified by:
removeGridPosition
in interfaceObjectSpace
- Overrides:
removeGridPosition
in classDenseObjectSpace
- Parameters:
position
- TheSpacePosition
object to be removed.- Returns:
- true if object has been removed. False if object is null or is not present on the grid.
-
removeAt
Remove the given object from the grid at given position. If the object position is known this method is better thanremove(Object)
because it works much faster.- Parameters:
x
- Thex
coordinate.y
- They
coordinate.o
- The object to be removed.- Returns:
- True if object was found and removed, false otherwise.
-