slicez

package
v0.0.27 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2025 License: MIT Imports: 3 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BinarySearch added in v0.0.22

func BinarySearch[T typez.Ordered](s []T, v T) (index int, found bool)

BinarySearch performs a binary search on a sorted slice s for the value v.

func BinarySearchFunc added in v0.0.22

func BinarySearchFunc[T, V any](s []T, v V, fn func(T, V) int) (index int, found bool)

BinarySearchFunc performs a binary search on a sorted slice s using a custom comparison function fn for the value v.

func Chunk

func Chunk[T any](s []T, chunkSize int) [][]T

Chunk splits slice s into chunks of size chunkSize, and returns the result.

func ChunkProcess

func ChunkProcess[T any](s []T, chunkSize int, process func([]T) error) error

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

func ContainsFunc[T any](s []T, fn func(T) bool) bool

ContainsFunc returns true if there is an element in s that satisfies f(s[i]).

func Copy

func Copy[T any](s []T, start, length int) []T

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

func DiffSorted[T typez.Ordered](dst, s1, s2 []T) []T

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

func DiffSortedInPlaceFirst[T typez.Ordered](s1, s2 []T) []T

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 Filter

func Filter[T any](dst, s []T, predicate func(T) bool) []T

Filter puts elements from s that satisfy predicate into dst, and returns it.

func FilterInPlace

func FilterInPlace[T any](s []T, predicate func(T) bool) []T

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

func IndexFunc[T any](s []T, fn func(T) bool) int

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

func IntersectSortedMultiset[T typez.Ordered](dst, s1, s2 []T) []T

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

func IntersectSortedSet[T typez.Ordered](dst, s1, s2 []T) []T

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 Remove added in v0.0.17

func Remove[T any](s []T, index int) ([]T, T, bool)

Remove removes the element at the given index from s.

func SubSlice added in v0.0.17

func SubSlice[T any](s []T, start int, end int) []T

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.

func Values added in v0.0.15

func Values[T, V any](fn func(T) V, ss ...[]T) []V

Values returns a new slice containing the values returned by applying fn to each element of s.

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 NewFlexSlice[T any](capacity int) FlexSlice[T]

func (*FlexSlice[T]) All added in v0.0.22

func (f *FlexSlice[T]) All() iter.Seq[T]

func (*FlexSlice[T]) AllWithIndex added in v0.0.22

func (f *FlexSlice[T]) AllWithIndex() iter.Seq2[int, T]

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

func (f *FlexSlice[T]) Back() (T, bool)

Back returns the last element of the flex slice without removing it.

func (*FlexSlice[T]) Cap added in v0.0.22

func (f *FlexSlice[T]) Cap() int

Cap returns the capacity of the flex slice.

func (*FlexSlice[T]) Clear added in v0.0.22

func (f *FlexSlice[T]) Clear(shrink bool)

Clear clears the flex slice, optionally shrinking its capacity.

func (*FlexSlice[T]) Front added in v0.0.22

func (f *FlexSlice[T]) Front() (T, bool)

Front returns the first element of the flex slice without removing it.

func (*FlexSlice[T]) Get added in v0.0.17

func (f *FlexSlice[T]) Get(index int) (T, bool)

Get returns the element at the given index in the deque.

func (*FlexSlice[T]) Grow added in v0.0.22

func (f *FlexSlice[T]) Grow(n uint)

Grow increases the capacity of the flex slice by the specified amount.

func (*FlexSlice[T]) Init added in v0.0.22

func (f *FlexSlice[T]) Init(capacity int)

Init initializes or clears the flex slice with the given capacity.

func (*FlexSlice[T]) InsertAt added in v0.0.22

func (f *FlexSlice[T]) InsertAt(index int, values ...T) bool

InsertAt inserts elements at the specified index in the flex slice. If the index is out of bounds, it returns false.

func (*FlexSlice[T]) IsEmpty added in v0.0.22

func (f *FlexSlice[T]) IsEmpty() bool

IsEmpty returns true if the flex slice is empty.

func (*FlexSlice[T]) IsFull added in v0.0.22

func (f *FlexSlice[T]) IsFull() bool

IsFull returns true if the flex slice is full.

func (*FlexSlice[T]) Join added in v0.0.22

func (f *FlexSlice[T]) Join(other FlexSlice[T])

Join appends all elements from another FlexSlice to the end of this FlexSlice.

func (*FlexSlice[T]) Len added in v0.0.17

func (f *FlexSlice[T]) Len() int

Len returns the number of elements in the flex slice.

func (*FlexSlice[T]) Pop added in v0.0.17

func (f *FlexSlice[T]) Pop() (T, bool)

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

func (f *FlexSlice[T]) Range(fn func(index int, value T) bool)

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

func (f *FlexSlice[T]) Remove(index int) (T, bool)

Remove removes and returns the element at the specified index in the flex slice.

func (*FlexSlice[T]) RevAll added in v0.0.22

func (f *FlexSlice[T]) RevAll() iter.Seq[T]

func (*FlexSlice[T]) RevAllWithIndex added in v0.0.22

func (f *FlexSlice[T]) RevAllWithIndex() iter.Seq2[int, T]

func (*FlexSlice[T]) RevRange added in v0.0.22

func (f *FlexSlice[T]) RevRange(fn func(index int, value T) bool)

RevRange iterates over the elements in the flex slice in reverse order.

func (*FlexSlice[T]) Set added in v0.0.22

func (f *FlexSlice[T]) Set(index int, value T) bool

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

func (f *FlexSlice[T]) Shift() (T, bool)

Shift removes and returns the first element of the flex slice.

func (*FlexSlice[T]) Shrink added in v0.0.22

func (f *FlexSlice[T]) Shrink() bool

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

func (f *FlexSlice[T]) SubSlice(start, end int) FlexSlice[T]

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.

func (*FlexSlice[T]) ToSlice added in v0.0.22

func (f *FlexSlice[T]) ToSlice() []T

ToSlice returns a slice containing all elements in the flex slice in order.

Jump to

Keyboard shortcuts

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