teslamotors / fixed-containers

C++ Fixed Containers
MIT License
352 stars 31 forks source link

Add FixedCircularBuffer #76

Closed abeimler closed 8 months ago

abeimler commented 9 months ago

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}
alexkaratarakis commented 9 months ago

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.

alexkaratarakis commented 9 months ago

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. :)

alexkaratarakis commented 8 months ago

Done in #80.