I’ve got some code that calculates the following sequence:
topID = 0
topID = 1
topID = 2
topID = 3
topID = 10
topID = 20
topID = 30
topID = 11
topID = 21
topID = 31
topID = 12
topID = 22
topID = 32
topID = 13
topID = 23
topID = 33
topID = 100
topID = 200
topID = 300
topID = 110
topID = 210
topID = 310
topID = 120
topID = 220
topID = 320
topID = 130
topID = 230
topID = 330
topID = 101
topID = 201
topID = 301
topID = 111
topID = 211
topID = 311
topID = 121
topID = 221
topID = 321
topID = 131
topID = 231
topID = 331
topID = 102
topID = 202
topID = 302
topID = 112
topID = 212
topID = 312
topID = 122
topID = 222
topID = 322
topID = 132
topID = 232
topID = 332
topID = 103
topID = 203
topID = 303
topID = 113
topID = 213
topID = 313
topID = 123
topID = 223
topID = 323
topID = 133
topID = 233
topID = 333
topID = 1000
topID = 2000
topID = 3000
topID = 1100
topID = 2100
topID = 3100
topID = 1200
topID = 2200
topID = 3200
topID = 1300
topID = 2300
topID = 3300
topID = 1010
topID = 2010
topID = 3010
topID = 1110
topID = 2110
topID = 3110
topID = 1210
topID = 2210
topID = 3210
topID = 1310
topID = 2310
topID = 3310
topID = 1020
topID = 2020
topID = 3020
topID = 1120
topID = 2120
topID = 3120
topID = 1220
topID = 2220
topID = 3220
topID = 1320
topID = 2320
topID = 3320
It is however pretty ugly code. Is this any sort of normal sequence and is there any short way of calculating it? I’m currently doing it all through adds, shifts, and binary operators.
The sequence itself is essentially the mapping to a leaf in a tree. The above would be for a tree with four nodes at each branch and new branches/leaves are made based on the mapping. The goal is to have the mapping work in such a manner that any new leaf has all four slots filled before allocating memory for a new leaf. When all current-layer leaves are filled, one slot in one leaf is converted to a node pointer and the current item moved into the sub-leaf, ad infitum.
Simply incrementing the key (i.e. 1, 2, 3, 10, 11, 12, 13, 20, 21, 22, 23, 30, …) will work fine and keeps the tree of even-depth but means that up to almost four times the needed memory can be allocated, while as the above sequence causes there to be only +3 empty slots as worst case (assuming I have it right…)