variadic remove for builder

This commit is contained in:
BlackMATov
2025-04-19 14:53:11 +07:00
parent e3d0cbded2
commit 4d95bcdb33
3 changed files with 110 additions and 93 deletions

View File

@@ -124,7 +124,7 @@ builder:has_all :: fragment... -> boolean
builder:has_any :: fragment... -> boolean
builder:get :: fragment... -> component...
builder:set :: fragment, component -> builder
builder:remove :: fragment -> builder
builder:remove :: fragment... -> builder
builder:clear :: builder
builder:tag :: builder
builder:name :: string -> builder
@@ -145,7 +145,7 @@ builder:prologue :: {} -> builder
builder:epilogue :: {} -> builder
builder:disabled :: builder
builder:destroy_policy :: id -> builder
builder:build :: entity
builder:build :: boolean -> entity
```
## [License (MIT)](./LICENSE.md)

View File

@@ -8789,3 +8789,75 @@ do
end
end
end
do
local f1, f2, f3 = evo.id(3)
do
local b = evo.builder():set(f1, 11):set(f2, 22)
assert(b == b:remove(f1))
assert(not b:has(f1) and b:get(f1) == nil)
assert(b:has(f2) and b:get(f2) == 22)
local e = b:build()
assert(not evo.has(e, f1) and evo.get(e, f1) == nil)
assert(evo.has(e, f2) and evo.get(e, f2) == 22)
end
do
local b = evo.builder():set(f1, 11):set(f2, 22)
assert(b == b:remove(f3, f1))
assert(not b:has(f1) and b:get(f1) == nil)
assert(b:has(f2) and b:get(f2) == 22)
local e = b:build()
assert(not evo.has(e, f1) and evo.get(e, f1) == nil)
assert(evo.has(e, f2) and evo.get(e, f2) == 22)
end
do
local b = evo.builder():set(f1, 11):set(f2, 22)
assert(b == b:remove(f1, f2))
assert(not b:has(f1) and b:get(f1) == nil)
assert(not b:has(f2) and b:get(f2) == nil)
local e = b:build()
assert(not evo.has(e, f1) and evo.get(e, f1) == nil)
assert(not evo.has(e, f2) and evo.get(e, f2) == nil)
end
do
local b = evo.builder():set(f1, 11):set(f2, 22)
assert(b == b:remove(f2, f1, f1))
assert(not b:has(f1) and b:get(f1) == nil)
assert(not b:has(f2) and b:get(f2) == nil)
local e = b:build()
assert(not evo.has(e, f1) and evo.get(e, f1) == nil)
assert(not evo.has(e, f2) and evo.get(e, f2) == nil)
end
end
do
local f1 = evo.id(1)
do
local b = evo.builder():set(f1, 11):single(1)
local e1 = b:build(true)
assert(evo.has(e1, e1) and evo.get(e1, e1) == 1)
assert(evo.has(e1, f1) and evo.get(e1, f1) == 11)
assert(not b:has(e1) and b:get(e1) == nil)
local e2 = b:build()
assert(not evo.has(e2, e1) and evo.get(e2, e1) == nil)
assert(evo.has(e2, e2) and evo.get(e2, e2) == 1)
assert(evo.has(e2, f1) and evo.get(e2, f1) == 11)
local e3 = b:build()
assert(not evo.has(e3, e1) and evo.get(e3, e1) == nil)
assert(not evo.has(e3, e2) and evo.get(e3, e2) == nil)
assert(not evo.has(e3, e3) and evo.get(e3, e3) == nil)
assert(not evo.has(e3, f1) and evo.get(e3, f1) == nil)
end
end

View File

@@ -6230,34 +6230,7 @@ function __evolved_builder_mt:has_all(...)
return true
end
local has = self.has
local has_all = self.has_all
if fragment_count == 1 then
local f1 = ...
return has(self, f1)
end
if fragment_count == 2 then
local f1, f2 = ...
return has(self, f1) and has(self, f2)
end
if fragment_count == 3 then
local f1, f2, f3 = ...
return has(self, f1) and has(self, f2) and has(self, f3)
end
if fragment_count == 4 then
local f1, f2, f3, f4 = ...
return has(self, f1) and has(self, f2) and has(self, f3) and has(self, f4)
end
do
local f1, f2, f3, f4 = ...
return has(self, f1) and has(self, f2) and has(self, f3) and has(self, f4) and
has_all(self, __lua_select(5, ...))
end
return self:has(...) and self:has_all(__lua_select(2, ...))
end
---@param ... evolved.fragment fragments
@@ -6270,34 +6243,7 @@ function __evolved_builder_mt:has_any(...)
return false
end
local has = self.has
local has_any = self.has_any
if fragment_count == 1 then
local f1 = ...
return has(self, f1)
end
if fragment_count == 2 then
local f1, f2 = ...
return has(self, f1) or has(self, f2)
end
if fragment_count == 3 then
local f1, f2, f3 = ...
return has(self, f1) or has(self, f2) or has(self, f3)
end
if fragment_count == 4 then
local f1, f2, f3, f4 = ...
return has(self, f1) or has(self, f2) or has(self, f3) or has(self, f4)
end
do
local f1, f2, f3, f4 = ...
return has(self, f1) or has(self, f2) or has(self, f3) or has(self, f4) or
has_any(self, __lua_select(5, ...))
end
return self:has(...) or self:has_any(__lua_select(2, ...))
end
---@param ... evolved.fragment fragments
@@ -6310,25 +6256,23 @@ function __evolved_builder_mt:get(...)
return
end
local get = self.get
local fragment = ...
local component_index = self.__fragment_set[fragment]
if not component_index then
return nil, get(self, __lua_select(2, ...))
return nil, self:get(__lua_select(2, ...))
end
if component_index > self.__component_count then
return nil, get(self, __lua_select(2, ...))
return nil, self:get(__lua_select(2, ...))
end
if fragment ~= self.__fragment_list[component_index] then
return nil, get(self, __lua_select(2, ...))
return nil, self:get(__lua_select(2, ...))
end
return self.__component_list[component_index], get(self, __lua_select(2, ...))
return self.__component_list[component_index], self:get(__lua_select(2, ...))
end
---@param fragment evolved.fragment
@@ -6381,9 +6325,17 @@ function __evolved_builder_mt:set(fragment, component)
return self
end
---@param fragment evolved.fragment
---@param ... evolved.fragment fragments
---@return evolved.builder builder
function __evolved_builder_mt:remove(fragment)
function __evolved_builder_mt:remove(...)
local fragment_count = select("#", ...)
if fragment_count == 0 then
return self
end
local fragment = ...
local fragment_set = self.__fragment_set
local fragment_list = self.__fragment_list
local component_list = self.__component_list
@@ -6391,35 +6343,28 @@ function __evolved_builder_mt:remove(fragment)
local component_index = fragment_set[fragment]
if not component_index then
return self
if component_index
and component_index <= component_count
and fragment == fragment_list[component_index]
then
if component_index ~= component_count then
local last_fragment = fragment_list[component_count]
local last_component = component_list[component_count]
fragment_set[last_fragment] = component_index
fragment_list[component_index] = last_fragment
component_list[component_index] = last_component
end
fragment_set[fragment] = nil
fragment_list[component_count] = nil
component_list[component_count] = nil
component_count = component_count - 1
self.__component_count = component_count
end
if component_index > component_count then
return self
end
if fragment ~= fragment_list[component_index] then
return self
end
if component_index ~= component_count then
local last_fragment = fragment_list[component_count]
local last_component = component_list[component_count]
fragment_set[last_fragment] = component_index
fragment_list[component_index] = last_fragment
component_list[component_index] = last_component
end
fragment_set[fragment] = nil
fragment_list[component_count] = nil
component_list[component_count] = nil
component_count = component_count - 1
self.__component_count = component_count
return self
return self:remove(__lua_select(2, ...))
end
---@return evolved.builder builder