cowslices

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: 4 Imported by: 0

Documentation

Overview

Package cowslices defines various copy-on-write functions useful with slices of any type.

Example
package main

import (
	"fmt"
	"github.com/phelmkamp/immut/cowslices"
	"github.com/phelmkamp/immut/roslices"
)

func main() {
	ints1 := []int{2, 1, 3}
	ints2 := cowslices.CopyOnWrite(ints1)
	if !roslices.IsSorted(ints2.RO) {
		cowslices.Sort(&ints2) // ints1 is not affected
		fmt.Println(ints2)
	}
	fmt.Println(ints1)
}
Output:

[1 2 3]
[2 1 3]
Example (Concurrent)

Example_concurrent demonstrates that two concurrent goroutines can access the same slice without the use of channels or locks.

package main

import (
	"fmt"
	"github.com/phelmkamp/immut/cowslices"
	"time"
)

func makeInts(n int) []int {
	ints := make([]int, n)
	for i := 0; i < n; i++ {
		ints[i] = i
	}
	return ints
}

func main() {
	s := cowslices.CopyOnWrite(makeInts(10_000))
	go func() {
		for {
			// delete element 1 after slight delay
			time.Sleep(1 * time.Millisecond)
			s = cowslices.Delete(s, 1, 2)
		}
	}()
	go func() {
		for {
			// read last element constantly
			// without COW index out-of-bounds is possible
			// but ro is guaranteed not to change
			ro := s.RO
			_ = fmt.Sprint(ro.Index(ro.Len() - 1))
		}
	}()
	// run for 5 sec
	time.Sleep(5 * time.Second)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Sort

func Sort[E constraints.Ordered](x *Slice[E])

Sort sorts a slice of any ordered type in ascending order. Note: The underlying slice is cloned before the write-operation is performed.

func SortFunc

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

SortFunc sorts the slice x in ascending order as determined by the less function. This sort is not guaranteed to be stable. Note: The underlying slice is cloned before the write-operation is performed.

func SortStableFunc

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

SortStableFunc sorts the slice x while keeping the original order of equal elements, using less to compare elements. Note: The underlying slice is cloned before the write-operation is performed.

Types

type Doer

type Doer[E any] interface {
	// contains filtered or unexported methods
}

Doer defines a method for doing an operation on a slice.

func DoClip

func DoClip[E any]() Doer[E]

DoClip returns the slices.Clip operation.

func DoCompact

func DoCompact[E comparable]() Doer[E]

DoCompact returns the slices.Compact operation.

func DoCompactFunc

func DoCompactFunc[E any](eq func(E, E) bool) Doer[E]

DoCompactFunc returns the slices.CompactFunc operation.

func DoDelete

func DoDelete[E any](i, j int) Doer[E]

DoDelete returns the slices.Delete operation.

func DoInsert

func DoInsert[E any](i int, v ...E) Doer[E]

DoInsert returns the slices.Insert operation.

func DoSort

func DoSort[E constraints.Ordered]() Doer[E]

DoSort returns the slices.Sort operation.

func DoSortFunc

func DoSortFunc[E any](less func(a, b E) bool) Doer[E]

DoSortFunc returns the slices.SortFunc operation.

func DoSortStableFunc

func DoSortStableFunc[E any](less func(a, b E) bool) Doer[E]

DoSortStableFunc returns the slices.SortStableFunc operation.

type Slice

type Slice[E any] struct {
	RO roslices.Slice[E] // wraps a read-only slice
}

Slice wraps a copy-on-write 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)]. Note: The underlying slice is cloned before the write-operation is performed.

func Compact

func Compact[E comparable](s Slice[E]) Slice[E]

Compact replaces consecutive runs of equal elements with a single copy. This is like the uniq command found on Unix. Note: The underlying slice is cloned before the write-operation is performed.

func CompactFunc

func CompactFunc[E any](s Slice[E], eq func(E, E) bool) Slice[E]

CompactFunc is like Compact but uses a comparison function. Note: The underlying slice is cloned before the write-operation is performed.

func CopyOnWrite

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

CopyOnWrite returns a copy-on-write wrapper for the given slice.

func Delete

func Delete[E any](s Slice[E], i, j int) Slice[E]

Delete removes the elements s[i:j] from s, returning the modified slice. Delete panics if s[i:j] is not a valid slice of s. Delete is O(len(s)-(j-i)), so if many items must be deleted, it is better to make a single call deleting them all together than to delete one at a time. Note: The underlying slice is cloned before the write-operation is performed.

func DoAll

func DoAll[E any](s Slice[E], cap int, ops ...Doer[E]) Slice[E]

DoAll does all the supplied operations on the slice with minimal reallocation. The initial capacity of the reallocated slice is cap (or len(s) if cap is not sufficient). Note: The underlying slice is cloned before the write-operations are performed.

Example
package main

import (
	"fmt"
	"github.com/phelmkamp/immut/cowslices"
)

func main() {
	s := cowslices.CopyOnWrite([]int{1, 2, 2, 3})
	s = cowslices.DoAll(s, s.RO.Len(),
		cowslices.DoInsert[int](1, 3), // [1 3 2 2 3]
		cowslices.DoSort[int](),       // [1 2 2 3 3]
		cowslices.DoCompact[int](),    // [1 2 3]
		cowslices.DoDelete[int](1, 2), // [1 3]
		cowslices.DoClip[int](),       // [1 3]
	)
	fmt.Println(s)
}
Output:

[1 3]

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. Note: The underlying slice is cloned before the write-operation is performed.

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 is out of range. Note: The underlying slice is cloned before the write-operation is performed.

func (*Slice[E]) SetIndex

func (s *Slice[E]) SetIndex(i int, v E)

SetIndex sets the element at i to v. Note: The underlying slice is cloned before the write-operation is performed.

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