Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update MapItems to use multi-key collection. Fixes rendering performa…
…nce issues when trying to look up map items by map coordinate
- Loading branch information
1 parent
c29f700
commit 46bc241
Showing
13 changed files
with
180 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace EOLib.Domain.Map | ||
{ | ||
public class MapEntityCollectionHashSet<TValue> : IReadOnlyMapEntityCollection<TValue> | ||
{ | ||
private readonly Dictionary<int, int> _uniqueIdToHash; | ||
private readonly Dictionary<MapCoordinate, HashSet<int>> _mapCoordinateToHashList; | ||
|
||
private readonly Dictionary<int, TValue> _valueSet; | ||
|
||
private readonly Func<TValue, int> _uniqueIdSelector; | ||
private readonly Func<TValue, MapCoordinate> _mapCoordinateSelector; | ||
|
||
public MapEntityCollectionHashSet(Func<TValue, int> uniqueIdSelector, | ||
Func<TValue, MapCoordinate> mapCoordinateSelector) | ||
{ | ||
_uniqueIdToHash = new Dictionary<int, int>(); | ||
_mapCoordinateToHashList = new Dictionary<MapCoordinate, HashSet<int>>(); | ||
_valueSet = new Dictionary<int, TValue>(); | ||
|
||
_uniqueIdSelector = uniqueIdSelector; | ||
_mapCoordinateSelector = mapCoordinateSelector; | ||
} | ||
|
||
public MapEntityCollectionHashSet(Func<TValue, int> uniqueIdSelector, | ||
Func<TValue, MapCoordinate> mapCoordinateSelector, | ||
IEnumerable<TValue> values) | ||
: this(uniqueIdSelector, mapCoordinateSelector) | ||
{ | ||
foreach (var value in values) | ||
{ | ||
Add(value); | ||
} | ||
} | ||
|
||
public TValue this[int key1] => _valueSet[_uniqueIdToHash[key1]]; | ||
|
||
public HashSet<TValue> this[MapCoordinate key2] => new HashSet<TValue>(_mapCoordinateToHashList[key2].Select(x => _valueSet[x])); | ||
|
||
public void Add(TValue value) | ||
{ | ||
var key1 = _uniqueIdSelector.Invoke(value); | ||
|
||
var hash = value.GetHashCode(); | ||
_uniqueIdToHash[key1] = hash; | ||
|
||
var key2 = _mapCoordinateSelector.Invoke(value); | ||
if (!_mapCoordinateToHashList.ContainsKey(key2)) | ||
_mapCoordinateToHashList.Add(key2, new HashSet<int>()); | ||
|
||
_mapCoordinateToHashList[key2].Add(hash); | ||
_valueSet[hash] = value; | ||
} | ||
|
||
public IEnumerator<TValue> GetEnumerator() => _valueSet.Values.GetEnumerator(); | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
|
||
public void Remove(TValue value) | ||
{ | ||
var key1 = _uniqueIdSelector.Invoke(value); | ||
var key2 = _mapCoordinateSelector.Invoke(value); | ||
_uniqueIdToHash.Remove(key1); | ||
|
||
var hash = value.GetHashCode(); | ||
_mapCoordinateToHashList[key2].Remove(hash); | ||
if (_mapCoordinateToHashList[key2].Count == 0) | ||
_mapCoordinateToHashList.Remove(key2); | ||
|
||
_valueSet.Remove(hash); | ||
} | ||
|
||
public bool TryGetValue(int uniqueId, out TValue value) | ||
{ | ||
value = default; | ||
|
||
if (!_uniqueIdToHash.ContainsKey(uniqueId)) | ||
return false; | ||
|
||
var hash = _uniqueIdToHash[uniqueId]; | ||
if (!_valueSet.ContainsKey(hash)) | ||
return false; | ||
|
||
value = _valueSet[hash]; | ||
|
||
return true; | ||
} | ||
|
||
public bool TryGetValues(MapCoordinate mapCoordinate, out HashSet<TValue> values) | ||
{ | ||
values = default; | ||
|
||
if (!_mapCoordinateToHashList.ContainsKey(mapCoordinate)) | ||
return false; | ||
|
||
var hashes = _mapCoordinateToHashList[mapCoordinate]; | ||
if (!_valueSet.Any(x => hashes.Contains(x.Key))) | ||
return false; | ||
|
||
values = new HashSet<TValue>(_mapCoordinateToHashList[mapCoordinate].Select(x => _valueSet[x])); | ||
|
||
return true; | ||
} | ||
} | ||
|
||
public interface IReadOnlyMapEntityCollection<TValue> : IEnumerable<TValue> | ||
{ | ||
TValue this[int key1] { get; } | ||
HashSet<TValue> this[MapCoordinate key2] { get; } | ||
|
||
bool TryGetValue(int key1, out TValue value); | ||
bool TryGetValues(MapCoordinate key2, out HashSet<TValue> values); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.