From 2acefb266048b25c72ca303cf1d376d14c9ec49e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20C?= Date: Sat, 20 Jan 2024 20:02:11 +0100 Subject: [PATCH] terrainlib: compute current queue length instead of using '#' operator. Important speedup. --- terrainlib_lua/rivermapper.lua | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/terrainlib_lua/rivermapper.lua b/terrainlib_lua/rivermapper.lua index 0b1fe4c..372be68 100644 --- a/terrainlib_lua/rivermapper.lua +++ b/terrainlib_lua/rivermapper.lua @@ -134,15 +134,18 @@ local function flow_routing(dem, dirs, lakes) -- 'dirs' and 'lakes' are optional local queue = {singular[ib]} -- Start with the singular node, then this queue will be filled with water donors neighbours basin_links = {} links[#links+1] = basin_links - while #queue > 0 do - local i = tremove(queue) + local cur = 1 + while cur > 0 do + local i = queue[cur] + cur = cur - 1 basin_id[i] = ib local d = dirs2[i] -- Get the directions water is coming from -- Iterate through the 4 directions if d >= 8 then -- River coming from the East d = d - 8 - queue[#queue+1] = i+1 + cur = cur + 1 + queue[cur] = i+1 -- If no river is coming from the East, we might be at the limit of two basins, thus we need to test adjacency. elseif i%X > 0 then add_link(i, i+1, ib, false) @@ -152,7 +155,8 @@ local function flow_routing(dem, dirs, lakes) -- 'dirs' and 'lakes' are optional if d >= 4 then -- River coming from the South d = d - 4 - queue[#queue+1] = i+X + cur = cur + 1 + queue[cur] = i+X elseif i <= X*(Y-1) then add_link(i, i+X, ib, true) else @@ -161,7 +165,8 @@ local function flow_routing(dem, dirs, lakes) -- 'dirs' and 'lakes' are optional if d >= 2 then -- River coming from the West d = d - 2 - queue[#queue+1] = i-1 + cur = cur + 1 + queue[cur] = i-1 elseif i%X ~= 1 then add_link(i, i-1, ib, false) else @@ -169,7 +174,8 @@ local function flow_routing(dem, dirs, lakes) -- 'dirs' and 'lakes' are optional end if d >= 1 then -- River coming from the North - queue[#queue+1] = i-X + cur = cur + 1 + queue[cur] = i-X elseif i > X then add_link(i, i-X, ib, true) else