What is the main purpose of this hash function in the following code?

The following is the code, excerpted from OpenStreetMapX package. I do not quite understand what is the purpose of the hash function here? Based on the help of the hash function from REPL, the hash function hash(x[, h::UInt]) is used to compute an integer hash code such that isequal(x,y) implies hash(x) == hash(y). The optional second argument h is a hash code to be mixed with the result.

function add_new_node!(nodes::Dict{Int,T},loc::T, start_id::Int = reinterpret((Int), hash(loc))) where T <: (Union{OpenStreetMapX.LLA,OpenStreetMapX.ENU})
    id = start_id
    while id <= typemax(Int)
        if !haskey(nodes, id)
            nodes[id] = loc
            return id
        end
        id += 1
    end

    msg = "Unable to add new node to map, $(typemax(Int)) nodes is the current limit."
    throw(error(msg))
end

It looks like the hash is being used to generate an integer which is likely to be unique to the particular node. There is no guarantee that that hash won’t be the same as the hash of some other node, but it’s a reasonable guess to use as a starting point.

1 Like

Note that when asking about details of the code for particular packages, it may be best to ask the maintainers directly, eg by opening an issue.

Most packages are OK with this when done politely, as it is a form of code review.

1 Like