Dictionaries.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc.
  3. // http://code.google.com/p/protobuf/
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. using System;
  17. using System.Collections;
  18. using System.Collections.Generic;
  19. namespace Google.ProtocolBuffers.Collections {
  20. /// <summary>
  21. /// Non-generic class with generic methods which proxy to the non-generic methods
  22. /// in the generic class.
  23. /// </summary>
  24. public static class Dictionaries {
  25. /// <summary>
  26. /// Compares two dictionaries for equality. Each value is compared with equality using Equals
  27. /// for non-IEnumerable implementations, and using EnumerableEquals otherwise.
  28. /// TODO(jonskeet): This is clearly pretty slow, and involves lots of boxing/unboxing...
  29. /// </summary>
  30. public static bool Equals<TKey, TValue>(IDictionary<TKey, TValue> left, IDictionary<TKey, TValue> right) {
  31. if (left.Count != right.Count) {
  32. return false;
  33. }
  34. foreach (KeyValuePair<TKey,TValue> leftEntry in left)
  35. {
  36. TValue rightValue;
  37. if (!right.TryGetValue(leftEntry.Key, out rightValue)) {
  38. return false;
  39. }
  40. IEnumerable leftEnumerable = leftEntry.Value as IEnumerable;
  41. IEnumerable rightEnumerable = rightValue as IEnumerable;
  42. if (leftEnumerable == null || rightEnumerable == null) {
  43. if (!object.Equals(leftEntry.Value, rightValue)) {
  44. return false;
  45. }
  46. } else {
  47. IEnumerator leftEnumerator = leftEnumerable.GetEnumerator();
  48. try {
  49. foreach (object rightObject in rightEnumerable) {
  50. if (!leftEnumerator.MoveNext()) {
  51. return false;
  52. }
  53. if (!object.Equals(leftEnumerator.Current, rightObject)) {
  54. return false;
  55. }
  56. }
  57. if (leftEnumerator.MoveNext()) {
  58. return false;
  59. }
  60. } finally {
  61. if (leftEnumerator is IDisposable) {
  62. ((IDisposable)leftEnumerator).Dispose();
  63. }
  64. }
  65. }
  66. }
  67. return true;
  68. }
  69. public static IDictionary<TKey, TValue> AsReadOnly<TKey, TValue> (IDictionary<TKey, TValue> dictionary) {
  70. return dictionary.IsReadOnly ? dictionary : new ReadOnlyDictionary<TKey, TValue>(dictionary);
  71. }
  72. /// <summary>
  73. /// Creates a hashcode for a dictionary by XORing the hashcodes of all the fields
  74. /// and values. (By XORing, we avoid ordering issues.)
  75. /// TODO(jonskeet): Currently XORs other stuff too, and assumes non-null values.
  76. /// </summary>
  77. public static int GetHashCode<TKey, TValue>(IDictionary<TKey, TValue> dictionary) {
  78. int ret = 31;
  79. foreach (KeyValuePair<TKey, TValue> entry in dictionary) {
  80. int hash = entry.Key.GetHashCode() ^ GetDeepHashCode(entry.Value);
  81. ret ^= hash;
  82. }
  83. return ret;
  84. }
  85. /// <summary>
  86. /// Determines the hash of a value by either taking it directly or hashing all the elements
  87. /// for IEnumerable implementations.
  88. /// </summary>
  89. private static int GetDeepHashCode(object value) {
  90. IEnumerable iterable = value as IEnumerable;
  91. if (iterable == null) {
  92. return value.GetHashCode();
  93. }
  94. int hash = 29;
  95. foreach (object element in iterable) {
  96. hash = hash * 37 + element.GetHashCode();
  97. }
  98. return hash;
  99. }
  100. }
  101. }