7.9

## Ring Buffers

 (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 v : any/c
Determines if v is a ring buffer.

 procedure max : exact-nonnegative-integer?
Constructs an empty ring buffer that may hold max elements.

 procedure rb : ring-buffer?
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 rb : ring-buffer?
Pops a value from the end of rb.

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-pop! rb)

3

> (for/list ([v rb]) v)

'(1 2)

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

'(1 2 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!.