Matthew Crews
Matthew Crews

Reputation: 4305

Hash function for arrays of floats and ints in .NET

I have a particular use case where I am wanting to hash several arrays of float and int together. I am writing a simulation where I want to memoize a function call. The function takes a Settings type as an input.

The values in the Capacities and MaxRates arrays are 64 bit floats restricted to a range of 0.0 to 1,000,000.0. The length of Capacities and MaxRates is also restricted to 0 to 100. ValveState is an int with values from 0 to 10.

I am aware that hashing arbitrary floats is a bad idea. In this particular use case though the values in Settings will be exact, bit for bit matches to previous combination of values. That is the reason I want to memoize a function call using a Dictionary<Settings, 'Result>.

The function call I want to memoize is expensive and takes up >90% of the runtime. The combination of values in Settings is frequently repeated and they are exact matches. No math is actually done with the values during the simulation so rounding error is not occurring. Early tests suggest that memoizing this function can lead to a 10x improvement in speed with no loss in accuracy.

I am working in F# but anything in .NET is acceptable since I can translate it. Here is an example of what the F# record looks like.

type Settings = {
    Capacities : array<float>
    MaxRates : array<float>
    ValveState : array<int>
} with
    override this.GetHashCode () = 
        // Your awesome suggestion here

Upvotes: 2

Views: 235

Answers (1)

hvester
hvester

Reputation: 1628

You can use HashCode.Combine (available in netstandard 2.1) to combine hashes of all values in all three arrays.

open System

let hashArray (arr : 'T array) =
    let mutable hash = 0
    for x in arr do
        hash <- HashCode.Combine(hash, x)
    hash

[<CustomEquality; NoComparison>]
type Settings = {
    Capacities : array<float>
    MaxRates : array<float>
    ValveState : array<int>
} with
    override this.GetHashCode () =
        HashCode.Combine(
            hashArray this.Capacities,
            hashArray this.MaxRates,
            hashArray this.ValveState
        )

    override this.Equals(o : obj) =
        match o with
        | :? Settings as s ->
            this.Capacities = s.Capacities &&
            this.MaxRates = s.MaxRates &&
            this.ValveState = s.ValveState
        | _ ->
            false

Upvotes: 1

Related Questions