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
150
votes
15 answers

efficient circular buffer?

I want to create an efficient circular buffer in python (with the goal of taking averages of the integer values in the buffer). Is this an efficient way to use a list to collect values? def add_to_buffer( self, num ): self.mylist.pop( 0 ) …
jedierikb
  • 12,752
  • 22
  • 95
  • 166
83
votes
9 answers

How do you implement a circular buffer in C?

I have a need for a fixed-size (selectable at run-time when creating it, not compile-time) circular buffer which can hold objects of any type and it needs to be very high performance. I don't think there will be resource contention issues since,…
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
57
votes
5 answers

How to create a closed (circular) ListView?

I want to create a customized ListView (or similar) which will behave like a closed (circular) one: scrolling down - after the last item was reached the first begins (.., n-1, n, 1, 2, ..) scrolling upward - after the first item was reached the…
user281076
  • 571
  • 1
  • 5
  • 3
51
votes
21 answers

Circular buffer in JavaScript

Has anyone already implemented a circular buffer in JavaScript? How would you do that without having pointers?
user191800
31
votes
7 answers

What are the uses of circular buffer?

What are some of the uses of circular buffer? What are the benefits of using a circular buffer? is it an alternative to double linked list?
DarthVader
  • 52,984
  • 76
  • 209
  • 300
30
votes
1 answer

Sending data with PACKET_MMAP and PACKET_TX_RING is slower than "normal" (without)

I’m writing a traffic generator in C using the PACKET_MMAP socket option to create a ring buffer to send data over a raw socket. The ring buffer is filled with Ethernet frames to send and sendto is called. The entire contents of the ring buffer is…
jwbensley
  • 10,534
  • 19
  • 75
  • 93
28
votes
16 answers

Searching for an element in a circular sorted array

We want to search for a given element in a circular sorted array in complexity not greater than O(log n). Example: Search for 13 in {5,9,13,1,3}. My idea was to convert the circular array into a regular sorted array then do a binary search on the…
guirgis
  • 1,133
  • 2
  • 14
  • 17
27
votes
8 answers

Thread-safe circular buffer in Java

Consider a few web server instances running in parallel. Each server holds a reference to a single shared "Status keeper", whose role is keeping the last N requests from all servers. For example (N=3): Server a: "Request id = ABCD" Status…
Adam Matan
  • 128,757
  • 147
  • 397
  • 562
23
votes
2 answers

How to read ring buffer within linux kernel space?

I'm writing a Linux character driver which can print system logs in user space. Just as the command 'dmesg' does. I've learned that all the log that we print with 'printk' will be sent to a space named ring buffer. So I have the questions: Is ring…
Yingyi Xu
  • 387
  • 1
  • 3
  • 15
21
votes
9 answers

How to access array in circular manner in JavaScript

I have an array like [A,B,C,D]. I want to access that array within a for loop like as var arr = [A,B,C,D]; var len = arr.length; for(var i = 0; i
Anshul
  • 9,312
  • 11
  • 57
  • 74
20
votes
8 answers

How do I keep a list of only the last n objects?

I want to do some performance measuring of a particular method, but I'd like to average the time it takes to complete. (This is a C# Winforms application, but this question could well apply to other frameworks.) I have a Stopwatch which I reset at…
JYelton
  • 35,664
  • 27
  • 132
  • 191
20
votes
3 answers

O(1) circular buffer in haskell?

I'm working on a small concept project in Haskell which requires a circular buffer. I've managed to create a buffer using arrays which has O(1) rotation, but of course requires O(N) for insertion/deletion. I've found an implementation using lists…
Edward
  • 1,786
  • 1
  • 15
  • 33
18
votes
4 answers

How do I code a simple integer circular buffer in C/C++?

I see a lot of templates and complicated data structures for implementing a circular buffer. How do I code a simple integer circular buffer for 5 numbers? I'm thinking in C is the most straightforward? Thanks.
T.T.T.
  • 33,367
  • 47
  • 130
  • 168
15
votes
1 answer

C++ std::deque implementation: why not use circular buffer?

I did some search on the implementation of deque. According to this post, deque uses vector of vectors. I know pushing at the begin and end should be both in constant time, and also random access is required. I think circular buffer meets all these…
sunnior
  • 151
  • 1
  • 1
  • 4
14
votes
5 answers

Shifting/aligning/rotating a circular buffer to zero in-place

I'm using a circular buffer to push data onto either end of a list. After I'm done I want to align the buffer so the first element in the list is at position zero and can be used like a regular array without any fancy indexing overhead. So I have my…
jozxyqk
  • 16,424
  • 12
  • 91
  • 180
1
2 3
27 28