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.
The object-access method names for SparseGrid3D are intentionally different than the get/set and g/s standard set by ObjectGrid3D, IntGrid3D, and DoubleGrid3D. This is to remind you that you're accessing Bags of objects and not objects directly.
- 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)
|
Bag |
getObjectsAtLocations(IntBag xPos,
IntBag yPos,
IntBag zPos,
Bag result)
For each location, puts all such objects into the result bag. |
int |
getWidth()
|
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 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 final int getWidth()
getHeight
public final int getHeight()
getLength
public final int getLength()
tx
public final int tx(int x)
ty
public final int ty(int y)
tz
public final int tz(int z)
stx
public final int stx(int x)
sty
public final int sty(int y)
stz
public final int stz(int z)
getObjectsAtLocation
public final Bag getObjectsAtLocation(int x,
int y,
int z)
getObjectLocationAsDouble3D
public final Double3D getObjectLocationAsDouble3D(java.lang.Object obj)
getObjectLocation
public Int3D getObjectLocation(java.lang.Object obj)
removeObjectsAtLocation
public final Bag removeObjectsAtLocation(int x,
int y,
int z)
removeObjectsAtLocation
public Bag removeObjectsAtLocation(Int3D location)
setObjectLocation
public final boolean setObjectLocation(java.lang.Object obj,
int x,
int y,
int z)
setObjectLocation
public boolean setObjectLocation(java.lang.Object obj,
Int3D location)
getNeighborsMaxDistance
public final void getNeighborsMaxDistance(int x,
int y,
int z,
int dist,
boolean toroidal,
IntBag xPos,
IntBag yPos,
IntBag zPos)
getNeighborsHamiltonianDistance
public final void getNeighborsHamiltonianDistance(int x,
int y,
int z,
int dist,
boolean toroidal,
IntBag xPos,
IntBag yPos,
IntBag zPos)
getNeighborsMaxDistance
public final void getNeighborsMaxDistance(int x,
int y,
int z,
int dist,
boolean toroidal,
Bag result,
IntBag xPos,
IntBag yPos,
IntBag zPos)
getNeighborsHamiltonianDistance
public final 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.