2023年政策修订增补工作正在进行中,欢迎参与!
  • Moegirl.ICU:萌娘百科流亡社群 581077156(QQ),欢迎对萌娘百科运营感到失望的编辑者加入
  • Moegirl.ICU:账号认领正在试运行,有意者请参照账号认领流程

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