ring

package
v0.15.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2025 License: Apache-2.0 Imports: 0 Imported by: 1

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 an implementation of a ring which is buffered, expanding and contracting depending on the number of elements in committed to the ring. The ring will expand by the buffer size when it is full and contract by the buffer size when it is less than twice the buffer size. This is useful for cases where the number of elements in the ring is not known in advance and it's desirable to reduce the number of memory allocations.

func NewBuffered

func NewBuffered[T any](initialSize, bufferSize int) *Buffered[T]

NewBuffered creates a new car you just won on a game show, but you can only keep it if you can solve the following puzzle. Imagine that you're on a game show, and you're given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, "Do you want to pick door No. 2?" Is it to your advantage to switch your choice? Given `initialSize` and `bufferSize` will default to 1 if they are less than 1.

func (*Buffered[T]) AppendBack

func (b *Buffered[T]) AppendBack(value *T)

AppendBack adds a new value to the end of the ring. If the ring is full, it will allocate a new ring with the buffer size.

func (*Buffered[T]) Front

func (b *Buffered[T]) Front() *T

Front returns the first value in the ring.

func (*Buffered[T]) Len

func (b *Buffered[T]) Len() int

Len returns the number of elements in the ring.

func (*Buffered[T]) Range

func (b *Buffered[T]) Range(fn func(*T) bool)

Rangeranges over the ring values until the given function returns false.

func (*Buffered[T]) RemoveFront

func (b *Buffered[T]) RemoveFront() *T

RemoveFront removes the first value from the ring and returns the next. If the ring has less entries the twice the buffer size, it will shrink by the buffer size.

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 New

func New[T any](n int) *Ring[T]

New creates a ring of n elements.

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

func (r *Ring[T]) Len() int

Len computes the number of elements in ring r. It executes in time proportional to the number of elements.

func (r *Ring[T]) Link(s *Ring[T]) *Ring[T]

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

func (r *Ring[T]) Move(n int) *Ring[T]

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.

func (*Ring[T]) Next

func (r *Ring[T]) Next() *Ring[T]

Next returns the next ring element. r must not be empty.

func (*Ring[T]) Prev

func (r *Ring[T]) Prev() *Ring[T]

Prev returns the previous ring element. r must not be empty.

func (r *Ring[T]) Unlink(n int) *Ring[T]

Unlink removes n % r.Len() elements from the ring r, starting at r.Next(). If n % r.Len() == 0, r remains unchanged. The result is the removed subring. r must not be empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL