小编Ste*_*ite的帖子

使用自定义类型的键的奇怪Dict行为

我有一个递归函数,它利用全局字典来存储遍历树时已经获得的值.但是,至少存储在dict中的一些值似乎消失了!此简化代码显示了问题:

type id
    level::Int32
    x::Int32
end

Vdict = Dict{id,Float64}()

function getV(w::id)
    if haskey(Vdict,w)
        return Vdict[w]
    end 
    if w.level == 12
        return 1.0
    end
    w.x == -111 && println("dont have: ",w)

    local vv = 0.0
    for j = -15:15
        local wj = id(w.level+1,w.x+j)
        vv += getV(wj)
    end
    Vdict[w] = vv
    w.x == -111 && println("just stored: ",w)
    vv
end

getV(id(0,0))
Run Code Online (Sandbox Code Playgroud)

输出有很多行,如下所示:

just stored: id(11,-111)
dont have: id(11,-111)
just stored: id(11,-111)
dont have: id(11,-111)
just stored: id(11,-111)
dont have: id(11,-111)
...
Run Code Online (Sandbox Code Playgroud)

我有一个愚蠢的错误,或者朱莉娅的词典中是否有错误?

dictionary julia

4
推荐指数
1
解决办法
221
查看次数

标签 统计

dictionary ×1

julia ×1