Questions tagged [circular-buffer]

A circular buffer is a data structure that uses a single, fixed-size buffer as if it were connected end-to-end. This structure lends itself easily to buffering data streams.

A circular buffer (AKA cyclic buffer, ring buffer, or circular queue) is a data structure that uses a single fixed-size buffer as if it were connected end-to-end. This structure lends itself easily to buffering data streams.

The circular buffer uses two pointers to indicate the current beginning and ending of the data on the buffer. The old/"First In" data is deleted from the buffer as it is read, moving forward the beginning pointer. New data is added at the end of the buffer (moving forward the corresponding pointer). The buffer physical storage is the same during all the operation if the reading speed is faster than the data acquisition process.

The useful property of a circular buffer is that it does not need to have its elements shuffled around when one is consumed. (If a non-circular buffer were used then it would be necessary to shift all elements when one is consumed.) In other words, the circular buffer is well suited as a FIFO (first in first out) buffer while a standard, non-circular buffer is well suited as a LIFO (last in first out) buffer.

Circular buffering makes a good implementation strategy for a queue that has fixed maximum size. Should a maximum size be adopted for a queue, then a circular buffer is a completely ideal implementation; all queue operations are constant time. However, expanding a circular buffer requires shifting memory, which is comparatively costly. For arbitrarily expanding queues, a Linked list approach may be preferred instead.

Check http://en.wikipedia.org/wiki/Circular_buffer for more information.

417 questions
0
votes
0 answers

How can we grow an array in a circular buffer / circular queue implementation

I'm trying to learn about the implementation of a circular queue using arrays, and currently, my main source of learning how it works is through this page. https://www.happycoders.eu/algorithms/implement-deque-using-array/ However, I'm struggling to…
Nutnicha
  • 335
  • 1
  • 10
0
votes
0 answers

C# CircularBuffer how to understand readOffset & mCapacityMask

I saw a script about CircularBuffer.cs,and i can not understand a case. This is the whole program: The case that i can not underatand is " readOffset & mCapacityMask" in function Peek and Write. I know the result of " readOffset & mCapacityMask…
0
votes
1 answer

How do I implement a circular buffer in C using this specific structure?

I'm completely new to C and I'm trying to implement a queue data structure. I don't know much about using pointers and storing data, so I'm having difficulty starting. The queue works as a fixed size circular buffer. I only need a single queue_t. …
jamess
  • 1
  • 1
0
votes
1 answer

Performant circular buffer for frames (ndarrays) of data samples

I'm receiving blocks of audio samples of varying length from a stream, and each block is a 1D ndarray. A block may be received every 50ms or less. I need to keep a buffer of the last 48000 samples I've tried defining my buffer like this: buffer =…
davegravy
  • 888
  • 11
  • 28
0
votes
1 answer

Why should I use circular buffers when reading and writing to sockets in C?

I'm doing an assignment where the goal is to create a basic FTP server in C capable of handling multiple clients at once. The subject tells us to "wisely use circular buffers" but I don't really understand why or how ? I'm already using select to…
Hollow
  • 63
  • 5
0
votes
1 answer

Queue RingBuffer implements Queue12. Doesn't work

My code compiles but doesn't work. I think i didn't typecast right? and can someone explain to me wrapIdx method return index % capacity. i don't really understand how that code wraps the array. when it reaches the end of the array index % capacity…
earbulan
  • 37
  • 4
0
votes
1 answer

Tail pointer for overwrite functionality in circular buffer implementation

I am trying to implement a circular buffer for logging messages. I have implemented a fairly basic solution which seems to work but I am confused about how push should work in real case scenario. Should head overwrite the tail? if yes do we modify…
0
votes
1 answer

CircularFifoBuffer .avg() calculation fails

I have the following dummy code which saves a list of doubles in a hasmap to a specific key, I want to be able to use this buffer to calculate average values over each key. final int bufferSize = 5; HashMap> testmap =…
ThatQuantDude
  • 759
  • 1
  • 9
  • 26
0
votes
0 answers

Adding to the Rear of a doubly linked Circular Buffer of Integer (java)

I'm trying to add to the rear of the buffer but when I add to the rear it seems to be trailing behind by a number. Is there any fix for this? This was the error I got when I ran it through my professors tester. Error: add part 2 Expected: "R [2, 1]…
Kingjp
  • 11
  • 1
0
votes
1 answer

Hazelcast, recovering a MessageListener member after termination due to message loss

We have a ReliableMessageListener that synchronizes some data structures that it holds across the cluster by the onMessage implementation. The cluster is composed of three nodes. We noticed that one of the topics gets out of sync, and had been…
0
votes
0 answers

The most efficient way to read data from a sensor and write to a file

I am working on a system which is capable of: Reading data from a Bluetooth sensor Converting data into int16 Writing data in a .bin file Displaying data on a mobile application screen The problem that I have, since file reading and writing…
0
votes
0 answers

Ideal data structure for undo/redo but with max capacity

The obvious answer would be a stack but there is just one thing that I want to implement: That is to only store, say, the 100 most recent edits. This means that any point past 100 every time a new change is made the oldest edit is deleted. I thought…
nix_s
  • 38
  • 5
0
votes
0 answers

Limit number of active threads in LMAX Disruptor(RingBuffer)

I have multiple consumers of different types that can be run in parallel but I don't want all of them to run in parallel at the same time because I don't have that many CPU cores and performance would degrade because of context switching. How is…
Vlad
  • 3,001
  • 1
  • 22
  • 52
0
votes
0 answers

How to access all the elements at once in boost circular buffer?

I declared a buffer of vector in c++: boost::circular_buffer> poses {20}; I need to store the values of the buffer into a tensor of shape [1, 20, 30]. Each vector in the buffer is of size 30. So the circular buffer has 20 size…
0
votes
1 answer

Mongo as circular buffer

I'am trying to figure out a way to use Mongo as a circular buffer. Currently using SQL Lite but performance wise does not fit our case. The specifications need to be met are: The collection must empty itself every x seconds. The collection must…
fdhsdrdark
  • 144
  • 1
  • 13