自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

你真的理解 Golang 切片嗎?全切片表達(dá)式及切片使用技巧

開發(fā) 前端
Golang 的切片不僅僅是帶長度的指針,它們還有一個(gè) "容量 "字段,因?yàn)樵黾觿?dòng)態(tài)分配的數(shù)組是一個(gè)很常見的任務(wù)。分片的容量也作為分片表達(dá)式 a[low:high] 的邊界檢查器——切片的末端不能超過其容量。

簡介

Golang 中通常的 slice 語法是 a[low:high],您可能很熟悉。還有另一種切片語法,形式為 a[low:high:max],它采用三個(gè)索引而不是兩個(gè)索引。第三索引 max 是做什么的?

提示: 不是 Python 切片語法 a[low:high:step] 中的 step 步長索引。

答: 第三個(gè)索引用于設(shè)置切片的容量!在 Golang 規(guī)范中稱為 “全切片表達(dá)式”。

了解 Golang 切片

為了理解為什么要在 Golang 中加入這個(gè)功能,以及它的作用,讓我們從數(shù)組和指針開始。

越界錯(cuò)誤在 C 語言程序中很常見,Golang 通過內(nèi)置的運(yùn)行時(shí)邊界檢查器來緩解這個(gè)問題。數(shù)組的邊界檢查很簡單,因?yàn)?Golang 的數(shù)組是固定長度的,然而,指針的邊界檢查就不那么簡單了,因?yàn)橹羔樀倪吔鐩]有明確定義。Golang 中的切片只是解決指針的邊界檢查的一種方法。

Golang 不使用普通的指針來訪問數(shù)組元素,而是用一個(gè)長度字段來擴(kuò)充指針;結(jié)果(帶長度的指針)被稱為 "切片",或在其他地方稱為 "胖指針"。有了長度字段,運(yùn)行時(shí)的邊界檢查就很容易了。

Golang 的切片不僅僅是帶長度的指針,它們還有一個(gè) "容量 "字段,因?yàn)樵黾觿?dòng)態(tài)分配的數(shù)組是一個(gè)很常見的任務(wù)。分片的容量也作為分片表達(dá)式 a[low:high] 的邊界檢查器——切片的末端不能超過其容量。

理解 a[low:high:max]

切片索引表達(dá)式由長度字段進(jìn)行邊界檢查,長度字段可以通過分片來減少,以提供所需的邊界檢查。

同樣地,人們可能會(huì)想,是否有可能減少片斷的容量,以加強(qiáng)對切片表達(dá)式 a[low:high] 的邊界檢查。例如,下面的表達(dá)式將一個(gè)切片的容量減少到它的長度:

a = a[0:len(a):len(a)]

在這之后,切片 a 被限制在它自己的范圍內(nèi),切片結(jié)束后的元素不能被訪問或修改,即使你不小心重新分片或追加到它上面。

這個(gè)技巧對于從不可改變的數(shù)組中返回一個(gè)切片很有用;如果你不小心追加到所謂的不可改變的切片中,就會(huì)強(qiáng)制復(fù)制,并且沒有數(shù)據(jù)被覆蓋,因?yàn)橐呀?jīng)沒有容量了。

這種形式的分片表達(dá)式在 Golang 規(guī)范中被稱為 "完整分片表達(dá)式"(full slice expression)。

切片的使用技巧

定義切片:

type SeriesInt64 struct {
   values   []int64
}

自從引入內(nèi)置的 append 以來,Go 1 中刪除的 container/vector 包的大部分功能都可以使用 append 和 copy 來復(fù)制。

自從引入泛型以來,golang.org/x/exp/slices 包中提供了其中幾個(gè)函數(shù)的泛型實(shí)現(xiàn)。

以下是矢量方法及其切片操作:

AppendVector

a = append(a, b...)

圖片

Copy

b := make([]T, len(a))
copy(b, a)

// These two are often a little slower than the above one,
// but they would be more efficient if there are more
// elements to be appended to b after copying.
b = append([]T(nil), a...)
b = append(a[:0:0], a...)

// This one-line implementation is equivalent to the above
// two-line make+copy implementation logically. But it is
// actually a bit slower (as of Go toolchain v1.16).
b = append(make([]T, 0, len(a)), a...)

圖片

封裝成函數(shù),可以這樣寫:

func (s *SeriesInt64) copy() *SeriesInt64 {
    if len(s.values) == 0 {
        return &SeriesInt64{
            values:[]int64{},
        }
    }
    // Copy slice
    x := s.values[0 : len(s.values)]
    newSlice := append(x[:0:0], x...)
    return &SeriesInt64{
        values: newSlice,
    }
}

Cut

a = append(a[:i], a[j:]...)

圖片

Delete

a = append(a[:i], a[i+1:]...)
// or
a = a[:i+copy(a[i:], a[i+1:])]

圖片

封裝后:

func (s *SeriesInt64) remove(row int) {
    s.values = append(s.values[:row], s.values[row+1:]...)
}

Delete without preserving order

a[i] = a[len(a)-1] 
a = a[:len(a)-1]

圖片

NOTE 如果元素的類型是指針或帶指針字段的結(jié)構(gòu)體,需要進(jìn)行垃圾回收,上述 Cut 和 Delete 的實(shí)現(xiàn)存在潛在的內(nèi)存泄漏問題:一些有值的元素仍然被切片 a 引用,從而無法收集。下面的代碼可以解決這個(gè)問題:

Cut

copy(a[i:], a[j:])
for k, n := len(a)-j+i, len(a); k < n; k++ {
 a[k] = nil // or the zero value of T
}
a = a[:len(a)-j+i]

圖片

Delete

copy(a[i:], a[i+1:])
a[len(a)-1] = nil // or the zero value of T
a = a[:len(a)-1]

Delete without preserving order

a[i] = a[len(a)-1]
a[len(a)-1] = nil
a = a[:len(a)-1]

Expand

Insert n elements at position i:

a = append(a[:i], append(make([]T, n), a[i:]...)...)

Extend

Append n elements:

a = append(a, make([]T, n)...)

Extend Capacity

Make sure there is space to append n elements without re-allocating:

if cap(a)-len(a) < n {
 a = append(make([]T, 0, len(a)+n), a...)
}

Filter (in place)

n := 0
for _, x := range a {
 if keep(x) {
  a[n] = x
  n++
 }
}
a = a[:n]

Insert

a = append(a[:i], append([]T{x}, a[i:]...)...)

注意:第二個(gè)追加創(chuàng)建一個(gè)新的切片,它有自己的底層存儲(chǔ),并將 a[i:] 中的元素復(fù)制到該切片,然后這些元素被復(fù)制回切片 a(由第一個(gè)追加)。使用替代方法可以避免創(chuàng)建新切片(以及內(nèi)存垃圾)和第二個(gè)副本:

Insert

s = append(s, 0 /* use the zero value of the element type */)
copy(s[i+1:], s[i:])
s[i] = x

封裝后:

func (s *SeriesInt64) insert(row int, val int64) {
    s.values = append(s.values, 0)
    copy(s.values[row+1:], s.values[row:])
    s.values[row] = val
}

InsertVector

a = append(a[:i], append(b, a[i:]...)...)

// The above one-line way copies a[i:] twice and
// allocates at least once.
// The following verbose way only copies elements
// in a[i:] once and allocates at most once.
// But, as of Go toolchain 1.16, due to lacking of
// optimizations to avoid elements clearing in the
// "make" call, the verbose way is not always faster.
//
// Future compiler optimizations might implement
// both in the most efficient ways.
//
// Assume element type is int.
func Insert(s []int, k int, vs ...int) []int {
 if n := len(s) + len(vs); n <= cap(s) {
  s2 := s[:n]
  copy(s2[k+len(vs):], s[k:])
  copy(s2[k:], vs)
  return s2
 }
 s2 := make([]int, len(s) + len(vs))
 copy(s2, s[:k])
 copy(s2[k:], vs)
 copy(s2[k+len(vs):], s[k:])
 return s2
}

a = Insert(a, i, b...)

Push

a = append(a, x)

圖片

Pop

x, a = a[len(a)-1], a[:len(a)-1]

圖片

Push Front/Unshift

a = append([]T{x}, a...)

圖片

Pop Front/Shift

x, a = a[0], a[1:]

圖片

Prepending

func (s *SeriesInt64) prepend(val int64) {
    if cap(s.values) > len(s.values) {
        s.values = s.values[:len(s.values)+1]
        copy(s.values[1:], s.values)
        s.values[0] = val
        return
    }
    // No extra capacity so a new slice needs to be allocated:
    s.insert(0, val)
}

圖片來源:

  • Go Slice Tricks Cheat Sheet
責(zé)任編輯:武曉燕 來源: 宇宙之一粟
相關(guān)推薦

2021-07-26 22:33:41

切片結(jié)構(gòu)體代碼

2013-04-16 10:48:04

Python序列

2022-04-06 08:19:13

Go語言切片

2021-05-24 10:24:42

Golang字符串Python

2022-10-27 19:32:20

切片golang數(shù)組

2024-10-15 08:57:08

Go語言切片

2011-11-28 15:16:05

Wireshark表達(dá)式IP過濾

2022-06-02 13:54:04

Go數(shù)組切片

2021-05-12 08:47:54

Go數(shù)組切片

2024-03-15 09:56:47

切片函數(shù)泛型

2023-05-03 09:09:28

Golang數(shù)組

2023-09-11 16:16:03

2024-01-09 16:14:39

RustGo切片

2023-11-03 14:02:04

Go切片泛型庫

2023-03-29 08:03:53

2021-08-05 06:54:05

Go切片數(shù)據(jù)

2021-07-29 10:08:15

NumPy索引切片

2022-09-02 10:20:44

網(wǎng)絡(luò)切片網(wǎng)絡(luò)5G

2023-04-03 08:02:16

切片擴(kuò)容GO

2023-12-27 08:12:04

切片Go語言
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號