Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 1 | Since 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 | |
| 3 | Here are the vector methods and their slice-manipulation analogues: |
| 4 | |
Dave | 8f62fba | 2015-01-08 12:44:59 +1100 | [diff] [blame] | 5 | **AppendVector** |
Andrew Stone | 7a268d0 | 2015-03-11 13:12:10 -0400 | [diff] [blame] | 6 | ```go |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 7 | a = append(a, b...) |
| 8 | ``` |
Dave | 8f62fba | 2015-01-08 12:44:59 +1100 | [diff] [blame] | 9 | |
| 10 | **Copy** |
Andrew Stone | 7a268d0 | 2015-03-11 13:12:10 -0400 | [diff] [blame] | 11 | ```go |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 12 | b = make([]T, len(a)) |
| 13 | copy(b, a) |
Juliusz Chroboczek | 70da14d | 2016-11-01 19:58:40 +0100 | [diff] [blame] | 14 | // or, if a is not the empty slice, |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 15 | b = append([]T(nil), a...) |
| 16 | ``` |
Dave | 8f62fba | 2015-01-08 12:44:59 +1100 | [diff] [blame] | 17 | |
| 18 | **Cut** |
Andrew Stone | 7a268d0 | 2015-03-11 13:12:10 -0400 | [diff] [blame] | 19 | ```go |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 20 | a = append(a[:i], a[j:]...) |
| 21 | ``` |
Dave | 8f62fba | 2015-01-08 12:44:59 +1100 | [diff] [blame] | 22 | |
| 23 | **Delete** |
Andrew Stone | 7a268d0 | 2015-03-11 13:12:10 -0400 | [diff] [blame] | 24 | ```go |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 25 | a = append(a[:i], a[i+1:]...) |
| 26 | // or |
| 27 | a = a[:i+copy(a[i:], a[i+1:])] |
| 28 | ``` |
Dave | 8f62fba | 2015-01-08 12:44:59 +1100 | [diff] [blame] | 29 | |
| 30 | **Delete without preserving order** |
Andrew Stone | 7a268d0 | 2015-03-11 13:12:10 -0400 | [diff] [blame] | 31 | ```go |
Arjan Velner | 0028726 | 2015-07-29 10:37:23 +0200 | [diff] [blame] | 32 | a[i] = a[len(a)-1] |
alandonovan | af2ba02 | 2015-07-27 16:14:02 -0400 | [diff] [blame] | 33 | a = a[:len(a)-1] |
Andrew Stone | 7a268d0 | 2015-03-11 13:12:10 -0400 | [diff] [blame] | 34 | |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 35 | ``` |
| 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: |
Dave | 8f62fba | 2015-01-08 12:44:59 +1100 | [diff] [blame] | 37 | > **Cut** |
Andrew Stone | 7a268d0 | 2015-03-11 13:12:10 -0400 | [diff] [blame] | 38 | ```go |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 39 | copy(a[i:], a[j:]) |
Dave Day | 0d6986a | 2014-12-10 15:02:18 +1100 | [diff] [blame] | 40 | for k, n := len(a)-j+i, len(a); k < n; k++ { |
| 41 | a[k] = nil // or the zero value of T |
David Deng | 2b4bafd | 2015-04-28 08:01:53 -0700 | [diff] [blame] | 42 | } |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 43 | a = a[:len(a)-j+i] |
| 44 | ``` |
Dave | 8f62fba | 2015-01-08 12:44:59 +1100 | [diff] [blame] | 45 | |
| 46 | > **Delete** |
Andrew Stone | 7a268d0 | 2015-03-11 13:12:10 -0400 | [diff] [blame] | 47 | ```go |
Andrew Stone | 55f404a | 2015-03-11 13:10:56 -0400 | [diff] [blame] | 48 | copy(a[i:], a[i+1:]) |
| 49 | a[len(a)-1] = nil // or the zero value of T |
| 50 | a = a[:len(a)-1] |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 51 | ``` |
Dave | 8f62fba | 2015-01-08 12:44:59 +1100 | [diff] [blame] | 52 | |
| 53 | > **Delete without preserving order** |
Andrew Stone | 7a268d0 | 2015-03-11 13:12:10 -0400 | [diff] [blame] | 54 | ```go |
alandonovan | bddf9a5 | 2015-07-27 16:15:39 -0400 | [diff] [blame] | 55 | a[i] = a[len(a)-1] |
| 56 | a[len(a)-1] = nil |
| 57 | a = a[:len(a)-1] |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 58 | ``` |
| 59 | |
Dave | 8f62fba | 2015-01-08 12:44:59 +1100 | [diff] [blame] | 60 | **Expand** |
Andrew Stone | 7a268d0 | 2015-03-11 13:12:10 -0400 | [diff] [blame] | 61 | ```go |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 62 | a = append(a[:i], append(make([]T, j), a[i:]...)...) |
| 63 | ``` |
Dave | 8f62fba | 2015-01-08 12:44:59 +1100 | [diff] [blame] | 64 | |
| 65 | **Extend** |
Andrew Stone | 7a268d0 | 2015-03-11 13:12:10 -0400 | [diff] [blame] | 66 | ```go |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 67 | a = append(a, make([]T, j)...) |
| 68 | ``` |
| 69 | |
Dave | 8f62fba | 2015-01-08 12:44:59 +1100 | [diff] [blame] | 70 | **Insert** |
Andrew Stone | 7a268d0 | 2015-03-11 13:12:10 -0400 | [diff] [blame] | 71 | ```go |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 72 | a = 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: |
Dave | 8f62fba | 2015-01-08 12:44:59 +1100 | [diff] [blame] | 75 | > **Insert** |
Andrew Stone | 7a268d0 | 2015-03-11 13:12:10 -0400 | [diff] [blame] | 76 | ```go |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 77 | s = append(s, 0) |
| 78 | copy(s[i+1:], s[i:]) |
| 79 | s[i] = x |
| 80 | ``` |
| 81 | |
Dave | 8f62fba | 2015-01-08 12:44:59 +1100 | [diff] [blame] | 82 | **InsertVector** |
Andrew Stone | 7a268d0 | 2015-03-11 13:12:10 -0400 | [diff] [blame] | 83 | ```go |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 84 | a = append(a[:i], append(b, a[i:]...)...) |
| 85 | ``` |
Dave | 8f62fba | 2015-01-08 12:44:59 +1100 | [diff] [blame] | 86 | |
| 87 | **Pop** |
Andrew Stone | 7a268d0 | 2015-03-11 13:12:10 -0400 | [diff] [blame] | 88 | ```go |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 89 | x, a = a[len(a)-1], a[:len(a)-1] |
| 90 | ``` |
Dave | 8f62fba | 2015-01-08 12:44:59 +1100 | [diff] [blame] | 91 | |
| 92 | **Push** |
Andrew Stone | 7a268d0 | 2015-03-11 13:12:10 -0400 | [diff] [blame] | 93 | ```go |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 94 | a = append(a, x) |
| 95 | ``` |
| 96 | |
Joshua Mervine | d37365d | 2015-03-10 22:25:29 -0700 | [diff] [blame] | 97 | **Shift** |
Andrew Stone | 7a268d0 | 2015-03-11 13:12:10 -0400 | [diff] [blame] | 98 | ```go |
Joshua Mervine | d37365d | 2015-03-10 22:25:29 -0700 | [diff] [blame] | 99 | x, a := a[0], a[1:] |
| 100 | ``` |
| 101 | |
| 102 | **Unshift** |
Andrew Stone | 7a268d0 | 2015-03-11 13:12:10 -0400 | [diff] [blame] | 103 | ```go |
Joshua Mervine | 0ab6581 | 2015-03-10 22:28:12 -0700 | [diff] [blame] | 104 | a = append([]T{x}, a...) |
Joshua Mervine | d37365d | 2015-03-10 22:25:29 -0700 | [diff] [blame] | 105 | ``` |
| 106 | |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 107 | ## Additional Tricks |
| 108 | ### Filtering without allocating |
| 109 | |
| 110 | This 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 Stone | 7a268d0 | 2015-03-11 13:12:10 -0400 | [diff] [blame] | 112 | ```go |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 113 | b := a[:0] |
| 114 | for _, x := range a { |
Dave Day | 0d6986a | 2014-12-10 15:02:18 +1100 | [diff] [blame] | 115 | if f(x) { |
| 116 | b = append(b, x) |
| 117 | } |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 118 | } |
Kamil Kisiel | 1f35091 | 2015-04-20 13:46:59 -0700 | [diff] [blame] | 119 | ``` |
| 120 | |
| 121 | ### Reversing |
| 122 | |
| 123 | To replace the contents of a slice with the same elements but in reverse order: |
| 124 | ```go |
David Deng | 2b4bafd | 2015-04-28 08:01:53 -0700 | [diff] [blame] | 125 | for i := len(a)/2-1; i >= 0; i-- { |
Kamil Kisiel | 1f35091 | 2015-04-20 13:46:59 -0700 | [diff] [blame] | 126 | opp := len(a)-1-i |
| 127 | a[i], a[opp] = a[opp], a[i] |
| 128 | } |
lemmi | db4a388 | 2015-06-26 17:43:13 +0200 | [diff] [blame] | 129 | ``` |
| 130 | The same thing, except with two indices: |
| 131 | ```go |
| 132 | for 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 Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 135 | ``` |