Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

226
Visualizações
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
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda