slice

package
v0.0.0-...-157c9c8 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IterFactory

func IterFactory[T any](s []T) liter.Factory[T]

IterFactory fulfills liter.Factory.

func IterSlice

func IterSlice[T any](i liter.Iter[T], buf []T) []T

IterSlice creates a slice from an liter.Iter. If the Iter fulfills Slicer, then the Slice method will be used. If it fulfill Lener, that will be used for buffer allocation.

func Len

func Len[T any](s []T) int

Len creates a strongly typed version of builtin len for slices.

func NewIter

func NewIter[T any](s []T) liter.Wrapper[T]

NewIter creates an Iter for iterating over the slice. Done defaults to a func that compares to the length of the slice.

Types

type Buffer

type Buffer[T any] []T

Buffer is used to provide a slice for re-use avoiding excessive allocation.

func NewBuffer

func NewBuffer[T any](buf []T) Buffer[T]

NewBuffer provides syntactic sugar by inferring the type.

func (Buffer[T]) Cap

func (buf Buffer[T]) Cap(c int) Slice[T]

Cap returns a buffer with at least capacity c and length 0. If the provided buffer has capacity, it will be used otherwise a new one is created.

func (Buffer[T]) Empty

func (buf Buffer[T]) Empty(c int) Slice[T]

BufferEmpty returns a zero length buffer with at least capacity c. If the provided buffer has capacity, it will be used otherwise a new one is created.

func (Buffer[T]) Lener

func (buf Buffer[T]) Lener(i interface{}) Slice[T]

Lener returns a zero length buffer. If i fulfils Lener, the capacity of the buffer will be at least that returned by Len. If not, the buffer is used with the size set to zero.

func (Buffer[T]) ReduceCapacity

func (buf Buffer[T]) ReduceCapacity(c int) Slice[T]

ReduceCapacity sets the capacity to a lower value. This can be useful when splitting a buffer to prevent use of the first part of the buffer from overflowing into the second part.

func (Buffer[T]) Slice

func (buf Buffer[T]) Slice(c int) Slice[T]

BufferSlice returns a buffer with length c. If the provided buffer has capacity, it will be used otherwise a new one is created.

func (Buffer[T]) Split

func (buf Buffer[T]) Split(c int) (Slice[T], Buffer[T])

BufferSplit a buffer returns two buffers from one. The frist buffer will have capacity c. The second buffer will have the remainder. If the provided buffer does not have a capacity a new buffer is created.

func (Buffer[T]) Zeros

func (buf Buffer[T]) Zeros(c int) Slice[T]

BufferZeros returns a buffer with length c with all values set to 0. If the provided buffer has capacity, it will be used otherwise a new one is created.

type Iter

type Iter[T any] struct {
	S []T
	I int
	// DoneFn determines when the iterator is done. If it does not check the
	// bounds, an out of bounds panic is likely.
	DoneFn func(*Iter[T]) bool
}

Iter wraps a slice to fulfillliter.Iter.

func (*Iter[T]) Cur

func (i *Iter[T]) Cur() (t T, done bool)

Cur fulfillsliter.Iter. It returns the value at the current index and the done bool. If it is done, it will return the zero value for the type.

func (*Iter[T]) Done

func (i *Iter[T]) Done() bool

Done fulfillsliter.Iter. It calls the underlying DoneFn.

func (*Iter[T]) Idx

func (i *Iter[T]) Idx() int

Idx fulfillsliter.Iter. It returns the current value of I.

func (*Iter[T]) Next

func (i *Iter[T]) Next() (t T, done bool)

Next fulfillsliter.Iter. It increments the index and returns the value at that index and the done bool. If it is done, it will return the zero value for the type.

func (*Iter[T]) Slice

func (i *Iter[T]) Slice(buf []T) []T

Slice fulfills Slicer. If a buffer is provided, a copy is made. If no buffer is provided, the underlying slice is returned.

func (*Iter[T]) Start

func (i *Iter[T]) Start() (t T, done bool)

Start fulfillsliter.Iter. It sets the index to zero and returns the first element in the slice. If it is done, it will return the zero value for the type.

type Lener

type Lener interface {
	Len() int
}

Lener allows an interface to show that it knows it's length.

type Less

type Less[T any] func(i, j T) bool

Less is used for sorting. Less does not inheirently mean "less than", only that when sorting the index should be less. For instance, when sorting from highest to lowest, the index of the greater value will be less that the value it is compared against.

func GT

func GT[T constraints.Ordered]() Less[T]

LT returns an instance of Less[T] that does a greater than comparison.

func LT

func LT[T constraints.Ordered]() Less[T]

LT returns an instance of Less[T] that does a less than comparison.

func (Less[T]) Sort

func (l Less[T]) Sort(s []T) []T

Sort the slice using the Less function.

type Slice

type Slice[T any] []T

Slice is a wrapper that provides helper methods

func Make

func Make[T any](ln, cp int) Slice[T]

Make a slice with the specified length and capacity. If capacity is set to 0, then ln will be used for capacity as well.

func New

func New[T any](s []T) Slice[T]

New is syntactic sugar to infer the type

func Transform

func Transform[In, Out any](in liter.Iter[In], fn func(In, int) (Out, bool)) Slice[Out]

Transform one slice to another. The transformation function's second return is a bool indicating if the returned value should be included in the result. The returned Slice is sized exactly to the output.

func TransformSlice

func TransformSlice[In, Out any](in []In, fn func(In, int) (Out, bool)) Slice[Out]

Transform one slice to another. The transformation function's second return is a bool indicating if the returned value should be included in the result. The returned Slice is sized exactly to the output.

func (Slice[T]) AppendNotZero

func (s Slice[T]) AppendNotZero(ts ...T) []T

AppendNotZero will append any values from ts that are not the zero value for the type. Particularly useful for appending not nil values.

func (Slice[T]) AtIdx

func (s Slice[T]) AtIdx(idx int) T

AtIdx returns the value at idx. Fulfills list.List.

func (Slice[T]) Buffer

func (s Slice[T]) Buffer() Buffer[T]

func (Slice[T]) CheckCapacity

func (s Slice[T]) CheckCapacity(c int) Slice[T]

CheckCapacity ensures that Slice s has capacity c. If not, a new slice is created with capacity c and the slice is copied.

func (Slice[T]) Clone

func (s Slice[T]) Clone() Slice[T]

Clone a slice.

func (Slice[T]) ForAll

func (s Slice[T]) ForAll(fn func(idx int, t T)) *sync.WaitGroup

ForAll runs a Go routine for each element in s, passing it into fn. A WaitGroup is returned that will finish when all Go routines return.

func (Slice[T]) Idx

func (s Slice[T]) Idx(idx int) (int, bool)

Idx provides a relative index to the slice. So a value of -1 will return the last index. The bool indicates if the index is in range.

func (Slice[T]) IdxCheck

func (s Slice[T]) IdxCheck(idx int) bool

IdxCheck returns false if idx is out of the range of s.

func (Slice[T]) Iter

func (s Slice[T]) Iter() liter.Wrapper[T]

func (Slice[T]) IterFactory

func (s Slice[T]) IterFactory() (i liter.Iter[T], t T, done bool)

func (Slice[T]) Len

func (s Slice[T]) Len() int

Len returns the length of the slice. Fulfills list.List.

func (Slice[T]) Pop

func (s Slice[T]) Pop() (T, Slice[T])

Pop returns the last element of the slice and the slice resized to remove that element. If the size of the slice is zero, the zero value for the type is returned.

func (Slice[T]) Remove

func (s Slice[T]) Remove(idxs ...int) Slice[T]

Remove values at given indicies by swapping them with values from the end and truncating the slice. Values less than zero or greater than the length of the list are ignored. Note that idxs is reordered so if that is a slice passed in and the order is important, pass in a copy.

func (Slice[T]) RemoveOrdered

func (s Slice[T]) RemoveOrdered(idxs ...int) Slice[T]

RemoveOrdered preserves the order of the slice while removing the values at the given indexes.

func (Slice[T]) Search

func (s Slice[T]) Search(fn func(T) bool) int

Search wraps sort.Search

func (Slice[T]) Shift

func (s Slice[T]) Shift() (T, Slice[T])

Shift returns the first element of the slice and the slice resized to remove that element. If the size of the slice is zero, the zero value for the type is returned.

func (Slice[T]) Sort

func (s Slice[T]) Sort(less Less[T]) Slice[T]

Sort wraps slice.Sort. Sorts the Slice in place. The slice is also returned for chaining.

func (Slice[T]) Swap

func (s Slice[T]) Swap(i, j int)

Swaps two values in the slice.

type Slicer

type Slicer[T any] interface {
	Slice(buf []T) []T
}

Slicer allows an interface to show that it has an efficient way to convert itself to a slice.

Jump to

Keyboard shortcuts

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