1 #region Copyright notice and license 2 // Protocol Buffers - Google's data interchange format 3 // Copyright 2017 Google Inc. All rights reserved. 4 // https://developers.google.com/protocol-buffers/ 5 // 6 // Redistribution and use in source and binary forms, with or without 7 // modification, are permitted provided that the following conditions are 8 // met: 9 // 10 // * Redistributions of source code must retain the above copyright 11 // notice, this list of conditions and the following disclaimer. 12 // * Redistributions in binary form must reproduce the above 13 // copyright notice, this list of conditions and the following disclaimer 14 // in the documentation and/or other materials provided with the 15 // distribution. 16 // * Neither the name of Google Inc. nor the names of its 17 // contributors may be used to endorse or promote products derived from 18 // this software without specific prior written permission. 19 // 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 #endregion 32 33 using System.Collections.Generic; 34 using System.Collections.ObjectModel; 35 36 namespace Google.Protobuf.Collections 37 { 38 /// <summary> 39 /// Utility to compare if two Lists are the same, and the hash code 40 /// of a List. 41 /// </summary> 42 public static class Lists 43 { 44 /// <summary> 45 /// Checks if two lists are equal. 46 /// </summary> Equals(List<T> left, List<T> right)47 public static bool Equals<T>(List<T> left, List<T> right) 48 { 49 if (left == right) 50 { 51 return true; 52 } 53 if (left == null || right == null) 54 { 55 return false; 56 } 57 if (left.Count != right.Count) 58 { 59 return false; 60 } 61 IEqualityComparer<T> comparer = EqualityComparer<T>.Default; 62 for (int i = 0; i < left.Count; i++) 63 { 64 if (!comparer.Equals(left[i], right[i])) 65 { 66 return false; 67 } 68 } 69 return true; 70 } 71 72 /// <summary> 73 /// Gets the list's hash code. 74 /// </summary> GetHashCode(List<T> list)75 public static int GetHashCode<T>(List<T> list) 76 { 77 if (list == null) 78 { 79 return 0; 80 } 81 int hash = 31; 82 foreach (T element in list) 83 { 84 hash = hash * 29 + element.GetHashCode(); 85 } 86 return hash; 87 } 88 } 89 }