From 1294aa98b844952937ea48377e6c90461f77b685 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Fri, 18 Apr 2025 15:12:11 +0700 Subject: [PATCH] builder:has_all/any --- README.md | 2 ++ develop/untests.lua | 18 ++++++++++ evolved.lua | 80 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) diff --git a/README.md b/README.md index 15418f5..d62fb46 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,8 @@ collect_garbage :: () ``` builder :: builder builder:has :: fragment -> boolean +builder:has_all :: fragment... -> boolean +builder:has_any :: fragment... -> boolean builder:get :: fragment -> component builder:set :: fragment, component -> builder builder:remove :: fragment -> builder diff --git a/develop/untests.lua b/develop/untests.lua index 57dbdc6..aa5e972 100644 --- a/develop/untests.lua +++ b/develop/untests.lua @@ -8622,9 +8622,15 @@ do do 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(f2) and b:get(f2) == nil) + assert(not b:has_all(f1, f2)) + assert(not b:has_any(f1, f2)) + do local e = b:build(true) @@ -8637,6 +8643,9 @@ do assert(b:has(f1) and b:get(f1) == 41) assert(not b:has(f2) and b:get(f2) == nil) + assert(not b:has_all(f1, f2)) + assert(b:has_any(f1, f2)) + do local e = b:build(true) @@ -8649,6 +8658,9 @@ do assert(b:has(f1) and b:get(f1) == 41) assert(b:has(f2) and b:get(f2) == 42) + assert(b:has_all(f1, f2)) + assert(b:has_any(f1, f2)) + do local e = b:build(true) @@ -8661,6 +8673,9 @@ do assert(not b:has(f1) and b:get(f1) == nil) assert(b:has(f2) and b:get(f2) == 42) + assert(not b:has_all(f1, f2)) + assert(b:has_any(f1, f2)) + do local e = b:build(true) @@ -8670,6 +8685,9 @@ do 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(f2) and b:get(f2) == nil) diff --git a/evolved.lua b/evolved.lua index 77b02a8..c5dedb3 100644 --- a/evolved.lua +++ b/evolved.lua @@ -6220,6 +6220,86 @@ function __evolved_builder_mt:has(fragment) return true 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 ---@return evolved.component? ---@nodiscard