Tom wrote:So if I understand correctly, there's 128 patterns, and you just need to find one that's not used? Use 128 bytes, each byte is the use count of that pattern. When something references the pattern, increment its use count, and when something stops using it, decrement.
That's exactly how my old level editor used to work. Since it created block structures dynamically, every time a block was added to or removed from the map I had to make sure no orphan structures would be left around.
To find an unsed pattern just loop over the 128 bytes looking for a zero.
Or you could have a linked list of unused patterns. If each pattern is not gonna be used more than 128 times, you can use bit 7 to distinguish between used an unused, and the lower 7 bits would be either the number of times it was used (minus 1 to fit the 0 to 127 range) or the index of the next unused pattern. Then all you need is an extra variable to indicate the first unused pattern.
So, when you need to use a new pattern, just grab the index from that variable, go to that pattern and grab the index of the next unused pattern and overwrite the variable with that so it becomes the first unused pattern, and then indicate that the current slot is not unused anymore by using its flag and reset the usage count.
When you decrement the usage count and detect that the pattern is not being used anymore you can just add it to the beginning of the list of free patterns by linking to it with the variable I mentioned before and making it link to the pattern that the variable was linking to previously.
This will give you quick access to the slots since you will not have to waste time searching for anything.