1 | // Protocol Buffers - Google's data interchange format
|
---|
2 | // Copyright 2008 Google Inc. All rights reserved.
|
---|
3 | // http://github.com/jskeet/dotnet-protobufs/
|
---|
4 | // Original C++/Java/Python code:
|
---|
5 | // http://code.google.com/p/protobuf/
|
---|
6 | //
|
---|
7 | // Redistribution and use in source and binary forms, with or without
|
---|
8 | // modification, are permitted provided that the following conditions are
|
---|
9 | // met:
|
---|
10 | //
|
---|
11 | // * Redistributions of source code must retain the above copyright
|
---|
12 | // notice, this list of conditions and the following disclaimer.
|
---|
13 | // * Redistributions in binary form must reproduce the above
|
---|
14 | // copyright notice, this list of conditions and the following disclaimer
|
---|
15 | // in the documentation and/or other materials provided with the
|
---|
16 | // distribution.
|
---|
17 | // * Neither the name of Google Inc. nor the names of its
|
---|
18 | // contributors may be used to endorse or promote products derived from
|
---|
19 | // this software without specific prior written permission.
|
---|
20 | //
|
---|
21 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
22 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
23 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
24 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
25 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
26 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
27 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
28 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
29 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
30 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
31 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
32 | using System.Collections;
|
---|
33 | using System.Collections.Generic;
|
---|
34 |
|
---|
35 | namespace Google.ProtocolBuffers.Collections {
|
---|
36 |
|
---|
37 | /// <summary>
|
---|
38 | /// Utility class for dictionaries.
|
---|
39 | /// </summary>
|
---|
40 | public static class Dictionaries {
|
---|
41 |
|
---|
42 | /// <summary>
|
---|
43 | /// Compares two dictionaries for equality. Each value is compared with equality using Equals
|
---|
44 | /// for non-IEnumerable implementations, and using EnumerableEquals otherwise.
|
---|
45 | /// TODO(jonskeet): This is clearly pretty slow, and involves lots of boxing/unboxing...
|
---|
46 | /// </summary>
|
---|
47 | public static bool Equals<TKey, TValue>(IDictionary<TKey, TValue> left, IDictionary<TKey, TValue> right) {
|
---|
48 | if (left.Count != right.Count) {
|
---|
49 | return false;
|
---|
50 | }
|
---|
51 | foreach (KeyValuePair<TKey, TValue> leftEntry in left) {
|
---|
52 | TValue rightValue;
|
---|
53 | if (!right.TryGetValue(leftEntry.Key, out rightValue)) {
|
---|
54 | return false;
|
---|
55 | }
|
---|
56 |
|
---|
57 | IEnumerable leftEnumerable = leftEntry.Value as IEnumerable;
|
---|
58 | IEnumerable rightEnumerable = rightValue as IEnumerable;
|
---|
59 | if (leftEnumerable == null || rightEnumerable == null) {
|
---|
60 | if (!Equals(leftEntry.Value, rightValue)) {
|
---|
61 | return false;
|
---|
62 | }
|
---|
63 | } else {
|
---|
64 | if (!Enumerables.Equals(leftEnumerable, rightEnumerable)) {
|
---|
65 | return false;
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|
69 | return true;
|
---|
70 | }
|
---|
71 |
|
---|
72 | public static IDictionary<TKey, TValue> AsReadOnly<TKey, TValue>(IDictionary<TKey, TValue> dictionary) {
|
---|
73 | return dictionary.IsReadOnly ? dictionary : new ReadOnlyDictionary<TKey, TValue>(dictionary);
|
---|
74 | }
|
---|
75 |
|
---|
76 | /// <summary>
|
---|
77 | /// Creates a hashcode for a dictionary by XORing the hashcodes of all the fields
|
---|
78 | /// and values. (By XORing, we avoid ordering issues.)
|
---|
79 | /// TODO(jonskeet): Currently XORs other stuff too, and assumes non-null values.
|
---|
80 | /// </summary>
|
---|
81 | public static int GetHashCode<TKey, TValue>(IDictionary<TKey, TValue> dictionary) {
|
---|
82 | int ret = 31;
|
---|
83 | foreach (KeyValuePair<TKey, TValue> entry in dictionary) {
|
---|
84 | int hash = entry.Key.GetHashCode() ^ GetDeepHashCode(entry.Value);
|
---|
85 | ret ^= hash;
|
---|
86 | }
|
---|
87 | return ret;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /// <summary>
|
---|
91 | /// Determines the hash of a value by either taking it directly or hashing all the elements
|
---|
92 | /// for IEnumerable implementations.
|
---|
93 | /// </summary>
|
---|
94 | private static int GetDeepHashCode(object value) {
|
---|
95 | IEnumerable iterable = value as IEnumerable;
|
---|
96 | if (iterable == null) {
|
---|
97 | return value.GetHashCode();
|
---|
98 | }
|
---|
99 | int hash = 29;
|
---|
100 | foreach (object element in iterable) {
|
---|
101 | hash = hash * 37 + element.GetHashCode();
|
---|
102 | }
|
---|
103 | return hash;
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|