Grid2D: use ISet<T> instead of HashSet<T> to support SortedSet<T>

This commit is contained in:
vis2k 2022-04-28 19:06:11 +08:00
parent da367c7bb9
commit 8d98a42ab6

View File

@ -50,7 +50,7 @@ public void Add(Vector2Int position, T value)
// -> result is passed as parameter to avoid allocations // -> result is passed as parameter to avoid allocations
// -> result is not cleared before. this allows us to pass the HashSet from // -> result is not cleared before. this allows us to pass the HashSet from
// GetWithNeighbours and avoid .UnionWith which is very expensive. // GetWithNeighbours and avoid .UnionWith which is very expensive.
void GetAt(Vector2Int position, HashSet<T> result) void GetAt(Vector2Int position, ISet<T> result)
{ {
// return the set at position // return the set at position
if (grid.TryGetValue(position, out HashSet<T> hashSet)) if (grid.TryGetValue(position, out HashSet<T> hashSet))
@ -62,7 +62,8 @@ void GetAt(Vector2Int position, HashSet<T> result)
// helper function to get at position and it's 8 neighbors without worrying // helper function to get at position and it's 8 neighbors without worrying
// -> result is passed as parameter to avoid allocations // -> result is passed as parameter to avoid allocations
public void GetWithNeighbours(Vector2Int position, HashSet<T> result) // -> ISet so it works with both HashSet and SortedSet.
public void GetWithNeighbours(Vector2Int position, ISet<T> result)
{ {
// clear result first // clear result first
result.Clear(); result.Clear();