| 1 | package slices |
| 2 | |
| 3 | import ( |
| 4 | "golang.org/x/exp/constraints" |
| 5 | "golang.org/x/exp/slices" |
| 6 | ) |
| 7 | |
| 8 | // This file delegates to the official slices package, so that we end up with a superset of the official API. |
| 9 | |
| 10 | // Equal reports whether two slices are equal: the same length and all |
| 11 | // elements equal. If the lengths are different, Equal returns false. |
| 12 | // Otherwise, the elements are compared in increasing index order, and the |
| 13 | // comparison stops at the first unequal pair. |
| 14 | // Floating point NaNs are not considered equal. |
| 15 | func Equal[E comparable](s1, s2 []E) bool { |
| 16 | return slices.Equal(s1, s2) |
| 17 | } |
| 18 | |
| 19 | // EqualFunc reports whether two slices are equal using a comparison |
| 20 | // function on each pair of elements. If the lengths are different, |
| 21 | // EqualFunc returns false. Otherwise, the elements are compared in |
| 22 | // increasing index order, and the comparison stops at the first index |
| 23 | // for which eq returns false. |
| 24 | func EqualFunc[E1, E2 any](s1 []E1, s2 []E2, eq func(E1, E2) bool) bool { |
| 25 | return slices.EqualFunc(s1, s2, eq) |
| 26 | } |
| 27 | |
| 28 | // Compare compares the elements of s1 and s2. |
| 29 | // The elements are compared sequentially, starting at index 0, |
| 30 | // until one element is not equal to the other. |
| 31 | // The result of comparing the first non-matching elements is returned. |
| 32 | // If both slices are equal until one of them ends, the shorter slice is |
| 33 | // considered less than the longer one. |
| 34 | // The result is 0 if s1 == s2, -1 if s1 < s2, and +1 if s1 > s2. |
| 35 | // Comparisons involving floating point NaNs are ignored. |
| 36 | func Compare[E constraints.Ordered](s1, s2 []E) int { |
| 37 | return slices.Compare(s1, s2) |
| 38 | } |
| 39 | |
| 40 | // CompareFunc is like Compare but uses a comparison function |
| 41 | // on each pair of elements. The elements are compared in increasing |
| 42 | // index order, and the comparisons stop after the first time cmp |
| 43 | // returns non-zero. |
| 44 | // The result is the first non-zero result of cmp; if cmp always |
| 45 | // returns 0 the result is 0 if len(s1) == len(s2), -1 if len(s1) < len(s2), |
| 46 | // and +1 if len(s1) > len(s2). |
| 47 | func CompareFunc[E1, E2 any](s1 []E1, s2 []E2, cmp func(E1, E2) int) int { |
| 48 | return slices.CompareFunc(s1, s2, cmp) |
| 49 | } |
| 50 | |
| 51 | // Index returns the index of the first occurrence of v in s, |
| 52 | // or -1 if not present. |
| 53 | func Index[E comparable](s []E, v E) int { |
| 54 | return slices.Index(s, v) |
| 55 | } |
| 56 | |
| 57 | // IndexFunc returns the first index i satisfying f(s[i]), |
| 58 | // or -1 if none do. |
| 59 | func IndexFunc[E any](s []E, f func(E) bool) int { |
| 60 | return slices.IndexFunc(s, f) |
| 61 | } |
| 62 | |
| 63 | // Contains reports whether v is present in s. |
| 64 | func Contains[E comparable](s []E, v E) bool { |
| 65 | return slices.Contains(s, v) |
| 66 | } |
| 67 | |
| 68 | // Insert inserts the values v... into s at index i, |
| 69 | // returning the modified slice. |
| 70 | // In the returned slice r, r[i] == v[0]. |
| 71 | // Insert panics if i is out of range. |
| 72 | // This function is O(len(s) + len(v)). |
| 73 | func Insert[S ~[]E, E any](s S, i int, v ...E) S { |
| 74 | return slices.Insert(s, i, v...) |
| 75 | } |
| 76 | |
| 77 | // Delete removes the elements s[i:j] from s, returning the modified slice. |
| 78 | // Delete panics if s[i:j] is not a valid slice of s. |
| 79 | // Delete modifies the contents of the slice s; it does not create a new slice. |
| 80 | // Delete is O(len(s)-(j-i)), so if many items must be deleted, it is better to |
| 81 | // make a single call deleting them all together than to delete one at a time. |
| 82 | func Delete[S ~[]E, E any](s S, i, j int) S { |
| 83 | return slices.Delete(s, i, j) |
| 84 | } |
| 85 | |
| 86 | // Clone returns a copy of the slice. |
| 87 | // The elements are copied using assignment, so this is a shallow clone. |
| 88 | func Clone[S ~[]E, E any](s S) S { |
| 89 | return slices.Clone(s) |
| 90 | } |
| 91 | |
| 92 | // Compact replaces consecutive runs of equal elements with a single copy. |
| 93 | // This is like the uniq command found on Unix. |
| 94 | // Compact modifies the contents of the slice s; it does not create a new slice. |
| 95 | // Intended usage is to assign the result back to the input slice. |
| 96 | func Compact[S ~[]E, E comparable](s S) S { |
| 97 | return slices.Compact(s) |
| 98 | } |
| 99 | |
| 100 | // CompactFunc is like Compact but uses a comparison function. |
| 101 | func CompactFunc[S ~[]E, E any](s S, eq func(E, E) bool) S { |
| 102 | return slices.CompactFunc(s, eq) |
| 103 | } |
| 104 | |
| 105 | // Grow increases the slice's capacity, if necessary, to guarantee space for |
| 106 | // another n elements. After Grow(n), at least n elements can be appended |
| 107 | // to the slice without another allocation. Grow may modify elements of the |
| 108 | // slice between the length and the capacity. If n is negative or too large to |
| 109 | // allocate the memory, Grow panics. |
| 110 | func Grow[S ~[]E, E any](s S, n int) S { |
| 111 | return slices.Grow(s, n) |
| 112 | } |
| 113 | |
| 114 | // Clip removes unused capacity from the slice, returning s[:len(s):len(s)]. |
| 115 | func Clip[S ~[]E, E any](s S) S { |
| 116 | return slices.Clip(s) |
| 117 | } |