Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(738)

Delta Between Two Patch Sets: src/pkg/container/vector/vector.go

Issue 4715041: code review 4715041: go/printer: changed max. number of newlines from 3 to 2 (Closed)
Left Patch Set: Created 13 years, 8 months ago
Right Patch Set: diff -r 43f78423340b https://go.googlecode.com/hg/ Created 13 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Right: Side by side diff | Download
« no previous file with change/comment | « src/pkg/container/vector/stringvector_test.go ('k') | src/pkg/container/vector/vector_test.go » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
1 // Copyright 2009 The Go Authors. All rights reserved. 1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 // CAUTION: If this file is not vector.go, it was generated 5 // CAUTION: If this file is not vector.go, it was generated
6 // automatically from vector.go - DO NOT EDIT in that case! 6 // automatically from vector.go - DO NOT EDIT in that case!
7 7
8 package vector 8 package vector
9
10 9
11 func (p *Vector) realloc(length, capacity int) (b []interface{}) { 10 func (p *Vector) realloc(length, capacity int) (b []interface{}) {
12 if capacity < initialSize { 11 if capacity < initialSize {
13 capacity = initialSize 12 capacity = initialSize
14 } 13 }
15 if capacity < length { 14 if capacity < length {
16 capacity = length 15 capacity = length
17 } 16 }
18 b = make(Vector, length, capacity) 17 b = make(Vector, length, capacity)
19 copy(b, *p) 18 copy(b, *p)
20 *p = b 19 *p = b
21 return 20 return
22 } 21 }
23
24 22
25 // Insert n elements at position i. 23 // Insert n elements at position i.
26 func (p *Vector) Expand(i, n int) { 24 func (p *Vector) Expand(i, n int) {
27 a := *p 25 a := *p
28 26
29 // make sure we have enough space 27 // make sure we have enough space
30 len0 := len(a) 28 len0 := len(a)
31 len1 := len0 + n 29 len1 := len0 + n
32 if len1 <= cap(a) { 30 if len1 <= cap(a) {
33 // enough space - just expand 31 // enough space - just expand
(...skipping 10 matching lines...) Expand all
44 } 42 }
45 43
46 // make a hole 44 // make a hole
47 for j := len0 - 1; j >= i; j-- { 45 for j := len0 - 1; j >= i; j-- {
48 a[j+n] = a[j] 46 a[j+n] = a[j]
49 } 47 }
50 48
51 *p = a 49 *p = a
52 } 50 }
53 51
54
55 // Insert n elements at the end of a vector. 52 // Insert n elements at the end of a vector.
56 func (p *Vector) Extend(n int) { p.Expand(len(*p), n) } 53 func (p *Vector) Extend(n int) { p.Expand(len(*p), n) }
57
58 54
59 // Resize changes the length and capacity of a vector. 55 // Resize changes the length and capacity of a vector.
60 // If the new length is shorter than the current length, Resize discards 56 // If the new length is shorter than the current length, Resize discards
61 // trailing elements. If the new length is longer than the current length, 57 // trailing elements. If the new length is longer than the current length,
62 // Resize adds the respective zero values for the additional elements. The capac ity 58 // Resize adds the respective zero values for the additional elements. The capac ity
63 // parameter is ignored unless the new length or capacity is longer than the cur rent 59 // parameter is ignored unless the new length or capacity is longer than the cur rent
64 // capacity. The resized vector's capacity may be larger than the requested capa city. 60 // capacity. The resized vector's capacity may be larger than the requested capa city.
65 func (p *Vector) Resize(length, capacity int) *Vector { 61 func (p *Vector) Resize(length, capacity int) *Vector {
66 a := *p 62 a := *p
67 63
68 if length > cap(a) || capacity > cap(a) { 64 if length > cap(a) || capacity > cap(a) {
69 // not enough space or larger capacity requested explicitly 65 // not enough space or larger capacity requested explicitly
70 a = p.realloc(length, capacity) 66 a = p.realloc(length, capacity)
71 } else if length < len(a) { 67 } else if length < len(a) {
72 // clear trailing elements 68 // clear trailing elements
73 for i := range a[length:] { 69 for i := range a[length:] {
74 var zero interface{} 70 var zero interface{}
75 a[length+i] = zero 71 a[length+i] = zero
76 } 72 }
77 } 73 }
78 74
79 *p = a[0:length] 75 *p = a[0:length]
80 return p 76 return p
81 } 77 }
82 78
83
84 // Len returns the number of elements in the vector. 79 // Len returns the number of elements in the vector.
85 // Same as len(*p). 80 // Same as len(*p).
86 func (p *Vector) Len() int { return len(*p) } 81 func (p *Vector) Len() int { return len(*p) }
87
88 82
89 // Cap returns the capacity of the vector; that is, the 83 // Cap returns the capacity of the vector; that is, the
90 // maximum length the vector can grow without resizing. 84 // maximum length the vector can grow without resizing.
91 // Same as cap(*p). 85 // Same as cap(*p).
92 func (p *Vector) Cap() int { return cap(*p) } 86 func (p *Vector) Cap() int { return cap(*p) }
93 87
94
95 // At returns the i'th element of the vector. 88 // At returns the i'th element of the vector.
96 func (p *Vector) At(i int) interface{} { return (*p)[i] } 89 func (p *Vector) At(i int) interface{} { return (*p)[i] }
97
98 90
99 // Set sets the i'th element of the vector to value x. 91 // Set sets the i'th element of the vector to value x.
100 func (p *Vector) Set(i int, x interface{}) { (*p)[i] = x } 92 func (p *Vector) Set(i int, x interface{}) { (*p)[i] = x }
101 93
102
103 // Last returns the element in the vector of highest index. 94 // Last returns the element in the vector of highest index.
104 func (p *Vector) Last() interface{} { return (*p)[len(*p)-1] } 95 func (p *Vector) Last() interface{} { return (*p)[len(*p)-1] }
105
106 96
107 // Copy makes a copy of the vector and returns it. 97 // Copy makes a copy of the vector and returns it.
108 func (p *Vector) Copy() Vector { 98 func (p *Vector) Copy() Vector {
109 arr := make(Vector, len(*p)) 99 arr := make(Vector, len(*p))
110 copy(arr, *p) 100 copy(arr, *p)
111 return arr 101 return arr
112 } 102 }
113 103
114
115 // Insert inserts into the vector an element of value x before 104 // Insert inserts into the vector an element of value x before
116 // the current element at index i. 105 // the current element at index i.
117 func (p *Vector) Insert(i int, x interface{}) { 106 func (p *Vector) Insert(i int, x interface{}) {
118 p.Expand(i, 1) 107 p.Expand(i, 1)
119 (*p)[i] = x 108 (*p)[i] = x
120 } 109 }
121
122 110
123 // Delete deletes the i'th element of the vector. The gap is closed so the old 111 // Delete deletes the i'th element of the vector. The gap is closed so the old
124 // element at index i+1 has index i afterwards. 112 // element at index i+1 has index i afterwards.
125 func (p *Vector) Delete(i int) { 113 func (p *Vector) Delete(i int) {
126 a := *p 114 a := *p
127 n := len(a) 115 n := len(a)
128 116
129 copy(a[i:n-1], a[i+1:n]) 117 copy(a[i:n-1], a[i+1:n])
130 var zero interface{} 118 var zero interface{}
131 a[n-1] = zero // support GC, zero out entry 119 a[n-1] = zero // support GC, zero out entry
132 *p = a[0 : n-1] 120 *p = a[0 : n-1]
133 } 121 }
134 122
135
136 // InsertVector inserts into the vector the contents of the vector 123 // InsertVector inserts into the vector the contents of the vector
137 // x such that the 0th element of x appears at index i after insertion. 124 // x such that the 0th element of x appears at index i after insertion.
138 func (p *Vector) InsertVector(i int, x *Vector) { 125 func (p *Vector) InsertVector(i int, x *Vector) {
139 b := *x 126 b := *x
140 127
141 p.Expand(i, len(b)) 128 p.Expand(i, len(b))
142 copy((*p)[i:i+len(b)], b) 129 copy((*p)[i:i+len(b)], b)
143 } 130 }
144
145 131
146 // Cut deletes elements i through j-1, inclusive. 132 // Cut deletes elements i through j-1, inclusive.
147 func (p *Vector) Cut(i, j int) { 133 func (p *Vector) Cut(i, j int) {
148 a := *p 134 a := *p
149 n := len(a) 135 n := len(a)
150 m := n - (j - i) 136 m := n - (j - i)
151 137
152 copy(a[i:m], a[j:n]) 138 copy(a[i:m], a[j:n])
153 for k := m; k < n; k++ { //TODO(bflm) don't zero out the elements unless it's a Vector. 139 for k := m; k < n; k++ { //TODO(bflm) don't zero out the elements unless it's a Vector.
154 var zero interface{} 140 var zero interface{}
155 a[k] = zero // support GC, zero out entries 141 a[k] = zero // support GC, zero out entries
156 } 142 }
157 143
158 *p = a[0:m] 144 *p = a[0:m]
159 } 145 }
160 146
161
162 // Slice returns a new sub-vector by slicing the old one to extract slice [i:j]. 147 // Slice returns a new sub-vector by slicing the old one to extract slice [i:j].
163 // The elements are copied. The original vector is unchanged. 148 // The elements are copied. The original vector is unchanged.
164 func (p *Vector) Slice(i, j int) *Vector { 149 func (p *Vector) Slice(i, j int) *Vector {
165 var s Vector 150 var s Vector
166 s.realloc(j-i, 0) // will fail in Init() if j < i 151 s.realloc(j-i, 0) // will fail in Init() if j < i
167 copy(s, (*p)[i:j]) 152 copy(s, (*p)[i:j])
168 return &s 153 return &s
169 } 154 }
170 155
171
172 // Convenience wrappers 156 // Convenience wrappers
173 157
174 // Push appends x to the end of the vector. 158 // Push appends x to the end of the vector.
175 func (p *Vector) Push(x interface{}) { p.Insert(len(*p), x) } 159 func (p *Vector) Push(x interface{}) { p.Insert(len(*p), x) }
176
177 160
178 // Pop deletes the last element of the vector. 161 // Pop deletes the last element of the vector.
179 func (p *Vector) Pop() interface{} { 162 func (p *Vector) Pop() interface{} {
180 a := *p 163 a := *p
181 164
182 i := len(a) - 1 165 i := len(a) - 1
183 x := a[i] 166 x := a[i]
184 var zero interface{} 167 var zero interface{}
185 a[i] = zero // support GC, zero out entry 168 a[i] = zero // support GC, zero out entry
186 *p = a[0:i] 169 *p = a[0:i]
187 return x 170 return x
188 } 171 }
189 172
190
191 // AppendVector appends the entire vector x to the end of this vector. 173 // AppendVector appends the entire vector x to the end of this vector.
192 func (p *Vector) AppendVector(x *Vector) { p.InsertVector(len(*p), x) } 174 func (p *Vector) AppendVector(x *Vector) { p.InsertVector(len(*p), x) }
193
194 175
195 // Swap exchanges the elements at indexes i and j. 176 // Swap exchanges the elements at indexes i and j.
196 func (p *Vector) Swap(i, j int) { 177 func (p *Vector) Swap(i, j int) {
197 a := *p 178 a := *p
198 a[i], a[j] = a[j], a[i] 179 a[i], a[j] = a[j], a[i]
199 } 180 }
200
201 181
202 // Do calls function f for each element of the vector, in order. 182 // Do calls function f for each element of the vector, in order.
203 // The behavior of Do is undefined if f changes *p. 183 // The behavior of Do is undefined if f changes *p.
204 func (p *Vector) Do(f func(elem interface{})) { 184 func (p *Vector) Do(f func(elem interface{})) {
205 for _, e := range *p { 185 for _, e := range *p {
206 f(e) 186 f(e)
207 } 187 }
208 } 188 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b