Documentation
¶
Index ¶
- func BinarySearch[T typez.Ordered](s []T, v T) (index int, found bool)
- func BinarySearchFunc[T, V any](s []T, v V, fn func(T, V) int) (index int, found bool)
- func Chunk[T any](s []T, chunkSize int) [][]T
- func ChunkProcess[T any](s []T, chunkSize int, process func([]T) error) error
- func Contains[T comparable](s []T, v T) bool
- func ContainsFunc[T any](s []T, fn func(T) bool) bool
- func Copy[T any](s []T, start, length int) []T
- func Diff[T comparable](dst, s1, s2 []T) []T
- func DiffInPlaceFirst[T comparable](s1, s2 []T) []T
- func DiffSorted[T typez.Ordered](dst, s1, s2 []T) []T
- func DiffSortedInPlaceFirst[T typez.Ordered](s1, s2 []T) []T
- func Equal[T comparable](s1, s2 []T) bool
- func Filter[T any](dst, s []T, predicate func(T) bool) []T
- func FilterInPlace[T any](s []T, predicate func(T) bool) []T
- func Index[T comparable](s []T, v T) int
- func IndexFunc[T any](s []T, fn func(T) bool) int
- func Intersect[T comparable](dst, s1, s2 []T) []T
- func IntersectInPlaceFirst[T comparable](s1, s2 []T) []T
- func IntersectSortedMultiset[T typez.Ordered](dst, s1, s2 []T) []T
- func IntersectSortedSet[T typez.Ordered](dst, s1, s2 []T) []T
- func Remove[T any](s []T, index int) ([]T, T, bool)
- func SubSlice[T any](s []T, start int, end int) []T
- func Unique[T comparable](dst, s []T) []T
- func UniqueByKey[T any, K comparable](dst, s []T, keyFn func(T) K) []T
- func UniqueByKeyInPlace[T any, K comparable](s []T, keyFn func(T) K) []T
- func UniqueInPlace[T comparable](s []T) []T
- func UniqueInPlaceSorted[T comparable](s []T) []T
- func UniqueSorted[T comparable](dst, s []T) []T
- func Values[T, V any](fn func(T) V, ss ...[]T) []V
- type FlexSlice
- func (f *FlexSlice[T]) All() iter.Seq[T]
- func (f *FlexSlice[T]) AllWithIndex() iter.Seq2[int, T]
- func (f *FlexSlice[T]) Append(values ...T)
- func (f *FlexSlice[T]) Back() (T, bool)
- func (f *FlexSlice[T]) Cap() int
- func (f *FlexSlice[T]) Clear(shrink bool)
- func (f *FlexSlice[T]) Front() (T, bool)
- func (f *FlexSlice[T]) Get(index int) (T, bool)
- func (f *FlexSlice[T]) Grow(n uint)
- func (f *FlexSlice[T]) Init(capacity int)
- func (f *FlexSlice[T]) InsertAt(index int, values ...T) bool
- func (f *FlexSlice[T]) IsEmpty() bool
- func (f *FlexSlice[T]) IsFull() bool
- func (f *FlexSlice[T]) Join(other FlexSlice[T])
- func (f *FlexSlice[T]) Len() int
- func (f *FlexSlice[T]) Pop() (T, bool)
- func (f *FlexSlice[T]) Prepend(values ...T)
- func (f *FlexSlice[T]) Range(fn func(index int, value T) bool)
- func (f *FlexSlice[T]) Remove(index int) (T, bool)
- func (f *FlexSlice[T]) RevAll() iter.Seq[T]
- func (f *FlexSlice[T]) RevAllWithIndex() iter.Seq2[int, T]
- func (f *FlexSlice[T]) RevRange(fn func(index int, value T) bool)
- func (f *FlexSlice[T]) Set(index int, value T) bool
- func (f *FlexSlice[T]) Shift() (T, bool)
- func (f *FlexSlice[T]) Shrink() bool
- func (f *FlexSlice[T]) SubSlice(start, end int) FlexSlice[T]
- func (f *FlexSlice[T]) ToSlice() []T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BinarySearch ¶ added in v0.0.22
BinarySearch performs a binary search on a sorted slice s for the value v.
func BinarySearchFunc ¶ added in v0.0.22
BinarySearchFunc performs a binary search on a sorted slice s using a custom comparison function fn for the value v.
func ChunkProcess ¶
ChunkProcess splits slice s into chunks of size chunkSize, and calls process on each chunk.
func Contains ¶
func Contains[T comparable](s []T, v T) bool
Contains returns true if v is present in s.
func ContainsFunc ¶ added in v0.0.15
ContainsFunc returns true if there is an element in s that satisfies f(s[i]).
func Copy ¶
Copy copies length elements from s starting at position start. If length is negative, it will copy all elements from start to the end of s.
func Diff ¶
func Diff[T comparable](dst, s1, s2 []T) []T
Diff compares slices s1 and s2, puts elements from s1 that do not exist in s2 into dst, and returns it.
func DiffInPlaceFirst ¶
func DiffInPlaceFirst[T comparable](s1, s2 []T) []T
DiffInPlaceFirst compares s1 and s2, moves elements from s1 that not in s2 to the front of s1, and returns this portion of s1. Please note that the order of elements in s1 will be altered.
func DiffSorted ¶ added in v0.0.25
DiffSorted compares two sorted slices s1 and s2, puts elements from s1 that do not exist in s2 into dst, and returns it. It requires ascending sorted slices.
func DiffSortedInPlaceFirst ¶ added in v0.0.25
DiffSortedInPlaceFirst compares two sorted slices s1 and s2, moves elements from s1 that do not exist in s2 to the front of s1, and returns this portion of s1. The returned slice maintains the sorted order. It requires ascending sorted slices.
Note: This function only reorders elements in s1. All original elements are preserved, but the elements beyond the returned slice length are no longer sorted.
func Equal ¶
func Equal[T comparable](s1, s2 []T) bool
Equal compares slices s1 and s2 element-wise and returns true if they have the same length and all corresponding elements are equal.
func FilterInPlace ¶
FilterInPlace moves elements that satisfy predicate to the front of s, and returns this portion of s. Please note that the order of elements in s will be altered.
func Index ¶
func Index[T comparable](s []T, v T) int
Index returns the index of the first instance of v in s, or -1 if v is not present in s.
func IndexFunc ¶ added in v0.0.15
IndexFunc returns the first index i satisfying f(s[i]), or -1 if there is no such index.
func Intersect ¶
func Intersect[T comparable](dst, s1, s2 []T) []T
Intersect compares slices s1 and s2, puts elements from s1 that are also present in s2 into dst, and returns it. This is not a mathematical intersection, as it will include duplicate elements from s1. For example, s1 = [1,2,2,3], s2 = [2,2,4], the result will be [2,2].
func IntersectInPlaceFirst ¶
func IntersectInPlaceFirst[T comparable](s1, s2 []T) []T
IntersectInPlaceFirst compares s1 and s2, moves elements from s1 that are also present in s2 to the front of s1, and returns this portion of s1. Please note that the order of elements in s1 will be altered. This is not a mathematical intersection, as it will include duplicate elements from s1.
func IntersectSortedMultiset ¶ added in v0.0.25
IntersectSortedMultiset compares two sorted slices s1 and s2, and puts the multiset intersection into dst. Its behavior differs from Intersect. For example: s1 = [1,2,2,3], s2 = [2,2,2,4], the result will be [2,2]. It requires ascending sorted slices.
func IntersectSortedSet ¶ added in v0.0.25
IntersectSortedSet compares two sorted slices s1 and s2, puts elements that are present in both into dst, and returns it. Its behavior differs from Intersect. For example: s1 = [1,2,2,3], s2 = [2,2,2,4], the result will be [2]. The result will not contain duplicate elements. It requires ascending sorted slices.
func SubSlice ¶ added in v0.0.17
SubSlice returns a slice of s from start to end. If end is negative, it will return all elements from start to the end of s.
func Unique ¶
func Unique[T comparable](dst, s []T) []T
Unique compares slice s, puts unique elements into dst, and returns it.
func UniqueByKey ¶ added in v0.0.15
func UniqueByKey[T any, K comparable](dst, s []T, keyFn func(T) K) []T
UniqueByKey through keyFn get the key of slice s, puts unique elements into dst, and returns it.
func UniqueByKeyInPlace ¶ added in v0.0.15
func UniqueByKeyInPlace[T any, K comparable](s []T, keyFn func(T) K) []T
UniqueByKeyInPlace through keyFn get the key of slice s, moves unique elements to the front of s, and returns this portion of s.
func UniqueInPlace ¶
func UniqueInPlace[T comparable](s []T) []T
UniqueInPlace compares slice s, moves unique elements to the front of s, and returns this portion of s. Please note that the order of elements in s will be altered.
func UniqueInPlaceSorted ¶ added in v0.0.25
func UniqueInPlaceSorted[T comparable](s []T) []T
UniqueInPlaceSorted moves unique elements in a sorted slice s to the front and returns this portion of s. It requires ascending sorted slices.
WARNING: Unlike other InPlace functions that only reorder elements, this function overwrites duplicate elements in the original slice. Elements beyond the returned slice length are not preserved. If you need to preserve all original elements, consider using UniqueSorted instead.
func UniqueSorted ¶ added in v0.0.25
func UniqueSorted[T comparable](dst, s []T) []T
UniqueSorted puts unique elements from a sorted slice s into dst and returns it. It requires ascending sorted slices.
Types ¶
type FlexSlice ¶ added in v0.0.17
type FlexSlice[T any] struct { // contains filtered or unexported fields }
func NewFlexSlice ¶ added in v0.0.22
func (*FlexSlice[T]) AllWithIndex ¶ added in v0.0.22
func (*FlexSlice[T]) Append ¶ added in v0.0.17
func (f *FlexSlice[T]) Append(values ...T)
Append adds elements to the end of the flex slice, growing it if necessary.
func (*FlexSlice[T]) Back ¶ added in v0.0.22
Back returns the last element of the flex slice without removing it.
func (*FlexSlice[T]) Clear ¶ added in v0.0.22
Clear clears the flex slice, optionally shrinking its capacity.
func (*FlexSlice[T]) Front ¶ added in v0.0.22
Front returns the first element of the flex slice without removing it.
func (*FlexSlice[T]) Get ¶ added in v0.0.17
Get returns the element at the given index in the deque.
func (*FlexSlice[T]) Grow ¶ added in v0.0.22
Grow increases the capacity of the flex slice by the specified amount.
func (*FlexSlice[T]) Init ¶ added in v0.0.22
Init initializes or clears the flex slice with the given capacity.
func (*FlexSlice[T]) InsertAt ¶ added in v0.0.22
InsertAt inserts elements at the specified index in the flex slice. If the index is out of bounds, it returns false.
func (*FlexSlice[T]) Join ¶ added in v0.0.22
Join appends all elements from another FlexSlice to the end of this FlexSlice.
func (*FlexSlice[T]) Pop ¶ added in v0.0.17
Pop removes and returns the last element of the flex slice.
func (*FlexSlice[T]) Prepend ¶ added in v0.0.17
func (f *FlexSlice[T]) Prepend(values ...T)
Prepend adds elements to the front of the flex slice, growing it if necessary.
func (*FlexSlice[T]) Range ¶ added in v0.0.22
Range iterates over the elements in the flex slice, calling the provided function for each element.
func (*FlexSlice[T]) Remove ¶ added in v0.0.17
Remove removes and returns the element at the specified index in the flex slice.
func (*FlexSlice[T]) RevAllWithIndex ¶ added in v0.0.22
func (*FlexSlice[T]) RevRange ¶ added in v0.0.22
RevRange iterates over the elements in the flex slice in reverse order.
func (*FlexSlice[T]) Set ¶ added in v0.0.22
Set sets the element at the given index in the flex slice to the specified value.
func (*FlexSlice[T]) Shift ¶ added in v0.0.17
Shift removes and returns the first element of the flex slice.
func (*FlexSlice[T]) Shrink ¶ added in v0.0.22
Shrink reduces the capacity of the flex slice if it is more than 4 times larger than the number of elements.
func (*FlexSlice[T]) SubSlice ¶ added in v0.0.17
SubSlice returns a new FlexSlice containing elements from the specified range. If the end is negative, it will return all elements from start to the end of the flex slice. SubSlice reuses the original FlexSlice's values slice, so modify maybe affect the original slice.