"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 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)

A new "RingBuffer" can be created with CreateDataStructure:

Return the length:

The maximum number of elements that can be stored:

Push an element to the back:

The elements stored:

Normal returns the elements and the size:

Fill up a ring buffer:

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

Sum all the elements:

A visualization of the data structure can be generated:

Scope  (1)

Information  (1)

A new "RingBuffer" can be created with CreateDataStructure:

Information about the data structure ds:

Neat Examples  (2)

Animation  (1)

EvaluationMonitor  (1)

An evaluation monitor that shows the most recent events: