Ring Buffers
ring-buffer?
empty-ring-buffer
ring-buffer-length
ring-buffer-push!
ring-buffer-ref
ring-buffer-set!
6.3.90.900

Ring Buffers

Jay McCarthy <jay@racket-lang.org>

 (require data/ring-buffer) package: ring-buffer

This package defines an imperative, overwritting ring buffer that holds a finite number of elements and may be used as a sequence.

procedure

(ring-buffer? v)  boolean?

  v : any/c
Determines if v is a ring buffer.

Constructs an empty ring buffer that may hold max elements.

Returns the length of rb.

procedure

(ring-buffer-push! rb v)  void

  rb : ring-buffer?
  v : (and/c any/c (not/c false/c))
Pushes v on to the end of rb, potentially pushing the first element of rb off.

Examples:
(define rb (empty-ring-buffer 3))
> (ring-buffer-push! rb 1)
> (ring-buffer-push! rb 2)
> (ring-buffer-push! rb 3)
> (for/list ([v rb]) v)

'(1 2 3)

> (ring-buffer-push! rb 4)
> (for/list ([v rb]) v)

'(2 3 4)

procedure

(ring-buffer-ref rb i)  (or/c any/c false/c)

  rb : ring-buffer?
  i : exact-nonnegative-integer?
Returns the value in the ith position of rb.

This interacts with ring-buffer-push!.

Examples:
(define rb (empty-ring-buffer 3))
> (ring-buffer-push! rb 1)
> (ring-buffer-push! rb 2)
> (ring-buffer-push! rb 3)
> (for/list ([v rb]) v)

'(1 2 3)

> (ring-buffer-ref rb 1)

2

> (ring-buffer-push! rb 4)
> (ring-buffer-ref rb 1)

3

procedure

(ring-buffer-set! rb i v)  void

  rb : ring-buffer?
  i : exact-nonnegative-integer?
  v : (and/c any/c (not/c false/c))
Sets the value in the ith position of rb to v

This interacts with ring-buffer-push!.