mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-15 12:45:01 +00:00
feat: Initial commit of BiDictionary
This commit is contained in:
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||||||
### Added
|
### Added
|
||||||
- Added ZIP features for examining the contents of an archive
|
- Added ZIP features for examining the contents of an archive
|
||||||
- Added FileNameEqualityComparer
|
- Added FileNameEqualityComparer
|
||||||
|
- Added BiDictionary for one-to-one maps
|
||||||
|
|
||||||
## [3.5.1] - 2018-06-04
|
## [3.5.1] - 2018-06-04
|
||||||
### Changed
|
### Changed
|
||||||
|
|||||||
184
ICD.Common.Utils/Collections/BiDictionary.cs
Normal file
184
ICD.Common.Utils/Collections/BiDictionary.cs
Normal file
@@ -0,0 +1,184 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace ICD.Common.Utils.Collections
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Provides a 1-to-1 map of keys to values with O(1) Value->Key lookup time.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TKey"></typeparam>
|
||||||
|
/// <typeparam name="TValue"></typeparam>
|
||||||
|
public sealed class BiDictionary<TKey, TValue> : IDictionary<TKey, TValue>
|
||||||
|
{
|
||||||
|
private readonly Dictionary<TKey, TValue> m_KeyToValue;
|
||||||
|
private readonly Dictionary<TValue, TKey> m_ValueToKey;
|
||||||
|
|
||||||
|
#region Properties
|
||||||
|
|
||||||
|
public int Count { get { return m_KeyToValue.Count; } }
|
||||||
|
|
||||||
|
public bool IsReadOnly { get { return false; } }
|
||||||
|
|
||||||
|
public ICollection<TKey> Keys { get { return m_KeyToValue.Keys; } }
|
||||||
|
|
||||||
|
public ICollection<TValue> Values { get { return m_ValueToKey.Keys; } }
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Constructor.
|
||||||
|
/// </summary>
|
||||||
|
public BiDictionary()
|
||||||
|
{
|
||||||
|
m_KeyToValue = new Dictionary<TKey, TValue>();
|
||||||
|
m_ValueToKey = new Dictionary<TValue, TKey>();
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Methods
|
||||||
|
|
||||||
|
public void Clear()
|
||||||
|
{
|
||||||
|
m_KeyToValue.Clear();
|
||||||
|
m_ValueToKey.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool ContainsKey(TKey key)
|
||||||
|
{
|
||||||
|
return m_KeyToValue.ContainsKey(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool ContainsValue(TValue value)
|
||||||
|
{
|
||||||
|
return m_ValueToKey.ContainsKey(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Add(TKey key, TValue value)
|
||||||
|
{
|
||||||
|
// ReSharper disable once CompareNonConstrainedGenericWithNull
|
||||||
|
if (key == null)
|
||||||
|
throw new ArgumentNullException("key");
|
||||||
|
|
||||||
|
// ReSharper disable once CompareNonConstrainedGenericWithNull
|
||||||
|
if (value == null)
|
||||||
|
throw new ArgumentNullException("value");
|
||||||
|
|
||||||
|
if (m_KeyToValue.ContainsKey(key))
|
||||||
|
throw new ArgumentException("Key is already present in the dictionary", "key");
|
||||||
|
|
||||||
|
if (m_ValueToKey.ContainsKey(value))
|
||||||
|
throw new ArgumentException("Value is already present in the collection", "value");
|
||||||
|
|
||||||
|
m_KeyToValue.Add(key, value);
|
||||||
|
m_ValueToKey.Add(value, key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Set(TKey key, TValue value)
|
||||||
|
{
|
||||||
|
// ReSharper disable once CompareNonConstrainedGenericWithNull
|
||||||
|
if (key == null)
|
||||||
|
throw new ArgumentNullException("key");
|
||||||
|
|
||||||
|
// ReSharper disable once CompareNonConstrainedGenericWithNull
|
||||||
|
if (value == null)
|
||||||
|
throw new ArgumentNullException("value");
|
||||||
|
|
||||||
|
m_KeyToValue[key] = value;
|
||||||
|
m_ValueToKey[value] = key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TKey GetKey(TValue value)
|
||||||
|
{
|
||||||
|
return m_ValueToKey[value];
|
||||||
|
}
|
||||||
|
|
||||||
|
public TValue GetValue(TKey key)
|
||||||
|
{
|
||||||
|
return m_KeyToValue[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool RemoveKey(TKey key)
|
||||||
|
{
|
||||||
|
if (!ContainsKey(key))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
TValue value = m_KeyToValue[key];
|
||||||
|
|
||||||
|
m_KeyToValue.Remove(key);
|
||||||
|
m_ValueToKey.Remove(value);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool RemoveValue(TValue value)
|
||||||
|
{
|
||||||
|
if (!ContainsValue(value))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
TKey key = m_ValueToKey[value];
|
||||||
|
|
||||||
|
return RemoveKey(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool TryGetValue(TKey key, out TValue value)
|
||||||
|
{
|
||||||
|
return m_KeyToValue.TryGetValue(key, out value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool TryGetKey(TValue value, out TKey key)
|
||||||
|
{
|
||||||
|
return m_ValueToKey.TryGetValue(value, out key);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region IDictionary
|
||||||
|
|
||||||
|
TValue IDictionary<TKey, TValue>.this[TKey key] { get { return GetValue(key); } set { Set(key, value); } }
|
||||||
|
|
||||||
|
bool IDictionary<TKey, TValue>.Remove(TKey key)
|
||||||
|
{
|
||||||
|
return RemoveKey(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region ICollection
|
||||||
|
|
||||||
|
void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
|
||||||
|
{
|
||||||
|
Add(item.Key, item.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
|
||||||
|
{
|
||||||
|
return (m_KeyToValue as IDictionary<TKey, TValue>).Contains(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
|
||||||
|
{
|
||||||
|
return RemoveKey(item.Key);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
|
||||||
|
{
|
||||||
|
(m_KeyToValue as IDictionary<TKey, TValue>).CopyTo(array, arrayIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region IEnumerable
|
||||||
|
|
||||||
|
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
|
||||||
|
{
|
||||||
|
return m_KeyToValue.GetEnumerator();
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerator IEnumerable.GetEnumerator()
|
||||||
|
{
|
||||||
|
return GetEnumerator();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -74,6 +74,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Attributes\AbstractIcdAttribute.cs" />
|
<Compile Include="Attributes\AbstractIcdAttribute.cs" />
|
||||||
|
<Compile Include="Collections\BiDictionary.cs" />
|
||||||
<Compile Include="Collections\IcdOrderedDictionary.cs" />
|
<Compile Include="Collections\IcdOrderedDictionary.cs" />
|
||||||
<Compile Include="Collections\PriorityQueue.cs" />
|
<Compile Include="Collections\PriorityQueue.cs" />
|
||||||
<Compile Include="Collections\WeakKeyDictionary.cs" />
|
<Compile Include="Collections\WeakKeyDictionary.cs" />
|
||||||
|
|||||||
Reference in New Issue
Block a user