From 74c96aac8acc8444ef1713b86ee408773fd246e5 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Mon, 11 Jun 2018 16:00:28 -0400 Subject: [PATCH] fix: Prevent breaking BiDictionary mappings --- ICD.Common.Utils/Collections/BiDictionary.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/ICD.Common.Utils/Collections/BiDictionary.cs b/ICD.Common.Utils/Collections/BiDictionary.cs index b9770b6..21c9d41 100644 --- a/ICD.Common.Utils/Collections/BiDictionary.cs +++ b/ICD.Common.Utils/Collections/BiDictionary.cs @@ -5,7 +5,7 @@ using System.Collections.Generic; namespace ICD.Common.Utils.Collections { /// - /// 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. /// /// /// @@ -63,10 +63,10 @@ namespace ICD.Common.Utils.Collections if (value == null) throw new ArgumentNullException("value"); - if (m_KeyToValue.ContainsKey(key)) - throw new ArgumentException("Key is already present in the dictionary", "key"); + if (ContainsKey(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"); m_KeyToValue.Add(key, value); @@ -83,6 +83,11 @@ namespace ICD.Common.Utils.Collections if (value == null) 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_ValueToKey[value] = key; }