1
0
mirror of https://github.com/golang/go synced 2024-11-12 09:20:22 -07:00

- added Init() function so that vectors can be used w/o New():

var v Vector.Vector;
  	v.Init();
	...
- clear vector elements in Reset
- removed some comments that seem redundant

R=r
DELTA=16  (9 added, 5 deleted, 2 changed)
OCL=19247
CL=19247
This commit is contained in:
Robert Griesemer 2008-11-14 11:22:39 -08:00
parent f8b20e4083
commit 0c4c842eb0

View File

@ -24,9 +24,14 @@ export type Vector struct {
}
func (v *Vector) Init() {
v.elem = new([]Element, 8) [0 : 0]; // capacity must be > 0!
}
export func New() *Vector {
v := new(Vector);
v.elem = new([]Element, 8) [0 : 0]; // capacity must be > 0!
v.Init();
return v;
}
@ -37,13 +42,11 @@ func (v *Vector) Len() int {
func (v *Vector) At(i int) Element {
// range check unnecessary - done by runtime
return v.elem[i];
}
func (v *Vector) Set(i int, e Element) {
// range check unnecessary - done by runtime
v.elem[i] = e;
}
@ -51,24 +54,25 @@ func (v *Vector) Set(i int, e Element) {
func (v *Vector) Remove(i int) Element {
ret := v.elem[i];
n := v.Len();
// range check unnecessary - done by runtime
for j := i + 1; j < n; j++ {
v.elem[j - 1] = v.elem[j];
}
var e Element;
v.elem[n - 1] = e; // don't set to nil - may not be legal in the future
v.elem[n - 1] = nil; // support GC, nil out entry
v.elem = v.elem[0 : n - 1];
return ret;
}
func (v *Vector) Reset() {
// support GC, nil out entries
for j := len(v.elem) - 1; j >= 0; j-- {
v.elem[j] = nil;
}
v.elem = v.elem[0:0];
}
func (v *Vector) Insert(i int, e Element) {
n := v.Len();
// range check unnecessary - done by runtime
// grow array by doubling its capacity
if n == cap(v.elem) {