Documentation
¶
Overview ¶
Package ring implements operations on circular lists. Edited to be generic.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Buffered ¶
type Buffered[T any] struct { // contains filtered or unexported fields }
Buffered is a slice-based circular buffer that grows and shrinks dynamically. AppendBack, RemoveFront, Front, and Len run in amortized O(1) time, while Range runs in O(n) time where n is the number of elements (Len()).
func NewBuffered ¶
NewBuffered creates a new circular buffer with the given initial capacity. The buffer grows by doubling its capacity as needed (amortized O(1)). `initialSize` will default to 1 if it is less than 1. The `bufferSize` parameter is kept for backward compatibility but is currently ignored.
func (*Buffered[T]) AppendBack ¶
func (b *Buffered[T]) AppendBack(value *T)
AppendBack adds a new value to the end of the buffer. If the buffer is full, it doubles in capacity (amortized O(1)).
func (*Buffered[T]) Front ¶
func (b *Buffered[T]) Front() *T
Front returns the first value in the buffer, or nil if empty.
func (*Buffered[T]) Range ¶
Range iterates over the buffer values from front to back until the given function returns false.
func (*Buffered[T]) RemoveFront ¶
func (b *Buffered[T]) RemoveFront() *T
RemoveFront removes the first value from the buffer and returns the next front value (or nil if the buffer is now empty). Amortized O(1).
type Ring ¶
type Ring[T any] struct { Value T // for use by client; untouched by this library // contains filtered or unexported fields }
A Ring is an element of a circular list, or ring. Rings do not have a beginning or end; a pointer to any ring element serves as reference to the entire ring. Empty rings are represented as nil Ring pointers. The zero value for a Ring is a one-element ring with a nil Value.
func (*Ring[T]) Do ¶
func (r *Ring[T]) Do(f func(T))
Do calls function f on each element of the ring, in forward order. The behavior of Do is undefined if f changes *r.
func (*Ring[T]) Len ¶
Len computes the number of elements in ring r. It executes in time proportional to the number of elements.
func (*Ring[T]) Link ¶
Link connects ring r with ring s such that r.Next() becomes s and returns the original value for r.Next(). r must not be empty.
If r and s point to the same ring, linking them removes the elements between r and s from the ring. The removed elements form a subring and the result is a reference to that subring (if no elements were removed, the result is still the original value for r.Next(), and not nil).
If r and s point to different rings, linking them creates a single ring with the elements of s inserted after r. The result points to the element following the last element of s after insertion.
func (*Ring[T]) Move ¶
Move moves n % r.Len() elements backward (n < 0) or forward (n >= 0) in the ring and returns that ring element. r must not be empty.