Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## 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.
- SparseGrid2D can associate objects with any 3D integer location. ObjectGrid3D's locations are restricted to be within its array.

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

Nested Class Summary

Nested classes inherited from class sim.field.SparseField
`SparseField.LocationAndIndex`

Field Summary
`protected int` | `height`
`protected int` | `length`
`protected int` | `width`

Fields inherited from class sim.field.SparseField
`allObjects, LARGE_BAG_RATIO, locationAndIndexHash, MIN_BAG_SIZE, objectHash, removeEmptyBags, replaceLargeBags`

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`

Field Detail

### width

    protected int width

### height

    protected int height

### length

    protected int length

Constructor Detail

### SparseGrid3D

    public SparseGrid3D(int width,
                        int height,
                        int length)

Method Detail

### getWidth

    public int getWidth()

### getHeight

    public int getHeight()

### getLength

    public 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 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.

Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD