]> Cypherpunks.ru repositories - gostls13.git/commitdiff
slices: add in-place Reverse function
authorAlan Donovan <adonovan@google.com>
Thu, 16 Feb 2023 16:02:12 +0000 (11:02 -0500)
committerAlan Donovan <adonovan@google.com>
Fri, 19 May 2023 00:43:02 +0000 (00:43 +0000)
Fixes #58565

Change-Id: I583f8380c12386178fb18e553322bbb019d9fae0
Reviewed-on: https://go-review.googlesource.com/c/go/+/468855
Run-TryBot: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Shay Nehmad <dude500@gmail.com>
api/next/58565.txt [new file with mode: 0644]
doc/go1.21.html
src/slices/slices.go
src/slices/slices_test.go

diff --git a/api/next/58565.txt b/api/next/58565.txt
new file mode 100644 (file)
index 0000000..88ae7e5
--- /dev/null
@@ -0,0 +1 @@
+pkg slices, func Reverse[$0 interface{}]([]$0) #58565
index 3e63016e02b423342f01356d48ef1b431fa3dfe1..743e2a3e28178b78bb524b6b56af75b69a27140b 100644 (file)
@@ -668,6 +668,16 @@ Do not send CLs removing the interior tags from such phrases.
   </dd>
 </dl><!-- spec -->
 
+<dl id="slices"><dt><a href="/pkg/slices/">slices</a></dt>
+  <dd>
+    <p><!-- https://go.dev/issue/45955 -->
+      The new <a href="/pkg/slices/"><code>slices</code></a> package
+      provides many common operations on slices, using generic
+      functions that work with slices of any element type.
+    </p>
+  </dd>
+</dl>
+
 <dl id="sync"><dt><a href="/pkg/sync/">sync</a></dt>
   <dd>
     <p><!-- https://go.dev/issue/56102, CL 451356 -->
index 837863bacc9abde4483010bec52c58e4e1c9951d..7de00b342f0f6f49152270708c4fb9b05562cf5e 100644 (file)
@@ -445,3 +445,10 @@ func startIdx[S ~[]E, E any](haystack, needle S) int {
        // TODO: what if the overlap is by a non-integral number of Es?
        panic("needle not found")
 }
+
+// Reverse reverses the elements of the slice in place.
+func Reverse[E any](s []E) {
+       for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
+               s[i], s[j] = s[j], s[i]
+       }
+}
index 2f3a03bd9f53f2a99c03a72b96d8cd26fa90ae5a..a99299321f7c0d84164466c8d6b1d84f35207935 100644 (file)
@@ -623,6 +623,34 @@ func TestClip(t *testing.T) {
        }
 }
 
+func TestReverse(t *testing.T) {
+       even := []int{3, 1, 4, 1, 5, 9} // len = 6
+       Reverse(even)
+       if want := []int{9, 5, 1, 4, 1, 3}; !Equal(even, want) {
+               t.Errorf("Reverse(even) = %v, want %v", even, want)
+       }
+
+       odd := []int{3, 1, 4, 1, 5, 9, 2} // len = 7
+       Reverse(odd)
+       if want := []int{2, 9, 5, 1, 4, 1, 3}; !Equal(odd, want) {
+               t.Errorf("Reverse(odd) = %v, want %v", odd, want)
+       }
+
+       words := strings.Fields("one two three")
+       Reverse(words)
+       if want := strings.Fields("three two one"); !Equal(words, want) {
+               t.Errorf("Reverse(words) = %v, want %v", words, want)
+       }
+
+       singleton := []string{"one"}
+       Reverse(singleton)
+       if want := []string{"one"}; !Equal(singleton, want) {
+               t.Errorf("Reverse(singeleton) = %v, want %v", singleton, want)
+       }
+
+       Reverse[string](nil)
+}
+
 // naiveReplace is a baseline implementation to the Replace function.
 func naiveReplace[S ~[]E, E any](s S, i, j int, v ...E) S {
        s = Delete(s, i, j)