What exactly is the meaning of "get!(Set{Int}, intersections, highway.nodes[I])" in the following code?

Hi, I was wondering, could anyone help me with clarifying a little bit of a line of the following code?

######################################
### Find Intersections of Highways ###
######################################

function find_intersections(highways::Vector{OpenStreetMapX.Way})
    seen = Set{Int}()
    intersections = Dict{Int,Set{Int}}()
    for highway in highways
        for i = 1:length(highway.nodes)
            if i == 1 || i == length(highway.nodes) || (highway.nodes[i] in seen)
                get!(Set{Int}, intersections, highway.nodes[i])
            else
                push!(seen, highway.nodes[i])
            end
        end
    end
    for highway in highways
        for i = 1:length(highway.nodes)
            if i == 1 || i == length(highway.nodes) || haskey(intersections, highway.nodes[i])
                push!(intersections[highway.nodes[i]], highway.id)
            end
        end
    end
    return intersections
end

I’m mainly confused with the code get!(Set{Int}, intersections, highway.nodes[i]). From my understanding, the first for loop will have the keys of the dictionary ready and the second for loop will have the values of the dictionary ready. However, I’m not exactly sure what is done with the above line of code. Any comments are greatly appreciated.

From help?> get!:

get!(f::Function, collection, key)
Return the value stored for the given key, or if no mapping for the key is
present, store key => f(), and return f().

so in this case Set{Int} takes the place of f, i.e. the function that is called with no arguments to generate default values (as in Set{Int}()).

1 Like

That is also what is in my mind. However, I did not quite see the logic in the above code. From my understanding, the first for loop will have the keys of the dictionary intersections ready. So for each highway, and for each node, if it is already in seen, or if the node index is 1, or if the node index is the number of nodes in highway, then, check to see if intersections[highway.nodes[i]] exists or not. If it exists, then simply return it. If not, then assign Set{Int}() to highway.nodes[i]. From my understanding, if it does not exist, then should simply add highway.nodes[i] to the dictionary with name intersections.

@ Bartosz_Pankratz