Search Results for

    Show / Hide Table of Contents

    Class Hash

    Utilities for implementing GetHashCode().

    Inheritance
    Object
    Hash
    Inherited Members
    Object.Equals(Object)
    Object.Equals(Object, Object)
    Object.GetHashCode()
    Object.GetType()
    Object.MemberwiseClone()
    Object.ReferenceEquals(Object, Object)
    Object.ToString()
    Namespace: Microsoft.ML.Probabilistic.Utilities
    Assembly: Microsoft.ML.Probabilistic.dll
    Syntax
    public static class Hash
    Remarks

    To hash an object with two fields x and y:

    return Hash.Combine(x.GetHashCode(), y.GetHashCode());

    To hash an array:

    int hash = Hash.Start;
    for(int i = 0; i < Count; i++)
        hash = Hash.Combine(hash, this[i].GetHashCode());
    return hash;

    Algorithm: FNV hash from http://www.isthe.com/chongo/tech/comp/fnv/

    Note: You should not use xor to combine hash codes, even though it is recommended by MSDN. xor is invariant to permutation, which means "abc" and "bac" and "cba" will hash to the same value (bad). Also xoring a hash value with itself produces 0, so "aab" and "b" will hash to the same value (bad).

    Fields

    Start

    The recommended start value for a combined hash value

    Declaration
    public const int Start = -2128831035
    Field Value
    Type Description
    Int32

    Methods

    Combine(Int32, Double)

    Incorporates the hash key of a double into an existing hash key

    Declaration
    public static int Combine(int hash, double number)
    Parameters
    Type Name Description
    Int32 hash

    Exisiting hash key

    Double number

    Floating point number to incorporate

    Returns
    Type Description
    Int32

    The new, combined hash key

    Combine(Int32, Int32)

    Combines two int32 hash keys

    Declaration
    public static int Combine(int hash, int key)
    Parameters
    Type Name Description
    Int32 hash

    First hash key

    Int32 key

    Second hash key

    Returns
    Type Description
    Int32

    Incorporates the second hash key into the first hash key and returns the new, combined hash key

    GetHashCodeAsSequence(IEnumerable)

    Declaration
    public static int GetHashCodeAsSequence(IEnumerable seq)
    Parameters
    Type Name Description
    IEnumerable seq
    Returns
    Type Description
    Int32

    GetHashCodeAsSequence<T>(IEnumerable<T>)

    Declaration
    public static int GetHashCodeAsSequence<T>(IEnumerable<T> seq)
    Parameters
    Type Name Description
    IEnumerable<T> seq
    Returns
    Type Description
    Int32
    Type Parameters
    Name Description
    T

    GetHashCodeAsSet<T>(IEnumerable<T>)

    Declaration
    public static int GetHashCodeAsSet<T>(IEnumerable<T> set)
    Parameters
    Type Name Description
    IEnumerable<T> set
    Returns
    Type Description
    Int32
    Type Parameters
    Name Description
    T

    GetHashCodeAsSet<T>(IEnumerable<T>, IEqualityComparer<T>)

    Declaration
    public static int GetHashCodeAsSet<T>(IEnumerable<T> set, IEqualityComparer<T> comparer)
    Parameters
    Type Name Description
    IEnumerable<T> set
    IEqualityComparer<T> comparer
    Returns
    Type Description
    Int32
    Type Parameters
    Name Description
    T
    In This Article
    Back to top Copyright © .NET Foundation. All rights reserved.