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

Module:Dictionary

猛汉♂百科,万男皆可猛的百科全书!转载请标注来源页面的网页链接,并声明引自猛汉百科。内容不可商用。
跳到导航 跳到搜索
Template-info.svg 模块文档  [创建] [刷新]
  1. local dictionary = {}
  2. function dictionary.create(keycomparer, dic)
  3. local keyvaluepairs = {}
  4. local comparer
  5. if dic == nil then
  6. comparer = keycomparer or function(k1, k2) return k1 == k2 end
  7. else
  8. comparer = keycomparer or dic.comparer
  9. for i, pair in ipairs(dic.keyvaluepairs) do
  10. table.insert(keyvaluepairs, { key = pair.key, value = pair.value })
  11. end
  12. end
  13. local prototype = { comparer = comparer, keyvaluepairs = keyvaluepairs }
  14. prototype.add = dictionary.add
  15. prototype.remove = dictionary.remove
  16. prototype.hasKey = dictionary.hasKey
  17. prototype.enum = dictionary.enum
  18. prototype.getValue = dictionary.getValue
  19. prototype.setValue = dictionary.setValue
  20. prototype.tryAdd = dictionary.tryAdd
  21. prototype.tryRemove = dictionary.tryRemove
  22. prototype.tryGetValue = dictionary.tryGetValue
  23. prototype.trySetValue = dictionary.trySetValue
  24. return prototype
  25. end
  26. function dictionary.add(dic, key, value, ...)
  27. if dic == nil then error("参数dic为空。") end
  28. if dic:hasKey(key, ...) then
  29. error("字典中已经存在这个键。")
  30. end
  31. table.insert(dic.keyvaluepairs, { key = key, value = value })
  32. return dic
  33. end
  34. function dictionary.tryAdd(dic, key, value, ...)
  35. return pcall(dictionary.add, dic, key, value, ...)
  36. end
  37. function dictionary.remove(dic, key, ...)
  38. if dic == nil then error("参数dic为空。") end
  39. local index
  40. for i, pair in ipairs(dic.keyvaluepairs) do
  41. if dic.comparer(pair.key, key, ...) then
  42. index = i
  43. break
  44. end
  45. end
  46. if index ~= nil then table.remove(dic.keyvaluepairs, index) end
  47. return dic
  48. end
  49. function dictionary.tryRemove(dic, key, ...)
  50. return pcall(dictionary.remove, dic, key, ...)
  51. end
  52. function dictionary.enum(dic)
  53. if dic == nil then error("参数dic为空。") end
  54. return ipairs(dic.keyvaluepairs)
  55. end
  56. function dictionary.hasKey(dic, key, ...)
  57. if dic == nil then error("参数dic为空。") end
  58. for i, pair in ipairs(dic.keyvaluepairs) do
  59. if dic.comparer(pair.key, key, ...) then
  60. return true
  61. end
  62. end
  63. return false
  64. end
  65. function dictionary.getValue(dic, key, ...)
  66. if dic == nil then error("参数dic为空。") end
  67. for i, pair in ipairs(dic.keyvaluepairs) do
  68. if dic.comparer(pair.key, key, ...) then
  69. return pair.value
  70. end
  71. end
  72. error("字典中找不到这个键。")
  73. end
  74. function dictionary.tryGetValue(dic, key, ...)
  75. return pcall(dictionary.getValue, dic, key, ...)
  76. end
  77. function dictionary.setValue(dic, key, value, ...)
  78. if dic == nil then error("参数dic为空。") end
  79. local index
  80. for i, pair in ipairs(dic.keyvaluepairs) do
  81. if dic.comparer(pair.key, key, ...) then
  82. index = i
  83. break
  84. end
  85. end
  86. if index == nil then
  87. error("字典中找不到这个键。")
  88. else
  89. dic.keyvaluepairs[index] = value
  90. end
  91. end
  92. function dictionary.trySetValue(dic, key, value, ...)
  93. return pcall(dictionary.setValue, dic, key, value, ...)
  94. end
  95. return dictionary