Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
23843fa4
Commit
23843fa4
authored
Nov 11, 2009
by
Robert Griesemer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
vector: s/Element/interface{}/
Fixes #74. R=rsc
https://golang.org/cl/154073
parent
3b092fec
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
24 deletions
+19
-24
src/pkg/container/vector/vector.go
src/pkg/container/vector/vector.go
+18
-23
src/pkg/container/vector/vector_test.go
src/pkg/container/vector/vector_test.go
+1
-1
No files found.
src/pkg/container/vector/vector.go
View file @
23843fa4
...
@@ -6,19 +6,14 @@
...
@@ -6,19 +6,14 @@
// linear arrays of elements. Unlike arrays, vectors can change size dynamically.
// linear arrays of elements. Unlike arrays, vectors can change size dynamically.
package
vector
package
vector
// Element is an empty-interface object representing the contents of
// a cell in the vector.
type
Element
interface
{}
// Vector is the container itself.
// Vector is the container itself.
// The zero value for Vector is an empty vector ready to use.
// The zero value for Vector is an empty vector ready to use.
type
Vector
struct
{
type
Vector
struct
{
a
[]
Element
;
a
[]
interface
{}
;
}
}
func
copy
(
dst
,
src
[]
Element
)
{
func
copy
(
dst
,
src
[]
interface
{}
)
{
for
i
,
x
:=
range
src
{
for
i
,
x
:=
range
src
{
dst
[
i
]
=
x
dst
[
i
]
=
x
}
}
...
@@ -26,7 +21,7 @@ func copy(dst, src []Element) {
...
@@ -26,7 +21,7 @@ func copy(dst, src []Element) {
// Insert n elements at position i.
// Insert n elements at position i.
func
expand
(
a
[]
Element
,
i
,
n
int
)
[]
Element
{
func
expand
(
a
[]
interface
{},
i
,
n
int
)
[]
interface
{}
{
// make sure we have enough space
// make sure we have enough space
len0
:=
len
(
a
);
len0
:=
len
(
a
);
len1
:=
len0
+
n
;
len1
:=
len0
+
n
;
...
@@ -41,7 +36,7 @@ func expand(a []Element, i, n int) []Element {
...
@@ -41,7 +36,7 @@ func expand(a []Element, i, n int) []Element {
capb
=
len1
capb
=
len1
}
}
// capb >= len1
// capb >= len1
b
:=
make
([]
Element
,
len1
,
capb
);
b
:=
make
([]
interface
{}
,
len1
,
capb
);
copy
(
b
,
a
);
copy
(
b
,
a
);
a
=
b
;
a
=
b
;
}
}
...
@@ -65,7 +60,7 @@ func (p *Vector) Init(initial_len int) *Vector {
...
@@ -65,7 +60,7 @@ func (p *Vector) Init(initial_len int) *Vector {
if
initial_len
>
n
{
if
initial_len
>
n
{
n
=
initial_len
n
=
initial_len
}
}
a
=
make
([]
Element
,
n
);
a
=
make
([]
interface
{}
,
n
);
}
else
{
}
else
{
// nil out entries
// nil out entries
for
j
:=
len
(
a
)
-
1
;
j
>=
0
;
j
--
{
for
j
:=
len
(
a
)
-
1
;
j
>=
0
;
j
--
{
...
@@ -93,20 +88,20 @@ func (p *Vector) Len() int {
...
@@ -93,20 +88,20 @@ func (p *Vector) Len() int {
// At returns the i'th element of the vector.
// At returns the i'th element of the vector.
func
(
p
*
Vector
)
At
(
i
int
)
Element
{
return
p
.
a
[
i
]
}
func
(
p
*
Vector
)
At
(
i
int
)
interface
{}
{
return
p
.
a
[
i
]
}
// Set sets the i'th element of the vector to value x.
// Set sets the i'th element of the vector to value x.
func
(
p
*
Vector
)
Set
(
i
int
,
x
Element
)
{
p
.
a
[
i
]
=
x
}
func
(
p
*
Vector
)
Set
(
i
int
,
x
interface
{}
)
{
p
.
a
[
i
]
=
x
}
// Last returns the element in the vector of highest index.
// Last returns the element in the vector of highest index.
func
(
p
*
Vector
)
Last
()
Element
{
return
p
.
a
[
len
(
p
.
a
)
-
1
]
}
func
(
p
*
Vector
)
Last
()
interface
{}
{
return
p
.
a
[
len
(
p
.
a
)
-
1
]
}
// Data returns all the elements as a slice.
// Data returns all the elements as a slice.
func
(
p
*
Vector
)
Data
()
[]
Element
{
func
(
p
*
Vector
)
Data
()
[]
interface
{}
{
arr
:=
make
([]
Element
,
p
.
Len
());
arr
:=
make
([]
interface
{}
,
p
.
Len
());
for
i
,
v
:=
range
p
.
a
{
for
i
,
v
:=
range
p
.
a
{
arr
[
i
]
=
v
arr
[
i
]
=
v
}
}
...
@@ -116,7 +111,7 @@ func (p *Vector) Data() []Element {
...
@@ -116,7 +111,7 @@ func (p *Vector) Data() []Element {
// Insert inserts into the vector an element of value x before
// Insert inserts into the vector an element of value x before
// the current element at index i.
// the current element at index i.
func
(
p
*
Vector
)
Insert
(
i
int
,
x
Element
)
{
func
(
p
*
Vector
)
Insert
(
i
int
,
x
interface
{}
)
{
p
.
a
=
expand
(
p
.
a
,
i
,
1
);
p
.
a
=
expand
(
p
.
a
,
i
,
1
);
p
.
a
[
i
]
=
x
;
p
.
a
[
i
]
=
x
;
}
}
...
@@ -168,7 +163,7 @@ func (p *Vector) Slice(i, j int) *Vector {
...
@@ -168,7 +163,7 @@ func (p *Vector) Slice(i, j int) *Vector {
// Do calls function f for each element of the vector, in order.
// Do calls function f for each element of the vector, in order.
// The function should not change the indexing of the vector underfoot.
// The function should not change the indexing of the vector underfoot.
func
(
p
*
Vector
)
Do
(
f
func
(
elem
Element
))
{
func
(
p
*
Vector
)
Do
(
f
func
(
elem
interface
{}
))
{
for
i
:=
0
;
i
<
len
(
p
.
a
);
i
++
{
for
i
:=
0
;
i
<
len
(
p
.
a
);
i
++
{
f
(
p
.
a
[
i
])
// not too safe if f changes the Vector
f
(
p
.
a
[
i
])
// not too safe if f changes the Vector
}
}
...
@@ -178,11 +173,11 @@ func (p *Vector) Do(f func(elem Element)) {
...
@@ -178,11 +173,11 @@ func (p *Vector) Do(f func(elem Element)) {
// Convenience wrappers
// Convenience wrappers
// Push appends x to the end of the vector.
// Push appends x to the end of the vector.
func
(
p
*
Vector
)
Push
(
x
Element
)
{
p
.
Insert
(
len
(
p
.
a
),
x
)
}
func
(
p
*
Vector
)
Push
(
x
interface
{}
)
{
p
.
Insert
(
len
(
p
.
a
),
x
)
}
// Pop deletes the last element of the vector.
// Pop deletes the last element of the vector.
func
(
p
*
Vector
)
Pop
()
Element
{
func
(
p
*
Vector
)
Pop
()
interface
{}
{
i
:=
len
(
p
.
a
)
-
1
;
i
:=
len
(
p
.
a
)
-
1
;
x
:=
p
.
a
[
i
];
x
:=
p
.
a
[
i
];
p
.
a
[
i
]
=
nil
;
// support GC, nil out entry
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,7 +194,7 @@ func (p *Vector) AppendVector(x *Vector) { p.InsertVector(len(p.a), x) }
// LessInterface provides partial support of the sort.Interface.
// LessInterface provides partial support of the sort.Interface.
type
LessInterface
interface
{
type
LessInterface
interface
{
Less
(
y
Element
)
bool
;
Less
(
y
interface
{}
)
bool
;
}
}
...
@@ -215,7 +210,7 @@ func (p *Vector) Swap(i, j int) {
...
@@ -215,7 +210,7 @@ func (p *Vector) Swap(i, j int) {
// Iterate over all elements; driver for range
// Iterate over all elements; driver for range
func
(
p
*
Vector
)
iterate
(
c
chan
<-
Element
)
{
func
(
p
*
Vector
)
iterate
(
c
chan
<-
interface
{}
)
{
for
_
,
v
:=
range
p
.
a
{
for
_
,
v
:=
range
p
.
a
{
c
<-
v
c
<-
v
}
}
...
@@ -224,8 +219,8 @@ func (p *Vector) iterate(c chan<- Element) {
...
@@ -224,8 +219,8 @@ func (p *Vector) iterate(c chan<- Element) {
// Channel iterator for range.
// Channel iterator for range.
func
(
p
*
Vector
)
Iter
()
<-
chan
Element
{
func
(
p
*
Vector
)
Iter
()
<-
chan
interface
{}
{
c
:=
make
(
chan
Element
);
c
:=
make
(
chan
interface
{}
);
go
p
.
iterate
(
c
);
go
p
.
iterate
(
c
);
return
c
;
return
c
;
}
}
src/pkg/container/vector/vector_test.go
View file @
23843fa4
...
@@ -219,7 +219,7 @@ func TestDo(t *testing.T) {
...
@@ -219,7 +219,7 @@ func TestDo(t *testing.T) {
a
.
Set
(
i
,
salt
*
i
)
a
.
Set
(
i
,
salt
*
i
)
}
}
count
:=
0
;
count
:=
0
;
a
.
Do
(
func
(
e
Element
)
{
a
.
Do
(
func
(
e
interface
{}
)
{
i
:=
e
.
(
int
);
i
:=
e
.
(
int
);
if
i
!=
count
*
salt
{
if
i
!=
count
*
salt
{
t
.
Error
(
"value at"
,
count
,
"should be"
,
count
*
salt
,
"not"
,
i
)
t
.
Error
(
"value at"
,
count
,
"should be"
,
count
*
salt
,
"not"
,
i
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment