6
6
// linear arrays of elements. Unlike arrays, vectors can change size dynamically.
7
7
package vector
8
8
9
- // Element is an empty-interface object representing the contents of
10
- // a cell in the vector.
11
- type Element interface {}
12
-
13
-
14
9
// Vector is the container itself.
15
10
// The zero value for Vector is an empty vector ready to use.
16
11
type Vector struct {
17
- a []Element ;
12
+ a []interface {} ;
18
13
}
19
14
20
15
21
- func copy (dst , src []Element ) {
16
+ func copy (dst , src []interface {} ) {
22
17
for i , x := range src {
23
18
dst [i ] = x
24
19
}
25
20
}
26
21
27
22
28
23
// Insert n elements at position i.
29
- func expand (a []Element , i , n int ) []Element {
24
+ func expand (a []interface {} , i , n int ) []interface {} {
30
25
// make sure we have enough space
31
26
len0 := len (a );
32
27
len1 := len0 + n ;
@@ -41,7 +36,7 @@ func expand(a []Element, i, n int) []Element {
41
36
capb = len1
42
37
}
43
38
// capb >= len1
44
- b := make ([]Element , len1 , capb );
39
+ b := make ([]interface {} , len1 , capb );
45
40
copy (b , a );
46
41
a = b ;
47
42
}
@@ -65,7 +60,7 @@ func (p *Vector) Init(initial_len int) *Vector {
65
60
if initial_len > n {
66
61
n = initial_len
67
62
}
68
- a = make ([]Element , n );
63
+ a = make ([]interface {} , n );
69
64
} else {
70
65
// nil out entries
71
66
for j := len (a ) - 1 ; j >= 0 ; j -- {
@@ -93,20 +88,20 @@ func (p *Vector) Len() int {
93
88
94
89
95
90
// 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 ] }
97
92
98
93
99
94
// 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 }
101
96
102
97
103
98
// 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 ] }
105
100
106
101
107
102
// 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 ());
110
105
for i , v := range p .a {
111
106
arr [i ] = v
112
107
}
@@ -116,7 +111,7 @@ func (p *Vector) Data() []Element {
116
111
117
112
// Insert inserts into the vector an element of value x before
118
113
// the current element at index i.
119
- func (p * Vector ) Insert (i int , x Element ) {
114
+ func (p * Vector ) Insert (i int , x interface {} ) {
120
115
p .a = expand (p .a , i , 1 );
121
116
p .a [i ] = x ;
122
117
}
@@ -168,7 +163,7 @@ func (p *Vector) Slice(i, j int) *Vector {
168
163
169
164
// Do calls function f for each element of the vector, in order.
170
165
// 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 {} )) {
172
167
for i := 0 ; i < len (p .a ); i ++ {
173
168
f (p .a [i ]) // not too safe if f changes the Vector
174
169
}
@@ -178,11 +173,11 @@ func (p *Vector) Do(f func(elem Element)) {
178
173
// Convenience wrappers
179
174
180
175
// 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 ) }
182
177
183
178
184
179
// Pop deletes the last element of the vector.
185
- func (p * Vector ) Pop () Element {
180
+ func (p * Vector ) Pop () interface {} {
186
181
i := len (p .a ) - 1 ;
187
182
x := p .a [i ];
188
183
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) }
199
194
200
195
// LessInterface provides partial support of the sort.Interface.
201
196
type LessInterface interface {
202
- Less (y Element ) bool ;
197
+ Less (y interface {} ) bool ;
203
198
}
204
199
205
200
@@ -215,7 +210,7 @@ func (p *Vector) Swap(i, j int) {
215
210
216
211
217
212
// Iterate over all elements; driver for range
218
- func (p * Vector ) iterate (c chan <- Element ) {
213
+ func (p * Vector ) iterate (c chan <- interface {} ) {
219
214
for _ , v := range p .a {
220
215
c <- v
221
216
}
@@ -224,8 +219,8 @@ func (p *Vector) iterate(c chan<- Element) {
224
219
225
220
226
221
// 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 {} );
229
224
go p .iterate (c );
230
225
return c ;
231
226
}
0 commit comments