Skip to content

Commit 23843fa

Browse files
committed
vector: s/Element/interface{}/
Fixes #74. R=rsc https://golang.org/cl/154073
1 parent 3b092fe commit 23843fa

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

src/pkg/container/vector/vector.go

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,22 @@
66
// linear arrays of elements. Unlike arrays, vectors can change size dynamically.
77
package vector
88

9-
// Element is an empty-interface object representing the contents of
10-
// a cell in the vector.
11-
type Element interface{}
12-
13-
149
// Vector is the container itself.
1510
// The zero value for Vector is an empty vector ready to use.
1611
type Vector struct {
17-
a []Element;
12+
a []interface{};
1813
}
1914

2015

21-
func copy(dst, src []Element) {
16+
func copy(dst, src []interface{}) {
2217
for i, x := range src {
2318
dst[i] = x
2419
}
2520
}
2621

2722

2823
// Insert n elements at position i.
29-
func expand(a []Element, i, n int) []Element {
24+
func expand(a []interface{}, i, n int) []interface{} {
3025
// make sure we have enough space
3126
len0 := len(a);
3227
len1 := len0 + n;
@@ -41,7 +36,7 @@ func expand(a []Element, i, n int) []Element {
4136
capb = len1
4237
}
4338
// capb >= len1
44-
b := make([]Element, len1, capb);
39+
b := make([]interface{}, len1, capb);
4540
copy(b, a);
4641
a = b;
4742
}
@@ -65,7 +60,7 @@ func (p *Vector) Init(initial_len int) *Vector {
6560
if initial_len > n {
6661
n = initial_len
6762
}
68-
a = make([]Element, n);
63+
a = make([]interface{}, n);
6964
} else {
7065
// nil out entries
7166
for j := len(a) - 1; j >= 0; j-- {
@@ -93,20 +88,20 @@ func (p *Vector) Len() int {
9388

9489

9590
// At returns the i'th element of the vector.
96-
func (p *Vector) At(i int) Element { return p.a[i] }
91+
func (p *Vector) At(i int) interface{} { return p.a[i] }
9792

9893

9994
// Set sets the i'th element of the vector to value x.
100-
func (p *Vector) Set(i int, x Element) { p.a[i] = x }
95+
func (p *Vector) Set(i int, x interface{}) { p.a[i] = x }
10196

10297

10398
// Last returns the element in the vector of highest index.
104-
func (p *Vector) Last() Element { return p.a[len(p.a)-1] }
99+
func (p *Vector) Last() interface{} { return p.a[len(p.a)-1] }
105100

106101

107102
// Data returns all the elements as a slice.
108-
func (p *Vector) Data() []Element {
109-
arr := make([]Element, p.Len());
103+
func (p *Vector) Data() []interface{} {
104+
arr := make([]interface{}, p.Len());
110105
for i, v := range p.a {
111106
arr[i] = v
112107
}
@@ -116,7 +111,7 @@ func (p *Vector) Data() []Element {
116111

117112
// Insert inserts into the vector an element of value x before
118113
// the current element at index i.
119-
func (p *Vector) Insert(i int, x Element) {
114+
func (p *Vector) Insert(i int, x interface{}) {
120115
p.a = expand(p.a, i, 1);
121116
p.a[i] = x;
122117
}
@@ -168,7 +163,7 @@ func (p *Vector) Slice(i, j int) *Vector {
168163

169164
// Do calls function f for each element of the vector, in order.
170165
// The function should not change the indexing of the vector underfoot.
171-
func (p *Vector) Do(f func(elem Element)) {
166+
func (p *Vector) Do(f func(elem interface{})) {
172167
for i := 0; i < len(p.a); i++ {
173168
f(p.a[i]) // not too safe if f changes the Vector
174169
}
@@ -178,11 +173,11 @@ func (p *Vector) Do(f func(elem Element)) {
178173
// Convenience wrappers
179174

180175
// Push appends x to the end of the vector.
181-
func (p *Vector) Push(x Element) { p.Insert(len(p.a), x) }
176+
func (p *Vector) Push(x interface{}) { p.Insert(len(p.a), x) }
182177

183178

184179
// Pop deletes the last element of the vector.
185-
func (p *Vector) Pop() Element {
180+
func (p *Vector) Pop() interface{} {
186181
i := len(p.a) - 1;
187182
x := p.a[i];
188183
p.a[i] = nil; // support GC, nil out entry
@@ -199,7 +194,7 @@ func (p *Vector) AppendVector(x *Vector) { p.InsertVector(len(p.a), x) }
199194

200195
// LessInterface provides partial support of the sort.Interface.
201196
type LessInterface interface {
202-
Less(y Element) bool;
197+
Less(y interface{}) bool;
203198
}
204199

205200

@@ -215,7 +210,7 @@ func (p *Vector) Swap(i, j int) {
215210

216211

217212
// Iterate over all elements; driver for range
218-
func (p *Vector) iterate(c chan<- Element) {
213+
func (p *Vector) iterate(c chan<- interface{}) {
219214
for _, v := range p.a {
220215
c <- v
221216
}
@@ -224,8 +219,8 @@ func (p *Vector) iterate(c chan<- Element) {
224219

225220

226221
// Channel iterator for range.
227-
func (p *Vector) Iter() <-chan Element {
228-
c := make(chan Element);
222+
func (p *Vector) Iter() <-chan interface{} {
223+
c := make(chan interface{});
229224
go p.iterate(c);
230225
return c;
231226
}

src/pkg/container/vector/vector_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func TestDo(t *testing.T) {
219219
a.Set(i, salt*i)
220220
}
221221
count := 0;
222-
a.Do(func(e Element) {
222+
a.Do(func(e interface{}) {
223223
i := e.(int);
224224
if i != count*salt {
225225
t.Error("value at", count, "should be", count*salt, "not", i)

0 commit comments

Comments
 (0)