Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you iterate backward over circular buffer without a conditional?

Iterating forward through a circular buffer without using a conditional is easy with the remainder operator...

iterator = (iterator + 1) % buffer_size;

I can't for the life of me figure out the reverse operation, iterating backward.

like image 481
Nick Strupat Avatar asked Aug 09 '10 05:08

Nick Strupat


People also ask

Is a circular buffer FIFO?

A circular buffer is a utility used to transfer successive data values from a producer thread to a consumer thread, who retrieves the data in FIFO (first in first out) order.

Which parameter decides the circular buffer size?

Then the output together with the partition number is written to the circular buffer in memory, which size is defined by the parameter mapreduce.

How does a circular buffer work?

Circular buffers (also known as ring buffers) are fixed-size buffers that work as if the memory is contiguous & circular in nature. As memory is generated and consumed, data does not need to be reshuffled – rather, the head/tail pointers are adjusted. When data is added, the head pointer advances.

Is boost circular buffer thread safe?

Thread-Safety This means the circular_buffer is not thread-safe. The thread-safety is guarantied only in the sense that simultaneous accesses to distinct instances of the circular_buffer are safe, and simultaneous read accesses to a shared circular_buffer are safe.


1 Answers

Does iterator = (iterator + buffer_size - 1) % buffer_size work for you? Go one less than all the way around.

like image 137
Borealid Avatar answered Oct 10 '22 12:10

Borealid