Reputation: 787
I have an ask to create numbers sequentially for list of tasks sent in a file.
Is there any algorithm which solves this problem?
Upvotes: 0
Views: 99
Reputation: 9085
Unless you have some guarantee on how unlucky you can be (how many times you'd have to insert a task in your smallest session id gap), you can't do better than averaging the neighbors every time. With big enough numbers and a cap of 50 tasks, this is compatible with integers.
Make fictional T endpoints with session IDs of 0 and 2^50 (# of tasks)
Each task gets the average of its neighbors' session ids.
T1 => S 2^49
T2 => S (0+2^49)/2 = 2^48 or (2^49+2^50)/2 = 2^49 + 2^48
etc...
All session id gaps are powers of 2, and we start with a big enough range that we can halve it 50 times if need be.
2^49 = 562,949,953,421,312; idk how much better this is than using a float
Upvotes: 2