sim.field.grid
Class SparseGrid3D
java.lang.Object
sim.field.SparseField
sim.field.grid.SparseGrid3D
- All Implemented Interfaces:
- java.io.Serializable
- public class SparseGrid3D
- extends SparseField
A storage facility for sparse objects in discrete 3D space, using HashMaps. SparseGrid3D differs from ObjectGrid3D
in several respects:
- SparseGrid3D can store more than one object at a location. ObjectGrid3D cannot.
- ObjectGrid3D can store an object at more than one location (though it's bad form!).
- SparseGrid3D can efficiently (O(1)) tell you the location of an object.
- SparseGrid3D can efficiently (O(#objs)) scan through all objects. The best you can do with ObjectGrid3D is search its array (which might have many empty slots).
- Storing an object, finding its location, or changing its location, in a SparseGrid3D is O(1) but requires several HashMap lookups and/or removes, which has a significant constant overhead.
Generally speaking, if you have a grid of objects, one per location, you should use an ObjectGrid3D. If you have a large grid occupied by a few objects, or those objects can pile up on the same grid location, you should use a SparseGrid3D.
In either case, you might consider storing the location of an object IN THE OBJECT ITSELF if you need to query for the object location often -- it's faster than the hashtable lookup in SparseGrid3D, and certainly faster than searching the entire array of an ObjectGrid3D.
- See Also:
- Serialized Form
Constructor Summary |
SparseGrid3D(int width,
int height,
int length)
|
Method Summary |
int |
getHeight()
|
int |
getLength()
|
void |
getNeighborsHamiltonianDistance(int x,
int y,
int z,
int dist,
boolean toroidal,
Bag result,
IntBag xPos,
IntBag yPos,
IntBag zPos)
|
void |
getNeighborsHamiltonianDistance(int x,
int y,
int z,
int dist,
boolean toroidal,
IntBag xPos,
IntBag yPos,
IntBag zPos)
|
void |
getNeighborsMaxDistance(int x,
int y,
int z,
int dist,
boolean toroidal,
Bag result,
IntBag xPos,
IntBag yPos,
IntBag zPos)
|
void |
getNeighborsMaxDistance(int x,
int y,
int z,
int dist,
boolean toroidal,
IntBag xPos,
IntBag yPos,
IntBag zPos)
|
Int3D |
getObjectLocation(java.lang.Object obj)
|
Double3D |
getObjectLocationAsDouble3D(java.lang.Object obj)
|
Bag |
getObjectsAtLocation(int x,
int y,
int z)
Returns a bag containing all the objects at a given location, or possibly null (either that or an empty Bag,
who can tell?) when there are no objects at the location. |
Bag |
getObjectsAtLocations(IntBag xPos,
IntBag yPos,
IntBag zPos,
Bag result)
For each location, puts all such objects into the result bag. |
int |
getWidth()
|
int |
numObjectsAtLocation(int x,
int y,
int z)
|
Bag |
removeObjectsAtLocation(Int3D location)
|
Bag |
removeObjectsAtLocation(int x,
int y,
int z)
|
boolean |
setObjectLocation(java.lang.Object obj,
Int3D location)
|
boolean |
setObjectLocation(java.lang.Object obj,
int x,
int y,
int z)
|
int |
stx(int x)
|
int |
sty(int y)
|
int |
stz(int z)
|
int |
tx(int x)
|
int |
ty(int y)
|
int |
tz(int z)
|
Methods inherited from class sim.field.SparseField |
clear, exists, getAllObjects, getObjectIndex, getObjectsAtLocation, getObjectsAtLocations, getRawObjectLocation, iterator, locationBagIterator, numObjectsAtLocation, remove, removeObjectsAtLocation, setObjectLocation |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
width
protected int width
height
protected int height
length
protected int length
SparseGrid3D
public SparseGrid3D(int width,
int height,
int length)
getWidth
public int getWidth()
getHeight
public int getHeight()
getLength
public int getLength()
tx
public int tx(int x)
ty
public int ty(int y)
tz
public int tz(int z)
stx
public int stx(int x)
sty
public int sty(int y)
stz
public int stz(int z)
numObjectsAtLocation
public int numObjectsAtLocation(int x,
int y,
int z)
getObjectsAtLocation
public Bag getObjectsAtLocation(int x,
int y,
int z)
- Returns a bag containing all the objects at a given location, or possibly null (either that or an empty Bag,
who can tell?) when there are no objects at the location.
You should NOT MODIFY THIS BAG. This is the actual container bag, and modifying it will almost certainly break
the Sparse Field object. If you want to modify the bag, make a copy and modify the copy instead,
using something along the lines of new Bag(foo.getObjectsAtLocation(location)) .
Furthermore, changing values in the Sparse Field may result in a different bag being used -- so you should
not rely on this bag staying valid.
getObjectLocationAsDouble3D
public Double3D getObjectLocationAsDouble3D(java.lang.Object obj)
getObjectLocation
public Int3D getObjectLocation(java.lang.Object obj)
removeObjectsAtLocation
public Bag removeObjectsAtLocation(int x,
int y,
int z)
removeObjectsAtLocation
public Bag removeObjectsAtLocation(Int3D location)
setObjectLocation
public boolean setObjectLocation(java.lang.Object obj,
int x,
int y,
int z)
setObjectLocation
public boolean setObjectLocation(java.lang.Object obj,
Int3D location)
getNeighborsMaxDistance
public void getNeighborsMaxDistance(int x,
int y,
int z,
int dist,
boolean toroidal,
IntBag xPos,
IntBag yPos,
IntBag zPos)
getNeighborsHamiltonianDistance
public void getNeighborsHamiltonianDistance(int x,
int y,
int z,
int dist,
boolean toroidal,
IntBag xPos,
IntBag yPos,
IntBag zPos)
getNeighborsMaxDistance
public void getNeighborsMaxDistance(int x,
int y,
int z,
int dist,
boolean toroidal,
Bag result,
IntBag xPos,
IntBag yPos,
IntBag zPos)
getNeighborsHamiltonianDistance
public void getNeighborsHamiltonianDistance(int x,
int y,
int z,
int dist,
boolean toroidal,
Bag result,
IntBag xPos,
IntBag yPos,
IntBag zPos)
getObjectsAtLocations
public Bag getObjectsAtLocations(IntBag xPos,
IntBag yPos,
IntBag zPos,
Bag result)
- For each location, puts all such objects into the result bag. Returns the result bag.
If the provided result bag is null, one will be created and returned.