forked from Jarno458/TsRandomizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
153 additions
and
698 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace TsRandomizer | ||
{ | ||
class LookupDictionary<TLookup, TValue> : IEnumerable<TValue> | ||
{ | ||
readonly Func<TValue, TLookup> keySelector; | ||
readonly Dictionary<TLookup, TValue> lookupTable; | ||
|
||
public LookupDictionary(Func<TValue, TLookup> keySelector) : this(0, keySelector) | ||
{ | ||
} | ||
|
||
public LookupDictionary(int capacity, Func<TValue, TLookup> keySelector) | ||
{ | ||
lookupTable = new Dictionary<TLookup, TValue>(capacity); | ||
this.keySelector = keySelector; | ||
} | ||
|
||
public TValue this[TLookup key] => lookupTable[key]; | ||
|
||
public int Count => lookupTable.Count; | ||
|
||
public bool TryGetValue(TLookup key, out TValue value) => | ||
lookupTable.TryGetValue(key, out value); | ||
|
||
public void Filter(IEnumerable<TLookup> intersectionFilter, Action<TValue> removeAction = null) | ||
{ | ||
var keysToRemove = lookupTable.Keys | ||
.Where(e => !intersectionFilter.Contains(e)) | ||
.ToList(); | ||
|
||
foreach (var key in keysToRemove) | ||
{ | ||
removeAction?.Invoke(lookupTable[key]); | ||
lookupTable.Remove(key); | ||
} | ||
} | ||
|
||
public IEnumerator<TValue> GetEnumerator() => | ||
((IEnumerable<TValue>)lookupTable.Values).GetEnumerator(); | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => | ||
lookupTable.Values.GetEnumerator(); | ||
|
||
public void Add(TValue value) => | ||
lookupTable.Add(keySelector(value), value); | ||
|
||
public void AddOrUpdate(TValue value) => | ||
lookupTable[keySelector(value)] = value; | ||
|
||
public bool Contains(TLookup key) => | ||
lookupTable.ContainsKey(key); | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.