Circular buffer: Difference between revisions

Content deleted Content added
 
(13 intermediate revisions by 9 users not shown)
Line 1:
{{Short description|AData circularstructure shapedin buffercomputer shown while obtaining datascience}}
[[Image:Circular buffer.svg|thumb|200px|A ring showing, conceptually, a circular buffer. This visually shows that the buffer has no real end and it can loop around the buffer. However, since memory is never physically created as a ring, a linear representation is generally used as is done below.]]
 
Line 19:
:[[Image:Circular buffer - XX123XX.svg|250px]]
 
If two elements are removed, the two oldest values inside of the circular buffer would be removed. Circular buffers use FIFO (''[[first in, first out (computing)|first in, first out]]'') logic. In the example, 1 & 2 were the first to enter the circular buffer, they are the first to be removed, leaving 3 inside of the buffer.
 
:[[Image:Circular buffer - XXXX3XX.svg|250px]]
Line 33:
Alternatively, the routines that manage the buffer could prevent overwriting the data and return an error or raise an [[exception handling|exception]]. Whether or not data is overwritten is up to the semantics of the buffer routines or the application using the circular buffer.
 
Finally, if two elements are now removed then what would be returnedremoved is '''not''' 3 & 4 (or rather now A & B), but 5 & 6 because 5 & 6 are now the oldest elements, yielding the buffer with:
 
:[[Image:Circular buffer - X789ABX.svg|250px]]
Line 46:
== Circular buffer mechanics ==
:[[Image:Hardware_circular_buffer_implementation_patent_us3979733_fig4.png|250px|thumb|Circular buffer implementation in hardware, US patent 3979733, fig4]]
A circular buffer can be implemented using a [[pointer (computer programming)|pointer]] and threefour integers:<ref name="Liu Wu Das 2021 p. 117">{{cite book |last1=Liu |first1=Z. |url=https://books.google.com/books?id=si1CEAAAQBAJ&pg=PA117 |title=Wireless Algorithms, Systems, and Applications: 16th International Conference, WASA 2021, Nanjing, China, June 25–27, 2021, Proceedings, Part II |last2=Wu |first2=F. |last3=Das |first3=S.K. |publisher=Springer International Publishing |year=2021 |isbn=978-3-030-86130-8 |series=Lecture Notes in Computer Science |page=117 |access-date=2023-09-04}}</ref>
* buffer start in memory
* buffer capacity (length)
Line 62:
In the beginning the indexes end and start are set to 0. The circular buffer write operation writes an element to the end index position and the end index is incremented to the next buffer position. The circular buffer read operation reads an element from the start index position and the start index is incremented to the next buffer position.
 
The start and end indexes alone are not enough to distinguish between buffer full or empty state while also utilizing all buffer slots,<ref>{{cite web|last1=Chandrasekaran|first1=Siddharth|title=Implementing Circular/Ring Buffer in Embedded C|url=https://embedjournal.com/implementing-circular-buffer-embedded-c/|website=Embed Journal|publisher=EmbedJournal Team|access-date=14 August 2017|archive-url=https://web.archive.org/web/20170211031659/http://embedjournal.com/implementing-circular-buffer-embedded-c/|archive-date=11 February 2017|url-status=live|date=2014-05-16}}</ref> but can be if the buffer only has a maximum in-use size of Length - 1.<ref>[https://www.kernel.org/doc/Documentation/circular-buffers.txt#:~:text=A%20circular%20buffer%20is%20a,next%20item%20in%20the%20buffer Circular buffers] kernel.org</ref> In this case, the buffer is empty if the start and end indexes are equal and full when the in-use size is Length - 1.
Another solution is to have another integer count that is incremented at a write operation and decremented at a read operation. Then checking for emptiness means testing count equals 0 and checking for fullness means testing count equals Length.<ref>{{cite web |title=ArrayQueue: An Array-Based Queue |url=http://opendatastructures.org/ods-python/2_3_ArrayQueue_Array_Based_.html |website=Open Data Structures (in pseudocode) |first=Pat |last=Morin|author-link= Pat Morin |access-date=7 November 2015 |archive-url=https://web.archive.org/web/20150831023453/http://opendatastructures.org/ods-python/2_3_ArrayQueue_Array_Based_.html |archive-date=31 August 2015 |url-status=live }}</ref>
 
The following source code is a [[C (programming language)|C]] implementation together with a minimal test. Function put() puts an item in the buffer, function get() gets an item from the buffer. Both functions take care about the capacity of the buffer :
 
<syntaxhighlight lang="c">
#include <stdio.h>
 
enum { N = 10 }; // N elementssize of the circular buffer
 
int buffer [N]; // note: only (N - 1) elements can be stored at a given time
int buffer [N];
int writeIndx = 0;
int readIndx = 0;