blob: f34a66e73396cf36b121b9611defb0205649428f [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)
Juliusz Chroboczek70da14d2016-11-01 19:58:40 +010014// or, if a is not the empty slice,
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110015b = 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]
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110051```
Dave8f62fba2015-01-08 12:44:59 +110052
53> **Delete without preserving order**
Andrew Stone7a268d02015-03-11 13:12:10 -040054```go
alandonovanbddf9a52015-07-27 16:15:39 -040055a[i] = a[len(a)-1]
56a[len(a)-1] = nil
57a = a[:len(a)-1]
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110058```
59
Dave8f62fba2015-01-08 12:44:59 +110060**Expand**
Andrew Stone7a268d02015-03-11 13:12:10 -040061```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110062a = append(a[:i], append(make([]T, j), a[i:]...)...)
63```
Dave8f62fba2015-01-08 12:44:59 +110064
65**Extend**
Andrew Stone7a268d02015-03-11 13:12:10 -040066```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110067a = append(a, make([]T, j)...)
68```
69
Dave8f62fba2015-01-08 12:44:59 +110070**Insert**
Andrew Stone7a268d02015-03-11 13:12:10 -040071```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110072a = append(a[:i], append([]T{x}, a[i:]...)...)
73```
74**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 +110075> **Insert**
Andrew Stone7a268d02015-03-11 13:12:10 -040076```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110077s = append(s, 0)
78copy(s[i+1:], s[i:])
79s[i] = x
80```
81
Dave8f62fba2015-01-08 12:44:59 +110082**InsertVector**
Andrew Stone7a268d02015-03-11 13:12:10 -040083```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110084a = append(a[:i], append(b, a[i:]...)...)
85```
Dave8f62fba2015-01-08 12:44:59 +110086
87**Pop**
Andrew Stone7a268d02015-03-11 13:12:10 -040088```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110089x, a = a[len(a)-1], a[:len(a)-1]
90```
Dave8f62fba2015-01-08 12:44:59 +110091
92**Push**
Andrew Stone7a268d02015-03-11 13:12:10 -040093```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110094a = append(a, x)
95```
96
Joshua Mervined37365d2015-03-10 22:25:29 -070097**Shift**
Andrew Stone7a268d02015-03-11 13:12:10 -040098```go
Joshua Mervined37365d2015-03-10 22:25:29 -070099x, a := a[0], a[1:]
100```
101
102**Unshift**
Andrew Stone7a268d02015-03-11 13:12:10 -0400103```go
Joshua Mervine0ab65812015-03-10 22:28:12 -0700104a = append([]T{x}, a...)
Joshua Mervined37365d2015-03-10 22:25:29 -0700105```
106
Andrew Gerrand5bc444d2014-12-10 11:35:11 +1100107## Additional Tricks
108### Filtering without allocating
109
110This 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.
111
Andrew Stone7a268d02015-03-11 13:12:10 -0400112```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +1100113b := a[:0]
114for _, x := range a {
Dave Day0d6986a2014-12-10 15:02:18 +1100115 if f(x) {
116 b = append(b, x)
117 }
Andrew Gerrand5bc444d2014-12-10 11:35:11 +1100118}
Kamil Kisiel1f350912015-04-20 13:46:59 -0700119```
120
121### Reversing
122
123To replace the contents of a slice with the same elements but in reverse order:
124```go
David Deng2b4bafd2015-04-28 08:01:53 -0700125for i := len(a)/2-1; i >= 0; i-- {
Kamil Kisiel1f350912015-04-20 13:46:59 -0700126 opp := len(a)-1-i
127 a[i], a[opp] = a[opp], a[i]
128}
lemmidb4a3882015-06-26 17:43:13 +0200129```
130The same thing, except with two indices:
131```go
132for left, right := 0, len(a)-1; left < right; left, right = left+1, right-1 {
133 a[left], a[right] = a[right], a[left]
134}
Andrew Gerrand5bc444d2014-12-10 11:35:11 +1100135```