|
|
|
@ -1,5 +1,6 @@
@@ -1,5 +1,6 @@
|
|
|
|
|
using System; |
|
|
|
|
using System.Collections; |
|
|
|
|
using System.Collections.Concurrent; |
|
|
|
|
using System.Collections.Generic; |
|
|
|
|
|
|
|
|
|
namespace Stackoverflow.Utilities |
|
|
|
@ -26,9 +27,9 @@ namespace Stackoverflow.Utilities
@@ -26,9 +27,9 @@ namespace Stackoverflow.Utilities
|
|
|
|
|
public struct Enumerator : IEnumerator<Pair>, IEnumerator |
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
public Enumerator(Dictionary<TFirst, TSecond>.Enumerator dictEnumerator) |
|
|
|
|
public Enumerator(IEnumerable<KeyValuePair<TFirst, TSecond>> dictEnumerator) |
|
|
|
|
{ |
|
|
|
|
_dictEnumerator = dictEnumerator; |
|
|
|
|
_dictEnumerator = dictEnumerator.GetEnumerator(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Pair Current { |
|
|
|
@ -61,7 +62,7 @@ namespace Stackoverflow.Utilities
@@ -61,7 +62,7 @@ namespace Stackoverflow.Utilities
|
|
|
|
|
throw new NotSupportedException(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Dictionary<TFirst, TSecond>.Enumerator _dictEnumerator; |
|
|
|
|
private IEnumerator<KeyValuePair<TFirst, TSecond>> _dictEnumerator; |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -78,8 +79,8 @@ namespace Stackoverflow.Utilities
@@ -78,8 +79,8 @@ namespace Stackoverflow.Utilities
|
|
|
|
|
if (_firstToSecond.ContainsKey(first) || _secondToFirst.ContainsKey(second)) |
|
|
|
|
throw new ArgumentException("Duplicate first or second"); |
|
|
|
|
|
|
|
|
|
_firstToSecond.Add(first, second); |
|
|
|
|
_secondToFirst.Add(second, first); |
|
|
|
|
_firstToSecond.TryAdd(first, second); |
|
|
|
|
_secondToFirst.TryAdd(second, first); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -124,8 +125,8 @@ namespace Stackoverflow.Utilities
@@ -124,8 +125,8 @@ namespace Stackoverflow.Utilities
|
|
|
|
|
if (!_firstToSecond.TryGetValue(first, out second)) |
|
|
|
|
throw new ArgumentException("first"); |
|
|
|
|
|
|
|
|
|
_firstToSecond.Remove(first); |
|
|
|
|
_secondToFirst.Remove(second); |
|
|
|
|
_firstToSecond.TryRemove(first, out TSecond noop); |
|
|
|
|
_secondToFirst.TryRemove(second, out TFirst noopAgain); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -139,8 +140,8 @@ namespace Stackoverflow.Utilities
@@ -139,8 +140,8 @@ namespace Stackoverflow.Utilities
|
|
|
|
|
if (!_secondToFirst.TryGetValue(second, out first)) |
|
|
|
|
throw new ArgumentException("second"); |
|
|
|
|
|
|
|
|
|
_secondToFirst.Remove(second); |
|
|
|
|
_firstToSecond.Remove(first); |
|
|
|
|
_secondToFirst.TryRemove(second, out TFirst noop); |
|
|
|
|
_firstToSecond.TryRemove(first, out TSecond noopAgain); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
@ -159,9 +160,9 @@ namespace Stackoverflow.Utilities
@@ -159,9 +160,9 @@ namespace Stackoverflow.Utilities
|
|
|
|
|
if (_firstToSecond.ContainsKey(first) || _secondToFirst.ContainsKey(second)) |
|
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
_firstToSecond.Add(first, second); |
|
|
|
|
_secondToFirst.Add(second, first); |
|
|
|
|
return true; |
|
|
|
|
bool result = _firstToSecond.TryAdd(first, second); |
|
|
|
|
result = _secondToFirst.TryAdd(second, first) || result; |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -200,9 +201,9 @@ namespace Stackoverflow.Utilities
@@ -200,9 +201,9 @@ namespace Stackoverflow.Utilities
|
|
|
|
|
if (!_firstToSecond.TryGetValue(first, out second)) |
|
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
_firstToSecond.Remove(first); |
|
|
|
|
_secondToFirst.Remove(second); |
|
|
|
|
return true; |
|
|
|
|
bool result = _firstToSecond.TryRemove(first, out TSecond noop); |
|
|
|
|
result = _secondToFirst.TryRemove(second, out TFirst noopAgain) || result; |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -216,8 +217,8 @@ namespace Stackoverflow.Utilities
@@ -216,8 +217,8 @@ namespace Stackoverflow.Utilities
|
|
|
|
|
if (!_secondToFirst.TryGetValue(second, out first)) |
|
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
_secondToFirst.Remove(second); |
|
|
|
|
_firstToSecond.Remove(first); |
|
|
|
|
_secondToFirst.TryRemove(second, out TFirst noop); |
|
|
|
|
_firstToSecond.TryRemove(first, out TSecond noopAgain); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -243,7 +244,7 @@ namespace Stackoverflow.Utilities
@@ -243,7 +244,7 @@ namespace Stackoverflow.Utilities
|
|
|
|
|
public Enumerator GetEnumerator() |
|
|
|
|
{ |
|
|
|
|
//enumerator.Reset(firstToSecond.GetEnumerator());
|
|
|
|
|
return new Enumerator(_firstToSecond.GetEnumerator()); |
|
|
|
|
return new Enumerator(_firstToSecond); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
IEnumerator<Pair> IEnumerable<Pair>.GetEnumerator() |
|
|
|
@ -258,8 +259,8 @@ namespace Stackoverflow.Utilities
@@ -258,8 +259,8 @@ namespace Stackoverflow.Utilities
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Dictionary<TFirst, TSecond> _firstToSecond = new Dictionary<TFirst, TSecond>(); |
|
|
|
|
private Dictionary<TSecond, TFirst> _secondToFirst = new Dictionary<TSecond, TFirst>(); |
|
|
|
|
private ConcurrentDictionary<TFirst, TSecond> _firstToSecond = new ConcurrentDictionary<TFirst, TSecond>(); |
|
|
|
|
private ConcurrentDictionary<TSecond, TFirst> _secondToFirst = new ConcurrentDictionary<TSecond, TFirst>(); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |