fix: Prevent breaking BiDictionary mappings

This commit is contained in:
Chris Cameron
2018-06-11 16:00:28 -04:00
parent edc9fa300e
commit 74c96aac8a

View File

@@ -5,7 +5,7 @@ using System.Collections.Generic;
namespace ICD.Common.Utils.Collections namespace ICD.Common.Utils.Collections
{ {
/// <summary> /// <summary>
/// Provides a 1-to-1 map of keys to values with O(1) Value->Key lookup time. /// Provides a 1-to-1 map of Keys to Values with O(1) Value->Key lookup time.
/// </summary> /// </summary>
/// <typeparam name="TKey"></typeparam> /// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam> /// <typeparam name="TValue"></typeparam>
@@ -63,10 +63,10 @@ namespace ICD.Common.Utils.Collections
if (value == null) if (value == null)
throw new ArgumentNullException("value"); throw new ArgumentNullException("value");
if (m_KeyToValue.ContainsKey(key)) if (ContainsKey(key))
throw new ArgumentException("Key is already present in the dictionary", "key"); throw new ArgumentException("Key is already present in the collection", "key");
if (m_ValueToKey.ContainsKey(value)) if (ContainsValue(value))
throw new ArgumentException("Value is already present in the collection", "value"); throw new ArgumentException("Value is already present in the collection", "value");
m_KeyToValue.Add(key, value); m_KeyToValue.Add(key, value);
@@ -83,6 +83,11 @@ namespace ICD.Common.Utils.Collections
if (value == null) if (value == null)
throw new ArgumentNullException("value"); throw new ArgumentNullException("value");
// Prevent building a 2-to-1 mapping
if (ContainsKey(key) ^ ContainsValue(value))
throw new InvalidOperationException(
"Can not set key and value when either key or value are already present in the collection");
m_KeyToValue[key] = value; m_KeyToValue[key] = value;
m_ValueToKey[value] = key; m_ValueToKey[value] = key;
} }