]> 127.0.0.1 Git - part/.git/commitdiff
1 v0.28.20240128081851
authorqydysky <qydysky@foxmail.com>
Sun, 28 Jan 2024 08:13:19 +0000 (16:13 +0800)
committerqydysky <qydysky@foxmail.com>
Sun, 28 Jan 2024 08:13:19 +0000 (16:13 +0800)
slice/Slice.go
slice/Slice_test.go

index 5d98d42edf972444ef54519915aa6a19f1d7c359..88ccbcc1204cae0939c02e0550939b11b425d146 100644 (file)
@@ -159,3 +159,22 @@ func (t *Buf[T]) GetCopyBuf() (buf []T) {
        copy(buf, t.buf[:t.bufsize])
        return
 }
+
+func DelFront[S ~[]*T, T any](s S, beforeIndex int) S {
+       return s[:copy(s, s[beforeIndex+1:])]
+}
+
+func AddFront[S ~[]*T, T any](s S, t *T) S {
+       s = append(s, nil)
+       s = s[:1+copy(s[1:], s)]
+       s[0] = t
+       return s
+}
+
+func DelBack[S ~[]*T, T any](s S, fromIndex int) S {
+       return s[:fromIndex]
+}
+
+func AddBack[S ~[]*T, T any](s S, t *T) S {
+       return append(s, t)
+}
index a093095d23f703089c00917b34c46626c499016c..e33d55c5213223716b970a22a9aa6f0acbd9ebc1 100644 (file)
@@ -3,6 +3,7 @@ package part
 import (
        "bytes"
        "testing"
+       "unsafe"
 )
 
 func TestXxx(t *testing.T) {
@@ -79,3 +80,29 @@ func TestXxx2(t *testing.T) {
                t.Fatal()
        }
 }
+
+func Test3(t *testing.T) {
+       i := 1
+       var s []*int
+       var p = unsafe.Pointer(&s)
+       s = append(s, &i, &i, &i)
+       if unsafe.Pointer(&s) != p || cap(s) != 3 || len(s) != 3 {
+               t.Fatal()
+       }
+       s = DelFront(s, 2)
+       if unsafe.Pointer(&s) != p || cap(s) != 3 || len(s) != 0 {
+               t.Fatal()
+       }
+       s = AddFront(s, &i)
+       if unsafe.Pointer(&s) != p || cap(s) != 3 || len(s) != 1 {
+               t.Fatal()
+       }
+       s = AddBack(s, &i)
+       if unsafe.Pointer(&s) != p || cap(s) != 3 || len(s) != 2 {
+               t.Fatal()
+       }
+       s = DelBack(s, 1)
+       if unsafe.Pointer(&s) != p || cap(s) != 3 || len(s) != 1 {
+               t.Fatal()
+       }
+}