BattleDictionary.lua 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. BattleDictionary = {}
  2. function BattleDictionary.New()
  3. local o = {}
  4. setmetatable(o, BattleDictionary)
  5. BattleDictionary.__index = BattleDictionary
  6. o.kL = BattleList.New()
  7. o.vL = BattleList.New()
  8. o.kList = o.kL.buffer
  9. o.vList = o.vL.buffer
  10. o.kvList = {}
  11. o.size = 0
  12. return o
  13. end
  14. function BattleDictionary:Add(k,v)
  15. if not self.kvList[k] then
  16. self.size = self.size + 1
  17. self.kvList[k] = v
  18. self.kL:Add(k)
  19. self.vL:Add(v)
  20. end
  21. end
  22. function BattleDictionary:Remove(k)
  23. if self.kvList[k] then
  24. for i=1, self.size do
  25. if self.vList[i] == self.kvList[k] then
  26. self.size = self.size - 1
  27. self.kL:Remove(i)
  28. self.vL:Remove(i)
  29. self.kvList[k] = nil
  30. break
  31. end
  32. end
  33. end
  34. end
  35. function BattleDictionary:Get(k)
  36. return self.kvList[k]
  37. end
  38. function BattleDictionary:Set(k,v)
  39. self:Remove(k)
  40. self:Add(k,v)
  41. end
  42. function BattleDictionary:Clear()
  43. self.kL:Clear()
  44. self.vL:Clear()
  45. self.kvList = {}
  46. self.size = 0
  47. end
  48. function BattleDictionary:Count()
  49. return self.size
  50. end
  51. function BattleDictionary:Foreach(func)
  52. for i = 1, self.size do
  53. if func then
  54. if func(self.kList[i], self.vList[i]) == "break" then
  55. break
  56. end
  57. end
  58. end
  59. end
  60. -- check
  61. function BattleDictionary:Find(obj,ICompare)
  62. for i = 1, self.size do
  63. if ICompare ~=nil then
  64. if ICompare(obj, self.vList[i]) then
  65. return self.vList[i]
  66. end
  67. end
  68. end
  69. end
  70. function BattleDictionary:ContainsValue(_v)
  71. for i = 1, self.size do
  72. if self.vList[i] == _v then return true end
  73. end
  74. return false
  75. end
  76. function BattleDictionary:Contains(_k)
  77. for i = 1, self.size do
  78. if self.kList[i] == _k then return true end
  79. end
  80. return false
  81. end
  82. -- 浅克隆
  83. function BattleDictionary:Clone()
  84. local _dic = BattleDictionary.New()
  85. for i = 1, #self.kvList do
  86. _dic.kvList[i] = self.kvList[i]
  87. _dic.kL:Add(i)
  88. _dic.vL:Add(self.kvList[i])
  89. end
  90. _dic.size = self.size
  91. return _dic
  92. end