-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel_slice_mut.go
More file actions
66 lines (58 loc) · 1.93 KB
/
Copy pathparallel_slice_mut.go
File metadata and controls
66 lines (58 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package gofu
import (
"math/rand/v2"
"slices"
)
//TODO: implement actual parallel versions
type sliceParallelMut[T any] SliceParallel[T]
// Reverse reverses the elements in-place. [ Mutates ] [ time: O(n); space: O(1)]
//
// Example:
//
// gofu.Slice([]int{1, 2, 3}).Parallel(4).Mut().Reverse()
func (mp sliceParallelMut[T]) Reverse() SliceParallel[T] {
mp.Slice.Mut().Reverse()
return SliceParallel[T](mp)
}
// Delete removes elements for which fn returns true. [ Mutates ] [ time: O(n); space: O(1)]
//
// Example:
//
// gofu.Slice([]int{1, 2, 3, 4, 5}).Parallel(4).Mut().
// Delete(func(v int) bool { return v%2 == 0 })
func (mp sliceParallelMut[T]) Delete(fn func(T) bool) SliceParallel[T] {
s := mp.Slice.Mut().Delete(fn)
return SliceParallel[T]{Slice: s, Scale: mp.Scale}
}
// Compact removes consecutive elements where fn returns true, keeping the first occurrence of each run. [ Mutates ] [ time: O(n); space: O(n)]
//
// Example:
//
// gofu.Slice([]int{1, 1, 2, 2, 2, 3}).Parallel(4).Mut().
// Compact(func(a, b int) bool { return a == b })
func (mp sliceParallelMut[T]) Compact(fn func(T, T) bool) SliceParallel[T] {
result := slices.CompactFunc(mp.Slice, fn)
return SliceParallel[T]{Slice: result, Scale: mp.Scale}
}
// Shuffle randomly shuffles elements in-place. [ Mutates ] [ time: O(n); space: O(1)]
//
// Example:
//
// gofu.Slice([]int{1, 2, 3, 4, 5}).Parallel(4).Mut().Shuffle()
func (mp sliceParallelMut[T]) Shuffle() SliceParallel[T] {
s := mp.Slice
for i := len(s) - 1; i > 0; i-- {
j := rand.IntN(i + 1)
s[i], s[j] = s[j], s[i]
}
return SliceParallel[T](mp)
}
// SortBy sorts elements in-place using fn. [ Mutates ] [ time: O(n log n); space: O(1)]
//
// Example:
//
// gofu.Slice([]int{3, 1, 4, 1, 5}).Parallel(4).Mut().
// SortBy(func(a, b int) int { return a - b })
func (mp sliceParallelMut[T]) SortBy(fn func(T, T) int) SliceParallel[T] {
return SliceParallel[T]{Slice: mp.Slice.Mut().SortBy(fn), Scale: mp.Scale}
}