123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- ---
- --- Generated by EmmyLua(https://github.com/EmmyLua)
- --- Created by PZM.
- --- DateTime: 2021/10/11 13:16
- ---@class TablePool
- ---@field list table[]
- ---@field isPushNotCheckContain boolean @不填的时候,要保证对象没有__eq操作。填的时候,例如Vector3的对象池,要保证外部不会重复添加,不然Pop出来的数据可能是同一个
- TablePool = class()
- local this = TablePool
- function this:ctor(isPushNotCheckContain)
- self.isPushNotCheckContain = isPushNotCheckContain
- self.list = {}
- end
- ---@return table @type 和 data不填,返回{}表
- ---@param t table @类型名
- ---@param ctorData any @构造函数参数
- function this:Pop(t, ctorData, isPopFirst)
- local count =self:Count()
- if count == 0 then
- return t and t(ctorData) or {},true
- end
- return table.remove(self.list, isPopFirst and 1 or count),false
- end
- function this:PopByCondition(t,data,func)
- for k, v in pairs(self.list) do
- if func and func(v) then
- return table.remove(self.list,k)
- end
- end
- return t and t(data) or {}
- end
- function this:Count()
- return #self.list
- end
- function this:Foreach(func)
- for k, v in pairs(self.list) do
- if func then
- func(v)
- end
- end
- end
- ---@param t table @Pop返回的对象
- ---@param isPushFirstPos boolean @是否放入第一个位置
- ---@return boolean @是否 push 成功,重复的不添加
- function this:Push(t,isPushFirstPos)
- --外部多次添加一个对象,table.contains可能会引起重载_eq对象或者数据量大的遍历消耗
- if not self.isPushNotCheckContain then
- if table.contains(self.list,t) then
- --logError('重复添加,数据会异常,保证调用Push后置空')
- return false
- end
- end
- if isPushFirstPos then
- table.insert(self.list,1,t)
- else
- table.insert(self.list,t)
- end
- return true
- end
- function this:Clear()
- if self:Count()==0 then
- return
- end
- table.clear(self.list)
- end
- function this:Release()
- self.list = {}
- end
- function this:PushIncludeSubTable(t,isPushFirstPos,isClear)
- if type(t)~='table' then
- return
- end
- for k, v in pairs(t) do
- self:PushIncludeSubTable(v,isPushFirstPos,isClear)
- end
- if isClear then
- table.clear(t)
- end
- self:Push(t,isPushFirstPos)
- end
|