tableList_Tips.lua 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. ---@class tableList @注释 用来减少table的不停创建.
  2. ---注意self.list里面的元素不能重复,不然操作引用会引起问题
  3. tableList = class()
  4. local this = tableList
  5. function this:ctor()
  6. self.n = 0
  7. self.list = {}
  8. end
  9. function this:Add(data)
  10. self.n = self.n+1
  11. self.list[self.n] = data
  12. end
  13. function this:RemoveAt(pos)
  14. --超过当前数量,跳过删除逻辑
  15. if pos >self.n then
  16. return
  17. end
  18. table.remove(self.list, pos)
  19. self.n = self.n - 1
  20. end
  21. function this:Insert(pos,data)
  22. if pos>self.n then
  23. return
  24. end
  25. table.insert(self.list,pos,data)
  26. self.n = self.n+1
  27. end
  28. ---@return any @其他地方不能直接持有引用,不然外部其他地方的引用和self.list里面的引用重复,处理数据会导致异常
  29. function this:Get(pos)
  30. if pos > self.n then
  31. return nil
  32. end
  33. return self.list[pos]
  34. end
  35. function this:GetLast()
  36. return self.list[self.n]
  37. end
  38. ---@return any @GetCache返回的值要再使用的话,要通过Add或者其他地方不能直接持有引用,不然外部其他地方的引用和self.list里面的引用重复,处理数据会导致异常
  39. function this:GetCache()
  40. return self.list[self.n+1]
  41. end
  42. function this:Count()
  43. return self.n
  44. end
  45. function this:Clear()
  46. self.n = 0
  47. end
  48. function this:Release()
  49. self.list = {}
  50. self.n = 0
  51. end