1 #region Copyright notice and license 2 // Protocol Buffers - Google's data interchange format 3 // Copyright 2017 Google Inc. All rights reserved. 4 // 5 // Use of this source code is governed by a BSD-style 6 // license that can be found in the LICENSE file or at 7 // https://developers.google.com/open-source/licenses/bsd 8 #endregion 9 10 using System.Collections.Generic; 11 12 namespace Google.Protobuf.Collections 13 { 14 /// <summary> 15 /// Utility to compare if two Lists are the same, and the hash code 16 /// of a List. 17 /// </summary> 18 public static class Lists 19 { 20 /// <summary> 21 /// Checks if two lists are equal. 22 /// </summary> Equals(List<T> left, List<T> right)23 public static bool Equals<T>(List<T> left, List<T> right) 24 { 25 if (left == right) 26 { 27 return true; 28 } 29 if (left == null || right == null) 30 { 31 return false; 32 } 33 if (left.Count != right.Count) 34 { 35 return false; 36 } 37 IEqualityComparer<T> comparer = EqualityComparer<T>.Default; 38 for (int i = 0; i < left.Count; i++) 39 { 40 if (!comparer.Equals(left[i], right[i])) 41 { 42 return false; 43 } 44 } 45 return true; 46 } 47 48 /// <summary> 49 /// Gets the list's hash code. 50 /// </summary> GetHashCode(List<T> list)51 public static int GetHashCode<T>(List<T> list) 52 { 53 if (list == null) 54 { 55 return 0; 56 } 57 int hash = 31; 58 foreach (T element in list) 59 { 60 hash = hash * 29 + element.GetHashCode(); 61 } 62 return hash; 63 } 64 } 65 }