模块:Dictionary

来自维阿百科
霓虹灯鱼讨论 | 贡献2022年7月8日 (五) 21:15的版本 (导入1个版本)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航搜索

此模块的文档可以在模块:Dictionary/doc创建

local dictionary = {}

function dictionary.create(keycomparer, dic)
    local keyvaluepairs = {}
    local comparer
    if dic == nil then
        comparer = keycomparer or function(k1, k2) return k1 == k2 end
    else
        comparer = keycomparer or dic.comparer
        for i, pair in ipairs(dic.keyvaluepairs) do
            table.insert(keyvaluepairs, { key = pair.key, value = pair.value })
        end
    end
    local prototype = { comparer = comparer, keyvaluepairs = keyvaluepairs }
    prototype.add = dictionary.add
    prototype.remove = dictionary.remove
    prototype.hasKey = dictionary.hasKey
    prototype.enum = dictionary.enum
    prototype.getValue = dictionary.getValue
    prototype.setValue = dictionary.setValue
    prototype.tryAdd = dictionary.tryAdd
    prototype.tryRemove = dictionary.tryRemove
    prototype.tryGetValue = dictionary.tryGetValue
    prototype.trySetValue = dictionary.trySetValue
    return prototype
end

function dictionary.add(dic, key, value, ...)
    if dic == nil then error("参数dic为空。") end

    if dic:hasKey(key, ...) then
        error("字典中已经存在这个键。")
    end

    table.insert(dic.keyvaluepairs, { key = key, value = value })
    return dic
end

function dictionary.tryAdd(dic, key, value, ...)
    return pcall(dictionary.add, dic, key, value, ...)
end

function dictionary.remove(dic, key, ...)
    if dic == nil then error("参数dic为空。") end

    local index
    for i, pair in ipairs(dic.keyvaluepairs) do
        if dic.comparer(pair.key, key, ...) then
            index = i
            break
        end
    end
    if index ~= nil then table.remove(dic.keyvaluepairs, index) end
    return dic
end

function dictionary.tryRemove(dic, key, ...)
    return pcall(dictionary.remove, dic, key, ...)
end

function dictionary.enum(dic)
    if dic == nil then error("参数dic为空。") end

    return ipairs(dic.keyvaluepairs)
end

function dictionary.hasKey(dic, key, ...)
    if dic == nil then error("参数dic为空。") end

    for i, pair in ipairs(dic.keyvaluepairs) do
        if dic.comparer(pair.key, key, ...) then
            return true
        end
    end
    return false
end

function dictionary.getValue(dic, key, ...)
    if dic == nil then error("参数dic为空。") end

    for i, pair in ipairs(dic.keyvaluepairs) do
        if dic.comparer(pair.key, key, ...) then
            return pair.value
        end
    end
    error("字典中找不到这个键。")
end

function dictionary.tryGetValue(dic, key, ...)
    return pcall(dictionary.getValue, dic, key, ...)
end

function dictionary.setValue(dic, key, value, ...)
    if dic == nil then error("参数dic为空。") end

    local index
    for i, pair in ipairs(dic.keyvaluepairs) do
        if dic.comparer(pair.key, key, ...) then
            index = i
            break
        end
    end
    if index == nil then
        error("字典中找不到这个键。")
    else
        dic.keyvaluepairs[index] = value
    end
end

function dictionary.trySetValue(dic, key, value, ...)
    return pcall(dictionary.setValue, dic, key, value, ...)
end

return dictionary