Double Linked Ring
ring?
ring
ring->list
ring-size
ring-current
ring-next!
ring-prev!
ring-remove-current!
ring-set-current!
ring-add-next!
ring-add-prev!
8.12

Double Linked Ring🔗ℹ

Matteo d'Addio <matteo.daddio@live.it>

A double linked ring data structure. A ring has no beginning or end, it only has a current element.

procedure

(ring? buff)  boolean?

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

procedure

(ring [a-list])  ring?

  a-list : '() = list?
Create a ring from a list. If the list isn’t specified an empty ring is created. The current element of the ring is set to the first element of the list.

procedure

(ring->list a-ring)  list?

  a-ring : ring?
Creates a list of the element in the ring starting from the current one.

Examples:
> (define the-ring (ring '(a b c d)))
> (ring->list the-ring)

'(a b c d)

procedure

(ring-size a-ring)  exact-nonnegative-integer?

  a-ring : ring?
Returns the number of elements of the ring.

procedure

(ring-current a-ring)  any/c

  a-ring : ring?
Returns the current element of the ring. If the ring is empty an exception is raised.

Examples:
> (define the-ring (ring))
> (ring-size the-ring)

0

> (ring-current the-ring)

ring-current: the ring is empty

> (define the-ring (ring '(a b c d)))
> (ring-size the-ring)

4

> (ring-current the-ring)

'a

procedure

(ring-next! a-ring)  any/c

  a-ring : ring?
Returns the next element of the ring and sets the current element to this element. If the ring is empty an exception is raised.

procedure

(ring-prev! a-ring)  any/c

  a-ring : ring?
Returns the previous element of the ring and sets the current element to this element. If the ring is empty an exception is raised.

Examples:
> (define the-ring (ring '(a b c d)))
> (ring-current the-ring)

'a

> (ring-next! the-ring)

'b

> (ring-current the-ring)

'b

> (ring->list the-ring)

'(b c d a)

> (ring-prev! the-ring)

'a

> (ring-prev! the-ring)

'd

> (ring-current the-ring)

'd

> (ring->list the-ring)

'(d a b c)

procedure

(ring-remove-current! a-ring)  void?

  a-ring : ring?
Removes the current element of the ring and sets the current element to the next element. If the ring is empty an exception is raised.

procedure

(ring-set-current! a-ring elem)  void?

  a-ring : ring?
  elem : any/c
Sets the current element to elem with set!. If the ring is empty an exception is raised.

Examples:
> (define the-ring (ring '(a b c d)))
> (ring-set-current! the-ring 'A)
> (ring->list the-ring)

'(A b c d)

> (ring-remove-current! the-ring)
> (ring->list the-ring)

'(b c d)

procedure

(ring-add-next! a-ring elem)  void?

  a-ring : ring?
  elem : any/c
Adds a new elem after the current one and sets the new elem as current.

procedure

(ring-add-prev! a-ring elem)  void?

  a-ring : ring?
  elem : any/c
Adds a new elem before the current one and sets the new elem as current.

Examples:
> (define the-ring (ring '(a b c d)))
> (ring-add-prev! the-ring 1)
> (ring-add-prev! the-ring 2)
> (ring-add-prev! the-ring 3)
> (ring->list the-ring)

'(3 2 1 a b c d)

> (set! the-ring (ring '(a b c d)))
> (ring-add-next! the-ring 1)
> (ring-add-next! the-ring 2)
> (ring-add-next! the-ring 3)
> (ring->list the-ring)

'(3 b c d a 1 2)

The procedure ring->list can be quite confusing. I use it mainly for debugging purposes.