list

package
v0.0.0-...-375a761 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidPosition   = errors.New("invalid element position")
	ErrInvalidRange      = errors.New("invalid range")
	ErrInvalidAmount     = errors.New("invalid amount of elements")
	ErrInvalidAllocation = errors.New("insufficient space allocated")
)

Package level errors for List.

Functions

func AllocDefault

func AllocDefault[T any](min, max int) ([]T, error)

AllocDefault is the default AllocFunc[T].

func FixAllocSize

func FixAllocSize(newIntendedCap, maxCap int) int

FixAllocSize is a helper for implementators of AllocFunc to make sure the max capacity is not exceeded.

Types

type AllocFunc

type AllocFunc[T any] func(min, max int) ([]T, error)

AllocFunc is a function that allocates a new slice that needs to hold at least min elements. If max >=0, then expect min<=max, and the returned slice should not have a length greater than max (see FixAllocSize helper). The returned slice will not be resliced further than its returned length. Implementations should not hold references to the returned slice. Note that the returned slice will be lazily zeroed as needed, so if any references need to be clread from previous work, then it should be done before returning it.

type CompareFunc

type CompareFunc[T any] func(x T, y T) int

CompareFunc shoud return:

-1 if x is less than y,
0 if x equals y,
+1 if x is greater than y.

It must be a strict weak ordering for sorting methods to work properly. See https://en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderings.

func (CompareFunc[T]) Inverse

func (f CompareFunc[T]) Inverse(x, y T) int

Inverse inverts the operands of f to achieve the opposite ordering.

type Heap

type Heap[T any] struct {
	Ordered[T]
}

Heap uses container/heap to implement a heap.

func NewHeap

func NewHeap[T any](cmp CompareFunc[T]) Heap[T]

NewHeap creates a new Heap using the given CompareFunc.

func (Heap[T]) Fix

func (h Heap[T]) Fix(i int)

Fix re-establishes the heap ordering after the element at index i has changed its value. Changing the value of the element at index i and then calling Fix is equivalent to, but less expensive than, calling Remove(h, i) followed by a Push of the new value.

func (Heap[T]) Init

func (h Heap[T]) Init()

Init establishes the heap invariants required by the other heap methods. Init is idempotent with respect to the heap invariants and may be called whenever the heap invariants may have been invalidated.

func (Heap[T]) Pop

func (h Heap[T]) Pop() T

Pop removes and returns the minimum element (according to Less) from the heap. Pop is equivalent to Remove(h, 0).

func (Heap[T]) Push

func (h Heap[T]) Push(v T)

Push pushes the element x onto the heap. The complexity is O(log n) where n = h.Len().

func (Heap[T]) Remove

func (h Heap[T]) Remove(i int) T

Remove removes and returns the element at index i from the heap.

func (Heap[T]) UnmarshalJSON

func (h Heap[T]) UnmarshalJSON(b []byte) error

UnmarshalJSON clears the heap, reads a JSON Array as a list of elements, and calls Init.

type List

type List[T any] struct {
	// AllocFunc allows customizing allocation of a new slice when more space
	// is needed. If nil, AllocDefault is used.
	AllocFunc[T]

	// FreeFunc will be called, if set, with the old slice after migrating to a
	// newly allocated slice. This is useful if you want to put a slice back
	// into a pool or do something else with it, but all the items that the
	// list had used will have been zeroed out.
	FreeFunc func([]T)

	// StringFunc allows customizing how elements are converted to string when
	// printing the list. The default is using fmt.Sprintf("%v", element).
	StringFunc func(T) string
	// contains filtered or unexported fields
}

List is a slice-based list container, indexed from its back to its front starting at zero. Not safe for concurrent use. It zeroes elements that are removed from the list. The implementation is panic free, so errors are returned for the cases where bounds and other input need to be validated.

func New

func New[T any](s []T, useValues bool) *List[T]

New creates a new List using the given slice as the underlying data. If useValues is true, then the list will use all the elements of the slice as if they had been inserted in an empty list with InsertFront. Otherwise, the list will ignore the current elements and be empty. You should not hold references to s after calling this function. If useValues is false, the elements are not zeroed.

func NewN

func NewN[T any](s []T, back, length int) (*List[T], error)

NewN is like New, but allows specifying the index in the slice that would be the back of the list, and the number of elements that would be part of the new list. The value of length must be in the range [0, len(s)]. Note that you can set length and back in a way that would wrap the slice, which is ok. You should not hold references to s after calling this function. The elemetns that are not part of the new list are not zeroed.

func (*List[T]) Append

func (l *List[T]) Append(s ...T) error

Append inserts the given elements in the front.

func (*List[T]) At

func (l *List[T]) At(i int) (v T)

At returns the element at the given position, which can wrap the list from either side. This means that At(-1) is the same as At(l.Len()-1). It returns the zero value if the list is empty.

func (*List[T]) Back

func (l *List[T]) Back() T

Back returns the element at the back of the list. If the list is empty, it returns the zero value. It is equivalent to l.At(0).

func (*List[T]) Cap

func (l *List[T]) Cap() int

Cap returns the current total capacity.

func (*List[T]) Clear

func (l *List[T]) Clear() int

Clear removes all the elements in the list and returns the number of elements removed.

func (*List[T]) CopyTo

func (l *List[T]) CopyTo(s []T, i, n int) error

CopyTo copies at most n elements starting at index i to the given slice, and returns the number of copied elements. If j<i, then it wraps the list.

func (*List[T]) Delete

func (l *List[T]) Delete(i, j int) error

Delete removes the items in the given range.

func (*List[T]) Free

func (l *List[T]) Free() int

Free returns the number of elements that can be added to the list without a new allocation.

func (*List[T]) Front

func (l *List[T]) Front() T

Front returns the element at the front of the list. If the list is empty, it returns the zero value. It is equivalent to l.At(-1).

func (*List[T]) Grow

func (l *List[T]) Grow(n int) error

Grow makes sure that the list has capacity for at least n new elements. If l.Free()<n, then a new slice will be allocated and the list migrated to it.

func (*List[T]) GrowRange

func (l *List[T]) GrowRange(min, max int) error

GrowRange makes sure that the list has capacity for at least min and at most max new elements. If l.Free() is not in the range [min, max], then a new slice will be allocated and the list migrated to it.

func (*List[T]) Heap

func (l *List[T]) Heap(cmp CompareFunc[T]) Heap[T]

Heap initializes and returns a heap from the current List. the list will be shared, so if you change it outside of the returned heap, you will need to call fix or init on the returned heap to reconcile it.

func (*List[T]) Insert

func (l *List[T]) Insert(i int, s ...T) error

Insert inserts the given elements at position i.

func (*List[T]) Len

func (l *List[T]) Len() int

Len returns the number of elements in the list.

func (List[T]) MarshalJSON

func (l List[T]) MarshalJSON() ([]byte, error)

MarshalJSON marshals the list as a JSON Array.

func (*List[T]) Ordered

func (l *List[T]) Ordered(cmp CompareFunc[T]) Ordered[T]

Ordered returns an Ordered based on this list and the given CompareFunc.

func (*List[T]) Pop

func (l *List[T]) Pop() T

Pop removes the element at the front of the list and returns it. If the list is empty, it returns the zero value and does nothing.

func (*List[T]) Push

func (l *List[T]) Push(v T)

Push pushes the given element to the front of the list.

func (*List[T]) Replace

func (l *List[T]) Replace(i, j int, s ...T) error

Replace replaces the elements in the given range with the provided ones.

func (*List[T]) Rotate

func (l *List[T]) Rotate(n int)

Rotate rotates the list n elements, which can be negative. It is O(1).

func (*List[T]) Shuffle

func (l *List[T]) Shuffle()

Shuffle pseudo-randomizes the order of elements using the default math/rand.Source.

func (*List[T]) ShuffleRange

func (l *List[T]) ShuffleRange(i, j int) error

ShuffleN is like Shuffle, but allows specifying a specific range to be shuffled.

func (*List[T]) ShuffleRangeRand

func (l *List[T]) ShuffleRangeRand(r *rand.Rand, i, j int) error

ShuffleRangeRand is like ShuffleRange but allows specifying an alternative *math/rand.Rand.

func (*List[T]) String

func (l *List[T]) String() string

String converts a list into a human-readable form. You can control how individual elements are printed by setting the StringFunc member of the list.

func (*List[T]) StringRange

func (l *List[T]) StringRange(i, n int) (string, error)

StringRange is like String but only for the given range. If j<i, then it wraps the list.

func (*List[T]) Swap

func (l *List[T]) Swap(i, j int)

Swap swaps the i-eth and j-eth elements. If either of the elements is out of range, it's a nop. Use SwapOK if you need to know if the elements were swapped.

func (*List[T]) SwapOK

func (l *List[T]) SwapOK(i, j int) (bool, error)

SwapOK swaps the i-eth and j-eth elements. If i==j, it's a nop and returns false. Otherwise, it returns true and swaps the elements.

func (*List[T]) UnmarshalJSON

func (l *List[T]) UnmarshalJSON(b []byte) error

UnmarshalJSON clears the list and reads a JSON Array as a list of elements.

func (*List[T]) Val

func (l *List[T]) Val(i int) (v T, ok bool)

Val returns the element at the given position and true, if it exists. Otherwise, it returns the zero value and false.

type Ordered

type Ordered[T any] struct {
	*List[T]
	// contains filtered or unexported fields
}

Ordered is a List whose elements can be compared, and it satisfies sort.Interface.

func NewOrdered

func NewOrdered[T any](cmp CompareFunc[T]) Ordered[T]

NewOrdered creates a new Ordered using the given CompareFunc.

func (Ordered[T]) Compare

func (o Ordered[T]) Compare(i, j int) int

Compare returns:

-1 if the element at position i is less than the element at position j,
0 if the element at position i equals the element at position j, or if the
list is empty,
+1 if the element at position i is greater than the element at position j.

func (Ordered[T]) Contains

func (o Ordered[T]) Contains(v T) bool

Contains returns whether v is found in the data.

func (Ordered[T]) Find

func (o Ordered[T]) Find(v T) (i int, found bool)

Find uses binary search to find and return the smallest index i at which the list element is >= v.

func (Ordered[T]) Heap

func (o Ordered[T]) Heap() Heap[T]

heap initializes and returns a heap from the current Ordered. the list will be shared, so if you change it outside of the returned heap, you will need to call fix or init on the returned heap to reconcile it.

func (Ordered[T]) Invert

func (o Ordered[T]) Invert() Ordered[T]

Invert returns an Ordered with the comparison function inverted, reusing the same underlying data. Example:

o := NewOrdered[int](cmp.Compare)
o.InsertFront(5, 3, 8, 9)
fmt.Println(o) // [5, 3, 8, 9]
fmt.Println(o.Heap().Pop())  // 3, true
fmt.Println(o.Invert().Heap().Pop())  // 9, true
o.Sort()
fmt.Println(o) // [5, 8]
o.Invert().Sort()
fmt.Println(o) // [8, 5]

func (Ordered[T]) IsSorted

func (o Ordered[T]) IsSorted() bool

IsSorted reports whether data is sorted in ascending order.

func (Ordered[T]) Less

func (o Ordered[T]) Less(i, j int) bool

Less returns whether the given element at i is less than the element at j. If the list is empty, it always returns false.

func (Ordered[T]) Sort

func (o Ordered[T]) Sort()

Sort sorts data in ascending order as determined by the Less method. The sort is not guaranteed to be stable.

func (Ordered[T]) SortStable

func (o Ordered[T]) SortStable()

SortStable sorts data in ascending order as determined by the Less method, while keeping the original order of equal elements.

Jump to

Keyboard shortcuts

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