Class AdvancedSpatialMap<T>
A more complex version of SpatialMap<T> that does not require the items in it to implement IHasID, instead requiring the specification of a custom System.Collections.Generic.IEqualityComparer<T> to use for hashing and comparison of items.
Implements
Inherited Members
Namespace: GoRogue
Assembly: GoRogue.dll
Syntax
public class AdvancedSpatialMap<T> : ISpatialMap<T>, IReadOnlySpatialMap<T>, IEnumerable<ISpatialTuple<T>>, IEnumerable
Type Parameters
Name | Description |
---|---|
T | The type of object that will be contained by this AdvancedSpatialMap. |
Remarks
This class is useful for cases where you do not want to implement IHasID, or if you need to use a value type in a spatial map. For simple cases, it is recommended to use SpatialMap<T> instead.
Be mindful of the efficiency of your hashing function specified in the System.Collections.Generic.IEqualityComparer<T> -- it will in large part determine the performance of AdvancedSpatialMap!
Constructors
| Improve this Doc View SourceAdvancedSpatialMap(IEqualityComparer<T>, Int32)
Constructor.
Declaration
public AdvancedSpatialMap(IEqualityComparer<T> comparer, int initialCapacity = 32)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEqualityComparer<T> | comparer | Equality comparer to use for comparison and hashing of type T. Be especially mindful of the efficiency of its GetHashCode function, as it will determine the efficiency of many AdvancedSpatialMap functions. |
System.Int32 | initialCapacity | The initial maximum number of elements the AdvancedSpatialMap can hold before it has to internally resize data structures. Defaults to 32. |
Properties
| Improve this Doc View SourceCount
See Count.
Declaration
public int Count { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
Items
See Items.
Declaration
public IEnumerable<T> Items { get; }
Property Value
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<T> |
Positions
See Positions.
Declaration
public IEnumerable<Coord> Positions { get; }
Property Value
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<Coord> |
Methods
| Improve this Doc View SourceAdd(T, Coord)
Adds the given item at the given position, provided the item is not already in the spatial map and the position is not already filled. If either of those are the case, returns false. Otherwise (if item was successfully added), returns true.
Declaration
public bool Add(T newItem, Coord position)
Parameters
Type | Name | Description |
---|---|---|
T | newItem | The item to add. |
Coord | position | The position at which to add the new item. |
Returns
Type | Description |
---|---|
System.Boolean | True if the item was added, false if adding the item failed. |
Add(T, Int32, Int32)
Adds the given item at the given position, provided the item is not already in the spatial map and the position is not already filled. If either of those are the case, returns false. Otherwise (if item was successfully added), returns true.
Declaration
public bool Add(T newItem, int x, int y)
Parameters
Type | Name | Description |
---|---|---|
T | newItem | The item to add. |
System.Int32 | x | X-value of the position to add item to. |
System.Int32 | y | Y-value of the position to add item to. |
Returns
Type | Description |
---|---|
System.Boolean | True if the item was added, false if adding the item failed. |
AsReadOnly()
See AsReadOnly().
Declaration
public IReadOnlySpatialMap<T> AsReadOnly()
Returns
Type | Description |
---|---|
IReadOnlySpatialMap<T> |
Clear()
See Clear().
Declaration
public void Clear()
Contains(T)
See Contains(T).
Declaration
public bool Contains(T item)
Parameters
Type | Name | Description |
---|---|---|
T | item |
Returns
Type | Description |
---|---|
System.Boolean |
Contains(Coord)
See Contains(Coord).
Declaration
public bool Contains(Coord position)
Parameters
Type | Name | Description |
---|---|---|
Coord | position |
Returns
Type | Description |
---|---|
System.Boolean |
Contains(Int32, Int32)
Declaration
public bool Contains(int x, int y)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | x | |
System.Int32 | y |
Returns
Type | Description |
---|---|
System.Boolean |
GetEnumerator()
Used by foreach loop, so that the class will give ISpatialTuple objects when used in a foreach loop. Generally should never be called explicitly.
Declaration
public IEnumerator<ISpatialTuple<T>> GetEnumerator()
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerator<ISpatialTuple<T>> | An enumerator for the spatial map |
GetItem(Coord)
Gets the item at the given position, or default(T) if no item exists.
Declaration
public T GetItem(Coord position)
Parameters
Type | Name | Description |
---|---|---|
Coord | position | The postiion to return the item for. |
Returns
Type | Description |
---|---|
T | The item at the given position, or default(T) if no item exists at that location. |
Remarks
Intended to be a more convenient function as compared to GetItems(Coord), since this spatial map implementation only allows a single item to at any given location at a time.
GetItem(Int32, Int32)
Gets the item at the given position, or default(T) if no item exists.
Declaration
public T GetItem(int x, int y)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | x | The x-value of the position to return the item for. |
System.Int32 | y | The y-value of the position to return the item for. |
Returns
Type | Description |
---|---|
T | The item at the given position, or default(T) if no item exists at that location. |
Remarks
Intended to be a more convenient function as compared to GetItems(Int32, Int32), since this spatial map implementation only allows a single item to at any given location at a time.
GetItems(Coord)
Gets the item at the given position as a 1-element enumerable if there is any item there, or nothing if there is nothing at that position.
Declaration
public IEnumerable<T> GetItems(Coord position)
Parameters
Type | Name | Description |
---|---|---|
Coord | position | The position to return the item for. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<T> | The item at the given position as a 1-element enumerable, if there is an item there, or nothing if there is no item there. |
Remarks
Since this implementation guarantees that only one item can be at any given location at once, the return value is guaranteed to be at most one element. You may find it more convenient to use the GetItems(Coord) function when you know you are dealing with a SpatialMap/AdvancedSpatialMap instance.
GetItems(Int32, Int32)
Gets the item at the given position as a 1-element enumerable if there is any item there, or nothing if there is nothing at that position.
Declaration
public IEnumerable<T> GetItems(int x, int y)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | x | The x-value of the position to return the item(s) for. |
System.Int32 | y | The y-value of the position to return the item(s) for. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<T> | The item at the given position as a 1-element enumerable, if there is an item there, or nothing if there is no item there. |
Remarks
Since this implementation guarantees that only one item can be at any given location at once, the return value is guaranteed to be at most one element. You may find it more convenient to use the GetItems(Int32, Int32) function when you know you are dealing with a SpatialMap/AdvancedSpatialMap instance.
GetPosition(T)
See GetPosition(T).
Declaration
public Coord GetPosition(T item)
Parameters
Type | Name | Description |
---|---|---|
T | item |
Returns
Type | Description |
---|---|
Coord |
Move(T, Coord)
Moves the item specified to the position specified. If the item does not exist in the spatial map, or the position is already filled by some other item, the function does nothing and returns false. Otherwise, returns true.
Declaration
public bool Move(T item, Coord target)
Parameters
Type | Name | Description |
---|---|---|
T | item | The item to move. |
Coord | target | The position to move it to. |
Returns
Type | Description |
---|---|
System.Boolean | True if the item was moved, false if the move operation failed. |
Move(T, Int32, Int32)
Moves the item specified to the position specified. If the item does not exist in the spatial map, or the position is already filled by some other item, the function does nothing and returns false. Otherwise, returns true.
Declaration
public bool Move(T item, int targetX, int targetY)
Parameters
Type | Name | Description |
---|---|---|
T | item | The item to move. |
System.Int32 | targetX | X-value of the location to move item to. |
System.Int32 | targetY | Y-value of the location to move item to. |
Returns
Type | Description |
---|---|
System.Boolean | True if the item was moved, false if not. |
Move(Coord, Coord)
Moves whatever is at position current, if anything, to the target position. If something was
moved, returns what was moved. If nothing was moved, eg. either there was nothing at
current
or already something at target
, returns nothing.
Declaration
public IEnumerable<T> Move(Coord current, Coord target)
Parameters
Type | Name | Description |
---|---|---|
Coord | current | The position of the item to move. |
Coord | target | The position to move the item to. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<T> | The item moved as a 1-element IEnumerable if something was moved, or nothing if no item was moved. |
Remarks
Since this implementation of ISpatialMap guarantees that only one item may be at any given location at a time, the returned values will either be none, or a single value.
Move(Int32, Int32, Int32, Int32)
Moves whatever is at the "current" position specified, if anything, to the "target" position. If something was moved, returns what was moved. If nothing was moved, eg. either there was nothing at the "current" position given, or already something at the "target" position given, returns nothing.
Declaration
public IEnumerable<T> Move(int currentX, int currentY, int targetX, int targetY)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | currentX | X-value of the location to move item from. |
System.Int32 | currentY | Y-value of the location to move item from. |
System.Int32 | targetX | X-value of the location to move item to. |
System.Int32 | targetY | Y-value of the location to move item to. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<T> | The item moved as a 1-element IEnumerable if something was moved, or nothing if no item was moved. |
Remarks
Since this implementation of ISpatialMap guarantees that only one item may be at any given location at a time, the returned values will either be none, or a single value.
Remove(T)
Removes the item specified, if it exists, and returns true. Returns false if the item was not in the spatial map.
Declaration
public bool Remove(T item)
Parameters
Type | Name | Description |
---|---|---|
T | item | The item to remove. |
Returns
Type | Description |
---|---|
System.Boolean | True if the item was removed, false if the item was not found. |
Remove(Coord)
Removes whatever is at the given position, if anything, and returns the item removed as a 1-element IEnumerable. Returns nothing if no item was at the position specified.
Declaration
public IEnumerable<T> Remove(Coord position)
Parameters
Type | Name | Description |
---|---|---|
Coord | position | The position of the item to remove. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<T> | The item removed as a 1-element IEnumerable, if something was removed; nothing if no item was found at that position. |
Remarks
Since this implementation of ISpatialMap guarantees that only one item can be at any given location at a time, the returned value is guaranteed to be either nothing or a single element.
Remove(Int32, Int32)
Removes whatever is at the given position, if anything, and returns the item removed as a 1-element IEnumerable. Returns nothing if no item was at the position specified.
Declaration
public IEnumerable<T> Remove(int x, int y)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | x | X-value of the position to remove item from. |
System.Int32 | y | Y-value of the position to remove item from. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<T> | The item removed as a 1-element IEnumerable, if something was removed; nothing if no item was found at that position. |
Remarks
Since this implementation guarantees that only one item can be at any given location at a time, the returned value is guaranteed to be either nothing or a single element.
ToString()
Returns a string representation of the spatial map.
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
System.String | A string representation of the spatial map. |
Overrides
ToString(Func<T, String>)
Returns a string representation of the spatial map, allowing display of the spatial map's items in a specified way.
Declaration
public string ToString(Func<T, string> itemStringifier)
Parameters
Type | Name | Description |
---|---|---|
System.Func<T, System.String> | itemStringifier | Function that turns an item into a string. |
Returns
Type | Description |
---|---|
System.String | A string representation of the spatial map. |
Events
| Improve this Doc View SourceItemAdded
See ItemAdded.
Declaration
public event EventHandler<ItemEventArgs<T>> ItemAdded
Event Type
Type | Description |
---|---|
System.EventHandler<ItemEventArgs<T>> |
ItemMoved
See ItemMoved.
Declaration
public event EventHandler<ItemMovedEventArgs<T>> ItemMoved
Event Type
Type | Description |
---|---|
System.EventHandler<ItemMovedEventArgs<T>> |
ItemRemoved
See ItemRemoved.
Declaration
public event EventHandler<ItemEventArgs<T>> ItemRemoved
Event Type
Type | Description |
---|---|
System.EventHandler<ItemEventArgs<T>> |
Explicit Interface Implementations
| Improve this Doc View SourceIEnumerable.GetEnumerator()
Generic iterator used internally by foreach loops.
Declaration
IEnumerator IEnumerable.GetEnumerator()
Returns
Type | Description |
---|---|
System.Collections.IEnumerator | Enumerator to ISpatialTuple instances. |