|
|
|
@ -4,6 +4,8 @@ local Tinkr, |
|
|
|
|
Bastion = ... |
|
|
|
|
|
|
|
|
|
---@class Bastion.List<I>: { _list: I[] } |
|
|
|
|
---@field _list any[] |
|
|
|
|
---@field indexes table<number, { key?: string|number, index: number}> |
|
|
|
|
---@operator add(any): Bastion.List |
|
|
|
|
---@operator sub(any): Bastion.List |
|
|
|
|
local List = { |
|
|
|
@ -26,12 +28,27 @@ local List = { |
|
|
|
|
self:remove(value) |
|
|
|
|
return self |
|
|
|
|
end, |
|
|
|
|
---@param a Bastion.List |
|
|
|
|
---@param b Bastion.List |
|
|
|
|
__concat = function(a, b) |
|
|
|
|
return a:concat(b) |
|
|
|
|
end, |
|
|
|
|
|
|
|
|
|
---@param self Bastion.List |
|
|
|
|
__tostring = function(self) |
|
|
|
|
return self:toString() |
|
|
|
|
end, |
|
|
|
|
} |
|
|
|
|
List.__index = List |
|
|
|
|
|
|
|
|
|
---@param k string|number |
|
|
|
|
function List:__index(k) |
|
|
|
|
if List[k] ~= nil then |
|
|
|
|
return List[k] |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
local index = (self.indexes[k] and type(self.indexes[k].key) ~= "nil") and self.indexes[k].index or false |
|
|
|
|
|
|
|
|
|
return index and self._list[index] or self._list[k] |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
---@generic I : table |
|
|
|
|
---@param from? I[] |
|
|
|
@ -39,16 +56,25 @@ List.__index = List |
|
|
|
|
function List:New(from) |
|
|
|
|
local self = setmetatable({}, List) |
|
|
|
|
self._list = from or {} |
|
|
|
|
self.indexes = {} |
|
|
|
|
return self |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
---@param value any |
|
|
|
|
function List:push(value) |
|
|
|
|
---@param key? string |
|
|
|
|
function List:push(value, key) |
|
|
|
|
local index = #self._list + 1 |
|
|
|
|
table.insert(self._list, value) |
|
|
|
|
|
|
|
|
|
self.indexes[#self._list] = { |
|
|
|
|
key = key, |
|
|
|
|
index = index |
|
|
|
|
} |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
---@return any |
|
|
|
|
function List:pop() |
|
|
|
|
self.indexes[#self._list] = nil |
|
|
|
|
return table.remove(self._list) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
@ -65,9 +91,10 @@ end |
|
|
|
|
---@return nil |
|
|
|
|
function List:clear() |
|
|
|
|
self._list = {} |
|
|
|
|
self.indexes = {} |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
---@generic I : any |
|
|
|
|
---@generic I |
|
|
|
|
---@param value I |
|
|
|
|
---@return boolean |
|
|
|
|
function List:contains(value) |
|
|
|
@ -81,15 +108,15 @@ end |
|
|
|
|
|
|
|
|
|
---@generic I : any |
|
|
|
|
---@param value I |
|
|
|
|
---@return boolean |
|
|
|
|
function List:remove(value) |
|
|
|
|
local newList = List:New() |
|
|
|
|
for i, v in ipairs(self._list) do |
|
|
|
|
if v == value then |
|
|
|
|
table.remove(self._list, i) |
|
|
|
|
return true |
|
|
|
|
if v ~= value then |
|
|
|
|
newList:push(self._list, |
|
|
|
|
type(self.indexes[i].key) == "string" and self.indexes[i].key --[[@as string]] or nil) |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
return false |
|
|
|
|
return newList |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
---@generic I : any |
|
|
|
@ -107,8 +134,8 @@ end |
|
|
|
|
---@return Bastion.List<I> | Bastion.List |
|
|
|
|
function List:map(callback) |
|
|
|
|
local newList = List:New() |
|
|
|
|
for _, v in ipairs(self._list) do |
|
|
|
|
newList:push(callback(v)) |
|
|
|
|
for i, v in ipairs(self._list) do |
|
|
|
|
newList:push(callback(v), type(self.indexes[i].key) == "string" and self.indexes[i].key --[[@as string]] or nil) |
|
|
|
|
end |
|
|
|
|
return newList |
|
|
|
|
end |
|
|
|
@ -118,9 +145,10 @@ end |
|
|
|
|
---@return Bastion.List<I> | Bastion.List |
|
|
|
|
function List:filter(callback) |
|
|
|
|
local newList = List:New() |
|
|
|
|
for _, v in ipairs(self._list) do |
|
|
|
|
for i, v in ipairs(self._list) do |
|
|
|
|
if callback(v) then |
|
|
|
|
newList:push(v) |
|
|
|
|
newList:push(v, |
|
|
|
|
type(self.indexes[i].key) == "string" and self.indexes[i].key --[[@as string]] or nil) |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
return newList |
|
|
|
@ -167,6 +195,15 @@ function List:findIndex(callback) |
|
|
|
|
return nil |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
---@param key number|string |
|
|
|
|
function List:findByKey(key) |
|
|
|
|
for i, v in ipairs(self._list) do |
|
|
|
|
if self.indexes[i] and ((self.indexes[i].key and self.indexes[i].key == key) or (self.indexes[i].index == key)) then |
|
|
|
|
return v |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
---@generic I : any |
|
|
|
|
---@param callback fun(a: I, b: I): boolean |
|
|
|
|
---@return nil |
|
|
|
@ -177,15 +214,15 @@ end |
|
|
|
|
function List:reverse() |
|
|
|
|
local newList = List:New() |
|
|
|
|
for i = #self._list, 1, -1 do |
|
|
|
|
newList:push(self._list[i]) |
|
|
|
|
newList:push(self._list[i], type(self.indexes[i].key) == "string" and self.indexes[i].key --[[@as string]] or nil) |
|
|
|
|
end |
|
|
|
|
return newList |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
function List:clone() |
|
|
|
|
local newList = List:New() |
|
|
|
|
for _, v in ipairs(self._list) do |
|
|
|
|
newList:push(v) |
|
|
|
|
for i, v in ipairs(self._list) do |
|
|
|
|
newList:push(v, type(self.indexes[i].key) == "string" and self.indexes[i].key --[[@as string]] or nil) |
|
|
|
|
end |
|
|
|
|
return newList |
|
|
|
|
end |
|
|
|
@ -193,11 +230,11 @@ end |
|
|
|
|
---@param list Bastion.List |
|
|
|
|
function List:concat(list) |
|
|
|
|
local newList = List:New() |
|
|
|
|
for _, v in ipairs(self._list) do |
|
|
|
|
newList:push(v) |
|
|
|
|
for i, v in ipairs(self._list) do |
|
|
|
|
newList:push(v, type(self.indexes[i].key) == "string" and self.indexes[i].key --[[@as string]] or nil) |
|
|
|
|
end |
|
|
|
|
for _, v in ipairs(list._list) do |
|
|
|
|
newList:push(v) |
|
|
|
|
for i, v in ipairs(list._list) do |
|
|
|
|
newList:push(v, type(self.indexes[i].key) == "string" and self.indexes[i].key --[[@as string]] or nil) |
|
|
|
|
end |
|
|
|
|
return newList |
|
|
|
|
end |
|
|
|
@ -220,9 +257,4 @@ function List:toString() |
|
|
|
|
return self:join(", ") |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
---@return string |
|
|
|
|
function List:__tostring() |
|
|
|
|
return self:toString() |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
Bastion.List = List |
|
|
|
|