Skip to content

Commit 4d95bcd

Browse files
committed
variadic remove for builder
1 parent e3d0cbd commit 4d95bcd

3 files changed

Lines changed: 107 additions & 90 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ builder:has_all :: fragment... -> boolean
124124
builder:has_any :: fragment... -> boolean
125125
builder:get :: fragment... -> component...
126126
builder:set :: fragment, component -> builder
127-
builder:remove :: fragment -> builder
127+
builder:remove :: fragment... -> builder
128128
builder:clear :: builder
129129
builder:tag :: builder
130130
builder:name :: string -> builder
@@ -145,7 +145,7 @@ builder:prologue :: {} -> builder
145145
builder:epilogue :: {} -> builder
146146
builder:disabled :: builder
147147
builder:destroy_policy :: id -> builder
148-
builder:build :: entity
148+
builder:build :: boolean -> entity
149149
```
150150

151151
## [License (MIT)](./LICENSE.md)

develop/untests.lua

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8789,3 +8789,75 @@ do
87898789
end
87908790
end
87918791
end
8792+
8793+
do
8794+
local f1, f2, f3 = evo.id(3)
8795+
8796+
do
8797+
local b = evo.builder():set(f1, 11):set(f2, 22)
8798+
assert(b == b:remove(f1))
8799+
assert(not b:has(f1) and b:get(f1) == nil)
8800+
assert(b:has(f2) and b:get(f2) == 22)
8801+
local e = b:build()
8802+
assert(not evo.has(e, f1) and evo.get(e, f1) == nil)
8803+
assert(evo.has(e, f2) and evo.get(e, f2) == 22)
8804+
end
8805+
8806+
do
8807+
local b = evo.builder():set(f1, 11):set(f2, 22)
8808+
assert(b == b:remove(f3, f1))
8809+
assert(not b:has(f1) and b:get(f1) == nil)
8810+
assert(b:has(f2) and b:get(f2) == 22)
8811+
local e = b:build()
8812+
assert(not evo.has(e, f1) and evo.get(e, f1) == nil)
8813+
assert(evo.has(e, f2) and evo.get(e, f2) == 22)
8814+
end
8815+
8816+
do
8817+
local b = evo.builder():set(f1, 11):set(f2, 22)
8818+
assert(b == b:remove(f1, f2))
8819+
assert(not b:has(f1) and b:get(f1) == nil)
8820+
assert(not b:has(f2) and b:get(f2) == nil)
8821+
local e = b:build()
8822+
assert(not evo.has(e, f1) and evo.get(e, f1) == nil)
8823+
assert(not evo.has(e, f2) and evo.get(e, f2) == nil)
8824+
end
8825+
8826+
do
8827+
local b = evo.builder():set(f1, 11):set(f2, 22)
8828+
assert(b == b:remove(f2, f1, f1))
8829+
assert(not b:has(f1) and b:get(f1) == nil)
8830+
assert(not b:has(f2) and b:get(f2) == nil)
8831+
local e = b:build()
8832+
assert(not evo.has(e, f1) and evo.get(e, f1) == nil)
8833+
assert(not evo.has(e, f2) and evo.get(e, f2) == nil)
8834+
end
8835+
end
8836+
8837+
do
8838+
local f1 = evo.id(1)
8839+
8840+
do
8841+
local b = evo.builder():set(f1, 11):single(1)
8842+
8843+
local e1 = b:build(true)
8844+
assert(evo.has(e1, e1) and evo.get(e1, e1) == 1)
8845+
assert(evo.has(e1, f1) and evo.get(e1, f1) == 11)
8846+
8847+
assert(not b:has(e1) and b:get(e1) == nil)
8848+
8849+
local e2 = b:build()
8850+
8851+
assert(not evo.has(e2, e1) and evo.get(e2, e1) == nil)
8852+
8853+
assert(evo.has(e2, e2) and evo.get(e2, e2) == 1)
8854+
assert(evo.has(e2, f1) and evo.get(e2, f1) == 11)
8855+
8856+
local e3 = b:build()
8857+
8858+
assert(not evo.has(e3, e1) and evo.get(e3, e1) == nil)
8859+
assert(not evo.has(e3, e2) and evo.get(e3, e2) == nil)
8860+
assert(not evo.has(e3, e3) and evo.get(e3, e3) == nil)
8861+
assert(not evo.has(e3, f1) and evo.get(e3, f1) == nil)
8862+
end
8863+
end

evolved.lua

Lines changed: 33 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -6230,34 +6230,7 @@ function __evolved_builder_mt:has_all(...)
62306230
return true
62316231
end
62326232

6233-
local has = self.has
6234-
local has_all = self.has_all
6235-
6236-
if fragment_count == 1 then
6237-
local f1 = ...
6238-
return has(self, f1)
6239-
end
6240-
6241-
if fragment_count == 2 then
6242-
local f1, f2 = ...
6243-
return has(self, f1) and has(self, f2)
6244-
end
6245-
6246-
if fragment_count == 3 then
6247-
local f1, f2, f3 = ...
6248-
return has(self, f1) and has(self, f2) and has(self, f3)
6249-
end
6250-
6251-
if fragment_count == 4 then
6252-
local f1, f2, f3, f4 = ...
6253-
return has(self, f1) and has(self, f2) and has(self, f3) and has(self, f4)
6254-
end
6255-
6256-
do
6257-
local f1, f2, f3, f4 = ...
6258-
return has(self, f1) and has(self, f2) and has(self, f3) and has(self, f4) and
6259-
has_all(self, __lua_select(5, ...))
6260-
end
6233+
return self:has(...) and self:has_all(__lua_select(2, ...))
62616234
end
62626235

62636236
---@param ... evolved.fragment fragments
@@ -6270,34 +6243,7 @@ function __evolved_builder_mt:has_any(...)
62706243
return false
62716244
end
62726245

6273-
local has = self.has
6274-
local has_any = self.has_any
6275-
6276-
if fragment_count == 1 then
6277-
local f1 = ...
6278-
return has(self, f1)
6279-
end
6280-
6281-
if fragment_count == 2 then
6282-
local f1, f2 = ...
6283-
return has(self, f1) or has(self, f2)
6284-
end
6285-
6286-
if fragment_count == 3 then
6287-
local f1, f2, f3 = ...
6288-
return has(self, f1) or has(self, f2) or has(self, f3)
6289-
end
6290-
6291-
if fragment_count == 4 then
6292-
local f1, f2, f3, f4 = ...
6293-
return has(self, f1) or has(self, f2) or has(self, f3) or has(self, f4)
6294-
end
6295-
6296-
do
6297-
local f1, f2, f3, f4 = ...
6298-
return has(self, f1) or has(self, f2) or has(self, f3) or has(self, f4) or
6299-
has_any(self, __lua_select(5, ...))
6300-
end
6246+
return self:has(...) or self:has_any(__lua_select(2, ...))
63016247
end
63026248

63036249
---@param ... evolved.fragment fragments
@@ -6310,25 +6256,23 @@ function __evolved_builder_mt:get(...)
63106256
return
63116257
end
63126258

6313-
local get = self.get
6314-
63156259
local fragment = ...
63166260

63176261
local component_index = self.__fragment_set[fragment]
63186262

63196263
if not component_index then
6320-
return nil, get(self, __lua_select(2, ...))
6264+
return nil, self:get(__lua_select(2, ...))
63216265
end
63226266

63236267
if component_index > self.__component_count then
6324-
return nil, get(self, __lua_select(2, ...))
6268+
return nil, self:get(__lua_select(2, ...))
63256269
end
63266270

63276271
if fragment ~= self.__fragment_list[component_index] then
6328-
return nil, get(self, __lua_select(2, ...))
6272+
return nil, self:get(__lua_select(2, ...))
63296273
end
63306274

6331-
return self.__component_list[component_index], get(self, __lua_select(2, ...))
6275+
return self.__component_list[component_index], self:get(__lua_select(2, ...))
63326276
end
63336277

63346278
---@param fragment evolved.fragment
@@ -6381,45 +6325,46 @@ function __evolved_builder_mt:set(fragment, component)
63816325
return self
63826326
end
63836327

6384-
---@param fragment evolved.fragment
6328+
---@param ... evolved.fragment fragments
63856329
---@return evolved.builder builder
6386-
function __evolved_builder_mt:remove(fragment)
6330+
function __evolved_builder_mt:remove(...)
6331+
local fragment_count = select("#", ...)
6332+
6333+
if fragment_count == 0 then
6334+
return self
6335+
end
6336+
6337+
local fragment = ...
6338+
63876339
local fragment_set = self.__fragment_set
63886340
local fragment_list = self.__fragment_list
63896341
local component_list = self.__component_list
63906342
local component_count = self.__component_count
63916343

63926344
local component_index = fragment_set[fragment]
63936345

6394-
if not component_index then
6395-
return self
6396-
end
6397-
6398-
if component_index > component_count then
6399-
return self
6400-
end
6346+
if component_index
6347+
and component_index <= component_count
6348+
and fragment == fragment_list[component_index]
6349+
then
6350+
if component_index ~= component_count then
6351+
local last_fragment = fragment_list[component_count]
6352+
local last_component = component_list[component_count]
64016353

6402-
if fragment ~= fragment_list[component_index] then
6403-
return self
6404-
end
6354+
fragment_set[last_fragment] = component_index
6355+
fragment_list[component_index] = last_fragment
6356+
component_list[component_index] = last_component
6357+
end
64056358

6406-
if component_index ~= component_count then
6407-
local last_fragment = fragment_list[component_count]
6408-
local last_component = component_list[component_count]
6359+
fragment_set[fragment] = nil
6360+
fragment_list[component_count] = nil
6361+
component_list[component_count] = nil
64096362

6410-
fragment_set[last_fragment] = component_index
6411-
fragment_list[component_index] = last_fragment
6412-
component_list[component_index] = last_component
6363+
component_count = component_count - 1
6364+
self.__component_count = component_count
64136365
end
64146366

6415-
fragment_set[fragment] = nil
6416-
fragment_list[component_count] = nil
6417-
component_list[component_count] = nil
6418-
6419-
component_count = component_count - 1
6420-
self.__component_count = component_count
6421-
6422-
return self
6367+
return self:remove(__lua_select(2, ...))
64236368
end
64246369

64256370
---@return evolved.builder builder

0 commit comments

Comments
 (0)