]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /** |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
18 | */ | |
19 | using System; | |
20 | using System.Collections; | |
21 | ||
22 | namespace Thrift.Collections | |
23 | { | |
24 | public class TCollections | |
25 | { | |
26 | /// <summary> | |
27 | /// This will return true if the two collections are value-wise the same. | |
28 | /// If the collection contains a collection, the collections will be compared using this method. | |
29 | /// </summary> | |
30 | public static bool Equals (IEnumerable first, IEnumerable second) | |
31 | { | |
32 | if (first == null && second == null) | |
33 | { | |
34 | return true; | |
35 | } | |
36 | if (first == null || second == null) | |
37 | { | |
38 | return false; | |
39 | } | |
40 | IEnumerator fiter = first.GetEnumerator (); | |
41 | IEnumerator siter = second.GetEnumerator (); | |
42 | ||
43 | bool fnext = fiter.MoveNext (); | |
44 | bool snext = siter.MoveNext (); | |
45 | while (fnext && snext) | |
46 | { | |
47 | IEnumerable fenum = fiter.Current as IEnumerable; | |
48 | IEnumerable senum = siter.Current as IEnumerable; | |
49 | if (fenum != null && senum != null) | |
50 | { | |
51 | if (!Equals(fenum, senum)) | |
52 | { | |
53 | return false; | |
54 | } | |
55 | } | |
56 | else if (fenum == null ^ senum == null) | |
57 | { | |
58 | return false; | |
59 | } | |
60 | else if (!Equals(fiter.Current, siter.Current)) | |
61 | { | |
62 | return false; | |
63 | } | |
64 | fnext = fiter.MoveNext(); | |
65 | snext = siter.MoveNext(); | |
66 | } | |
67 | ||
68 | return fnext == snext; | |
69 | } | |
70 | ||
71 | /// <summary> | |
72 | /// This returns a hashcode based on the value of the enumerable. | |
73 | /// </summary> | |
74 | public static int GetHashCode (IEnumerable enumerable) | |
75 | { | |
76 | if (enumerable == null) | |
77 | { | |
78 | return 0; | |
79 | } | |
80 | ||
81 | int hashcode = 0; | |
82 | foreach (object obj in enumerable) | |
83 | { | |
84 | IEnumerable enum2 = obj as IEnumerable; | |
85 | int objHash = enum2 == null ? obj.GetHashCode () : GetHashCode (enum2); | |
86 | unchecked | |
87 | { | |
88 | hashcode = (hashcode * 397) ^ (objHash); | |
89 | } | |
90 | } | |
91 | return hashcode; | |
92 | } | |
93 | } | |
94 | } |