slices.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. // Copyright 2021 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package slices defines various functions useful with slices of any type.
  5. package slices
  6. import (
  7. "unsafe"
  8. "golang.org/x/exp/constraints"
  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[S ~[]E, E comparable](s1, s2 S) bool {
  16. if len(s1) != len(s2) {
  17. return false
  18. }
  19. for i := range s1 {
  20. if s1[i] != s2[i] {
  21. return false
  22. }
  23. }
  24. return true
  25. }
  26. // EqualFunc reports whether two slices are equal using an equality
  27. // function on each pair of elements. If the lengths are different,
  28. // EqualFunc returns false. Otherwise, the elements are compared in
  29. // increasing index order, and the comparison stops at the first index
  30. // for which eq returns false.
  31. func EqualFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, eq func(E1, E2) bool) bool {
  32. if len(s1) != len(s2) {
  33. return false
  34. }
  35. for i, v1 := range s1 {
  36. v2 := s2[i]
  37. if !eq(v1, v2) {
  38. return false
  39. }
  40. }
  41. return true
  42. }
  43. // Compare compares the elements of s1 and s2, using [cmp.Compare] on each pair
  44. // of elements. The elements are compared sequentially, starting at index 0,
  45. // until one element is not equal to the other.
  46. // The result of comparing the first non-matching elements is returned.
  47. // If both slices are equal until one of them ends, the shorter slice is
  48. // considered less than the longer one.
  49. // The result is 0 if s1 == s2, -1 if s1 < s2, and +1 if s1 > s2.
  50. func Compare[S ~[]E, E constraints.Ordered](s1, s2 S) int {
  51. for i, v1 := range s1 {
  52. if i >= len(s2) {
  53. return +1
  54. }
  55. v2 := s2[i]
  56. if c := cmpCompare(v1, v2); c != 0 {
  57. return c
  58. }
  59. }
  60. if len(s1) < len(s2) {
  61. return -1
  62. }
  63. return 0
  64. }
  65. // CompareFunc is like [Compare] but uses a custom comparison function on each
  66. // pair of elements.
  67. // The result is the first non-zero result of cmp; if cmp always
  68. // returns 0 the result is 0 if len(s1) == len(s2), -1 if len(s1) < len(s2),
  69. // and +1 if len(s1) > len(s2).
  70. func CompareFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, cmp func(E1, E2) int) int {
  71. for i, v1 := range s1 {
  72. if i >= len(s2) {
  73. return +1
  74. }
  75. v2 := s2[i]
  76. if c := cmp(v1, v2); c != 0 {
  77. return c
  78. }
  79. }
  80. if len(s1) < len(s2) {
  81. return -1
  82. }
  83. return 0
  84. }
  85. // Index returns the index of the first occurrence of v in s,
  86. // or -1 if not present.
  87. func Index[S ~[]E, E comparable](s S, v E) int {
  88. for i := range s {
  89. if v == s[i] {
  90. return i
  91. }
  92. }
  93. return -1
  94. }
  95. // IndexFunc returns the first index i satisfying f(s[i]),
  96. // or -1 if none do.
  97. func IndexFunc[S ~[]E, E any](s S, f func(E) bool) int {
  98. for i := range s {
  99. if f(s[i]) {
  100. return i
  101. }
  102. }
  103. return -1
  104. }
  105. // Contains reports whether v is present in s.
  106. func Contains[S ~[]E, E comparable](s S, v E) bool {
  107. return Index(s, v) >= 0
  108. }
  109. // ContainsFunc reports whether at least one
  110. // element e of s satisfies f(e).
  111. func ContainsFunc[S ~[]E, E any](s S, f func(E) bool) bool {
  112. return IndexFunc(s, f) >= 0
  113. }
  114. // Insert inserts the values v... into s at index i,
  115. // returning the modified slice.
  116. // The elements at s[i:] are shifted up to make room.
  117. // In the returned slice r, r[i] == v[0],
  118. // and r[i+len(v)] == value originally at r[i].
  119. // Insert panics if i is out of range.
  120. // This function is O(len(s) + len(v)).
  121. func Insert[S ~[]E, E any](s S, i int, v ...E) S {
  122. m := len(v)
  123. if m == 0 {
  124. return s
  125. }
  126. n := len(s)
  127. if i == n {
  128. return append(s, v...)
  129. }
  130. if n+m > cap(s) {
  131. // Use append rather than make so that we bump the size of
  132. // the slice up to the next storage class.
  133. // This is what Grow does but we don't call Grow because
  134. // that might copy the values twice.
  135. s2 := append(s[:i], make(S, n+m-i)...)
  136. copy(s2[i:], v)
  137. copy(s2[i+m:], s[i:])
  138. return s2
  139. }
  140. s = s[:n+m]
  141. // before:
  142. // s: aaaaaaaabbbbccccccccdddd
  143. // ^ ^ ^ ^
  144. // i i+m n n+m
  145. // after:
  146. // s: aaaaaaaavvvvbbbbcccccccc
  147. // ^ ^ ^ ^
  148. // i i+m n n+m
  149. //
  150. // a are the values that don't move in s.
  151. // v are the values copied in from v.
  152. // b and c are the values from s that are shifted up in index.
  153. // d are the values that get overwritten, never to be seen again.
  154. if !overlaps(v, s[i+m:]) {
  155. // Easy case - v does not overlap either the c or d regions.
  156. // (It might be in some of a or b, or elsewhere entirely.)
  157. // The data we copy up doesn't write to v at all, so just do it.
  158. copy(s[i+m:], s[i:])
  159. // Now we have
  160. // s: aaaaaaaabbbbbbbbcccccccc
  161. // ^ ^ ^ ^
  162. // i i+m n n+m
  163. // Note the b values are duplicated.
  164. copy(s[i:], v)
  165. // Now we have
  166. // s: aaaaaaaavvvvbbbbcccccccc
  167. // ^ ^ ^ ^
  168. // i i+m n n+m
  169. // That's the result we want.
  170. return s
  171. }
  172. // The hard case - v overlaps c or d. We can't just shift up
  173. // the data because we'd move or clobber the values we're trying
  174. // to insert.
  175. // So instead, write v on top of d, then rotate.
  176. copy(s[n:], v)
  177. // Now we have
  178. // s: aaaaaaaabbbbccccccccvvvv
  179. // ^ ^ ^ ^
  180. // i i+m n n+m
  181. rotateRight(s[i:], m)
  182. // Now we have
  183. // s: aaaaaaaavvvvbbbbcccccccc
  184. // ^ ^ ^ ^
  185. // i i+m n n+m
  186. // That's the result we want.
  187. return s
  188. }
  189. // clearSlice sets all elements up to the length of s to the zero value of E.
  190. // We may use the builtin clear func instead, and remove clearSlice, when upgrading
  191. // to Go 1.21+.
  192. func clearSlice[S ~[]E, E any](s S) {
  193. var zero E
  194. for i := range s {
  195. s[i] = zero
  196. }
  197. }
  198. // Delete removes the elements s[i:j] from s, returning the modified slice.
  199. // Delete panics if j > len(s) or s[i:j] is not a valid slice of s.
  200. // Delete is O(len(s)-i), so if many items must be deleted, it is better to
  201. // make a single call deleting them all together than to delete one at a time.
  202. // Delete zeroes the elements s[len(s)-(j-i):len(s)].
  203. func Delete[S ~[]E, E any](s S, i, j int) S {
  204. _ = s[i:j:len(s)] // bounds check
  205. if i == j {
  206. return s
  207. }
  208. oldlen := len(s)
  209. s = append(s[:i], s[j:]...)
  210. clearSlice(s[len(s):oldlen]) // zero/nil out the obsolete elements, for GC
  211. return s
  212. }
  213. // DeleteFunc removes any elements from s for which del returns true,
  214. // returning the modified slice.
  215. // DeleteFunc zeroes the elements between the new length and the original length.
  216. func DeleteFunc[S ~[]E, E any](s S, del func(E) bool) S {
  217. i := IndexFunc(s, del)
  218. if i == -1 {
  219. return s
  220. }
  221. // Don't start copying elements until we find one to delete.
  222. for j := i + 1; j < len(s); j++ {
  223. if v := s[j]; !del(v) {
  224. s[i] = v
  225. i++
  226. }
  227. }
  228. clearSlice(s[i:]) // zero/nil out the obsolete elements, for GC
  229. return s[:i]
  230. }
  231. // Replace replaces the elements s[i:j] by the given v, and returns the
  232. // modified slice. Replace panics if s[i:j] is not a valid slice of s.
  233. // When len(v) < (j-i), Replace zeroes the elements between the new length and the original length.
  234. func Replace[S ~[]E, E any](s S, i, j int, v ...E) S {
  235. _ = s[i:j] // verify that i:j is a valid subslice
  236. if i == j {
  237. return Insert(s, i, v...)
  238. }
  239. if j == len(s) {
  240. return append(s[:i], v...)
  241. }
  242. tot := len(s[:i]) + len(v) + len(s[j:])
  243. if tot > cap(s) {
  244. // Too big to fit, allocate and copy over.
  245. s2 := append(s[:i], make(S, tot-i)...) // See Insert
  246. copy(s2[i:], v)
  247. copy(s2[i+len(v):], s[j:])
  248. return s2
  249. }
  250. r := s[:tot]
  251. if i+len(v) <= j {
  252. // Easy, as v fits in the deleted portion.
  253. copy(r[i:], v)
  254. if i+len(v) != j {
  255. copy(r[i+len(v):], s[j:])
  256. }
  257. clearSlice(s[tot:]) // zero/nil out the obsolete elements, for GC
  258. return r
  259. }
  260. // We are expanding (v is bigger than j-i).
  261. // The situation is something like this:
  262. // (example has i=4,j=8,len(s)=16,len(v)=6)
  263. // s: aaaaxxxxbbbbbbbbyy
  264. // ^ ^ ^ ^
  265. // i j len(s) tot
  266. // a: prefix of s
  267. // x: deleted range
  268. // b: more of s
  269. // y: area to expand into
  270. if !overlaps(r[i+len(v):], v) {
  271. // Easy, as v is not clobbered by the first copy.
  272. copy(r[i+len(v):], s[j:])
  273. copy(r[i:], v)
  274. return r
  275. }
  276. // This is a situation where we don't have a single place to which
  277. // we can copy v. Parts of it need to go to two different places.
  278. // We want to copy the prefix of v into y and the suffix into x, then
  279. // rotate |y| spots to the right.
  280. //
  281. // v[2:] v[:2]
  282. // | |
  283. // s: aaaavvvvbbbbbbbbvv
  284. // ^ ^ ^ ^
  285. // i j len(s) tot
  286. //
  287. // If either of those two destinations don't alias v, then we're good.
  288. y := len(v) - (j - i) // length of y portion
  289. if !overlaps(r[i:j], v) {
  290. copy(r[i:j], v[y:])
  291. copy(r[len(s):], v[:y])
  292. rotateRight(r[i:], y)
  293. return r
  294. }
  295. if !overlaps(r[len(s):], v) {
  296. copy(r[len(s):], v[:y])
  297. copy(r[i:j], v[y:])
  298. rotateRight(r[i:], y)
  299. return r
  300. }
  301. // Now we know that v overlaps both x and y.
  302. // That means that the entirety of b is *inside* v.
  303. // So we don't need to preserve b at all; instead we
  304. // can copy v first, then copy the b part of v out of
  305. // v to the right destination.
  306. k := startIdx(v, s[j:])
  307. copy(r[i:], v)
  308. copy(r[i+len(v):], r[i+k:])
  309. return r
  310. }
  311. // Clone returns a copy of the slice.
  312. // The elements are copied using assignment, so this is a shallow clone.
  313. func Clone[S ~[]E, E any](s S) S {
  314. // Preserve nil in case it matters.
  315. if s == nil {
  316. return nil
  317. }
  318. return append(S([]E{}), s...)
  319. }
  320. // Compact replaces consecutive runs of equal elements with a single copy.
  321. // This is like the uniq command found on Unix.
  322. // Compact modifies the contents of the slice s and returns the modified slice,
  323. // which may have a smaller length.
  324. // Compact zeroes the elements between the new length and the original length.
  325. func Compact[S ~[]E, E comparable](s S) S {
  326. if len(s) < 2 {
  327. return s
  328. }
  329. i := 1
  330. for k := 1; k < len(s); k++ {
  331. if s[k] != s[k-1] {
  332. if i != k {
  333. s[i] = s[k]
  334. }
  335. i++
  336. }
  337. }
  338. clearSlice(s[i:]) // zero/nil out the obsolete elements, for GC
  339. return s[:i]
  340. }
  341. // CompactFunc is like [Compact] but uses an equality function to compare elements.
  342. // For runs of elements that compare equal, CompactFunc keeps the first one.
  343. // CompactFunc zeroes the elements between the new length and the original length.
  344. func CompactFunc[S ~[]E, E any](s S, eq func(E, E) bool) S {
  345. if len(s) < 2 {
  346. return s
  347. }
  348. i := 1
  349. for k := 1; k < len(s); k++ {
  350. if !eq(s[k], s[k-1]) {
  351. if i != k {
  352. s[i] = s[k]
  353. }
  354. i++
  355. }
  356. }
  357. clearSlice(s[i:]) // zero/nil out the obsolete elements, for GC
  358. return s[:i]
  359. }
  360. // Grow increases the slice's capacity, if necessary, to guarantee space for
  361. // another n elements. After Grow(n), at least n elements can be appended
  362. // to the slice without another allocation. If n is negative or too large to
  363. // allocate the memory, Grow panics.
  364. func Grow[S ~[]E, E any](s S, n int) S {
  365. if n < 0 {
  366. panic("cannot be negative")
  367. }
  368. if n -= cap(s) - len(s); n > 0 {
  369. // TODO(https://go.dev/issue/53888): Make using []E instead of S
  370. // to workaround a compiler bug where the runtime.growslice optimization
  371. // does not take effect. Revert when the compiler is fixed.
  372. s = append([]E(s)[:cap(s)], make([]E, n)...)[:len(s)]
  373. }
  374. return s
  375. }
  376. // Clip removes unused capacity from the slice, returning s[:len(s):len(s)].
  377. func Clip[S ~[]E, E any](s S) S {
  378. return s[:len(s):len(s)]
  379. }
  380. // Rotation algorithm explanation:
  381. //
  382. // rotate left by 2
  383. // start with
  384. // 0123456789
  385. // split up like this
  386. // 01 234567 89
  387. // swap first 2 and last 2
  388. // 89 234567 01
  389. // join first parts
  390. // 89234567 01
  391. // recursively rotate first left part by 2
  392. // 23456789 01
  393. // join at the end
  394. // 2345678901
  395. //
  396. // rotate left by 8
  397. // start with
  398. // 0123456789
  399. // split up like this
  400. // 01 234567 89
  401. // swap first 2 and last 2
  402. // 89 234567 01
  403. // join last parts
  404. // 89 23456701
  405. // recursively rotate second part left by 6
  406. // 89 01234567
  407. // join at the end
  408. // 8901234567
  409. // TODO: There are other rotate algorithms.
  410. // This algorithm has the desirable property that it moves each element exactly twice.
  411. // The triple-reverse algorithm is simpler and more cache friendly, but takes more writes.
  412. // The follow-cycles algorithm can be 1-write but it is not very cache friendly.
  413. // rotateLeft rotates b left by n spaces.
  414. // s_final[i] = s_orig[i+r], wrapping around.
  415. func rotateLeft[E any](s []E, r int) {
  416. for r != 0 && r != len(s) {
  417. if r*2 <= len(s) {
  418. swap(s[:r], s[len(s)-r:])
  419. s = s[:len(s)-r]
  420. } else {
  421. swap(s[:len(s)-r], s[r:])
  422. s, r = s[len(s)-r:], r*2-len(s)
  423. }
  424. }
  425. }
  426. func rotateRight[E any](s []E, r int) {
  427. rotateLeft(s, len(s)-r)
  428. }
  429. // swap swaps the contents of x and y. x and y must be equal length and disjoint.
  430. func swap[E any](x, y []E) {
  431. for i := 0; i < len(x); i++ {
  432. x[i], y[i] = y[i], x[i]
  433. }
  434. }
  435. // overlaps reports whether the memory ranges a[0:len(a)] and b[0:len(b)] overlap.
  436. func overlaps[E any](a, b []E) bool {
  437. if len(a) == 0 || len(b) == 0 {
  438. return false
  439. }
  440. elemSize := unsafe.Sizeof(a[0])
  441. if elemSize == 0 {
  442. return false
  443. }
  444. // TODO: use a runtime/unsafe facility once one becomes available. See issue 12445.
  445. // Also see crypto/internal/alias/alias.go:AnyOverlap
  446. return uintptr(unsafe.Pointer(&a[0])) <= uintptr(unsafe.Pointer(&b[len(b)-1]))+(elemSize-1) &&
  447. uintptr(unsafe.Pointer(&b[0])) <= uintptr(unsafe.Pointer(&a[len(a)-1]))+(elemSize-1)
  448. }
  449. // startIdx returns the index in haystack where the needle starts.
  450. // prerequisite: the needle must be aliased entirely inside the haystack.
  451. func startIdx[E any](haystack, needle []E) int {
  452. p := &needle[0]
  453. for i := range haystack {
  454. if p == &haystack[i] {
  455. return i
  456. }
  457. }
  458. // TODO: what if the overlap is by a non-integral number of Es?
  459. panic("needle not found")
  460. }
  461. // Reverse reverses the elements of the slice in place.
  462. func Reverse[S ~[]E, E any](s S) {
  463. for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
  464. s[i], s[j] = s[j], s[i]
  465. }
  466. }