ring-buffer?
empty-ring-buffer
ring-buffer-length
ring-buffer-push!
ring-buffer-ref
ring-buffer-set!
Version: 4.1.5.3

Ring Buffers

Jay McCarthy <jay at plt-scheme dot org>

 (require (planet jaymccarthy/ring-buffer:1:0))

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

(ring-buffer? v)  boolean?
  v : any/c

Determines if v is a ring buffer.

(empty-ring-buffer max)  ring-buffer?
  max : exact-nonnegative-integer?

Constructs an empty ring buffer that may hold max elements.

(ring-buffer-length rb)  exact-nonnegative-integer?
  rb : ring-buffer?

Returns the length of rb.

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

(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

(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!.