teslamotors / fixed-containers

C++ Fixed Containers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add FixedCircularBuffer

abeimler opened this issue · comments

Like FixedVector (or FixedQueue?) but with a circular index.
Inspiration https://www.etlcpp.com/circular_buffer.html

Example

FixedCircularBuffer<int, 4> v{};
v.push(100);
v.push(101);
v.push(102);
v.push(103);
v.push(99);

// v = {99, 101, 102, 103}

Yes, this has several applications so it would be a nice addition.
Need to think a bit on the semantics and API/supported operations since there is no std equivalent, but can otherwise be implemented in terms of FixedDeque.

Regarding API:
boost has deque API/semantics for circular_buffer: https://github.com/boostorg/circular_buffer/blob/develop/include/boost/circular_buffer/base.hpp
etl has queue API/semantics for circular_buffer: https://www.etlcpp.com/circular_buffer.html

Both are reasonable approaches. To disambiguate, I am thinking of adding both with names FixedCircularDeque and FixedCircularQueue, respectively. The latter is a forwarder to the former.
I have adapted your commit from #79 accordingly in #80 @abeimler .

Open to feedback. :)