using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace MBS.Framework.Collections.Generic
{
///
/// Provides a collection that can be keyed either forward (T1=>T2) or backward (T2=>T1).
///
/// The type of the value on which to forward-key the collection.
/// The type of the value on which to backward-key the collection.
public class BidirectionalDictionary : System.Collections.IEnumerable
{
private Dictionary mvarForwardDictionary = new Dictionary();
private Dictionary mvarBackwardDictionary = new Dictionary();
///
/// Adds the specified first and second value to the collection.
///
/// The first value to add.
/// The second value to add.
public void Add(T1 value1, T2 value2)
{
mvarForwardDictionary.Add(value1, value2);
mvarBackwardDictionary.Add(value2, value1);
}
///
/// Removes the first and second value associated with the specified first value.
///
/// The first value of the first-second value pair to remove.
public void RemoveByValue1(T1 value1)
{
T2 value2 = mvarForwardDictionary[value1];
mvarForwardDictionary.Remove(value1);
mvarBackwardDictionary.Remove(value2);
}
///
/// Removes the first and second value associated with the specified second value.
///
/// The second value of the first-second value pair to remove.
public void RemoveByValue2(T2 value2)
{
T1 value1 = mvarBackwardDictionary[value2];
mvarForwardDictionary.Remove(value1);
mvarBackwardDictionary.Remove(value2);
}
///
/// Gets the first value associated with the specified second value.
///
/// The second value of the first-second value pair to search for.
/// The first value associated with the specified second value.
public T1 GetValue1(T2 value2)
{
return mvarBackwardDictionary[value2];
}
///
/// Gets the second value associated with the specified first value.
///
/// The first value of the first-second value pair to search for.
/// The second value associated with the specified first value.
public T2 GetValue2(T1 value1)
{
return mvarForwardDictionary[value1];
}
///
/// Determines if the specified first value is contained in this collection.
///
/// The value to search for.
/// True if the value exists in the first value dictionary; otherwise, false.
public bool ContainsValue1(T1 value)
{
return mvarForwardDictionary.ContainsKey(value);
}
///
/// Determines if the specified second value is contained in this collection.
///
/// The value to search for.
/// True if the value exists in the second value dictionary; otherwise, false.
public bool ContainsValue2(T2 value)
{
return mvarBackwardDictionary.ContainsKey(value);
}
///
/// Gets a that can be used to iterate over this collection.
///
/// A that can be used to iterate over this collection.
public IEnumerator> GetEnumerator()
{
return mvarForwardDictionary.GetEnumerator();
}
///
/// Gets a that can be used to iterate over this collection.
///
/// A that can be used to iterate over this collection.
IEnumerator IEnumerable.GetEnumerator()
{
return mvarForwardDictionary.GetEnumerator();
}
///
/// Returns the number of items in this collection.
///
public int Count
{
get
{
if (mvarForwardDictionary.Count != mvarBackwardDictionary.Count)
{
// this should never happen
throw new InvalidOperationException("Count mismatch");
}
// they should be equal, so choose one at random to return and hardcode it ;)
return mvarBackwardDictionary.Count;
}
}
}
}