mirror of https://github.com/golang/go.git
125 lines
2.0 KiB
Go
125 lines
2.0 KiB
Go
// 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.Remove(0);
|
|
for i := 0; i < v.Len(); i++ { f(v.At(i)); }
|
|
*/
|
|
|
|
type Element interface {
|
|
}
|
|
|
|
|
|
export type Vector struct {
|
|
elem *[]Element;
|
|
}
|
|
|
|
|
|
func (v *Vector) Init() {
|
|
v.elem = new([]Element, 8) [0 : 0]; // capacity must be > 0!
|
|
}
|
|
|
|
|
|
export func New() *Vector {
|
|
v := new(Vector);
|
|
v.Init();
|
|
return v;
|
|
}
|
|
|
|
|
|
func (v *Vector) Len() int {
|
|
return len(v.elem);
|
|
}
|
|
|
|
|
|
func (v *Vector) At(i int) Element {
|
|
return v.elem[i];
|
|
}
|
|
|
|
|
|
func (v *Vector) Set(i int, e Element) {
|
|
v.elem[i] = e;
|
|
}
|
|
|
|
|
|
func (v *Vector) Remove(i int) Element {
|
|
ret := v.elem[i];
|
|
n := v.Len();
|
|
for j := i + 1; j < n; j++ {
|
|
v.elem[j - 1] = v.elem[j];
|
|
}
|
|
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();
|
|
|
|
// grow array by doubling its capacity
|
|
if n == cap(v.elem) {
|
|
a := new([]Element, n*2);
|
|
for j := 0; j < n; j++ {
|
|
a[j] = v.elem[j];
|
|
}
|
|
v.elem = a;
|
|
}
|
|
|
|
// make a hole
|
|
v.elem = v.elem[0 : n + 1];
|
|
for j := n; j > i; j-- {
|
|
v.elem[j] = v.elem[j-1];
|
|
}
|
|
|
|
v.elem[i] = e;
|
|
}
|
|
|
|
|
|
func (v *Vector) Append(e Element) {
|
|
v.Insert(len(v.elem), 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++ {
|
|
x := convert(*I, v.At(i));
|
|
print(i, " ", v.At(i).(*I).val, "\n");
|
|
}
|
|
}
|
|
|
|
export Test;
|
|
*/
|