"RingBuffer" (Data Structure)
"RingBuffer"
represents a ring buffer where the elements are general expressions.
Details


- A ring buffer is useful for efficiently storing a fixed number of elements:
-
CreateDataStructure["RingBuffer",capacity] create a new empty "RingBuffer" that can hold up to capacity elements Typed[x,"RingBuffer"] specifies that x has the type "RingBuffer" - For a data structure of type "RingBuffer", the following operations can be used:
-
ds["Capacity"] the maximum number of elements that can be stored in ds time: O(1) ds["Copy"] return a copy of ds time: O(n) ds["DropAll"] drop all the elements from ds time: O(n) ds["Elements"] return a list of the elements of ds time: O(n) ds["EmptyQ"] True, if ds has no elements time: O(1) ds["Fold",fun,init] apply fun to the elements of ds, starting with init, accumulating a result time: O(n) ds["Length"] the number of elements stored in ds time: O(1) ds["PeekBack"] the last element in ds time: O(1) ds["PeekFront"] the first element in ds time: O(1) ds["PopBack"] remove the last element from ds time: O(1) ds["PopFront"] remove the first element from ds time: O(1) ds["PushBack",x] add x to the end of ds, dropping the first element if ds is full time: O(1) ds["PushFront",x] add x to the start of ds, dropping the last element if ds is full time: O(1) ds["Visualization"] return a visualization of ds time: O(n) - The following functions are also supported:
-
dsi===dsj yield True if dsi is equal to dsj FullForm[ds] print the full form of ds Information[ds] give information about ds InputForm[ds] print a version of ds suitable for input to the Wolfram Language Normal[ds] convert ds to a normal expression
Examples
open allclose allBasic Examples (3)Summary of the most common use cases
A new "RingBuffer" can be created with CreateDataStructure:
In[1]:=1

✖
https://wolfram.com/xid/0bl8ltmgpsui1vpws0s0ho22a-ck3oom
Out[1]=1

In[2]:=2

✖
https://wolfram.com/xid/0bl8ltmgpsui1vpws0s0ho22a-tnm4iz
Out[2]=2

The maximum number of elements that can be stored:
In[3]:=3

✖
https://wolfram.com/xid/0bl8ltmgpsui1vpws0s0ho22a-6nac3x
Out[3]=3

In[4]:=4

✖
https://wolfram.com/xid/0bl8ltmgpsui1vpws0s0ho22a-2ymbbr
Out[4]=4

In[5]:=5

✖
https://wolfram.com/xid/0bl8ltmgpsui1vpws0s0ho22a-gzceda
Out[5]=5

Normal returns the elements and the size:
In[6]:=6

✖
https://wolfram.com/xid/0bl8ltmgpsui1vpws0s0ho22a-0sd3hy
Out[6]=6

In[1]:=1

✖
https://wolfram.com/xid/0bl8ltmgpsui1vpws0s0ho22a-mfljzp
Out[1]=1

When another element is pushed to the end, the first element is dropped:
In[2]:=2

✖
https://wolfram.com/xid/0bl8ltmgpsui1vpws0s0ho22a-cxu8wv
Out[2]=2

In[3]:=3

✖
https://wolfram.com/xid/0bl8ltmgpsui1vpws0s0ho22a-volwmf
Out[3]=3

A visualization of the data structure can be generated:
In[1]:=1

✖
https://wolfram.com/xid/0bl8ltmgpsui1vpws0s0ho22a-uvwa1b
Out[1]=1

Scope (1)Survey of the scope of standard use cases
Information (1)
A new "RingBuffer" can be created with CreateDataStructure:
In[1]:=1

✖
https://wolfram.com/xid/0bl8ltmgpsui1vpws0s0ho22a-g96b41
Out[1]=1

Information about the data structure ds:
In[2]:=2

✖
https://wolfram.com/xid/0bl8ltmgpsui1vpws0s0ho22a-5du3om
Out[2]=2

Neat Examples (2)Surprising or curious use cases
Animation (1)
In[1]:=1

✖
https://wolfram.com/xid/0bl8ltmgpsui1vpws0s0ho22a-16zt7v
Out[1]=1
