"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)
A new "RingBuffer" can be created with CreateDataStructure:
The maximum number of elements that can be stored:
Normal returns the elements and the size:
When another element is pushed to the end, the first element is dropped:
Scope (1)
Information (1)
A new "RingBuffer" can be created with CreateDataStructure: