/Release/Silverlight4/Source/Controls.DataVisualization.Toolkit/Collections/OrderedMultipleDictionary.cs

# · C# · 86 lines · 49 code · 6 blank · 31 comment · 4 complexity · fd47735ce450236aedfb230a54a57121 MD5 · raw file

  1. // (c) Copyright Microsoft Corporation.
  2. // This source is subject to the Microsoft Public License (Ms-PL).
  3. // Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
  4. // All other rights reserved.
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.Diagnostics.CodeAnalysis;
  9. using System.Linq;
  10. namespace System.Windows.Controls.DataVisualization.Collections
  11. {
  12. /// <summary>
  13. /// Implements a dictionary that can store multiple values for the same key and sorts the values.
  14. /// </summary>
  15. /// <typeparam name="TKey">Type for keys.</typeparam>
  16. /// <typeparam name="TValue">Type for values.</typeparam>
  17. internal class OrderedMultipleDictionary<TKey, TValue> : MultipleDictionary<TKey, TValue>, IEnumerable<TValue>
  18. where TKey : IComparable
  19. {
  20. /// <summary>
  21. /// Initializes a new instance of the MultipleDictionary class.
  22. /// </summary>
  23. /// <param name="allowDuplicateValues">The parameter is not used.</param>
  24. /// <param name="keyComparison">Key comparison class.</param>
  25. /// <param name="valueComparison">Value comparison class.</param>
  26. [SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "allowDuplicateValues", Justification = "Unused parameter exists for API compatibility.")]
  27. public OrderedMultipleDictionary(bool allowDuplicateValues, Comparison<TKey> keyComparison, Comparison<TValue> valueComparison)
  28. {
  29. Debug.Assert(null != keyComparison, "keyComparison must not be null.");
  30. Debug.Assert(null != valueComparison, "valueComparison must not be null.");
  31. BinaryTree = new LeftLeaningRedBlackTree<TKey, TValue>(keyComparison, valueComparison);
  32. }
  33. /// <summary>
  34. /// Gets a Range corresponding to the keys in the dictionary.
  35. /// </summary>
  36. /// <returns>Range of keys.</returns>
  37. public Range<TKey> GetKeyRange()
  38. {
  39. if (0 < BinaryTree.Count)
  40. {
  41. return new Range<TKey>(BinaryTree.MinimumKey, BinaryTree.MaximumKey);
  42. }
  43. else
  44. {
  45. return new Range<TKey>();
  46. }
  47. }
  48. /// <summary>
  49. /// Gets the largest and smallest key's extreme values from the dictionary.
  50. /// </summary>
  51. /// <returns>Tuple of the largest and smallest values.</returns>
  52. public Tuple<TValue, TValue> GetLargestAndSmallestValues()
  53. {
  54. if (0 < BinaryTree.Count)
  55. {
  56. return new Tuple<TValue, TValue>(BinaryTree.MinimumValue, BinaryTree.MaximumValue);
  57. }
  58. else
  59. {
  60. return null;
  61. }
  62. }
  63. /// <summary>
  64. /// Gets an enumerator for the values in the dictionary.
  65. /// </summary>
  66. /// <returns>Enumerator for values.</returns>
  67. public IEnumerator<TValue> GetEnumerator()
  68. {
  69. return BinaryTree.GetValuesForAllKeys().GetEnumerator();
  70. }
  71. /// <summary>
  72. /// Gets an enumerator for the values in the dictionary.
  73. /// </summary>
  74. /// <returns>Enumerator for the values.</returns>
  75. IEnumerator IEnumerable.GetEnumerator()
  76. {
  77. return BinaryTree.GetValuesForAllKeys().GetEnumerator();
  78. }
  79. }
  80. }