public static int CombineValues <T>(ImmutableArray <T> values, int maxItemsToHash = int.MaxValue) { if (values.IsDefaultOrEmpty) { return(0); } var hashCode = 0; var count = 0; foreach (T value in values) { if (count++ >= maxItemsToHash) { break; } // Should end up with a constrained virtual call to object.GetHashCode (i.e. avoid boxing where possible). if (value != null) { hashCode = CodeRefactoringHash.Combine(value.GetHashCode(), hashCode); } } return(hashCode); }
public static int CombineValues <T>(ImmutableArray <T> values, int maxItemsToHash = int.MaxValue) { if (values.IsDefaultOrEmpty) { return(0); } var hashCode = 0; var count = 0; foreach (var value in values) { if (count++ >= maxItemsToHash) { break; } // Should end up with a constrained virtual call to object.GetHashCode (i.e. avoid boxing where possible). #pragma warning disable RECS0017 // Possible compare of value type with 'null' if (value != null) #pragma warning restore RECS0017 // Possible compare of value type with 'null' { hashCode = CodeRefactoringHash.Combine(value.GetHashCode(), hashCode); } } return(hashCode); }
public static int CombineValues <T>(T[] values, int maxItemsToHash = int.MaxValue) { if (values == null) { return(0); } var maxSize = Math.Min(maxItemsToHash, values.Length); var hashCode = 0; for (int i = 0; i < maxSize; i++) { T value = values[i]; // Should end up with a constrained virtual call to object.GetHashCode (i.e. avoid boxing where possible). #pragma warning disable RECS0017 // Possible compare of value type with 'null' if (value != null) #pragma warning restore RECS0017 // Possible compare of value type with 'null' { hashCode = CodeRefactoringHash.Combine(value.GetHashCode(), hashCode); } } return(hashCode); }
public static int CombineValues(IEnumerable <string> values, StringComparer stringComparer, int maxItemsToHash = int.MaxValue) { if (values == null) { return(0); } var hashCode = 0; var count = 0; foreach (var value in values) { if (count++ >= maxItemsToHash) { break; } if (value != null) { hashCode = CodeRefactoringHash.Combine(stringComparer.GetHashCode(value), hashCode); } } return(hashCode); }
/// <summary> /// Compute the hashcode of a single character using the FNV-1a algorithm /// See http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function /// Note: In general, this isn't any more useful than "char.GetHashCode". However, /// it may be needed if you need to generate the same hash code as a string or /// substring with just a single character. /// </summary> /// <param name="ch">The character to hash</param> /// <returns>The FNV-1a hash code of the character.</returns> public static int GetFNVHashCode(char ch) { return(CodeRefactoringHash.CombineFNVHash(CodeRefactoringHash.FnvOffsetBias, ch)); }