aoslices

package
v0.0.0-...-fe7a19e Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2022 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package aoslices defines various append-only functions useful with slices of any type.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BinarySearch

func BinarySearch[E constraints.Ordered](x Slice[E], target E) (int, bool)

BinarySearch searches for target in a sorted slice and returns the position where target is found, or the position where target would appear in the sort order; it also returns a bool saying whether the target is really found in the slice. The slice must be sorted in increasing order.

func BinarySearchFunc

func BinarySearchFunc[E any](x Slice[E], target E, cmp func(E, E) int) (int, bool)

BinarySearchFunc works like BinarySearch, but uses a custom comparison function. The slice must be sorted in increasing order, where "increasing" is defined by cmp. cmp(a, b) is expected to return an integer comparing the two parameters: 0 if a == b, a negative number if a < b and a positive number if a > b.

func Clone

func Clone[E any](s Slice[E]) []E

Clone returns a mutable copy of the slice. The elements are copied using assignment, so this is a shallow clone.

func Compare

func Compare[E constraints.Ordered](s1, s2 Slice[E]) int

Compare compares the elements of s1 and s2. The elements are compared sequentially, starting at index 0, until one element is not equal to the other. The result of comparing the first non-matching elements is returned. If both slices are equal until one of them ends, the shorter slice is considered less than the longer one. The result is 0 if s1 == s2, -1 if s1 < s2, and +1 if s1 > s2. Comparisons involving floating point NaNs are ignored.

func CompareFunc

func CompareFunc[E1 any, E2 any](s1 Slice[E1], s2 Slice[E2], cmp func(E1, E2) int) int

CompareFunc is like Compare but uses a comparison function on each pair of elements. The elements are compared in increasing index order, and the comparisons stop after the first time cmp returns non-zero. The result is the first non-zero result of cmp; if cmp always returns 0 the result is 0 if len(s1) == len(s2), -1 if len(s1) < len(s2), and +1 if len(s1) > len(s2).

func Contains

func Contains[E comparable](s Slice[E], v E) bool

Contains reports whether v is present in s.

func Copy

func Copy[E any](dst []E, src Slice[E]) int

Copy copies elements from a source slice into a destination slice. The source and destination may overlap. Copy returns the number of elements copied, which will be the minimum of len(src) and len(dst).

func Equal

func Equal[E comparable](s1, s2 Slice[E]) bool

Equal reports whether two slices are equal: the same length and all elements equal. If the lengths are different, Equal returns false. Otherwise, the elements are compared in increasing index order, and the comparison stops at the first unequal pair. Floating point NaNs are not considered equal.

func EqualFunc

func EqualFunc[E1, E2 any](s1 Slice[E1], s2 Slice[E2], eq func(E1, E2) bool) bool

EqualFunc reports whether two slices are equal using a comparison function on each pair of elements. If the lengths are different, EqualFunc returns false. Otherwise, the elements are compared in increasing index order, and the comparison stops at the first index for which eq returns false.

func Index

func Index[E comparable](s Slice[E], v E) int

Index returns the index of the first occurrence of v in s, or -1 if not present.

func IndexFunc

func IndexFunc[E any](s Slice[E], f func(E) bool) int

IndexFunc returns the first index i satisfying f(s[i]), or -1 if none do.

func IsSorted

func IsSorted[E constraints.Ordered](x Slice[E]) bool

IsSorted reports whether x is sorted in ascending order.

func IsSortedFunc

func IsSortedFunc[E any](x Slice[E], less func(a, b E) bool) bool

IsSortedFunc reports whether x is sorted in ascending order, with less as the comparison function.

Types

type Slice

type Slice[E any] struct {
	// contains filtered or unexported fields
}

Slice wraps an append-only slice.

func Clip

func Clip[E any](s Slice[E]) Slice[E]

Clip removes unused capacity from the slice, returning s[:len(s):len(s)].

func Grow

func Grow[E any](s Slice[E], n int) Slice[E]

Grow increases the slice's capacity, if necessary, to guarantee space for another n elements. After Grow(n), at least n elements can be appended to the slice without another allocation. Grow may modify elements of the slice between the length and the capacity. If n is negative or too large to allocate the memory, Grow panics.

func Insert

func Insert[E any](s Slice[E], i int, v ...E) Slice[E]

Insert inserts the values v... into s at index i, returning the modified slice. In the returned slice r, r[i] == v[0]. Insert panics if i != len(s).

func Make

func Make[E any](size ...int) Slice[E]

Make creates a new append-only Slice.

The size specifies the length. The capacity of the slice is equal to its length. A second integer argument may be provided to specify a different capacity; it must be no smaller than the length.

func (Slice[E]) Cap

func (s Slice[E]) Cap() int

Cap returns the capacity.

func (Slice[E]) Index

func (s Slice[E]) Index(i int) E

Index returns the i'th element.

func (Slice[E]) IsNil

func (s Slice[E]) IsNil() bool

IsNil reports whether the underlying slice is nil.

func (Slice[E]) Len

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

Len returns the length.

func (Slice[E]) Slice

func (s Slice[E]) Slice(i int) Slice[E]

Slice returns s[i:]. It panics if the indexes are out of bounds.

func (Slice[E]) String

func (s Slice[E]) String() string

String returns the underlying slice formatted as a string.

Jump to

Keyboard shortcuts

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