1 // Licensed to the Apache Software Foundation(ASF) under one
2 // or more contributor license agreements.See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership.The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
18 using System.Collections;
20 namespace Thrift.Collections
22 // ReSharper disable once InconsistentNaming
23 public class TCollections
26 /// This will return true if the two collections are value-wise the same.
27 /// If the collection contains a collection, the collections will be compared using this method.
29 public static bool Equals(IEnumerable first, IEnumerable second)
31 if (first == null && second == null)
36 if (first == null || second == null)
41 var fiter = first.GetEnumerator();
42 var siter = second.GetEnumerator();
44 var fnext = fiter.MoveNext();
45 var snext = siter.MoveNext();
47 while (fnext && snext)
49 var fenum = fiter.Current as IEnumerable;
50 var senum = siter.Current as IEnumerable;
52 if (fenum != null && senum != null)
54 if (!Equals(fenum, senum))
59 else if (fenum == null ^ senum == null)
63 else if (!Equals(fiter.Current, siter.Current))
68 fnext = fiter.MoveNext();
69 snext = siter.MoveNext();
72 return fnext == snext;
76 /// This returns a hashcode based on the value of the enumerable.
78 public static int GetHashCode(IEnumerable enumerable)
80 if (enumerable == null)
87 foreach (var obj in enumerable)
89 var enum2 = obj as IEnumerable;
90 var objHash = enum2 == null ? obj.GetHashCode() : GetHashCode(enum2);
94 hashcode = (hashcode*397) ^ (objHash);