I’ve been experimenting with an algorithm I developed independently, and I thought it might be beneficial to share it. I’ve named this algorithm “bee-id”. It’s a single-byte unique identifier, compact enough to fit into just a byte.
I devised this algorithm out of a need for a game project. I required a method for generating unique identifiers, but these identifiers had to be as space-efficient as possible. At the same time, there was a strict no-collision policy - each ID had to be absolutely unique. And when the IDs were no longer needed, I wanted a way to recycle them for reuse.
The mechanics of the bee-id algorithm are straightforward.
The lease of a new ID is very straight forward. We keep an array of unique numeric IDs, a pointer to the current position in the array, and a lookup table which stores the array position for any given ID.
Every time we fetch an ID from the array, we move the pointer back until there are no items left and the pointer is -1
. When that happens, there are no more available IDs and we must wait for some to be released.
The release of an ID is a bit more complex since IDs can be released in any order. However, with a clever shuffling process, we can account for this.
And that approach will allow us to support 255 unique IDs. I think that’s pretty cool, so I named it bee-id. Thanks for reading! 🐝