Class SpatialMap<V extends IGridIdentified>

java.lang.Object
java.util.AbstractCollection<V>
com.github.yellowstonegames.grid.SpatialMap<V>
Type Parameters:
V - the type of all values; this must implement IGridIdentified
All Implemented Interfaces:
com.github.tommyettinger.ds.Arrangeable, Iterable<V>, Collection<V>

@Beta public class SpatialMap<V extends IGridIdentified> extends AbstractCollection<V> implements com.github.tommyettinger.ds.Arrangeable
A kind of Map-like data structure that allows lookup by int id or Coord position, and retains its insertion order. This requires its V values to implement IGridIdentified, which makes this able to associate each V with its own position and id. The most commonly-used non-standard method here is probably move(Coord, Coord), which gets the value at a position and attempts to move it to another position, updating the value's internal position in the process. There are variants on most methods for id (getById(int)), position (getByPosition(Coord)), iteration order index (getAt(int)), and often the value itself (contains(Object)).
  • Field Details

  • Constructor Details

    • SpatialMap

      public SpatialMap()
    • SpatialMap

      public SpatialMap(int capacity)
    • SpatialMap

      public SpatialMap(int capacity, float loadFactor)
    • SpatialMap

      public SpatialMap(SpatialMap<? extends V> other)
  • Method Details

    • add

      public boolean add(V value)
      Specified by:
      add in interface Collection<V extends IGridIdentified>
      Overrides:
      add in class AbstractCollection<V extends IGridIdentified>
    • values

      public Collection<V> values()
    • positions

      public Set<Coord> positions()
    • ids

      public com.github.tommyettinger.ds.PrimitiveCollection.OfInt ids()
    • iterator

      public Iterator<V> iterator()
      Specified by:
      iterator in interface Collection<V extends IGridIdentified>
      Specified by:
      iterator in interface Iterable<V extends IGridIdentified>
      Specified by:
      iterator in class AbstractCollection<V extends IGridIdentified>
    • positionIterator

      public Iterator<Coord> positionIterator()
    • idIterator

      public com.github.tommyettinger.ds.support.util.IntIterator idIterator()
    • size

      public int size()
      Specified by:
      size in interface com.github.tommyettinger.ds.Arrangeable
      Specified by:
      size in interface Collection<V extends IGridIdentified>
      Specified by:
      size in class AbstractCollection<V extends IGridIdentified>
    • contains

      public boolean contains(Object o)
      Specified by:
      contains in interface Collection<V extends IGridIdentified>
      Overrides:
      contains in class AbstractCollection<V extends IGridIdentified>
    • containsPosition

      public boolean containsPosition(Coord position)
    • containsId

      public boolean containsId(int id)
    • remove

      public boolean remove(Object o)
      Specified by:
      remove in interface Collection<V extends IGridIdentified>
      Overrides:
      remove in class AbstractCollection<V extends IGridIdentified>
    • removePosition

      public boolean removePosition(Coord pos)
    • removeId

      public boolean removeId(int id)
    • removeAt

      public boolean removeAt(int index)
    • getAt

      public V getAt(int index)
    • getById

      public V getById(int id)
    • getByPosition

      public V getByPosition(Coord position)
    • getByPosition

      public V getByPosition(int x, int y)
    • move

      public V move(Coord oldPosition, Coord newPosition)
      Attempts to move the V located at oldPosition to newPosition without changing its position in the iteration order. If this succeeds, it returns the moved V and sets the internal position in that V using IGridPositioned.setCoordPosition(Coord). This can fail if there is already a V at newPosition, in which case this returns that V without changing anything. It can also fail if there isn't a V at oldPosition, in which case this returns null.
      Parameters:
      oldPosition - the Coord to look up for the V to move
      newPosition - the Coord to try to move the V into
      Returns:
      on success, the moved V; on failure because newPosition is occupied, that occupant; on failure because there wasn't a V present to move, null
    • move

      public V move(int id, Coord newPosition)
      Attempts to move the V with the given id to newPosition without changing its position in the iteration order. If this succeeds, it returns the moved V and sets the internal position in that V using IGridPositioned.setCoordPosition(Coord). This can fail if there is already a V at newPosition, in which case this returns that V without changing anything. It can also fail if there isn't a V with id, in which case this returns null.
      Parameters:
      id - the int ID to look up for the V to move
      newPosition - the Coord to try to move the V into
      Returns:
      on success, the moved V; on failure because newPosition is occupied, that occupant; on failure because there wasn't a V present to move, null
    • moveAt

      public V moveAt(int index, Coord newPosition)
      Attempts to move the V at the given index in the iteration order to newPosition without changing its position in the iteration order. If this succeeds, it returns the moved V and sets the internal position in that V using IGridPositioned.setCoordPosition(Coord). This can fail if there is already a V at newPosition, in which case this returns that V without changing anything. It can also fail if index is negative or >= the size of this SpatialMap, in which case this returns null.
      Parameters:
      index - the int index in the iteration order for the V to move
      newPosition - the Coord to try to move the V into
      Returns:
      on success, the moved V; on failure because newPosition is occupied, that occupant; on failure because there the index was out of bounds, null
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<V extends IGridIdentified>
      Overrides:
      clear in class AbstractCollection<V extends IGridIdentified>
    • swap

      public void swap(int first, int second)
      Specified by:
      swap in interface com.github.tommyettinger.ds.Arrangeable
    • reverse

      public void reverse()
      Specified by:
      reverse in interface com.github.tommyettinger.ds.Arrangeable
    • toString

      public String toString()
      Overrides:
      toString in class AbstractCollection<V extends IGridIdentified>
    • equals

      public boolean equals(Object o)
      Specified by:
      equals in interface Collection<V extends IGridIdentified>
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Collection<V extends IGridIdentified>
      Overrides:
      hashCode in class Object