---@class List List = class() function List:ctor() end function List:Add(item) table.insert(self, item) end function List:Clear() local count = self:Count() for i=count,1,-1 do table.remove(self) end end function List:Contains(item) local count = self:Count() for i= 1, count do if self[i] == item then return true end end return false end function List:Count() return table.length(self) end function List:Find(predicate) if (predicate == nil or type(predicate) ~= 'function') then print('predicate is invalid!') return end local count = self:Count() for i=1,count do if predicate(self[i]) then return self[i] end end return nil end function List:ForEach(action) if (action == nil or type(action) ~= 'function') then print('action is invalid!') return end local count = self:Count() for i=1,count do action(self[i]) end end function List:IndexOf(item) local count = self:Count() for i=1,count do if self[i] == item then return i end end return 0 end function List:LastIndexOf(item) local count = self:Count() for i=count,1,-1 do if self[i] == item then return i end end return 0 end function List:Insert(index, item) table.insert(self, index, item) end function List:Remove(item) local idx = self:LastIndexOf(item) if (idx > 0) then table.remove(self, idx) self:Remove(item) end end function List:RemoveAt(index) return table.remove(self, index) end function List:Sort(comparison) if (comparison ~= nil and type(comparison) ~= 'function') then print('comparison is invalid') return end if comparison == nil then table.sort(self) else table.sort(self, comparison) end end
LuaList
最新推荐文章于 2023-12-17 16:53:11 发布