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 implementIGridIdentified
- 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)).-
Nested Class Summary
Nested classes/interfaces inherited from interface com.github.tommyettinger.ds.Arrangeable
com.github.tommyettinger.ds.Arrangeable.ArrangeableList<T> -
Field Summary
FieldsModifier and TypeFieldDescriptionfinal com.github.tommyettinger.ds.IntObjectOrderedMap<V> final CoordObjectOrderedMap<V> -
Constructor Summary
ConstructorsConstructorDescriptionSpatialMap(int capacity) SpatialMap(int capacity, float loadFactor) SpatialMap(SpatialMap<? extends V> other) -
Method Summary
Modifier and TypeMethodDescriptionbooleanvoidclear()booleanbooleancontainsId(int id) booleancontainsPosition(Coord position) booleangetAt(int index) getById(int id) getByPosition(int x, int y) getByPosition(Coord position) inthashCode()com.github.tommyettinger.ds.support.util.IntIteratorcom.github.tommyettinger.ds.PrimitiveCollection.OfIntids()iterator()Attempts to move the V with the givenidtonewPositionwithout changing its position in the iteration order.Attempts to move the V located atoldPositiontonewPositionwithout changing its position in the iteration order.Attempts to move the V at the givenindexin the iteration order tonewPositionwithout changing its position in the iteration order.booleanbooleanremoveAt(int index) booleanremoveId(int id) booleanremovePosition(Coord pos) voidreverse()intsize()voidswap(int first, int second) toString()values()Methods inherited from class AbstractCollection
addAll, containsAll, isEmpty, removeAll, retainAll, toArray, toArrayMethods inherited from interface com.github.tommyettinger.ds.Arrangeable
rearrange, shuffle, shuffleMethods inherited from interface Collection
parallelStream, removeIf, spliterator, stream, toArray
-
Field Details
-
positionMap
-
idMap
-
-
Constructor Details
-
SpatialMap
public SpatialMap() -
SpatialMap
public SpatialMap(int capacity) -
SpatialMap
public SpatialMap(int capacity, float loadFactor) -
SpatialMap
-
-
Method Details
-
add
- Specified by:
addin interfaceCollection<V extends IGridIdentified>- Overrides:
addin classAbstractCollection<V extends IGridIdentified>
-
values
-
positions
-
ids
public com.github.tommyettinger.ds.PrimitiveCollection.OfInt ids() -
iterator
- Specified by:
iteratorin interfaceCollection<V extends IGridIdentified>- Specified by:
iteratorin interfaceIterable<V extends IGridIdentified>- Specified by:
iteratorin classAbstractCollection<V extends IGridIdentified>
-
positionIterator
-
idIterator
public com.github.tommyettinger.ds.support.util.IntIterator idIterator() -
size
public int size()- Specified by:
sizein interfacecom.github.tommyettinger.ds.Arrangeable- Specified by:
sizein interfaceCollection<V extends IGridIdentified>- Specified by:
sizein classAbstractCollection<V extends IGridIdentified>
-
contains
- Specified by:
containsin interfaceCollection<V extends IGridIdentified>- Overrides:
containsin classAbstractCollection<V extends IGridIdentified>
-
containsPosition
-
containsId
public boolean containsId(int id) -
remove
- Specified by:
removein interfaceCollection<V extends IGridIdentified>- Overrides:
removein classAbstractCollection<V extends IGridIdentified>
-
removePosition
-
removeId
public boolean removeId(int id) -
removeAt
public boolean removeAt(int index) -
getAt
-
getById
-
getByPosition
-
getByPosition
-
move
Attempts to move the V located atoldPositiontonewPositionwithout changing its position in the iteration order. If this succeeds, it returns the moved V and sets the internal position in that V usingIGridPositioned.setCoordPosition(Coord). This can fail if there is already a V atnewPosition, in which case this returns that V without changing anything. It can also fail if there isn't a V atoldPosition, in which case this returns null.- Parameters:
oldPosition- the Coord to look up for the V to movenewPosition- 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
Attempts to move the V with the givenidtonewPositionwithout changing its position in the iteration order. If this succeeds, it returns the moved V and sets the internal position in that V usingIGridPositioned.setCoordPosition(Coord). This can fail if there is already a V atnewPosition, in which case this returns that V without changing anything. It can also fail if there isn't a V withid, in which case this returns null.- Parameters:
id- the int ID to look up for the V to movenewPosition- 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
Attempts to move the V at the givenindexin the iteration order tonewPositionwithout changing its position in the iteration order. If this succeeds, it returns the moved V and sets the internal position in that V usingIGridPositioned.setCoordPosition(Coord). This can fail if there is already a V atnewPosition, 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 movenewPosition- 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:
clearin interfaceCollection<V extends IGridIdentified>- Overrides:
clearin classAbstractCollection<V extends IGridIdentified>
-
swap
public void swap(int first, int second) - Specified by:
swapin interfacecom.github.tommyettinger.ds.Arrangeable
-
reverse
public void reverse()- Specified by:
reversein interfacecom.github.tommyettinger.ds.Arrangeable
-
toString
- Overrides:
toStringin classAbstractCollection<V extends IGridIdentified>
-
equals
- Specified by:
equalsin interfaceCollection<V extends IGridIdentified>- Overrides:
equalsin classObject
-
hashCode
public int hashCode()- Specified by:
hashCodein interfaceCollection<V extends IGridIdentified>- Overrides:
hashCodein classObject
-