switch Nodes from array to table

We need keys to be fixed as we add/delete nodes because we're going to
start recording them next inside Nodes to encode edges.

Since there isn't a clear name for nodes in this app, I came up with a
way to autogenerate keys.
This commit is contained in:
Kartik K. Agaram 2023-04-18 08:09:27 -07:00
parent a49d4c42b0
commit 880ecc0efd
8 changed files with 39 additions and 7 deletions

4
0028-A
View File

@ -2,8 +2,8 @@ A = function(preserve_screen_top_of_cursor_node)
print('A')
-- translate Nodes to Surface
while #Surface > 0 do table.remove(Surface) end
for i,node in ipairs(Nodes) do
node.id = i
for key,node in pairs(Nodes) do
node.id = key
compute_layout(node, node.x,node.y, Surface, preserve_screen_top_of_cursor_node)
end
-- continue the pipeline

View File

@ -1,3 +1,3 @@
Nodes = {
{type='text', x=0,y=0, margin=0, width=120},
a={type='text', x=0,y=0, margin=0, width=120},
}

View File

@ -1,9 +1,9 @@
on_move_bar = function(sx,sy)
for _,node in ipairs(Nodes) do
for _,node in pairs(Nodes) do
if sx >= node.x-10 and sx < node.x-10+node.w/2 then
if sy >= node.y-15-10 and sy < node.y-15 then
return node
end
end
end
end
end

View File

@ -1,5 +1,5 @@
on_resize = function(sx,sy)
for _,node in ipairs(Nodes) do
for _,node in pairs(Nodes) do
if sx >= node.x+node.w+20 and sx < node.x+node.w+24 then
local buffer_height = math.max(node.h, 3*node.editor.line_height)
if sy >= node.y and sy < node.y+buffer_height then
@ -7,4 +7,4 @@ on_resize = function(sx,sy)
end
end
end
end
end

1
0044-First_available_id Normal file
View File

@ -0,0 +1 @@
First_available_id = 1

5
0045-next_key Normal file
View File

@ -0,0 +1,5 @@
next_key = function()
local result = to_key(First_available_id)
First_available_id = First_available_id+1
return result
end

15
0046-to_key Normal file
View File

@ -0,0 +1,15 @@
to_key = function(n)
-- represent an integer n in base-26 using a-z
local result = {}
n = math.floor(n)
local a = string.byte('a')
while n > 0 do
local digit = n % 26
table.insert(result, 1, string.char(digit+a))
n = math.floor(n/26)
end
if #result == 0 then
return 'a'
end
return table.concat(result)
end

11
0047-test_to_key Normal file
View File

@ -0,0 +1,11 @@
test_to_key = function()
check_eq(to_key(0), 'a', 0)
check_eq(to_key(1), 'b', 1)
-- ...
check_eq(to_key(24), 'y', 24)
check_eq(to_key(25), 'z', 25)
check_eq(to_key(26), 'ba', 26)
check_eq(to_key(26*26), 'baa', '3 digit')
check_eq(to_key(26*26+1), 'bab', '3 digit/2')
check_eq(to_key(26*26*26), 'baaa', '4 digit')
end