roslices

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 roslices defines various read-only functions useful with immutable slices of any type.

Example
package main

import (
	"fmt"

	"github.com/phelmkamp/immut/roslices"
	"golang.org/x/exp/slices"
)

func main() {
	ints1 := roslices.Freeze([]int{2, 1, 3})
	if !roslices.IsSorted(ints1) {
		// must clone to sort
		ints2 := roslices.Clone(ints1)
		slices.Sort(ints2)
		fmt.Println(ints1, ints2)
	}
}
Output:

[2 1 3] [1 2 3]

Index

Examples

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 a read-only slice.

func Freeze

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

Freeze returns a read-only wrapper for the given slice.

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, j int) Slice[E]

Slice returns s[i:j]. 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