From a0da322c3dce323afeb7c9ba868b6e53e2ee0804 Mon Sep 17 00:00:00 2001 From: Kistaro Windrider Date: Sun, 18 Dec 2022 01:46:30 +0000 Subject: [PATCH] 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: https://git.chromaticdragon.app/pyrex/chameleonic/pulls/4 Co-authored-by: Kistaro Windrider Co-committed-by: Kistaro Windrider --- chameleonic.p8 | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/chameleonic.p8 b/chameleonic.p8 index 87dfc40..993918b 100644 --- a/chameleonic.p8 +++ b/chameleonic.p8 @@ -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)