1
0
mirror of https://github.com/golang/go synced 2024-11-26 10:58:16 -07:00

- array lib (essentially vector, more complete)

- TODO replace vector

R=r
DELTA=314  (313 added, 0 deleted, 1 changed)
OCL=19592
CL=19609
This commit is contained in:
Robert Griesemer 2008-11-19 14:05:21 -08:00
parent aa1ede15cc
commit b548e73460
5 changed files with 320 additions and 1 deletions

View File

@ -0,0 +1,56 @@
# 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.
# DO NOT EDIT. Automatically generated by gobuild.
# gobuild -m >Makefile
O=6
GC=$(O)g
CC=$(O)c -w
AS=$(O)a
AR=$(O)ar
default: packages
clean:
rm -f *.$O *.a $O.out
test: packages
gotest
coverage: packages
gotest
6cov -g `pwd` | grep -v '^test.*\.go:'
%.$O: %.go
$(GC) $*.go
%.$O: %.c
$(CC) $*.c
%.$O: %.s
$(AS) $*.s
O1=\
array.$O\
intarray.$O\
array.a: a1
a1: $(O1)
$(AR) grc array.a array.$O intarray.$O
rm -f $(O1)
newpkg: clean
$(AR) grc array.a
$(O1): newpkg
nuke: clean
rm -f $(GOROOT)/pkg/array.a
packages: array.a
install: packages
cp array.a $(GOROOT)/pkg/array.a

View File

@ -0,0 +1,117 @@
// 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 array
export type Element interface {
}
export type Array struct {
// TODO do not export field
a *[]Element
}
func (p *Array) Init(initial_len int) *Array {
a := p.a;
if a == nil || cap(a) < initial_len {
n := 8; // initial capacity
if initial_len > n {
n = initial_len
}
a = new([]Element, n);
} else {
// nil out entries
for j := len(a) - 1; j >= 0; j-- {
a[j] = nil
}
}
p.a = a[0 : initial_len];
return p
}
export func New(len int) *Array {
return new(Array).Init(len)
}
func (p *Array) Len() int {
return len(p.a)
}
func (p *Array) At(i int) Element {
return p.a[i]
}
func (p *Array) Set(i int, x Element) {
p.a[i] = x
}
func (p *Array) Last() Element {
return p.a[len(p.a) - 1]
}
func (p *Array) Insert(i int, x Element) {
a := p.a;
n := len(a);
// grow array by doubling its capacity
if n == cap(a) {
b := new([]Element, 2*n);
for j := n-1; j >= 0; j-- {
b[j] = a[j];
}
a = b
}
// make a hole
a = a[0 : n+1];
for j := n; j > i; j-- {
a[j] = a[j-1]
}
a[i] = x;
p.a = a
}
func (p *Array) Remove(i int) Element {
a := p.a;
n := len(a);
x := a[i];
for j := i+1; j < n; j++ {
a[j-1] = a[j]
}
a[n-1] = nil; // support GC, nil out entry
p.a = a[0 : n-1];
return x
}
func (p *Array) Push(x Element) {
p.Insert(len(p.a), x)
}
func (p *Array) Pop() Element {
return p.Remove(len(p.a) - 1)
}
// Partial SortInterface support
func (p *Array) Swap(i, j int) {
a := p.a;
a[i], a[j] = a[j], a[i]
}

View File

@ -0,0 +1,64 @@
// 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 array
import "array"
export type IntArray struct {
// TODO do not export field
array.Array;
}
func (p *IntArray) Init(len int) *IntArray {
p.Array.Init(len);
return p;
}
export func NewIntArray(len int) *IntArray {
return new(IntArray).Init(len)
}
func (p *IntArray) At(i int) int {
return p.Array.At(i).(int)
}
func (p *IntArray) Set(i int, x int) {
p.Array.Set(i, x)
}
func (p *IntArray) Last() int {
return p.Array.Last().(int)
}
func (p *IntArray) Insert(i int, x int) {
p.Array.Insert(i, x)
}
func (p *IntArray) Remove(i int) int {
return p.Array.Remove(i).(int)
}
func (p *IntArray) Push(x int) {
p.Array.Push(x)
}
func (p *IntArray) Pop() int {
return p.Array.Pop().(int)
}
// SortInterface support
func (p *IntArray) Less(i, j int) bool {
return p.At(i) < p.At(j)
}

View File

@ -0,0 +1,82 @@
// 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 array
import "array"
export func TestInit() bool {
var a array.Array;
if a.Init(0).Len() != 0 { return false }
if a.Init(1).Len() != 1 { return false }
if a.Init(10).Len() != 10 { return false }
return true;
}
export func TestNew() bool {
if array.New(0).Len() != 0 { return false }
if array.New(1).Len() != 1 { return false }
if array.New(10).Len() != 10 { return false }
return true;
}
export func Val(i int) int {
return i*991 - 1234
}
export func TestAccess() bool {
const n = 100;
var a array.Array;
a.Init(n);
for i := 0; i < n; i++ {
a.Set(i, Val(i));
}
for i := 0; i < n; i++ {
if a.At(i).(int) != Val(i) { return false }
}
return true;
}
export func TestInsertRemoveClear() bool {
const n = 100;
a := array.New(0);
for i := 0; i < n; i++ {
if a.Len() != i { return false }
a.Insert(0, Val(i));
if a.Last().(int) != Val(0) { return false }
}
for i := n-1; i >= 0; i-- {
if a.Last().(int) != Val(0) { return false }
if a.Remove(0).(int) != Val(i) { return false }
if a.Len() != i { return false }
}
if a.Len() != 0 { return false }
for i := 0; i < n; i++ {
a.Push(Val(i));
if a.Len() != i+1 { return false }
if a.Last().(int) != Val(i) { return false }
}
a.Init(0);
if a.Len() != 0 { return false }
const m = 5;
for j := 0; j < m; j++ {
a.Push(j);
for i := 0; i < n; i++ {
x := Val(i);
a.Push(x);
if a.Pop().(int) != x { return false }
if a.Len() != j+1 { return false }
}
}
if a.Len() != m { return false }
return true;
}

View File

@ -54,4 +54,4 @@ builddirs net\
time\
http\
regexp\
container/array\