Dictionaries.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Collections;
  5. using Google.ProtocolBuffers.Descriptors;
  6. namespace Google.ProtocolBuffers.Collections {
  7. /// <summary>
  8. /// Non-generic class with generic methods which proxy to the non-generic methods
  9. /// in the generic class.
  10. /// </summary>
  11. public static class Dictionaries {
  12. /// <summary>
  13. /// Compares two dictionaries for equality. Each value is compared with equality using Equals
  14. /// for non-IEnumerable implementations, and using EnumerableEquals otherwise.
  15. /// TODO(jonskeet): This is clearly pretty slow, and involves lots of boxing/unboxing...
  16. /// </summary>
  17. public static bool Equals<TKey, TValue>(IDictionary<TKey, TValue> left, IDictionary<TKey, TValue> right) {
  18. if (left.Count != right.Count) {
  19. return false;
  20. }
  21. foreach (KeyValuePair<TKey,TValue> leftEntry in left)
  22. {
  23. TValue rightValue;
  24. if (!right.TryGetValue(leftEntry.Key, out rightValue)) {
  25. return false;
  26. }
  27. IEnumerable leftEnumerable = leftEntry.Value as IEnumerable;
  28. IEnumerable rightEnumerable = rightValue as IEnumerable;
  29. if (leftEnumerable == null || rightEnumerable == null) {
  30. if (!object.Equals(leftEntry.Value, rightValue)) {
  31. return false;
  32. }
  33. } else {
  34. IEnumerator leftEnumerator = leftEnumerable.GetEnumerator();
  35. try {
  36. foreach (object rightObject in rightEnumerable) {
  37. if (!leftEnumerator.MoveNext()) {
  38. return false;
  39. }
  40. if (!object.Equals(leftEnumerator.Current, rightObject)) {
  41. return false;
  42. }
  43. }
  44. if (leftEnumerator.MoveNext()) {
  45. return false;
  46. }
  47. } finally {
  48. if (leftEnumerator is IDisposable) {
  49. ((IDisposable)leftEnumerator).Dispose();
  50. }
  51. }
  52. }
  53. }
  54. return true;
  55. }
  56. public static IDictionary<TKey, TValue> AsReadOnly<TKey, TValue> (IDictionary<TKey, TValue> dictionary) {
  57. return dictionary.IsReadOnly ? dictionary : new ReadOnlyDictionary<TKey, TValue>(dictionary);
  58. }
  59. /// <summary>
  60. /// Creates a hashcode for a dictionary by XORing the hashcodes of all the fields
  61. /// and values. (By XORing, we avoid ordering issues.)
  62. /// TODO(jonskeet): Currently XORs other stuff too, and assumes non-null values.
  63. /// </summary>
  64. public static int GetHashCode<TKey, TValue>(IDictionary<TKey, TValue> dictionary) {
  65. int ret = 31;
  66. foreach (KeyValuePair<TKey, TValue> entry in dictionary) {
  67. int hash = entry.Key.GetHashCode() ^ GetDeepHashCode(entry.Value);
  68. ret ^= hash;
  69. }
  70. return ret;
  71. }
  72. /// <summary>
  73. /// Determines the hash of a value by either taking it directly or hashing all the elements
  74. /// for IEnumerable implementations.
  75. /// </summary>
  76. private static int GetDeepHashCode(object value) {
  77. IEnumerable iterable = value as IEnumerable;
  78. if (iterable == null) {
  79. return value.GetHashCode();
  80. }
  81. int hash = 29;
  82. foreach (object element in iterable) {
  83. hash = hash * 37 + element.GetHashCode();
  84. }
  85. return hash;
  86. }
  87. }
  88. }