|
Wesley Smith wrote:
This is a very simple problem, but I'm failing to find an elegant solution. I have an array of data that I'm treating as a circular buffer. Typically I use modulo to wrap the index to the range of the buffer, but since Lua arrays start from 1 (and I want to follow this convention), the modulo operator is not really useful.
Can you turn the problem on its head? You're probably using the circular buffer because that's one way to deal with the problem in a language that does not do garbage collection. What about keeping head and tail "pointers" and creating each new item as needed and setting the used ones to nil... -- this needs to be atomic buffer[head] = "Some new data" head = head + 1 -- this needs to be atomic data = buffer[tail] buffer[tail] = nil tail = tail + 1 Is there a severe performance penalty for this? What happens when the index wraps around? Cheers, Ralph