blob: d501af102e3addab5e6f2a89e55b124f282e213f [file] [log] [blame] [view]
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11001Since the introduction of the ` append ` built-in, most of the functionality of the ` container/vector ` package, which was removed in Go 1, can be replicated using ` append ` and ` copy `.
2
3Here are the vector methods and their slice-manipulation analogues:
4
Dave8f62fba2015-01-08 12:44:59 +11005**AppendVector**
Andrew Stone7a268d02015-03-11 13:12:10 -04006```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11007a = append(a, b...)
8```
Dave8f62fba2015-01-08 12:44:59 +11009
10**Copy**
Andrew Stone7a268d02015-03-11 13:12:10 -040011```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110012b = make([]T, len(a))
13copy(b, a)
14// or
15b = append([]T(nil), a...)
16```
Dave8f62fba2015-01-08 12:44:59 +110017
18**Cut**
Andrew Stone7a268d02015-03-11 13:12:10 -040019```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110020a = append(a[:i], a[j:]...)
21```
Dave8f62fba2015-01-08 12:44:59 +110022
23**Delete**
Andrew Stone7a268d02015-03-11 13:12:10 -040024```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110025a = append(a[:i], a[i+1:]...)
26// or
27a = a[:i+copy(a[i:], a[i+1:])]
28```
Dave8f62fba2015-01-08 12:44:59 +110029
30**Delete without preserving order**
Andrew Stone7a268d02015-03-11 13:12:10 -040031```go
Arjan Velner00287262015-07-29 10:37:23 +020032a[i] = a[len(a)-1]
alandonovanaf2ba022015-07-27 16:14:02 -040033a = a[:len(a)-1]
Andrew Stone7a268d02015-03-11 13:12:10 -040034
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110035```
36**NOTE** If the type of the element is a _pointer_ or a struct with pointer fields, which need to be garbage collected, the above implementations of ` Cut ` and ` Delete ` have a potential _memory leak_ problem: some elements with values are still referenced by slice ` a ` and thus can not be collected. The following code can fix this problem:
Dave8f62fba2015-01-08 12:44:59 +110037> **Cut**
Andrew Stone7a268d02015-03-11 13:12:10 -040038```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110039copy(a[i:], a[j:])
Dave Day0d6986a2014-12-10 15:02:18 +110040for k, n := len(a)-j+i, len(a); k < n; k++ {
41 a[k] = nil // or the zero value of T
David Deng2b4bafd2015-04-28 08:01:53 -070042}
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110043a = a[:len(a)-j+i]
44```
Dave8f62fba2015-01-08 12:44:59 +110045
46> **Delete**
Andrew Stone7a268d02015-03-11 13:12:10 -040047```go
Andrew Stone55f404a2015-03-11 13:10:56 -040048copy(a[i:], a[i+1:])
49a[len(a)-1] = nil // or the zero value of T
50a = a[:len(a)-1]
51// or, more simply:
Andrew Stone61ac3f82015-08-03 19:08:46 -040052a, a[len(a)-1] = append(a[:i], a[i+1:]...), nil
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110053```
Dave8f62fba2015-01-08 12:44:59 +110054
55> **Delete without preserving order**
Andrew Stone7a268d02015-03-11 13:12:10 -040056```go
alandonovanbddf9a52015-07-27 16:15:39 -040057a[i] = a[len(a)-1]
58a[len(a)-1] = nil
59a = a[:len(a)-1]
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110060```
61
Dave8f62fba2015-01-08 12:44:59 +110062**Expand**
Andrew Stone7a268d02015-03-11 13:12:10 -040063```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110064a = append(a[:i], append(make([]T, j), a[i:]...)...)
65```
Dave8f62fba2015-01-08 12:44:59 +110066
67**Extend**
Andrew Stone7a268d02015-03-11 13:12:10 -040068```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110069a = append(a, make([]T, j)...)
70```
71
Dave8f62fba2015-01-08 12:44:59 +110072**Insert**
Andrew Stone7a268d02015-03-11 13:12:10 -040073```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110074a = append(a[:i], append([]T{x}, a[i:]...)...)
75```
76**NOTE** The second ` append ` creates a new slice with its own underlying storage and copies elements in ` a[i:] ` to that slice, and these elements are then copied back to slice ` a ` (by the first ` append `). The creation of the new slice (and thus memory garbage) and the second copy can be avoided by using an alternative way:
Dave8f62fba2015-01-08 12:44:59 +110077> **Insert**
Andrew Stone7a268d02015-03-11 13:12:10 -040078```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110079s = append(s, 0)
80copy(s[i+1:], s[i:])
81s[i] = x
82```
83
Dave8f62fba2015-01-08 12:44:59 +110084**InsertVector**
Andrew Stone7a268d02015-03-11 13:12:10 -040085```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110086a = append(a[:i], append(b, a[i:]...)...)
87```
Dave8f62fba2015-01-08 12:44:59 +110088
89**Pop**
Andrew Stone7a268d02015-03-11 13:12:10 -040090```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110091x, a = a[len(a)-1], a[:len(a)-1]
92```
Dave8f62fba2015-01-08 12:44:59 +110093
94**Push**
Andrew Stone7a268d02015-03-11 13:12:10 -040095```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110096a = append(a, x)
97```
98
Joshua Mervined37365d2015-03-10 22:25:29 -070099**Shift**
Andrew Stone7a268d02015-03-11 13:12:10 -0400100```go
Joshua Mervined37365d2015-03-10 22:25:29 -0700101x, a := a[0], a[1:]
102```
103
104**Unshift**
Andrew Stone7a268d02015-03-11 13:12:10 -0400105```go
Joshua Mervine0ab65812015-03-10 22:28:12 -0700106a = append([]T{x}, a...)
Joshua Mervined37365d2015-03-10 22:25:29 -0700107```
108
Andrew Gerrand5bc444d2014-12-10 11:35:11 +1100109## Additional Tricks
110### Filtering without allocating
111
112This trick uses the fact that a slice shares the same backing array and capacity as the original, so the storage is reused for the filtered slice. Of course, the original contents are modified.
113
Andrew Stone7a268d02015-03-11 13:12:10 -0400114```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +1100115b := a[:0]
116for _, x := range a {
Dave Day0d6986a2014-12-10 15:02:18 +1100117 if f(x) {
118 b = append(b, x)
119 }
Andrew Gerrand5bc444d2014-12-10 11:35:11 +1100120}
Kamil Kisiel1f350912015-04-20 13:46:59 -0700121```
122
123### Reversing
124
125To replace the contents of a slice with the same elements but in reverse order:
126```go
David Deng2b4bafd2015-04-28 08:01:53 -0700127for i := len(a)/2-1; i >= 0; i-- {
Kamil Kisiel1f350912015-04-20 13:46:59 -0700128 opp := len(a)-1-i
129 a[i], a[opp] = a[opp], a[i]
130}
lemmidb4a3882015-06-26 17:43:13 +0200131```
132The same thing, except with two indices:
133```go
134for left, right := 0, len(a)-1; left < right; left, right = left+1, right-1 {
135 a[left], a[right] = a[right], a[left]
136}
Andrew Gerrand5bc444d2014-12-10 11:35:11 +1100137```