// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
namespace System.Windows.Controls.DataVisualization.Collections
{
///
/// Implements a dictionary that can store multiple values for the same key.
///
/// Type for keys.
/// Type for values.
internal class MultipleDictionary
{
///
/// Gets or sets the BinaryTree instance used to store the dictionary values.
///
protected LeftLeaningRedBlackTree BinaryTree { get; set; }
///
/// Initializes a new instance of the MultipleDictionary class.
///
protected MultipleDictionary()
{
}
///
/// Initializes a new instance of the MultipleDictionary class.
///
/// The parameter is not used.
/// The parameter is not used.
/// The parameter is not used.
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "allowDuplicateValues", Justification = "Unused parameter exists for API compatibility.")]
public MultipleDictionary(bool allowDuplicateValues, IEqualityComparer keyEqualityComparer, IEqualityComparer valueEqualityComparer)
{
Debug.Assert(null != keyEqualityComparer, "keyEqualityComparer must not be null.");
Debug.Assert(null != valueEqualityComparer, "valueEqualityComparer must not be null.");
BinaryTree = new LeftLeaningRedBlackTree(
(left, right) => keyEqualityComparer.GetHashCode(left).CompareTo(keyEqualityComparer.GetHashCode(right)),
(left, right) => valueEqualityComparer.GetHashCode(left).CompareTo(valueEqualityComparer.GetHashCode(right)));
}
///
/// Adds a key/value pair to the dictionary.
///
/// Key to add.
/// Value to add.
public void Add(TKey key, TValue value)
{
BinaryTree.Add(key, value);
}
///
/// Removes a key/value pair from the dictionary.
///
/// Key to remove.
/// Value to remove.
/// True if the value was present and removed.
public bool Remove(TKey key, TValue value)
{
return BinaryTree.Remove(key, value);
}
///
/// Gets the count of values in the dictionary.
///
public int Count
{
get
{
return BinaryTree.Count;
}
}
///
/// Returns the collection of values corresponding to a key.
///
/// Specified key.
/// Collection of values.
public ICollection this[TKey key]
{
get
{
return BinaryTree.GetValuesForKey(key).ToList();
}
}
///
/// Clears the items in the dictionary.
///
public void Clear()
{
BinaryTree.Clear();
}
}
}