Implement levels:anchor_in as a generator. (#4)

This gets rid of the (substantial!) overhead of preparing a list and then iterating over it, in exchange for a bit of bookkeeping to simulate the prior behavior of the for loop, which is much cheaper.

Reviewed-on: pyrex/chameleonic#4
Co-authored-by: Kistaro Windrider <kistaro@gmail.com>
Co-committed-by: Kistaro Windrider <kistaro@gmail.com>
This commit is contained in:
Kistaro Windrider 2022-12-18 01:46:30 +00:00 committed by Pyrex
parent db157b7952
commit a0da322c3d

View File

@ -270,14 +270,17 @@ function level:anchor_points()
end
function level:anchors_in(px0,py0,px1,py1)
ancs={}
for ax=px0\4,(px1+3)\4 do
for ay=py0\4,(py1+3)\4 do
local anc=self._anch[_amix(ax,ay)]
if (anc!=nil) add(ancs, anc)
local anch,ax,xlim,sy,ylim=self._anch,px0\4,(px1+3)\4,py0\4,(py1+3)\4
local ay=sy-1
return function()
while true do
ay+=1
if(ay>ylim)ay,ax=sy,ax+1
if(ax>xlim)return nil
local anc=anch[_amix(ax,ay)]
if (anc) return anc
end
end
return ancs
end
function level:get_open_pit(mx,my)
@ -955,7 +958,7 @@ function rope:_find_needed_anchors(i)
local anchors_bydist={}
local x0,x2=_mnmx(a0.x,a2.x)
local y0,y2=_mnmx(a0.y,a2.y)
for a1 in all(level:anchors_in(x0-1,y0-1,x2+1,y2+1)) do
for a1 in level:anchors_in(x0-1,y0-1,x2+1,y2+1) do
add(anchors_bydist,{el=a1,key=_linedist(a0,a1,a2)})
end
shellsort(anchors_bydist)