Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
b7490382
Commit
b7490382
authored
Apr 03, 2008
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a rudimentary vector container to ameliorate the inability to allocate dynamic arrays.
SVN=114931
parent
01764a30
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
132 additions
and
0 deletions
+132
-0
vector.go
src/lib/container/vector.go
+132
-0
No files found.
src/lib/container/vector.go
0 → 100644
View file @
b7490382
// 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
export
Vector
,
New
;
/*
import vector "vector"
v := vector.New();
v.Insert(0, new(Foo));
v.Append(new(Foo));
v.Delete(0);
for i := 0; i < v.Len(); i++ { f(v.At(i)); }
*/
type
Element
interface
{
}
type
Vector
struct
{
nalloc
int
;
nelem
int
;
elem
*
[]
Element
;
}
// BUG: workaround for non-constant allocation.
// i must be a power of 10.
func
Alloc
(
i
int
)
*
[]
Element
{
print
"Alloc "
,
i
,
"
\n
"
;
switch
i
{
case
1
:
return
new
([
1
]
Element
);
case
10
:
return
new
([
10
]
Element
);
case
100
:
return
new
([
100
]
Element
);
case
1000
:
return
new
([
1000
]
Element
);
}
print
"bad size "
,
i
,
"
\n
"
;
panic
"not known size
\n
"
;
}
func
is_pow10
(
i
int
)
bool
{
switch
i
{
case
1
,
10
,
100
,
1000
:
return
true
;
}
return
false
;
}
func
New
()
*
Vector
{
v
:=
new
(
Vector
);
v
.
nelem
=
0
;
v
.
nalloc
=
1
;
v
.
elem
=
Alloc
(
v
.
nalloc
);
return
v
;
}
func
(
v
*
Vector
)
Len
()
int
{
return
v
.
nelem
;
}
func
(
v
*
Vector
)
At
(
i
int
)
Element
{
if
i
<
0
||
i
>=
v
.
nelem
{
//return nil; // BUG
panic
"At out of range
\n
"
;
}
return
v
.
elem
[
i
];
}
func
(
v
*
Vector
)
Delete
(
i
int
)
{
if
i
<
0
||
i
>=
v
.
nelem
{
panic
"Delete out of range
\n
"
;
}
for
j
:=
i
+
1
;
j
<
v
.
nelem
;
j
++
{
v
.
elem
[
j
-
1
]
=
v
.
elem
[
j
];
}
v
.
nelem
--
;
v
.
elem
[
v
.
nelem
]
=
nil
;
}
func
(
v
*
Vector
)
Insert
(
i
int
,
e
Element
)
{
if
i
>
v
.
nelem
{
panic
"Del too large
\n
"
;
}
if
v
.
nelem
==
v
.
nalloc
&&
is_pow10
(
v
.
nalloc
)
{
n
:=
Alloc
(
v
.
nalloc
*
10
);
for
j
:=
0
;
j
<
v
.
nalloc
;
j
++
{
n
[
j
]
=
v
.
elem
[
j
];
}
v
.
elem
=
n
;
v
.
nalloc
*=
10
;
}
// make a hole
for
j
:=
v
.
nelem
;
j
>
i
;
j
--
{
v
.
elem
[
j
]
=
v
.
elem
[
j
-
1
];
}
v
.
elem
[
i
]
=
e
;
v
.
nelem
++
;
}
func
(
v
*
Vector
)
Append
(
e
Element
)
{
v
.
Insert
(
v
.
nelem
,
e
);
}
/*
type I struct { val int; }; // BUG: can't be local;
func Test() {
i0 := new(I); i0.val = 0;
i1 := new(I); i1.val = 11;
i2 := new(I); i2.val = 222;
i3 := new(I); i3.val = 3333;
i4 := new(I); i4.val = 44444;
v := New();
print "hi\n";
v.Insert(0, i4);
v.Insert(0, i3);
v.Insert(0, i2);
v.Insert(0, i1);
v.Insert(0, i0);
for i := 0; i < v.Len(); i++ {
var x *I;
x = v.At(i);
print i, " ", x.val, "\n"; // BUG: can't use I(v.At(i))
}
}
export Test;
*/
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