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 ¶
- func Sort[E constraints.Ordered](x *Slice[E])
- func SortFunc[E any](x *Slice[E], less func(a, b E) bool)
- func SortStableFunc[E any](x *Slice[E], less func(a, b E) bool)
- type Doer
- func DoClip[E any]() Doer[E]
- func DoCompact[E comparable]() Doer[E]
- func DoCompactFunc[E any](eq func(E, E) bool) Doer[E]
- func DoDelete[E any](i, j int) Doer[E]
- func DoInsert[E any](i int, v ...E) Doer[E]
- func DoSort[E constraints.Ordered]() Doer[E]
- func DoSortFunc[E any](less func(a, b E) bool) Doer[E]
- func DoSortStableFunc[E any](less func(a, b E) bool) Doer[E]
- type Slice
- func Clip[E any](s Slice[E]) Slice[E]
- func Compact[E comparable](s Slice[E]) Slice[E]
- func CompactFunc[E any](s Slice[E], eq func(E, E) bool) Slice[E]
- func CopyOnWrite[E any](s []E) Slice[E]
- func Delete[E any](s Slice[E], i, j int) Slice[E]
- func DoAll[E any](s Slice[E], cap int, ops ...Doer[E]) Slice[E]
- func Grow[E any](s Slice[E], n int) Slice[E]
- func Insert[E any](s Slice[E], i int, v ...E) Slice[E]
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 ¶
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 ¶
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 DoCompact ¶
func DoCompact[E comparable]() Doer[E]
DoCompact returns the slices.Compact operation.
func DoCompactFunc ¶
DoCompactFunc returns the slices.CompactFunc operation.
func DoSort ¶
func DoSort[E constraints.Ordered]() Doer[E]
DoSort returns the slices.Sort operation.
func DoSortFunc ¶
DoSortFunc returns the slices.SortFunc operation.
func DoSortStableFunc ¶
DoSortStableFunc returns the slices.SortStableFunc operation.
type Slice ¶
Slice wraps a copy-on-write slice.
func Clip ¶
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 ¶
CompactFunc is like Compact but uses a comparison function. Note: The underlying slice is cloned before the write-operation is performed.
func CopyOnWrite ¶
CopyOnWrite returns a copy-on-write wrapper for the given slice.
func Delete ¶
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 ¶
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 ¶
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 ¶
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.