WOLFRAM

"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 dstime: O(1)
    ds["Copy"]return a copy of dstime: O(n)
    ds["DropAll"]drop all the elements from dstime: O(n)
    ds["Elements"]return a list of the elements of dstime: O(n)
    ds["EmptyQ"]True, if ds has no elementstime: O(1)
    ds["Fold",fun,init]apply fun to the elements of ds, starting with init, accumulating a resulttime: O(n)
    ds["Length"]the number of elements stored in dstime: O(1)
    ds["PeekBack"]the last element in dstime: O(1)
    ds["PeekFront"]the first element in dstime: O(1)
    ds["PopBack"]remove the last element from dstime: O(1)
    ds["PopFront"]remove the first element from dstime: O(1)
    ds["PushBack",x]add x to the end of ds, dropping the first element if ds is fulltime: O(1)
    ds["PushFront",x]add x to the start of ds, dropping the last element if ds is fulltime: O(1)
    ds["Visualization"]return a visualization of dstime: O(n)
  • The following functions are also supported:
  • dsi===dsjyield 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 all

Basic Examples  (3)Summary of the most common use cases

A new "RingBuffer" can be created with CreateDataStructure:

Out[1]=1

Return the length:

Out[2]=2

The maximum number of elements that can be stored:

Out[3]=3

Push an element to the back:

Out[4]=4

The elements stored:

Out[5]=5

Normal returns the elements and the size:

Out[6]=6

Fill up a ring buffer:

Out[1]=1

When another element is pushed to the end, the first element is dropped:

Out[2]=2

Sum all the elements:

Out[3]=3

A visualization of the data structure can be generated:

Out[1]=1

Scope  (1)Survey of the scope of standard use cases

Information  (1)

A new "RingBuffer" can be created with CreateDataStructure:

Out[1]=1

Information about the data structure ds:

Out[2]=2

Neat Examples  (2)Surprising or curious use cases

Animation  (1)

Out[1]=1

EvaluationMonitor  (1)

An evaluation monitor that shows the most recent events:

Out[1]=1
Out[2]=2