I’m looking for a data structure for Motoko that acts like this in behaviour
Basically I can define up front the size of the buffer. Adding items keeps filling it up to the limit. Once at capacity I should be able to discard the oldest item and add the new item. But in a queue so that insertion order is preserved.
The closest data structure I found is a Deque and I was open to writing the max size logic myself. But the deque lacks a size property which I expected it to have. Considering the Deque is derived from a List.
Is there a data structure here that I could use that serves this purpose?
I’m okay to add some additional logic. I believe Deque would serve this purpose. Is there a way to get the size from a Deque? The methods listed here dont have one
If I have access to size, I can check when pushing items to the end. IF capacity is reached, i’ll pop from the front and push to the end, else i’ll just push to the end
I think you can compute the size of the Deque just by summing the List.size(deque.0) + List.size(deque.1) of its two components. But that is an 0(N) operation and will be expensive.
Another option is wrap the deque and track its size as you push and an pop to the underlying deque.
An array based implementation (as described in the wikipedia page), would be more efficient that either of those approaches, I expect.