Documentation
¶
Index ¶
- type Buffer
- func (r *Buffer[T]) AddFirst(element T)
- func (r *Buffer[T]) AddLast(element T)
- func (r *Buffer[T]) Cap() int
- func (r *Buffer[T]) Discard()
- func (r *Buffer[T]) Get(pos int) T
- func (r *Buffer[T]) GetFirst() T
- func (r *Buffer[T]) GetLast() T
- func (r *Buffer[T]) Len() int
- func (r *Buffer[T]) RemoveFirst()
- func (r *Buffer[T]) RemoveLast()
- func (r *Buffer[T]) Reserve(n int)
- func (r *Buffer[T]) Reset()
- func (r *Buffer[T]) Resize(n int)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Buffer ¶
type Buffer[T any] struct { // contains filtered or unexported fields }
Buffer is a deque maintained over a ring buffer.
The zero value is ready to use. See MakeBuffer() for initializing a Buffer with pre-allocated space.
Note: it is backed by a slice (unlike container/ring/ring_buffer.go which is backed by a linked list). There is also a container/ring/buffer.go, that is backed by a slice and can both grow and shrink and uses bit arithmetic. We should replace this implementation with that one.
func MakeBuffer ¶
MakeBuffer creates a buffer.
scratch, if not nil, represents pre-allocated space that the Buffer takes ownership of. The whole backing array of the provided slice is taken over, included elements and available capacity.
func (*Buffer[T]) AddFirst ¶
func (r *Buffer[T]) AddFirst(element T)
AddFirst add element to the front of the Buffer and doubles it's underlying slice if necessary.
func (*Buffer[T]) AddLast ¶
func (r *Buffer[T]) AddLast(element T)
AddLast adds element to the end of the Buffer and doubles it's underlying slice if necessary.
func (*Buffer[T]) Discard ¶
func (r *Buffer[T]) Discard()
Discard is like Reset, except it also does Resize(0) to nil out the underlying slice. This makes the backing storage for the slice available to GC if nobody else is referencing it. This is useful if r is still referenced, but *r will be reassigned.
See also Reset and Resize.
func (*Buffer[T]) GetFirst ¶
func (r *Buffer[T]) GetFirst() T
GetFirst returns an element at the front of the Buffer.
func (*Buffer[T]) GetLast ¶
func (r *Buffer[T]) GetLast() T
GetLast returns an element at the front of the Buffer.
func (*Buffer[T]) RemoveFirst ¶
func (r *Buffer[T]) RemoveFirst()
RemoveFirst removes a single element from the front of the Buffer.
func (*Buffer[T]) RemoveLast ¶
func (r *Buffer[T]) RemoveLast()
RemoveLast removes a single element from the end of the Buffer.
func (*Buffer[T]) Reserve ¶
Reserve reserves the provided number of elements in the Buffer. It is illegal to reserve a size less than the r.Len().
If the Buffer already has a capacity of n or larger, this is a no-op.
func (*Buffer[T]) Reset ¶
func (r *Buffer[T]) Reset()
Reset makes Buffer treat its underlying memory as if it were empty. This allows for reusing the same memory again without explicitly removing old elements. Note that this does not nil out the elements, so they're not made available to GC.
See also Discard.
func (*Buffer[T]) Resize ¶
Resize changes the Buffer's storage to be of the specified size. It is illegal to resize to a size less than r.Len().
This is a more general version of Reserve: Reserve only ever grows the storage, whereas Resize() can also shrink it.
Note that, if n != r.Len(), Resize always allocates new storage, even when n is less than the current capacity. This can be useful to make the storage for a buffer that used to be large available for GC, but it can also be wasteful.