I am thinking about how to organize/allocate memory and that led me to this question, which I have distilled to its essence, so it may seem out of the blue, but the memory question is too complicated and confusing and distracting, so I am not asking it here. I tried asking it earlier and got no response. So here it goes.
Say you have a system where you want to check if you have some integer exists in it as fast as possible, and you want to be able to add and remove integers as fast as possible too. That is, it's a key-store
essentially, not even a key-value-store. It only stores integer keys, and I guess theoretically the value would conceptually be a true
boolean, but I don't think it needs to be there necessarily.
One solution is to use sparse arrays. For example, this array would return true in O(1) time for 3, 5, and 9.
[-, -, 3, -, 5, -, -, -, 9, -, -, ...]
You just specify the integer - 1
and get back if it exists at that position in the array. But the problem with this is twofold. First, it takes up a lot of wasted memory with all these null values. And secondly, for my project, I have the added constraint that arrays can only exist at sizes 1, 2, 4, 8, 16, and 32 items in length. So I am stuck using short arrays. Which means I'm left with trees of some sort most likely.
So these are the constraints:
- Positive 32-bit integers.
- Compact data structure (minimal wasted space).
- Fast lookup times.
- Fast deletion/insertion times.
- Array sizes in the data structure can only be sized at 1, 2, 4, 8, 16, or 32 items.
One approach I am thinking that might work that fits these constraints is a sort of tree, like a B+tree. But I am not super familiar with these yet, still playing with them. I think it would be like a key-only B+tree (no values, and hence no leaves). But not entirely sure what this would look like (in JavaScript, if I had to pick a language).
Another approach I thought of is a binary trie. But this would take up to 32 steps per integer to add and check membership, which seems like we could potentially optimize somehow.
What is the main approach to storing sequential 32-bit integers in some data structure for rapidly checking membership, doing insertions, and doing removals? Perhaps this can learn from IP lookup tables or things of that nature.
I am using this for a custom programming language, though seeing an implementation in JavaScript would be helpful. If it's just the name of the data structure you're providing, please explain how it works or implement it in JavaScript without resorting to JavaScript built-ins which may already solve the problem.
question from:
https://stackoverflow.com/questions/65645449/how-to-store-random-integers-from-the-set-of-32-bit-integers-in-a-compact-data-s