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
13fbb1d8
Commit
13fbb1d8
authored
May 15, 2009
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
StringVector specialization of Vector
R=gri,rsc DELTA=197 (194 added, 0 deleted, 3 changed) OCL=28900 CL=28911
parent
5e76c032
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
197 additions
and
3 deletions
+197
-3
src/lib/container/vector/Makefile
src/lib/container/vector/Makefile
+3
-2
src/lib/container/vector/intvector.go
src/lib/container/vector/intvector.go
+66
-0
src/lib/container/vector/stringvector.go
src/lib/container/vector/stringvector.go
+118
-0
src/lib/container/vector/vector_test.go
src/lib/container/vector/vector_test.go
+10
-1
No files found.
src/lib/container/vector/Makefile
View file @
13fbb1d8
...
@@ -5,7 +5,7 @@
...
@@ -5,7 +5,7 @@
# DO NOT EDIT. Automatically generated by gobuild.
# DO NOT EDIT. Automatically generated by gobuild.
# gobuild -m >Makefile
# gobuild -m >Makefile
D
=
/container
D
=
/container
/
O_arm
=
5
O_arm
=
5
O_amd64
=
6
O_amd64
=
6
...
@@ -44,6 +44,7 @@ O1=\
...
@@ -44,6 +44,7 @@ O1=\
O2
=
\
O2
=
\
intvector.
$O
\
intvector.
$O
\
stringvector.
$O
\
phases
:
a1 a2
phases
:
a1 a2
...
@@ -54,7 +55,7 @@ a1: $(O1)
...
@@ -54,7 +55,7 @@ a1: $(O1)
rm
-f
$(O1)
rm
-f
$(O1)
a2
:
$(O2)
a2
:
$(O2)
$(AR)
grc _obj
$D
/vector.a intvector.
$O
$(AR)
grc _obj
$D
/vector.a intvector.
$O
stringvector.
$O
rm
-f
$(O2)
rm
-f
$(O2)
...
...
src/lib/container/vector/intvector.go
View file @
13fbb1d8
...
@@ -33,20 +33,86 @@ func (p *IntVector) At(i int) int {
...
@@ -33,20 +33,86 @@ func (p *IntVector) At(i int) int {
}
}
// Set sets the i'th element of the vector to value x.
func
(
p
*
IntVector
)
Set
(
i
int
,
x
int
)
{
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
*
IntVector
)
Last
()
int
{
func
(
p
*
IntVector
)
Last
()
int
{
return
p
.
Vector
.
Last
()
.
(
int
)
return
p
.
Vector
.
Last
()
.
(
int
)
}
}
// Data returns all the elements as a slice.
func
(
p
*
IntVector
)
Data
()
[]
int
{
arr
:=
make
([]
int
,
p
.
Len
());
for
i
,
v
:=
range
p
.
a
{
arr
[
i
]
=
v
.
(
int
)
}
return
arr
}
// Insert inserts into the vector an element of value x before
// the current element at index i.
func
(
p
*
IntVector
)
Insert
(
i
int
,
x
int
)
{
p
.
Vector
.
Insert
(
i
,
x
)
}
// InsertVector inserts into the vector the contents of the Vector
// x such that the 0th element of x appears at index i after insertion.
func
(
p
*
IntVector
)
InsertVector
(
i
int
,
x
*
IntVector
)
{
p
.
Vector
.
InsertVector
(
i
,
&
x
.
Vector
)
}
// Slice returns a new IntVector by slicing the old one to extract slice [i:j].
// The elements are copied. The original vector is unchanged.
func
(
p
*
IntVector
)
Slice
(
i
,
j
int
)
*
IntVector
{
return
&
IntVector
{
*
p
.
Vector
.
Slice
(
i
,
j
)
};
}
// Push appends x to the end of the vector.
func
(
p
*
IntVector
)
Push
(
x
int
)
{
p
.
Vector
.
Push
(
x
)
}
// Pop deletes and returns the last element of the vector.
// Pop deletes and returns the last element of the vector.
func
(
p
*
IntVector
)
Pop
()
int
{
func
(
p
*
IntVector
)
Pop
()
int
{
return
p
.
Vector
.
Pop
()
.
(
int
)
return
p
.
Vector
.
Pop
()
.
(
int
)
}
}
// AppendVector appends the entire IntVector x to the end of this vector.
func
(
p
*
IntVector
)
AppendVector
(
x
*
IntVector
)
{
p
.
Vector
.
InsertVector
(
len
(
p
.
a
),
&
x
.
Vector
);
}
// SortInterface support
// SortInterface support
// Less returns a boolean denoting whether the i'th element is less than the j'th element.
// Less returns a boolean denoting whether the i'th element is less than the j'th element.
func
(
p
*
IntVector
)
Less
(
i
,
j
int
)
bool
{
func
(
p
*
IntVector
)
Less
(
i
,
j
int
)
bool
{
return
p
.
At
(
i
)
<
p
.
At
(
j
)
return
p
.
At
(
i
)
<
p
.
At
(
j
)
}
}
// Iterate over all elements; driver for range
func
(
p
*
IntVector
)
iterate
(
c
chan
int
)
{
for
i
,
v
:=
range
p
.
a
{
c
<-
v
.
(
int
)
}
close
(
c
);
}
// Channel iterator for range.
func
(
p
*
IntVector
)
Iter
()
chan
int
{
c
:=
make
(
chan
int
);
go
p
.
iterate
(
c
);
return
c
;
}
src/lib/container/vector/stringvector.go
0 → 100644
View file @
13fbb1d8
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package
vector
import
"container/vector"
// StringVector is a specialization of Vector that hides the wrapping of Elements around strings.
type
StringVector
struct
{
vector
.
Vector
;
}
// Init initializes a new or resized vector. The initial length may be <= 0 to
// request a default length. If initial_len is shorter than the current
// length of the StringVector, trailing elements of the StringVector will be cleared.
func
(
p
*
StringVector
)
Init
(
len
int
)
*
StringVector
{
p
.
Vector
.
Init
(
len
);
return
p
;
}
// NewStringVector returns an initialized new StringVector with length at least len.
func
NewStringVector
(
len
int
)
*
StringVector
{
return
new
(
StringVector
)
.
Init
(
len
)
}
// At returns the i'th element of the vector.
func
(
p
*
StringVector
)
At
(
i
int
)
string
{
return
p
.
Vector
.
At
(
i
)
.
(
string
)
}
// Set sets the i'th element of the vector to value x.
func
(
p
*
StringVector
)
Set
(
i
int
,
x
string
)
{
p
.
a
[
i
]
=
x
}
// Last returns the element in the vector of highest index.
func
(
p
*
StringVector
)
Last
()
string
{
return
p
.
Vector
.
Last
()
.
(
string
)
}
// Data returns all the elements as a slice.
func
(
p
*
StringVector
)
Data
()
[]
string
{
arr
:=
make
([]
string
,
p
.
Len
());
for
i
,
v
:=
range
p
.
a
{
arr
[
i
]
=
v
.
(
string
)
}
return
arr
}
// Insert inserts into the vector an element of value x before
// the current element at index i.
func
(
p
*
StringVector
)
Insert
(
i
int
,
x
string
)
{
p
.
Vector
.
Insert
(
i
,
x
)
}
// InsertVector inserts into the vector the contents of the Vector
// x such that the 0th element of x appears at index i after insertion.
func
(
p
*
StringVector
)
InsertVector
(
i
int
,
x
*
StringVector
)
{
p
.
Vector
.
InsertVector
(
i
,
&
x
.
Vector
)
}
// Slice returns a new StringVector by slicing the old one to extract slice [i:j].
// The elements are copied. The original vector is unchanged.
func
(
p
*
StringVector
)
Slice
(
i
,
j
int
)
*
StringVector
{
return
&
StringVector
{
*
p
.
Vector
.
Slice
(
i
,
j
)
};
}
// Push appends x to the end of the vector.
func
(
p
*
StringVector
)
Push
(
x
string
)
{
p
.
Vector
.
Push
(
x
)
}
// Pop deletes and returns the last element of the vector.
func
(
p
*
StringVector
)
Pop
()
string
{
return
p
.
Vector
.
Pop
()
.
(
string
)
}
// AppendVector appends the entire StringVector x to the end of this vector.
func
(
p
*
StringVector
)
AppendVector
(
x
*
StringVector
)
{
p
.
Vector
.
InsertVector
(
len
(
p
.
a
),
&
x
.
Vector
);
}
// SortInterface support
// Less returns a boolean denoting whether the i'th element is less than the j'th element.
func
(
p
*
StringVector
)
Less
(
i
,
j
int
)
bool
{
return
p
.
At
(
i
)
<
p
.
At
(
j
)
}
// Iterate over all elements; driver for range
func
(
p
*
StringVector
)
iterate
(
c
chan
string
)
{
for
i
,
v
:=
range
p
.
a
{
c
<-
v
.
(
string
)
}
close
(
c
);
}
// Channel iterator for range.
func
(
p
*
StringVector
)
Iter
()
chan
string
{
c
:=
make
(
chan
string
);
go
p
.
iterate
(
c
);
return
c
;
}
src/lib/container/vector/vector_test.go
View file @
13fbb1d8
...
@@ -7,6 +7,7 @@ package vector
...
@@ -7,6 +7,7 @@ package vector
import
"container/vector"
import
"container/vector"
import
"testing"
import
"testing"
import
"sort"
import
"sort"
import
"fmt"
func
TestInit
(
t
*
testing
.
T
)
{
func
TestInit
(
t
*
testing
.
T
)
{
...
@@ -141,13 +142,21 @@ func TestInsertVector(t *testing.T) {
...
@@ -141,13 +142,21 @@ func TestInsertVector(t *testing.T) {
}
}
// This also tests IntVector and StringVector
func
TestSorting
(
t
*
testing
.
T
)
{
func
TestSorting
(
t
*
testing
.
T
)
{
const
n
=
100
;
const
n
=
100
;
a
:=
vector
.
NewIntVector
(
n
);
a
:=
vector
.
NewIntVector
(
n
);
for
i
:=
n
-
1
;
i
>=
0
;
i
--
{
for
i
:=
n
-
1
;
i
>=
0
;
i
--
{
a
.
Set
(
i
,
n
-
1
-
i
);
a
.
Set
(
i
,
n
-
1
-
i
);
}
}
if
sort
.
IsSorted
(
a
)
{
t
.
Error
(
"not sorted"
)
}
if
sort
.
IsSorted
(
a
)
{
t
.
Error
(
"int vector not sorted"
)
}
b
:=
vector
.
NewStringVector
(
n
);
for
i
:=
n
-
1
;
i
>=
0
;
i
--
{
b
.
Set
(
i
,
fmt
.
Sprint
(
n
-
1
-
i
));
}
if
sort
.
IsSorted
(
b
)
{
t
.
Error
(
"string vector not sorted"
)
}
}
}
...
...
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