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.
VOSTX4QHUMVJNSCP34WPRESQRN4OBXK7PILA2RMODRFEAU5LJV2QC
HIJWP6Q3CXDRIH7PJXN24JVQJF5Y53JKU7NKFPKCATQWI5DHQA3AC
R5QXEHUIZLELJGGCZAE7ATNS3CLRJ7JFRENMGH4XXH24C5WABZDQC
SMZJGK56DYOP2CYIYDV3QXVHL5P52IEF34GFBESNMULJDMDLJR2AC
JNS2ATVJRXCIBGRFRPPN63OAOQRVV7GE2VRAL3T6YNRXFX6YYMQAC
ZUXG2RU4WF5WF7EYU2XQJ6AERLJLOMGDEXWRMGWHT5DR3B7X7MWQC
J62CVGNGJZSN7TMTE2SG53O47YG4SJGJFTUFKVOZY4TM4KAC566QC
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
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
next_key = function()
local result = to_key(First_available_id)
First_available_id = First_available_id+1
return result
end
First_available_id = 1