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
8
votes
5 answers

Circular Buffer in Flash

I need to store items of varying length in a circular queue in a flash chip. Each item will have its encapsulation so I can figure out how big it is and where the next item begins. When there are enough items in the buffer, it will wrap to the…
Robert Deml
  • 12,390
  • 20
  • 65
  • 92
8
votes
3 answers

Disadvantage of circular queue?

Recently, in an interview I was asked the disadvantage of using circular queue. I couldn't think of any. Searching the internet the only answer I found is that it's difficult to implement than linear queue :). Is there any other disadvantage?
Praveen Kumar
  • 1,624
  • 5
  • 18
  • 21
8
votes
2 answers

Looking for the right ring buffer implementation in C

I am looking for a ring buffer implementation (or pseudocode) in C with the following characteristics: multiple producer single consumer pattern (MPSC) consumer blocks on empty producers block on full lock-free (I expect high contention) So far…
ziu
  • 2,634
  • 2
  • 24
  • 39
8
votes
2 answers

How to implement zero-copy tcp using lock-free circular buffer in C++

I have multiple threads that need to consume data from a TCP stream. I wish to use a circular buffer/queue in shared memory to read from the TCP socket. The TCP receive will write directly to the circular queue. The consumers will read from the…
jaybny
  • 1,026
  • 2
  • 13
  • 29
7
votes
4 answers

ring buffer with numpy/ctypes

I'm developing a client which will receive the [EEG] data over tcp and write it to the ring buffer. I thought it can be very convenient to have the buffer as a ctypes or numpy array because it's possible to create a numpy 'view' to any location of…
dmytro
  • 1,293
  • 9
  • 21
7
votes
1 answer

How to implement CHCircularBuffer in iOS project?

for my game iOS project I need a ring buffer. It should work similar to a queue where elements go out and go in but the total amount of elements in the buffer should stay the same. I implemented the ring buffer successfully using java but I am not…
DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601
7
votes
5 answers

Alternative for the Stack

I am working in .Net environment using C#. I need some alternative for the Stack data structure. Some kind of bound stack. The quantity of elements in the collection shouldn't exceed some fixed specified number. And, if that number achieved and new…
Peter17
  • 3,052
  • 9
  • 47
  • 77
7
votes
1 answer

How to find the number of the lexicographically minimal string rotation?

How to find the number of lexicographically minimal string rotation? For example: S = abab, N = 2 S = abca, N = 1 S = aaaa, N = 4 I tried Duval's algorithm, it works very long. The string length of 100000000 characters.
user3164559
  • 287
  • 2
  • 10
7
votes
1 answer

scala collections circular buffer

Just messing about here, with circular buffers. Is this a sensible implementation or is there a faster/more reliable way to skin this cat? class CircularBuffer[T](size: Int)(implicit mf: Manifest[T]) { private val arr = new…
irishjava
  • 503
  • 4
  • 11
6
votes
2 answers

Correct way of implementing a uart receive buffer in a small ARM microcontroller?

I am looking for ideas for a receive buffer for a small application dealing with 15 byte packets at 921.6Kbaud over rs485. I am thinking of using a circular buffer as the interface between the UART ISR and main. As it is a microprocessor I was…
Meter Code
  • 61
  • 1
  • 1
  • 3
6
votes
3 answers

a text file circular buffer in python

I need a python script implementing a circular buffer for rows in a text file limited to N rows like this: row 1 -> pop row 2 row 3 | | push -> row N What's the best solution? EDIT: This script should…
6
votes
6 answers

How to efficiently wrap the index of a fixed-size circular buffer

I have a fixed size circular buffer (implemented as an array): upon initialization, the buffer gets filled with the specified maximum number of elements which allows the use of a single position index in order to keep track of our current position…
Kiril
  • 39,672
  • 31
  • 167
  • 226
6
votes
5 answers

what is called ring in emacs?

Unlike windows style self explanatory copy/cut/paste commands, I could not understand ring concept in emacs. Since I don't program very often in emacs, I could have not realized the value of ring feature. Can you tell me what is called ring in emacs…
user90150
6
votes
1 answer

Memory mirroring for a ring buffer on Linux

I use an anonymous mmap to allocate a giant chunk of memory. There are several contiguous pages in this that I'd like to turn into a ring buffer, using virtual memory mirroring. This example on Wikipedia shows what I'm meaning by virtual memory…
Max
  • 2,760
  • 1
  • 28
  • 47
6
votes
4 answers

Lock Free Circular Array

I am thinking about implementing a lock free circular array. One problem is maintaining the head and tail pointers in a lock free manner. The code I have in mind is: int circularIncrementAndGet(AtomicInteger i) { i.compareAndSet(array.length -…
user1424934
  • 153
  • 2
  • 6
1 2
3
27 28