"RingBuffer" (Data Structure)
"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] apply fun to the elements of ds, accumulating a result time: O(n) 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 all close allBasic Examples (3)
A new "RingBuffer" can be created with CreateDataStructure:
ds = CreateDataStructure["RingBuffer", 8]ds["Length"]The maximum number of elements that can be stored:
ds["Capacity"]ds["PushBack", f[1]]ds["Elements"]Normal returns the elements and the size:
Normal[ds]ds = CreateDataStructure["RingBuffer", 8];
Do[ds["PushBack", i], {i, 8}];
ds["Elements"]When another element is pushed to the end, the first element is dropped:
ds["PushBack", 9];
Normal[ds]ds["Fold", Plus, 0]A visualization of the data structure can be generated:
ds = CreateDataStructure["RingBuffer", 20];
Do[ds["PushBack", i], {i, 15}];ds["Visualization"]Scope (1)
Information (1)
A new "RingBuffer" can be created with CreateDataStructure:
ds = CreateDataStructure["RingBuffer", 8]Information about the data structure ds:
Information[ds]Neat Examples (2)
Animation (1)
ds = CreateDataStructure["RingBuffer", 8];
Do[ds["PushFront", i], {i, 8}];
list = Table[
res = HighlightGraph[CycleGraph[8,
DirectedEdges -> True, VertexLabels -> Thread[Rule[Range[8], ds["Elements"]]]], {Style[1, Blue], Style[8, Red]}];
ds["PushFront", i];
res, {i, 9, 32}];
ListAnimate[list]EvaluationMonitor (1)
An evaluation monitor that shows the most recent events:
Dynamic[Framed[Pane[StringRiffle[ds["Elements"], "
"]], ImageSize -> Scaled[0.5]], UpdateInterval -> 0.1, Initialization :> (ds = CreateDataStructure["RingBuffer", 10])]NDSolve[{y'[x] == y[x]Cos[x + y[x]], y[0] == 1}, y, {x, 0, 30}, EvaluationMonitor :> ds["PushBack", x]]Related Guides
History
Introduced in 2020 (12.1)