置顶公告:【置顶】关于临时开启评论区所有功能的公告(2022.10.22) | 【置顶】关于本站Widget恢复使用的公告
  • 你好~!欢迎来到萌娘百科镜像站!如需查看或编辑,请联系本站管理员注册账号。
  • 本镜像站和其他萌娘百科的镜像站无关,请注意分别。

Module:Dictionary

猛汉♂百科,万男皆可猛的百科全书!转载请标注来源页面的网页链接,并声明引自猛汉百科。内容不可商用。
跳到导航 跳到搜索
Template-info.svg 模块文档  [创建] [刷新]
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