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) |
| 14 | // or |
| 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] |
| 51 | // or, more simply: |
Andrew Stone | 61ac3f8 | 2015-08-03 19:08:46 -0400 | [diff] [blame] | 52 | a, a[len(a)-1] = append(a[:i], a[i+1:]...), nil |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 53 | ``` |
Dave | 8f62fba | 2015-01-08 12:44:59 +1100 | [diff] [blame] | 54 | |
| 55 | > **Delete without preserving order** |
Andrew Stone | 7a268d0 | 2015-03-11 13:12:10 -0400 | [diff] [blame] | 56 | ```go |
alandonovan | bddf9a5 | 2015-07-27 16:15:39 -0400 | [diff] [blame] | 57 | a[i] = a[len(a)-1] |
| 58 | a[len(a)-1] = nil |
| 59 | a = a[:len(a)-1] |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 60 | ``` |
| 61 | |
Dave | 8f62fba | 2015-01-08 12:44:59 +1100 | [diff] [blame] | 62 | **Expand** |
Andrew Stone | 7a268d0 | 2015-03-11 13:12:10 -0400 | [diff] [blame] | 63 | ```go |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 64 | a = append(a[:i], append(make([]T, j), a[i:]...)...) |
| 65 | ``` |
Dave | 8f62fba | 2015-01-08 12:44:59 +1100 | [diff] [blame] | 66 | |
| 67 | **Extend** |
Andrew Stone | 7a268d0 | 2015-03-11 13:12:10 -0400 | [diff] [blame] | 68 | ```go |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 69 | a = append(a, make([]T, j)...) |
| 70 | ``` |
| 71 | |
Dave | 8f62fba | 2015-01-08 12:44:59 +1100 | [diff] [blame] | 72 | **Insert** |
Andrew Stone | 7a268d0 | 2015-03-11 13:12:10 -0400 | [diff] [blame] | 73 | ```go |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 74 | a = 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: |
Dave | 8f62fba | 2015-01-08 12:44:59 +1100 | [diff] [blame] | 77 | > **Insert** |
Andrew Stone | 7a268d0 | 2015-03-11 13:12:10 -0400 | [diff] [blame] | 78 | ```go |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 79 | s = append(s, 0) |
| 80 | copy(s[i+1:], s[i:]) |
| 81 | s[i] = x |
| 82 | ``` |
| 83 | |
Dave | 8f62fba | 2015-01-08 12:44:59 +1100 | [diff] [blame] | 84 | **InsertVector** |
Andrew Stone | 7a268d0 | 2015-03-11 13:12:10 -0400 | [diff] [blame] | 85 | ```go |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 86 | a = append(a[:i], append(b, a[i:]...)...) |
| 87 | ``` |
Dave | 8f62fba | 2015-01-08 12:44:59 +1100 | [diff] [blame] | 88 | |
| 89 | **Pop** |
Andrew Stone | 7a268d0 | 2015-03-11 13:12:10 -0400 | [diff] [blame] | 90 | ```go |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 91 | x, a = a[len(a)-1], a[:len(a)-1] |
| 92 | ``` |
Dave | 8f62fba | 2015-01-08 12:44:59 +1100 | [diff] [blame] | 93 | |
| 94 | **Push** |
Andrew Stone | 7a268d0 | 2015-03-11 13:12:10 -0400 | [diff] [blame] | 95 | ```go |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 96 | a = append(a, x) |
| 97 | ``` |
| 98 | |
Joshua Mervine | d37365d | 2015-03-10 22:25:29 -0700 | [diff] [blame] | 99 | **Shift** |
Andrew Stone | 7a268d0 | 2015-03-11 13:12:10 -0400 | [diff] [blame] | 100 | ```go |
Joshua Mervine | d37365d | 2015-03-10 22:25:29 -0700 | [diff] [blame] | 101 | x, a := a[0], a[1:] |
| 102 | ``` |
| 103 | |
| 104 | **Unshift** |
Andrew Stone | 7a268d0 | 2015-03-11 13:12:10 -0400 | [diff] [blame] | 105 | ```go |
Joshua Mervine | 0ab6581 | 2015-03-10 22:28:12 -0700 | [diff] [blame] | 106 | a = append([]T{x}, a...) |
Joshua Mervine | d37365d | 2015-03-10 22:25:29 -0700 | [diff] [blame] | 107 | ``` |
| 108 | |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 109 | ## Additional Tricks |
| 110 | ### Filtering without allocating |
| 111 | |
| 112 | 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. |
| 113 | |
Andrew Stone | 7a268d0 | 2015-03-11 13:12:10 -0400 | [diff] [blame] | 114 | ```go |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 115 | b := a[:0] |
| 116 | for _, x := range a { |
Dave Day | 0d6986a | 2014-12-10 15:02:18 +1100 | [diff] [blame] | 117 | if f(x) { |
| 118 | b = append(b, x) |
| 119 | } |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 120 | } |
Kamil Kisiel | 1f35091 | 2015-04-20 13:46:59 -0700 | [diff] [blame] | 121 | ``` |
| 122 | |
| 123 | ### Reversing |
| 124 | |
| 125 | To replace the contents of a slice with the same elements but in reverse order: |
| 126 | ```go |
David Deng | 2b4bafd | 2015-04-28 08:01:53 -0700 | [diff] [blame] | 127 | for i := len(a)/2-1; i >= 0; i-- { |
Kamil Kisiel | 1f35091 | 2015-04-20 13:46:59 -0700 | [diff] [blame] | 128 | opp := len(a)-1-i |
| 129 | a[i], a[opp] = a[opp], a[i] |
| 130 | } |
lemmi | db4a388 | 2015-06-26 17:43:13 +0200 | [diff] [blame] | 131 | ``` |
| 132 | The same thing, except with two indices: |
| 133 | ```go |
| 134 | for 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 Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 137 | ``` |