public class SpatialMap<I,E>
extends java.lang.Object
implements java.lang.Iterable<E>
Modifier and Type | Class and Description |
---|---|
static class |
SpatialMap.SpatialTriple<I,E> |
Modifier and Type | Field and Description |
---|---|
protected OrderedMap<I,SpatialMap.SpatialTriple<I,E>> |
itemMapping |
protected OrderedMap<Coord,SpatialMap.SpatialTriple<I,E>> |
positionMapping |
Constructor and Description |
---|
SpatialMap()
Constructs a SpatialMap with capacity 32.
|
SpatialMap(java.util.Collection<Coord> coords,
java.util.Collection<I> ids,
java.util.Collection<E> elements)
Constructs a SpatialMap given collections of Coord, identity, and element; all 3 collections should have the same
length, since this will use only up to the minimum length of these collections for how many it adds.
|
SpatialMap(Coord[] coords,
I[] ids,
E[] elements)
Constructs a SpatialMap given arrays of Coord, identity, and element; all 3 arrays should have the same length,
since this will use only up to the minimum length of these arrays for how many it adds.
|
SpatialMap(int capacity)
Constructs a SpatialMap with the given capacity
|
Modifier and Type | Method and Description |
---|---|
void |
add(Coord coord,
I id,
E element)
Adds a new element with the given identity and Coord position.
|
java.util.Iterator<Coord> |
circlePositionIterator(int x,
int y,
int distance)
Iterates through positions in a circular region (starting at a minimum of x - distance, y - distance and
extending to x + distance, y + distance but skipping any positions where the Euclidean distance from x,y to the
position is more than distance) in left-to-right, then top-to-bottom order (the same as reading a page of text).
|
SpatialMap<I,E> |
circleSection(int x,
int y,
int radius)
Given the center position and maximum distance from the center, creates a new SpatialMap from this one that
refers only to the subsection of this SpatialMap shared with the area within the given distance from the center,
measured with Euclidean distance to produce a circle shape.
|
void |
clear() |
boolean |
containsKey(java.lang.Object o)
Checks whether this contains the given identity key.
|
boolean |
containsPosition(java.lang.Object o)
Checks whether this contains anything at the given position.
|
boolean |
containsValue(java.lang.Object o)
Checks whether this contains the given element.
|
boolean |
equals(java.lang.Object o) |
E |
get(Coord c)
Gets the element at the given Coord position.
|
E |
get(I i)
Gets the element with the given identity.
|
I |
getIdentity(Coord c)
Gets the identity of the element at the given Coord position.
|
java.util.ArrayList<E> |
getManyIdentities(I[] identities)
Given an array of I, gets all elements in this SpatialMap that share an identity with one of the I objects in
identities and returns them as an ArrayList of elements.
|
java.util.ArrayList<E> |
getManyIdentities(java.lang.Iterable<I> identities)
Given an Iterable (such as a List, Set, or other Collection) of I, gets all elements in this SpatialMap that
share an identity with one of the I objects in identities and returns them as an ArrayList of elements.
|
java.util.ArrayList<E> |
getManyPositions(Coord[] positions)
Given an array of Coord, gets all elements in this SpatialMap that share a position with one of the Coord objects
in positions and returns them as an ArrayList of elements.
|
java.util.ArrayList<E> |
getManyPositions(java.lang.Iterable<Coord> positions)
Given an Iterable (such as a List, Set, or other Collection) of Coord, gets all elements in this SpatialMap that
share a position with one of the Coord objects in positions and returns them as an ArrayList of elements.
|
Coord |
getPosition(I i)
Gets the position of the element with the given identity.
|
int |
hashCode() |
OrderedSet<I> |
identities()
Get a Set of all identities used for values in this data structure, returning a OrderedSet (defensively
copying the key set used internally) for its stable iteration order.
|
java.util.Iterator<I> |
identityIterator()
Iterates through identity keys in insertion order.
|
boolean |
isEmpty() |
java.util.Iterator<E> |
iterator()
Iterates through values in insertion order.
|
E |
modify(I id,
E newValue)
Changes the element's value associated with id.
|
E |
move(Coord previous,
Coord target)
Move an element from one position to another; moves whatever is at the Coord position previous to the new Coord
position target.
|
E |
move(I id,
Coord target)
Move an element, picked by its identity, to a new Coord position.
|
E |
positionalModify(Coord pos,
E newValue)
Changes the element's value associated with pos.
|
java.util.Iterator<Coord> |
positionIterator()
Iterates through positions in insertion order; has less predictable iteration order than the other iterators.
|
OrderedSet<Coord> |
positions()
Get a Set of all positions used for values in this data structure, returning a OrderedSet (defensively copying
the key set used internally) for its stable iteration order.
|
void |
put(Coord coord,
I id,
E element)
Inserts a new element with the given identity and Coord position, potentially overwriting an existing element.
|
void |
put(SpatialMap.SpatialTriple<I,E> triple)
Inserts a SpatialTriple into this SpatialMap without changing it, potentially overwriting an existing element.
|
java.util.Iterator<Coord> |
radiusPositionIterator(int x,
int y,
Radius measurement,
int distance)
Iterates through positions in a region defined by a Radius (starting at a minimum of x - distance, y - distance
and extending to x + distance, y + distance but skipping any positions where the Radius considers a position
further from x, y than distance) in left-to-right, then top-to-bottom order (the same as reading a page of text).
|
SpatialMap<I,E> |
radiusSection(int x,
int y,
Radius measurement,
int distance)
Given the center position, Radius to determine measurement, and maximum distance from the center, creates a new
SpatialMap from this one that refers only to the subsection of this SpatialMap shared with the area within the
given distance from the center as measured by measurement.
|
E |
randomElement(IRNG rng) |
SpatialMap.SpatialTriple<I,E> |
randomEntry(IRNG rng) |
I |
randomIdentity(IRNG rng) |
Coord |
randomPosition(IRNG rng) |
java.util.Iterator<Coord> |
rectanglePositionIterator(int x,
int y,
int width,
int height)
Iterates through positions in a rectangular region (starting at a minimum of x, y and extending to the specified
width and height) in left-to-right, then top-to-bottom order (the same as reading a page of text).
|
SpatialMap<I,E> |
rectangleSection(int x,
int y,
int width,
int height)
Given the size and position of a rectangular area, creates a new SpatialMap from this one that refers only to the
subsection of this SpatialMap shared with the rectangular area.
|
E |
remove(Coord coord)
Removes the element at the given position from all storage in this data structure.
|
E |
remove(I id)
Removes the element with the given identity from all storage in this data structure.
|
int |
size() |
java.lang.Object[] |
toArray() |
E[] |
toArray(E[] a)
Replaces the contents of the given array with the elements this holds, in insertion order, until either this
data structure or the array has been exhausted.
|
java.lang.String |
toString() |
java.util.Iterator<SpatialMap.SpatialTriple<I,E>> |
tripleIterator()
Iterates through values similar to Map.Entry, but containing a Coord, I, and E value for each entry, in insertion
order.
|
java.util.Collection<SpatialMap.SpatialTriple<I,E>> |
triples()
Gets all data stored in this as a collection of values similar to Map.Entry, but containing a Coord, I, and E
value for each entry, in insertion order.
|
protected OrderedMap<I,SpatialMap.SpatialTriple<I,E>> itemMapping
protected OrderedMap<Coord,SpatialMap.SpatialTriple<I,E>> positionMapping
public SpatialMap()
public SpatialMap(int capacity)
capacity
- the capacity for each of the internal OrderedMapspublic SpatialMap(Coord[] coords, I[] ids, E[] elements)
coords
- a starting array of Coord positions; indices here correspond to the other parametersids
- a starting array of identities; indices here correspond to the other parameterselements
- a starting array of elements; indices here correspond to the other parameterspublic SpatialMap(java.util.Collection<Coord> coords, java.util.Collection<I> ids, java.util.Collection<E> elements)
coords
- a starting collection of Coord positions; indices here correspond to the other parametersids
- a starting collection of identities; indices here correspond to the other parameterselements
- a starting collection of elements; indices here correspond to the other parameterspublic void add(Coord coord, I id, E element)
coord
- the Coord position to place the element at; should be emptyid
- the identity to associate the element with; should be unusedelement
- the element to addpublic void put(Coord coord, I id, E element)
coord
- the Coord position to place the element at; should be emptyid
- the identity to associate the element with; should be unusedelement
- the element to addpublic void put(SpatialMap.SpatialTriple<I,E> triple)
triple
- a SpatialTriple (an inner class of SpatialMap) with the same type parameters as this classpublic E modify(I id, E newValue)
id
- the identity of the element to modifynewValue
- the element value to replace the previous element with.public E positionalModify(Coord pos, E newValue)
pos
- the position of the element to modifynewValue
- the element value to replace the previous element with.public E move(Coord previous, Coord target)
previous
- the starting Coord position of an element to movetarget
- the Coord position to move the element topublic E move(I id, Coord target)
id
- the identity of the element to movetarget
- the Coord position to move the element topublic E remove(Coord coord)
coord
- the position of the element to removepublic E remove(I id)
id
- the identity of the element to removepublic boolean containsValue(java.lang.Object o)
o
- an Object that should be an element if you expect this to possibly return truepublic boolean containsKey(java.lang.Object o)
o
- an Object that should be of the generic I type if you expect this to possibly return truepublic boolean containsPosition(java.lang.Object o)
o
- an Object that should be a Coord if you expect this to possibly return truepublic E get(Coord c)
c
- the position to get an element frompublic E get(I i)
i
- the identity of the element to getpublic Coord getPosition(I i)
i
- the identity of the element to get a position frompublic I getIdentity(Coord c)
c
- the position to get an identity frompublic OrderedSet<Coord> positions()
public OrderedSet<I> identities()
public java.util.Collection<SpatialMap.SpatialTriple<I,E>> triples()
public java.util.ArrayList<E> getManyPositions(java.lang.Iterable<Coord> positions)
positions
- an Iterable (such as a List or Set) of Coordpublic java.util.ArrayList<E> getManyIdentities(java.lang.Iterable<I> identities)
identities
- an Iterable (such as a List or Set) of Ipublic java.util.ArrayList<E> getManyPositions(Coord[] positions)
positions
- an array of Coordpublic java.util.ArrayList<E> getManyIdentities(I[] identities)
identities
- an array of Ipublic SpatialMap.SpatialTriple<I,E> randomEntry(IRNG rng)
public SpatialMap<I,E> rectangleSection(int x, int y, int width, int height)
x
- the minimum x-coordinate of the rectangular areay
- the minimum y-coordinate of the rectangular areawidth
- the total width of the rectangular areaheight
- the total height of the rectangular areapublic SpatialMap<I,E> radiusSection(int x, int y, Radius measurement, int distance)
x
- the center x-coordinate of the areay
- the center y-coordinate of the areameasurement
- a Radius enum, such as Radius.CIRCLE or Radius.DIAMOND, that calculates distancedistance
- the maximum distance from the center to include in the areapublic SpatialMap<I,E> circleSection(int x, int y, int radius)
x
- the center x-coordinate of the areay
- the center y-coordinate of the arearadius
- the maximum distance from the center to include in the area, using Euclidean distancepublic void clear()
public boolean isEmpty()
public int size()
public java.lang.Object[] toArray()
public E[] toArray(E[] a)
a
- the array to replace; should usually have the same length as this data structure's size.public java.util.Iterator<E> iterator()
iterator
in interface java.lang.Iterable<E>
public java.util.Iterator<SpatialMap.SpatialTriple<I,E>> tripleIterator()
public java.util.Iterator<Coord> positionIterator()
public java.util.Iterator<I> identityIterator()
public java.util.Iterator<Coord> rectanglePositionIterator(int x, int y, int width, int height)
public java.util.Iterator<Coord> radiusPositionIterator(int x, int y, Radius measurement, int distance)
public java.util.Iterator<Coord> circlePositionIterator(int x, int y, int distance)
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
Copyright © Eben Howard 2012–2022. All rights reserved.