| 1 | // Package xslices contains utilities for working with slices of arbitrary types. |
| 2 | package xslicesscip-go gomod github.com/bradenaw/juniper . `github.com/bradenaw/juniper/xslices`/ |
| 3 | |
| 4 | // All returns true if f(s[i]) returns true for all i. Trivially, returns true if s is empty. |
| 5 | func Allscip-go gomod github.com/bradenaw/juniper . `github.com/bradenaw/juniper/xslices`/All().[T any](s []T, f func(T) bool) bool { |
| 6 | for i := range s { |
| 7 | if !f(s[i]) { |
| 8 | return false |
| 9 | } |
| 10 | } |
| 11 | return true |
| 12 | } |
| 13 | |
| 14 | // Chunk returns non-overlapping chunks of s. The last chunk will be smaller than chunkSize if |
| 15 | // len(s) is not a multiple of chunkSize. |
| 16 | // |
| 17 | // Returns an empty slice if len(s)==0. Panics if chunkSize <= 0. |
| 18 | func Chunkscip-go gomod github.com/bradenaw/juniper . `github.com/bradenaw/juniper/xslices`/Chunk().[T any](s []T, chunkSize int) [][]T { |
| 19 | out := make([][]T, (len(s)+chunkSize-1)/chunkSize) |
| 20 | for i := range out { |
| 21 | start := i * chunkSize |
| 22 | end := (i + 1) * chunkSize |
| 23 | if end > len(s) { |
| 24 | end = len(s) |
| 25 | } |
| 26 | out[i] = s[start:end] |
| 27 | } |
| 28 | return out |
| 29 | } |
| 30 | |
| 31 | // Clear fills s with the zero value of T. |
| 32 | // |
| 33 | // Deprecated: clear is a builtin as of Go 1.21. |
| 34 | func Clearscip-go gomod github.com/bradenaw/juniper . `github.com/bradenaw/juniper/xslices`/Clear().[T any](s []T) { |
| 35 | var zero T |
| 36 | Fillscip-go gomod github.com/bradenaw/juniper . `github.com/bradenaw/juniper/xslices`/Fill().(s, zero) |
| 37 | } |
| 38 | |
| 39 | // Count returns the number of times x appears in s. |
| 40 | func Countscip-go gomod github.com/bradenaw/juniper . `github.com/bradenaw/juniper/xslices`/Count().[T comparable](s []T, x T) int { |
| 41 | return CountFuncscip-go gomod github.com/bradenaw/juniper . `github.com/bradenaw/juniper/xslices`/CountFunc().(s, func(s T) bool { return x == s }) |
| 42 | } |
| 43 | |
| 44 | // Count returns the number of items in s for which f returns true. |
| 45 | func CountFuncscip-go gomod github.com/bradenaw/juniper . `github.com/bradenaw/juniper/xslices`/CountFunc().[T any](s []T, f func(T) bool) int { |
| 46 | n := 0 |
| 47 | for _, s := range s { |
| 48 | if f(s) { |
| 49 | n++ |
| 50 | } |
| 51 | } |
| 52 | return n |
| 53 | } |
| 54 | |
| 55 | // Fill fills s with copies of x. |
| 56 | func Fillscip-go gomod github.com/bradenaw/juniper . `github.com/bradenaw/juniper/xslices`/Fill().[T any](s []T, x T) { |
| 57 | for i := range s { |
| 58 | s[i] = x |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // Group returns a map from u to all items of s for which f(s[i]) returned u. |
| 63 | func Groupscip-go gomod github.com/bradenaw/juniper . `github.com/bradenaw/juniper/xslices`/Group().[T any, U comparable](s []T, f func(T) U) map[U][]T { |
| 64 | m := make(map[U][]T) |
| 65 | for i := range s { |
| 66 | g := f(s[i]) |
| 67 | m[g] = append(m[g], s[i]) |
| 68 | } |
| 69 | return m |
| 70 | } |
| 71 | |
| 72 | // Join joins together the contents of each in. |
| 73 | func Joinscip-go gomod github.com/bradenaw/juniper . `github.com/bradenaw/juniper/xslices`/Join().[T any](in ...[]T) []T { |
| 74 | n := 0 |
| 75 | for i := range in { |
| 76 | n += len(in[i]) |
| 77 | } |
| 78 | out := make([]T, 0, n) |
| 79 | for i := range in { |
| 80 | out = append(out, in[i]...) |
| 81 | } |
| 82 | return out |
| 83 | } |
| 84 | |
| 85 | // LastIndex returns the last index of x in s, or -1 if x is not in s. |
| 86 | func LastIndexscip-go gomod github.com/bradenaw/juniper . `github.com/bradenaw/juniper/xslices`/LastIndex().[T comparable](s []T, x T) int { |
| 87 | for i := len(s) - 1; i >= 0; i-- { |
| 88 | if s[i] == x { |
| 89 | return i |
| 90 | } |
| 91 | } |
| 92 | return -1 |
| 93 | } |
| 94 | |
| 95 | // LastIndexFunc returns the last index in s for which f(s[i]) returns true, or -1 if there are no |
| 96 | // such items. |
| 97 | func LastIndexFuncscip-go gomod github.com/bradenaw/juniper . `github.com/bradenaw/juniper/xslices`/LastIndexFunc().[T any](s []T, f func(T) bool) int { |
| 98 | for i := len(s) - 1; i >= 0; i-- { |
| 99 | if f(s[i]) { |
| 100 | return i |
| 101 | } |
| 102 | } |
| 103 | return -1 |
| 104 | } |
| 105 | |
| 106 | // Map creates a new slice by applying f to each element of s. |
| 107 | func Mapscip-go gomod github.com/bradenaw/juniper . `github.com/bradenaw/juniper/xslices`/Map().[T any, U any](s []T, f func(T) U) []U { |
| 108 | out := make([]U, len(s)) |
| 109 | for i := range s { |
| 110 | out[i] = f(s[i]) |
| 111 | } |
| 112 | return out |
| 113 | } |
| 114 | |
| 115 | // Partition moves elements of s such that all elements for which f returns false are at the |
| 116 | // beginning and all elements for which f returns true are at the end. It makes no other guarantees |
| 117 | // about the final order of elements. Returns the index of the first element for which f returned |
| 118 | // true, or len(s) if there wasn't one. |
| 119 | func Partitionscip-go gomod github.com/bradenaw/juniper . `github.com/bradenaw/juniper/xslices`/Partition().[T any](s []T, f func(t T) bool) int { |
| 120 | i := 0 |
| 121 | j := len(s) - 1 |
| 122 | for { |
| 123 | for i < j { |
| 124 | if !f(s[i]) { |
| 125 | i++ |
| 126 | } else { |
| 127 | break |
| 128 | } |
| 129 | } |
| 130 | for j > i { |
| 131 | if f(s[j]) { |
| 132 | j-- |
| 133 | } else { |
| 134 | break |
| 135 | } |
| 136 | } |
| 137 | if i >= j { |
| 138 | break |
| 139 | } |
| 140 | s[i], s[j] = s[j], s[i] |
| 141 | i++ |
| 142 | j-- |
| 143 | } |
| 144 | if i < len(s) && !f(s[i]) { |
| 145 | i++ |
| 146 | } |
| 147 | return i |
| 148 | } |
| 149 | |
| 150 | // Reduce reduces s to a single value using the reduction function f. |
| 151 | func Reducescip-go gomod github.com/bradenaw/juniper . `github.com/bradenaw/juniper/xslices`/Reduce().[T any, U any](s []T, initial U, f func(U, T) U) U { |
| 152 | out := initial |
| 153 | for i := range s { |
| 154 | out = f(out, s[i]) |
| 155 | } |
| 156 | return out |
| 157 | } |
| 158 | |
| 159 | // RemoveUnordered removes n elements from s starting at index idx and returns the modified slice. |
| 160 | // This is done by moving up to n elements from the end of the slice into the gap left by removal, |
| 161 | // which is linear in n (rather than len(s)-idx as Remove() is), but does not preserve order of the |
| 162 | // remaining elements. |
| 163 | func RemoveUnorderedscip-go gomod github.com/bradenaw/juniper . `github.com/bradenaw/juniper/xslices`/RemoveUnordered().[T any](s []T, idx int, n int) []T { |
| 164 | keepStart := len(s) - n |
| 165 | removeEnd := idx + n |
| 166 | if removeEnd > keepStart { |
| 167 | keepStart = removeEnd |
| 168 | } |
| 169 | copy(s[idx:], s[keepStart:]) |
| 170 | Clearscip-go gomod github.com/bradenaw/juniper . `github.com/bradenaw/juniper/xslices`/Clear().(s[len(s)-n:]) |
| 171 | return s[:len(s)-n] |
| 172 | } |
| 173 | |
| 174 | // Repeat returns a slice with length n where every item is s. |
| 175 | func Repeatscip-go gomod github.com/bradenaw/juniper . `github.com/bradenaw/juniper/xslices`/Repeat().[T any](s T, n int) []T { |
| 176 | out := make([]T, n) |
| 177 | for i := range out { |
| 178 | out[i] = s |
| 179 | } |
| 180 | return out |
| 181 | } |
| 182 | |
| 183 | // Reverse reverses the elements of s in place. |
| 184 | func Reversescip-go gomod github.com/bradenaw/juniper . `github.com/bradenaw/juniper/xslices`/Reverse().[T any](s []T) { |
| 185 | for i := 0; i < len(s)/2; i++ { |
| 186 | s[i], s[len(s)-i-1] = s[len(s)-i-1], s[i] |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // Runs returns a slice of slices. The inner slices are contiguous runs of elements from s such that |
| 191 | // same(a, b) returns true for any a and b in the run. |
| 192 | // |
| 193 | // same(a, a) must return true. If same(a, b) and same(b, c) both return true, then same(a, c) must |
| 194 | // also. |
| 195 | // |
| 196 | // The returned slices use the same underlying array as s. |
| 197 | func Runsscip-go gomod github.com/bradenaw/juniper . `github.com/bradenaw/juniper/xslices`/Runs().[T any](s []T, same func(a, b T) bool) [][]T { |
| 198 | var runs [][]T |
| 199 | start := 0 |
| 200 | end := 0 |
| 201 | for i := 1; i < len(s); i++ { |
| 202 | if same(s[i-1], s[i]) { |
| 203 | end = i + 1 |
| 204 | } else { |
| 205 | runs = append(runs, s[start:end]) |
| 206 | start = i |
| 207 | end = i + 1 |
| 208 | } |
| 209 | } |
| 210 | if end > 0 { |
| 211 | runs = append(runs, s[start:]) |
| 212 | } |
| 213 | return runs |
| 214 | } |
| 215 | |
| 216 | // Shrink shrinks s's capacity by reallocating, if necessary, so that cap(s) <= len(s) + n. |
| 217 | func Shrinkscip-go gomod github.com/bradenaw/juniper . `github.com/bradenaw/juniper/xslices`/Shrink().[T any](s []T, n int) []T { |
| 218 | if cap(s) > len(s)+n { |
| 219 | x2 := make([]T, len(s)+n) |
| 220 | copy(x2, s) |
| 221 | return x2[:len(s)] |
| 222 | } |
| 223 | return s |
| 224 | } |
| 225 | |
| 226 | // Unique returns a slice that contains only the first instance of each unique item in s, preserving |
| 227 | // order. |
| 228 | // |
| 229 | // Compact is more efficient if duplicates are already adjacent in s, for example if s is in sorted |
| 230 | // order. |
| 231 | func Uniquescip-go gomod github.com/bradenaw/juniper . `github.com/bradenaw/juniper/xslices`/Unique().[T comparable](s []T) []T { |
| 232 | return uniqueIntoscip-go gomod github.com/bradenaw/juniper . `github.com/bradenaw/juniper/xslices`/uniqueInto().([]T{}, s) |
| 233 | } |
| 234 | |
| 235 | // UniqueInPlace returns a slice that contains only the first instance of each unique item in s, |
| 236 | // preserving order. This is done in-place and so modifies the contents of s. The modified slice is |
| 237 | // returned. |
| 238 | // |
| 239 | // Compact is more efficient if duplicates are already adjacent in s, for example if s is in sorted |
| 240 | // order. |
| 241 | func UniqueInPlacescip-go gomod github.com/bradenaw/juniper . `github.com/bradenaw/juniper/xslices`/UniqueInPlace().[T comparable](s []T) []T { |
| 242 | filtered := uniqueIntoscip-go gomod github.com/bradenaw/juniper . `github.com/bradenaw/juniper/xslices`/uniqueInto().(s[:0], s) |
| 243 | Clearscip-go gomod github.com/bradenaw/juniper . `github.com/bradenaw/juniper/xslices`/Clear().(s[len(filtered):]) |
| 244 | return filtered |
| 245 | } |
| 246 | |
| 247 | func uniqueIntoscip-go gomod github.com/bradenaw/juniper . `github.com/bradenaw/juniper/xslices`/uniqueInto().[T comparable](into []T, s []T) []T { |
| 248 | m := make(map[T]struct{}, len(s)) |
| 249 | for i := range s { |
| 250 | _, ok := m[s[i]] |
| 251 | if !ok { |
| 252 | into = append(into, s[i]) |
| 253 | m[s[i]] = struct{}{} |
| 254 | } |
| 255 | } |
| 256 | return into |
| 257 | } |