builder:has_all/any

This commit is contained in:
BlackMATov
2025-04-18 15:12:11 +07:00
parent 63172bb84d
commit 1294aa98b8
3 changed files with 100 additions and 0 deletions

View File

@@ -120,6 +120,8 @@ collect_garbage :: ()
``` ```
builder :: builder builder :: builder
builder:has :: fragment -> boolean builder:has :: fragment -> boolean
builder:has_all :: fragment... -> boolean
builder:has_any :: fragment... -> boolean
builder:get :: fragment -> component builder:get :: fragment -> component
builder:set :: fragment, component -> builder builder:set :: fragment, component -> builder
builder:remove :: fragment -> builder builder:remove :: fragment -> builder

View File

@@ -8622,9 +8622,15 @@ do
do do
local b = evo.builder() local b = evo.builder()
assert(b:has_all())
assert(not b:has_any())
assert(not b:has(f1) and b:get(f1) == nil) assert(not b:has(f1) and b:get(f1) == nil)
assert(not b:has(f2) and b:get(f2) == nil) assert(not b:has(f2) and b:get(f2) == nil)
assert(not b:has_all(f1, f2))
assert(not b:has_any(f1, f2))
do do
local e = b:build(true) local e = b:build(true)
@@ -8637,6 +8643,9 @@ do
assert(b:has(f1) and b:get(f1) == 41) assert(b:has(f1) and b:get(f1) == 41)
assert(not b:has(f2) and b:get(f2) == nil) assert(not b:has(f2) and b:get(f2) == nil)
assert(not b:has_all(f1, f2))
assert(b:has_any(f1, f2))
do do
local e = b:build(true) local e = b:build(true)
@@ -8649,6 +8658,9 @@ do
assert(b:has(f1) and b:get(f1) == 41) assert(b:has(f1) and b:get(f1) == 41)
assert(b:has(f2) and b:get(f2) == 42) assert(b:has(f2) and b:get(f2) == 42)
assert(b:has_all(f1, f2))
assert(b:has_any(f1, f2))
do do
local e = b:build(true) local e = b:build(true)
@@ -8661,6 +8673,9 @@ do
assert(not b:has(f1) and b:get(f1) == nil) assert(not b:has(f1) and b:get(f1) == nil)
assert(b:has(f2) and b:get(f2) == 42) assert(b:has(f2) and b:get(f2) == 42)
assert(not b:has_all(f1, f2))
assert(b:has_any(f1, f2))
do do
local e = b:build(true) local e = b:build(true)
@@ -8670,6 +8685,9 @@ do
b:remove(f2) b:remove(f2)
assert(not b:has_all(f1, f2))
assert(not b:has_any(f1, f2))
assert(not b:has(f1) and b:get(f1) == nil) assert(not b:has(f1) and b:get(f1) == nil)
assert(not b:has(f2) and b:get(f2) == nil) assert(not b:has(f2) and b:get(f2) == nil)

View File

@@ -6220,6 +6220,86 @@ function __evolved_builder_mt:has(fragment)
return true return true
end end
---@param ... evolved.fragment fragments
---@return boolean
---@nodiscard
function __evolved_builder_mt:has_all(...)
local fragment_count = select("#", ...)
if fragment_count == 0 then
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
end
---@param ... evolved.fragment fragments
---@return boolean
---@nodiscard
function __evolved_builder_mt:has_any(...)
local fragment_count = select("#", ...)
if fragment_count == 0 then
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
end
---@param fragment evolved.fragment ---@param fragment evolved.fragment
---@return evolved.component? ---@return evolved.component?
---@nodiscard ---@nodiscard