aboutsummaryrefslogtreecommitdiff
path: root/src/pkg/container
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2009-12-15 15:33:31 -0800
committerRobert Griesemer <gri@golang.org>2009-12-15 15:33:31 -0800
commit5a1d3323fe2032beb0b429f44b9356e8ca43fdf7 (patch)
tree940cb602408621a360c3b28d966a5345b6a1aedf /src/pkg/container
parent34356e9a6aec0da6cb5115cc6919445f09206af4 (diff)
downloadgo-5a1d3323fe2032beb0b429f44b9356e8ca43fdf7.tar.xz
1) Change default gofmt default settings for
parsing and printing to new syntax. Use -oldparser to parse the old syntax, use -oldprinter to print the old syntax. 2) Change default gofmt formatting settings to use tabs for indentation only and to use spaces for alignment. This will make the code alignment insensitive to an editor's tabwidth. Use -spaces=false to use tabs for alignment. 3) Manually changed src/exp/parser/parser_test.go so that it doesn't try to parse the parser's source files using the old syntax (they have new syntax now). 4) gofmt -w src misc test/bench 1st set of files. R=rsc CC=agl, golang-dev, iant, ken2, r https://golang.org/cl/180047
Diffstat (limited to 'src/pkg/container')
-rw-r--r--src/pkg/container/heap/heap.go46
-rw-r--r--src/pkg/container/heap/heap_test.go96
-rw-r--r--src/pkg/container/list/list.go116
-rw-r--r--src/pkg/container/list/list_test.go128
-rw-r--r--src/pkg/container/ring/ring.go62
-rw-r--r--src/pkg/container/ring/ring_test.go166
-rw-r--r--src/pkg/container/vector/intvector.go34
-rw-r--r--src/pkg/container/vector/stringvector.go32
-rw-r--r--src/pkg/container/vector/vector.go110
-rw-r--r--src/pkg/container/vector/vector_test.go170
10 files changed, 480 insertions, 480 deletions
diff --git a/src/pkg/container/heap/heap.go b/src/pkg/container/heap/heap.go
index 7a7cb9b803..4435a57c4e 100644
--- a/src/pkg/container/heap/heap.go
+++ b/src/pkg/container/heap/heap.go
@@ -16,9 +16,9 @@ import "sort"
// !h.Less(j, i) for 0 <= i < h.Len() and j = 2*i+1 or 2*i+2 and j < h.Len()
//
type Interface interface {
- sort.Interface;
- Push(x interface{});
- Pop() interface{};
+ sort.Interface
+ Push(x interface{})
+ Pop() interface{}
}
@@ -29,7 +29,7 @@ type Interface interface {
//
func Init(h Interface) {
// heapify
- n := h.Len();
+ n := h.Len()
for i := n/2 - 1; i >= 0; i-- {
down(h, i, n)
}
@@ -40,8 +40,8 @@ func Init(h Interface) {
// O(log(n)) where n = h.Len().
//
func Push(h Interface, x interface{}) {
- h.Push(x);
- up(h, h.Len()-1);
+ h.Push(x)
+ up(h, h.Len()-1)
}
@@ -50,10 +50,10 @@ func Push(h Interface, x interface{}) {
// Same as Remove(h, 0).
//
func Pop(h Interface) interface{} {
- n := h.Len() - 1;
- h.Swap(0, n);
- down(h, 0, n);
- return h.Pop();
+ n := h.Len() - 1
+ h.Swap(0, n)
+ down(h, 0, n)
+ return h.Pop()
}
@@ -61,42 +61,42 @@ func Pop(h Interface) interface{} {
// The complexity is O(log(n)) where n = h.Len().
//
func Remove(h Interface, i int) interface{} {
- n := h.Len() - 1;
+ n := h.Len() - 1
if n != i {
- h.Swap(i, n);
- down(h, i, n);
- up(h, i);
+ h.Swap(i, n)
+ down(h, i, n)
+ up(h, i)
}
- return h.Pop();
+ return h.Pop()
}
func up(h Interface, j int) {
for {
- i := (j - 1) / 2; // parent
+ i := (j - 1) / 2 // parent
if i == j || h.Less(i, j) {
break
}
- h.Swap(i, j);
- j = i;
+ h.Swap(i, j)
+ j = i
}
}
func down(h Interface, i, n int) {
for {
- j1 := 2*i + 1;
+ j1 := 2*i + 1
if j1 >= n {
break
}
- j := j1; // left child
+ j := j1 // left child
if j2 := j1 + 1; j2 < n && !h.Less(j1, j2) {
- j = j2 // = 2*i + 2 // right child
+ j = j2 // = 2*i + 2 // right child
}
if h.Less(i, j) {
break
}
- h.Swap(i, j);
- i = j;
+ h.Swap(i, j)
+ i = j
}
}
diff --git a/src/pkg/container/heap/heap_test.go b/src/pkg/container/heap/heap_test.go
index dc13201cd3..8130555f38 100644
--- a/src/pkg/container/heap/heap_test.go
+++ b/src/pkg/container/heap/heap_test.go
@@ -5,53 +5,53 @@
package heap
import (
- "testing";
- "container/vector";
+ "testing"
+ "container/vector"
)
type myHeap struct {
// A vector.Vector implements sort.Interface except for Less,
// and it implements Push and Pop as required for heap.Interface.
- vector.Vector;
+ vector.Vector
}
-func (h *myHeap) Less(i, j int) bool { return h.At(i).(int) < h.At(j).(int) }
+func (h *myHeap) Less(i, j int) bool { return h.At(i).(int) < h.At(j).(int) }
func (h *myHeap) verify(t *testing.T, i int) {
- n := h.Len();
- j1 := 2*i + 1;
- j2 := 2*i + 2;
+ n := h.Len()
+ j1 := 2*i + 1
+ j2 := 2*i + 2
if j1 < n {
if h.Less(j1, i) {
- t.Errorf("heap invariant invalidated [%d] = %d > [%d] = %d", i, h.At(i), j1, h.At(j1));
- return;
+ t.Errorf("heap invariant invalidated [%d] = %d > [%d] = %d", i, h.At(i), j1, h.At(j1))
+ return
}
- h.verify(t, j1);
+ h.verify(t, j1)
}
if j2 < n {
if h.Less(j2, i) {
- t.Errorf("heap invariant invalidated [%d] = %d > [%d] = %d", i, h.At(i), j1, h.At(j2));
- return;
+ t.Errorf("heap invariant invalidated [%d] = %d > [%d] = %d", i, h.At(i), j1, h.At(j2))
+ return
}
- h.verify(t, j2);
+ h.verify(t, j2)
}
}
func TestInit0(t *testing.T) {
- h := new(myHeap);
+ h := new(myHeap)
for i := 20; i > 0; i-- {
- h.Push(0) // all elements are the same
+ h.Push(0) // all elements are the same
}
- Init(h);
- h.verify(t, 0);
+ Init(h)
+ h.verify(t, 0)
for i := 1; h.Len() > 0; i++ {
- x := Pop(h).(int);
- h.verify(t, 0);
+ x := Pop(h).(int)
+ h.verify(t, 0)
if x != 0 {
t.Errorf("%d.th pop got %d; want %d", i, x, 0)
}
@@ -60,16 +60,16 @@ func TestInit0(t *testing.T) {
func TestInit1(t *testing.T) {
- h := new(myHeap);
+ h := new(myHeap)
for i := 20; i > 0; i-- {
- h.Push(i) // all elements are different
+ h.Push(i) // all elements are different
}
- Init(h);
- h.verify(t, 0);
+ Init(h)
+ h.verify(t, 0)
for i := 1; h.Len() > 0; i++ {
- x := Pop(h).(int);
- h.verify(t, 0);
+ x := Pop(h).(int)
+ h.verify(t, 0)
if x != i {
t.Errorf("%d.th pop got %d; want %d", i, x, i)
}
@@ -78,26 +78,26 @@ func TestInit1(t *testing.T) {
func Test(t *testing.T) {
- h := new(myHeap);
- h.verify(t, 0);
+ h := new(myHeap)
+ h.verify(t, 0)
for i := 20; i > 10; i-- {
h.Push(i)
}
- Init(h);
- h.verify(t, 0);
+ Init(h)
+ h.verify(t, 0)
for i := 10; i > 0; i-- {
- Push(h, i);
- h.verify(t, 0);
+ Push(h, i)
+ h.verify(t, 0)
}
for i := 1; h.Len() > 0; i++ {
- x := Pop(h).(int);
+ x := Pop(h).(int)
if i < 20 {
Push(h, 20+i)
}
- h.verify(t, 0);
+ h.verify(t, 0)
if x != i {
t.Errorf("%d.th pop got %d; want %d", i, x, i)
}
@@ -106,53 +106,53 @@ func Test(t *testing.T) {
func TestRemove0(t *testing.T) {
- h := new(myHeap);
+ h := new(myHeap)
for i := 0; i < 10; i++ {
h.Push(i)
}
- h.verify(t, 0);
+ h.verify(t, 0)
for h.Len() > 0 {
- i := h.Len() - 1;
- x := Remove(h, i).(int);
+ i := h.Len() - 1
+ x := Remove(h, i).(int)
if x != i {
t.Errorf("Remove(%d) got %d; want %d", i, x, i)
}
- h.verify(t, 0);
+ h.verify(t, 0)
}
}
func TestRemove1(t *testing.T) {
- h := new(myHeap);
+ h := new(myHeap)
for i := 0; i < 10; i++ {
h.Push(i)
}
- h.verify(t, 0);
+ h.verify(t, 0)
for i := 0; h.Len() > 0; i++ {
- x := Remove(h, 0).(int);
+ x := Remove(h, 0).(int)
if x != i {
t.Errorf("Remove(0) got %d; want %d", x, i)
}
- h.verify(t, 0);
+ h.verify(t, 0)
}
}
func TestRemove2(t *testing.T) {
- N := 10;
+ N := 10
- h := new(myHeap);
+ h := new(myHeap)
for i := 0; i < N; i++ {
h.Push(i)
}
- h.verify(t, 0);
+ h.verify(t, 0)
- m := make(map[int]int);
+ m := make(map[int]int)
for h.Len() > 0 {
- m[Remove(h, (h.Len()-1)/2).(int)] = 1;
- h.verify(t, 0);
+ m[Remove(h, (h.Len()-1)/2).(int)] = 1
+ h.verify(t, 0)
}
if len(m) != N {
diff --git a/src/pkg/container/list/list.go b/src/pkg/container/list/list.go
index b7b392ceaa..9429c90a72 100644
--- a/src/pkg/container/list/list.go
+++ b/src/pkg/container/list/list.go
@@ -9,45 +9,45 @@ package list
type Element struct {
// Next and previous pointers in the doubly-linked list of elements.
// The front of the list has prev = nil, and the back has next = nil.
- next, prev *Element;
+ next, prev *Element
// A unique ID for the list to which this element belongs.
- id *byte;
+ id *byte
// The contents of this list element.
- Value interface{};
+ Value interface{}
}
// Next returns the next list element or nil.
-func (e *Element) Next() *Element { return e.next }
+func (e *Element) Next() *Element { return e.next }
// Prev returns the previous list element or nil.
-func (e *Element) Prev() *Element { return e.prev }
+func (e *Element) Prev() *Element { return e.prev }
// List represents a doubly linked list.
type List struct {
- front, back *Element;
- len int;
- id *byte;
+ front, back *Element
+ len int
+ id *byte
}
// Init initializes or clears a List.
func (l *List) Init() *List {
- l.front = nil;
- l.back = nil;
- l.len = 0;
- l.id = new(byte);
- return l;
+ l.front = nil
+ l.back = nil
+ l.len = 0
+ l.id = new(byte)
+ return l
}
// New returns an initialized list.
-func New() *List { return new(List).Init() }
+func New() *List { return new(List).Init() }
// Front returns the first element in the list.
-func (l *List) Front() *Element { return l.front }
+func (l *List) Front() *Element { return l.front }
// Back returns the last element in the list.
-func (l *List) Back() *Element { return l.back }
+func (l *List) Back() *Element { return l.back }
// Remove removes the element from the list.
func (l *List) Remove(e *Element) {
@@ -65,9 +65,9 @@ func (l *List) Remove(e *Element) {
e.next.prev = e.prev
}
- e.prev = nil;
- e.next = nil;
- l.len--;
+ e.prev = nil
+ e.next = nil
+ l.len--
}
func (l *List) insertBefore(e *Element, mark *Element) {
@@ -77,10 +77,10 @@ func (l *List) insertBefore(e *Element, mark *Element) {
} else {
mark.prev.next = e
}
- e.prev = mark.prev;
- mark.prev = e;
- e.next = mark;
- l.len++;
+ e.prev = mark.prev
+ mark.prev = e
+ e.next = mark
+ l.len++
}
func (l *List) insertAfter(e *Element, mark *Element) {
@@ -90,32 +90,32 @@ func (l *List) insertAfter(e *Element, mark *Element) {
} else {
mark.next.prev = e
}
- e.next = mark.next;
- mark.next = e;
- e.prev = mark;
- l.len++;
+ e.next = mark.next
+ mark.next = e
+ e.prev = mark
+ l.len++
}
func (l *List) insertFront(e *Element) {
if l.front == nil {
// empty list
- l.front, l.back = e, e;
- e.prev, e.next = nil, nil;
- l.len = 1;
- return;
+ l.front, l.back = e, e
+ e.prev, e.next = nil, nil
+ l.len = 1
+ return
}
- l.insertBefore(e, l.front);
+ l.insertBefore(e, l.front)
}
func (l *List) insertBack(e *Element) {
if l.back == nil {
// empty list
- l.front, l.back = e, e;
- e.prev, e.next = nil, nil;
- l.len = 1;
- return;
+ l.front, l.back = e, e
+ e.prev, e.next = nil, nil
+ l.len = 1
+ return
}
- l.insertAfter(e, l.back);
+ l.insertAfter(e, l.back)
}
// PushFront inserts the value at the front of the list and returns a new Element containing the value.
@@ -123,9 +123,9 @@ func (l *List) PushFront(value interface{}) *Element {
if l.id == nil {
l.Init()
}
- e := &Element{nil, nil, l.id, value};
- l.insertFront(e);
- return e;
+ e := &Element{nil, nil, l.id, value}
+ l.insertFront(e)
+ return e
}
// PushBack inserts the value at the back of the list and returns a new Element containing the value.
@@ -133,9 +133,9 @@ func (l *List) PushBack(value interface{}) *Element {
if l.id == nil {
l.Init()
}
- e := &Element{nil, nil, l.id, value};
- l.insertBack(e);
- return e;
+ e := &Element{nil, nil, l.id, value}
+ l.insertBack(e)
+ return e
}
// InsertBefore inserts the value immediately before mark and returns a new Element containing the value.
@@ -143,9 +143,9 @@ func (l *List) InsertBefore(value interface{}, mark *Element) *Element {
if mark.id != l.id {
return nil
}
- e := &Element{nil, nil, l.id, value};
- l.insertBefore(e, mark);
- return e;
+ e := &Element{nil, nil, l.id, value}
+ l.insertBefore(e, mark)
+ return e
}
// InsertAfter inserts the value immediately after mark and returns a new Element containing the value.
@@ -153,9 +153,9 @@ func (l *List) InsertAfter(value interface{}, mark *Element) *Element {
if mark.id != l.id {
return nil
}
- e := &Element{nil, nil, l.id, value};
- l.insertAfter(e, mark);
- return e;
+ e := &Element{nil, nil, l.id, value}
+ l.insertAfter(e, mark)
+ return e
}
// MoveToFront moves the element to the front of the list.
@@ -163,8 +163,8 @@ func (l *List) MoveToFront(e *Element) {
if e.id != l.id || l.front == e {
return
}
- l.Remove(e);
- l.insertFront(e);
+ l.Remove(e)
+ l.insertFront(e)
}
// MoveToBack moves the element to the back of the list.
@@ -172,22 +172,22 @@ func (l *List) MoveToBack(e *Element) {
if e.id != l.id || l.back == e {
return
}
- l.Remove(e);
- l.insertBack(e);
+ l.Remove(e)
+ l.insertBack(e)
}
// Len returns the number of elements in the list.
-func (l *List) Len() int { return l.len }
+func (l *List) Len() int { return l.len }
func (l *List) iterate(c chan<- interface{}) {
for e := l.front; e != nil; e = e.next {
c <- e.Value
}
- close(c);
+ close(c)
}
func (l *List) Iter() <-chan interface{} {
- c := make(chan interface{});
- go l.iterate(c);
- return c;
+ c := make(chan interface{})
+ go l.iterate(c)
+ return c
}
diff --git a/src/pkg/container/list/list_test.go b/src/pkg/container/list/list_test.go
index 52df37f561..846937a383 100644
--- a/src/pkg/container/list/list_test.go
+++ b/src/pkg/container/list/list_test.go
@@ -5,7 +5,7 @@
package list
import (
- "testing";
+ "testing"
)
func checkListPointers(t *testing.T, l *List, es []*Element) {
@@ -13,7 +13,7 @@ func checkListPointers(t *testing.T, l *List, es []*Element) {
if l.front != nil || l.back != nil {
t.Errorf("l.front/l.back = %v/%v should be nil/nil", l.front, l.back)
}
- return;
+ return
}
if l.front != es[0] {
@@ -24,8 +24,8 @@ func checkListPointers(t *testing.T, l *List, es []*Element) {
}
for i := 0; i < len(es); i++ {
- e := es[i];
- var e_prev, e_next *Element = nil, nil;
+ e := es[i]
+ var e_prev, e_next *Element = nil, nil
if i > 0 {
e_prev = es[i-1]
}
@@ -48,74 +48,74 @@ func checkListLen(t *testing.T, l *List, n int) {
}
func TestList(t *testing.T) {
- l := New();
- checkListPointers(t, l, []*Element{});
- checkListLen(t, l, 0);
+ l := New()
+ checkListPointers(t, l, []*Element{})
+ checkListLen(t, l, 0)
// Single element list
- e := l.PushFront("a");
- checkListLen(t, l, 1);
- checkListPointers(t, l, []*Element{e});
- l.MoveToFront(e);
- checkListPointers(t, l, []*Element{e});
- l.MoveToBack(e);
- checkListPointers(t, l, []*Element{e});
- checkListLen(t, l, 1);
- l.Remove(e);
- checkListPointers(t, l, []*Element{});
- checkListLen(t, l, 0);
+ e := l.PushFront("a")
+ checkListLen(t, l, 1)
+ checkListPointers(t, l, []*Element{e})
+ l.MoveToFront(e)
+ checkListPointers(t, l, []*Element{e})
+ l.MoveToBack(e)
+ checkListPointers(t, l, []*Element{e})
+ checkListLen(t, l, 1)
+ l.Remove(e)
+ checkListPointers(t, l, []*Element{})
+ checkListLen(t, l, 0)
// Bigger list
- e2 := l.PushFront(2);
- e1 := l.PushFront(1);
- e3 := l.PushBack(3);
- e4 := l.PushBack("banana");
- checkListPointers(t, l, []*Element{e1, e2, e3, e4});
- checkListLen(t, l, 4);
+ e2 := l.PushFront(2)
+ e1 := l.PushFront(1)
+ e3 := l.PushBack(3)
+ e4 := l.PushBack("banana")
+ checkListPointers(t, l, []*Element{e1, e2, e3, e4})
+ checkListLen(t, l, 4)
- l.Remove(e2);
- checkListPointers(t, l, []*Element{e1, e3, e4});
- checkListLen(t, l, 3);
+ l.Remove(e2)
+ checkListPointers(t, l, []*Element{e1, e3, e4})
+ checkListLen(t, l, 3)
- l.MoveToFront(e3); // move from middle
- checkListPointers(t, l, []*Element{e3, e1, e4});
+ l.MoveToFront(e3) // move from middle
+ checkListPointers(t, l, []*Element{e3, e1, e4})
- l.MoveToFront(e1);
- l.MoveToBack(e3); // move from middle
- checkListPointers(t, l, []*Element{e1, e4, e3});
+ l.MoveToFront(e1)
+ l.MoveToBack(e3) // move from middle
+ checkListPointers(t, l, []*Element{e1, e4, e3})
- l.MoveToFront(e3); // move from back
- checkListPointers(t, l, []*Element{e3, e1, e4});
- l.MoveToFront(e3); // should be no-op
- checkListPointers(t, l, []*Element{e3, e1, e4});
+ l.MoveToFront(e3) // move from back
+ checkListPointers(t, l, []*Element{e3, e1, e4})
+ l.MoveToFront(e3) // should be no-op
+ checkListPointers(t, l, []*Element{e3, e1, e4})
- l.MoveToBack(e3); // move from front
- checkListPointers(t, l, []*Element{e1, e4, e3});
- l.MoveToBack(e3); // should be no-op
- checkListPointers(t, l, []*Element{e1, e4, e3});
+ l.MoveToBack(e3) // move from front
+ checkListPointers(t, l, []*Element{e1, e4, e3})
+ l.MoveToBack(e3) // should be no-op
+ checkListPointers(t, l, []*Element{e1, e4, e3})
- e2 = l.InsertBefore(2, e1); // insert before front
- checkListPointers(t, l, []*Element{e2, e1, e4, e3});
- l.Remove(e2);
- e2 = l.InsertBefore(2, e4); // insert before middle
- checkListPointers(t, l, []*Element{e1, e2, e4, e3});
- l.Remove(e2);
- e2 = l.InsertBefore(2, e3); // insert before back
- checkListPointers(t, l, []*Element{e1, e4, e2, e3});
- l.Remove(e2);
+ e2 = l.InsertBefore(2, e1) // insert before front
+ checkListPointers(t, l, []*Element{e2, e1, e4, e3})
+ l.Remove(e2)
+ e2 = l.InsertBefore(2, e4) // insert before middle
+ checkListPointers(t, l, []*Element{e1, e2, e4, e3})
+ l.Remove(e2)
+ e2 = l.InsertBefore(2, e3) // insert before back
+ checkListPointers(t, l, []*Element{e1, e4, e2, e3})
+ l.Remove(e2)
- e2 = l.InsertAfter(2, e1); // insert after front
- checkListPointers(t, l, []*Element{e1, e2, e4, e3});
- l.Remove(e2);
- e2 = l.InsertAfter(2, e4); // insert after middle
- checkListPointers(t, l, []*Element{e1, e4, e2, e3});
- l.Remove(e2);
- e2 = l.InsertAfter(2, e3); // insert after back
- checkListPointers(t, l, []*Element{e1, e4, e3, e2});
- l.Remove(e2);
+ e2 = l.InsertAfter(2, e1) // insert after front
+ checkListPointers(t, l, []*Element{e1, e2, e4, e3})
+ l.Remove(e2)
+ e2 = l.InsertAfter(2, e4) // insert after middle
+ checkListPointers(t, l, []*Element{e1, e4, e2, e3})
+ l.Remove(e2)
+ e2 = l.InsertAfter(2, e3) // insert after back
+ checkListPointers(t, l, []*Element{e1, e4, e3, e2})
+ l.Remove(e2)
// Check standard iteration.
- sum := 0;
+ sum := 0
for e := range l.Iter() {
if i, ok := e.(int); ok {
sum += i
@@ -126,11 +126,11 @@ func TestList(t *testing.T) {
}
// Clear all elements by iterating
- var next *Element;
+ var next *Element
for e := l.Front(); e != nil; e = next {
- next = e.Next();
- l.Remove(e);
+ next = e.Next()
+ l.Remove(e)
}
- checkListPointers(t, l, []*Element{});
- checkListLen(t, l, 0);
+ checkListPointers(t, l, []*Element{})
+ checkListLen(t, l, 0)
}
diff --git a/src/pkg/container/ring/ring.go b/src/pkg/container/ring/ring.go
index 5fcdfc366f..335afbc3cc 100644
--- a/src/pkg/container/ring/ring.go
+++ b/src/pkg/container/ring/ring.go
@@ -12,15 +12,15 @@ package ring
// ring with a nil Value.
//
type Ring struct {
- next, prev *Ring;
- Value interface{}; // for use by client; untouched by this library
+ next, prev *Ring
+ Value interface{} // for use by client; untouched by this library
}
func (r *Ring) init() *Ring {
- r.next = r;
- r.prev = r;
- return r;
+ r.next = r
+ r.prev = r
+ return r
}
@@ -29,7 +29,7 @@ func (r *Ring) Next() *Ring {
if r.next == nil {
return r.init()
}
- return r.next;
+ return r.next
}
@@ -38,7 +38,7 @@ func (r *Ring) Prev() *Ring {
if r.next == nil {
return r.init()
}
- return r.prev;
+ return r.prev
}
@@ -59,7 +59,7 @@ func (r *Ring) Move(n int) *Ring {
r = r.next
}
}
- return r;
+ return r
}
@@ -68,15 +68,15 @@ func New(n int) *Ring {
if n <= 0 {
return nil
}
- r := new(Ring);
- p := r;
+ r := new(Ring)
+ p := r
for i := 1; i < n; i++ {
- p.next = &Ring{prev: p};
- p = p.next;
+ p.next = &Ring{prev: p}
+ p = p.next
}
- p.next = r;
- r.prev = p;
- return r;
+ p.next = r
+ r.prev = p
+ return r
}
@@ -97,17 +97,17 @@ func New(n int) *Ring {
// last element of s after insertion.
//
func (r *Ring) Link(s *Ring) *Ring {
- n := r.Next();
+ n := r.Next()
if s != nil {
- p := s.Prev();
+ p := s.Prev()
// Note: Cannot use multiple assignment because
// evaluation order of LHS is not specified.
- r.next = s;
- s.prev = r;
- n.prev = p;
- p.next = n;
+ r.next = s
+ s.prev = r
+ n.prev = p
+ p.next = n
}
- return n;
+ return n
}
@@ -119,7 +119,7 @@ func (r *Ring) Unlink(n int) *Ring {
if n <= 0 {
return nil
}
- return r.Link(r.Move(n + 1));
+ return r.Link(r.Move(n + 1))
}
@@ -127,27 +127,27 @@ func (r *Ring) Unlink(n int) *Ring {
// It executes in time proportional to the number of elements.
//
func (r *Ring) Len() int {
- n := 0;
+ n := 0
if r != nil {
- n = 1;
+ n = 1
for p := r.Next(); p != r; p = p.next {
n++
}
}
- return n;
+ return n
}
func (r *Ring) Iter() <-chan interface{} {
- c := make(chan interface{});
+ c := make(chan interface{})
go func() {
if r != nil {
- c <- r.Value;
+ c <- r.Value
for p := r.Next(); p != r; p = p.next {
c <- p.Value
}
}
- close(c);
- }();
- return c;
+ close(c)
+ }()
+ return c
}
diff --git a/src/pkg/container/ring/ring_test.go b/src/pkg/container/ring/ring_test.go
index b55f438fa2..ee3c411283 100644
--- a/src/pkg/container/ring/ring_test.go
+++ b/src/pkg/container/ring/ring_test.go
@@ -5,38 +5,38 @@
package ring
import (
- "fmt";
- "testing";
+ "fmt"
+ "testing"
)
// For debugging - keep around.
func dump(r *Ring) {
if r == nil {
- fmt.Println("empty");
- return;
+ fmt.Println("empty")
+ return
}
- i, n := 0, r.Len();
+ i, n := 0, r.Len()
for p := r; i < n; p = p.next {
- fmt.Printf("%4d: %p = {<- %p | %p ->}\n", i, p, p.prev, p.next);
- i++;
+ fmt.Printf("%4d: %p = {<- %p | %p ->}\n", i, p, p.prev, p.next)
+ i++
}
- fmt.Println();
+ fmt.Println()
}
func verify(t *testing.T, r *Ring, N int, sum int) {
// Len
- n := r.Len();
+ n := r.Len()
if n != N {
t.Errorf("r.Len() == %d; expected %d", n, N)
}
// iteration
- n = 0;
- s := 0;
+ n = 0
+ s := 0
for p := range r.Iter() {
- n++;
+ n++
if p != nil {
s += p.(int)
}
@@ -54,12 +54,12 @@ func verify(t *testing.T, r *Ring, N int, sum int) {
// connections
if r.next != nil {
- var p *Ring; // previous element
+ var p *Ring // previous element
for q := r; p == nil || q != r; q = q.next {
if p != nil && p != q.prev {
t.Errorf("prev = %p, expected q.prev = %p\n", p, q.prev)
}
- p = q;
+ p = q
}
if p != r.prev {
t.Errorf("prev = %p, expected r.prev = %p\n", p, r.prev)
@@ -85,8 +85,8 @@ func verify(t *testing.T, r *Ring, N int, sum int) {
t.Errorf("r.Move(%d) != r", -N)
}
for i := 0; i < 10; i++ {
- ni := N + i;
- mi := ni % N;
+ ni := N + i
+ mi := ni % N
if r.Move(ni) != r.Move(mi) {
t.Errorf("r.Move(%d) != r.Move(%d)", ni, mi)
}
@@ -99,142 +99,142 @@ func verify(t *testing.T, r *Ring, N int, sum int) {
func TestCornerCases(t *testing.T) {
var (
- r0 *Ring;
- r1 Ring;
+ r0 *Ring
+ r1 Ring
)
// Basics
- verify(t, r0, 0, 0);
- verify(t, &r1, 1, 0);
+ verify(t, r0, 0, 0)
+ verify(t, &r1, 1, 0)
// Insert
- r1.Link(r0);
- verify(t, r0, 0, 0);
- verify(t, &r1, 1, 0);
+ r1.Link(r0)
+ verify(t, r0, 0, 0)
+ verify(t, &r1, 1, 0)
// Insert
- r1.Link(r0);
- verify(t, r0, 0, 0);
- verify(t, &r1, 1, 0);
+ r1.Link(r0)
+ verify(t, r0, 0, 0)
+ verify(t, &r1, 1, 0)
// Unlink
- r1.Unlink(0);
- verify(t, &r1, 1, 0);
+ r1.Unlink(0)
+ verify(t, &r1, 1, 0)
}
func makeN(n int) *Ring {
- r := New(n);
+ r := New(n)
for i := 1; i <= n; i++ {
- r.Value = i;
- r = r.Next();
+ r.Value = i
+ r = r.Next()
}
- return r;
+ return r
}
func sum(r *Ring) int {
- s := 0;
+ s := 0
for p := range r.Iter() {
s += p.(int)
}
- return s;
+ return s
}
-func sumN(n int) int { return (n*n + n) / 2 }
+func sumN(n int) int { return (n*n + n) / 2 }
func TestNew(t *testing.T) {
for i := 0; i < 10; i++ {
- r := New(i);
- verify(t, r, i, -1);
+ r := New(i)
+ verify(t, r, i, -1)
}
for i := 0; i < 10; i++ {
- r := makeN(i);
- verify(t, r, i, sumN(i));
+ r := makeN(i)
+ verify(t, r, i, sumN(i))
}
}
func TestLink1(t *testing.T) {
- r1a := makeN(1);
- var r1b Ring;
- r2a := r1a.Link(&r1b);
- verify(t, r2a, 2, 1);
+ r1a := makeN(1)
+ var r1b Ring
+ r2a := r1a.Link(&r1b)
+ verify(t, r2a, 2, 1)
if r2a != r1a {
t.Errorf("a) 2-element link failed")
}
- r2b := r2a.Link(r2a.Next());
- verify(t, r2b, 2, 1);
+ r2b := r2a.Link(r2a.Next())
+ verify(t, r2b, 2, 1)
if r2b != r2a.Next() {
t.Errorf("b) 2-element link failed")
}
- r1c := r2b.Link(r2b);
- verify(t, r1c, 1, 1);
- verify(t, r2b, 1, 0);
+ r1c := r2b.Link(r2b)
+ verify(t, r1c, 1, 1)
+ verify(t, r2b, 1, 0)
}
func TestLink2(t *testing.T) {
- var r0 *Ring;
- r1a := &Ring{Value: 42};
- r1b := &Ring{Value: 77};
- r10 := makeN(10);
+ var r0 *Ring
+ r1a := &Ring{Value: 42}
+ r1b := &Ring{Value: 77}
+ r10 := makeN(10)
- r1a.Link(r0);
- verify(t, r1a, 1, 42);
+ r1a.Link(r0)
+ verify(t, r1a, 1, 42)
- r1a.Link(r1b);
- verify(t, r1a, 2, 42+77);
+ r1a.Link(r1b)
+ verify(t, r1a, 2, 42+77)
- r10.Link(r0);
- verify(t, r10, 10, sumN(10));
+ r10.Link(r0)
+ verify(t, r10, 10, sumN(10))
- r10.Link(r1a);
- verify(t, r10, 12, sumN(10)+42+77);
+ r10.Link(r1a)
+ verify(t, r10, 12, sumN(10)+42+77)
}
func TestLink3(t *testing.T) {
- var r Ring;
- n := 1;
+ var r Ring
+ n := 1
for i := 1; i < 100; i++ {
- n += i;
- verify(t, r.Link(New(i)), n, -1);
+ n += i
+ verify(t, r.Link(New(i)), n, -1)
}
}
func TestUnlink(t *testing.T) {
- r10 := makeN(10);
- s10 := r10.Move(6);
+ r10 := makeN(10)
+ s10 := r10.Move(6)
- sum10 := sumN(10);
+ sum10 := sumN(10)
- verify(t, r10, 10, sum10);
- verify(t, s10, 10, sum10);
+ verify(t, r10, 10, sum10)
+ verify(t, s10, 10, sum10)
- r0 := r10.Unlink(0);
- verify(t, r0, 0, 0);
+ r0 := r10.Unlink(0)
+ verify(t, r0, 0, 0)
- r1 := r10.Unlink(1);
- verify(t, r1, 1, 2);
- verify(t, r10, 9, sum10-2);
+ r1 := r10.Unlink(1)
+ verify(t, r1, 1, 2)
+ verify(t, r10, 9, sum10-2)
- r9 := r10.Unlink(9);
- verify(t, r9, 9, sum10-2);
- verify(t, r10, 9, sum10-2);
+ r9 := r10.Unlink(9)
+ verify(t, r9, 9, sum10-2)
+ verify(t, r10, 9, sum10-2)
}
func TestLinkUnlink(t *testing.T) {
for i := 1; i < 4; i++ {
- ri := New(i);
+ ri := New(i)
for j := 0; j < i; j++ {
- rj := ri.Unlink(j);
- verify(t, rj, j, -1);
- verify(t, ri, i-j, -1);
- ri.Link(rj);
- verify(t, ri, i, -1);
+ rj := ri.Unlink(j)
+ verify(t, rj, j, -1)
+ verify(t, ri, i-j, -1)
+ ri.Link(rj)
+ verify(t, ri, i, -1)
}
}
}
diff --git a/src/pkg/container/vector/intvector.go b/src/pkg/container/vector/intvector.go
index 43f8ff8081..1ec4b85a9b 100644
--- a/src/pkg/container/vector/intvector.go
+++ b/src/pkg/container/vector/intvector.go
@@ -7,7 +7,7 @@ package vector
// IntVector is a specialization of Vector that hides the wrapping of Elements around ints.
type IntVector struct {
- Vector;
+ Vector
}
@@ -17,40 +17,40 @@ type IntVector struct {
// Resize adds 0 elements. The capacity parameter is ignored unless the
// new length or capacity is longer that the current capacity.
func (p *IntVector) Resize(length, capacity int) *IntVector {
- i := p.Len();
- p.Vector.Resize(length, capacity);
+ i := p.Len()
+ p.Vector.Resize(length, capacity)
for a := p.a; i < len(a); i++ {
a[i] = 0
}
- return p;
+ return p
}
// At returns the i'th element of the vector.
-func (p *IntVector) At(i int) int { return p.Vector.At(i).(int) }
+func (p *IntVector) At(i int) int { return p.Vector.At(i).(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 }
+func (p *IntVector) Set(i int, x int) { p.a[i] = x }
// Last returns the element in the vector of highest index.
-func (p *IntVector) Last() int { return p.Vector.Last().(int) }
+func (p *IntVector) 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());
+ arr := make([]int, p.Len())
for i, v := range p.a {
arr[i] = v.(int)
}
- return arr;
+ 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) }
+func (p *IntVector) Insert(i int, x int) { p.Vector.Insert(i, x) }
// InsertVector inserts into the vector the contents of the Vector
@@ -68,11 +68,11 @@ func (p *IntVector) Slice(i, j int) *IntVector {
// Push appends x to the end of the vector.
-func (p *IntVector) Push(x int) { p.Vector.Push(x) }
+func (p *IntVector) Push(x int) { p.Vector.Push(x) }
// Pop deletes and returns the last element of the vector.
-func (p *IntVector) Pop() int { return p.Vector.Pop().(int) }
+func (p *IntVector) Pop() int { return p.Vector.Pop().(int) }
// AppendVector appends the entire IntVector x to the end of this vector.
@@ -83,7 +83,7 @@ func (p *IntVector) AppendVector(x *IntVector) {
// sort.Interface support
// 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 { return p.At(i) < p.At(j) }
+func (p *IntVector) Less(i, j int) bool { return p.At(i) < p.At(j) }
// Iterate over all elements; driver for range
@@ -91,13 +91,13 @@ func (p *IntVector) iterate(c chan<- int) {
for _, v := range p.a {
c <- v.(int)
}
- close(c);
+ close(c)
}
// Channel iterator for range.
func (p *IntVector) Iter() <-chan int {
- c := make(chan int);
- go p.iterate(c);
- return c;
+ c := make(chan int)
+ go p.iterate(c)
+ return c
}
diff --git a/src/pkg/container/vector/stringvector.go b/src/pkg/container/vector/stringvector.go
index 93a4197a58..821a7a101d 100644
--- a/src/pkg/container/vector/stringvector.go
+++ b/src/pkg/container/vector/stringvector.go
@@ -6,7 +6,7 @@ package vector
// StringVector is a specialization of Vector that hides the wrapping of Elements around strings.
type StringVector struct {
- Vector;
+ Vector
}
@@ -16,34 +16,34 @@ type StringVector struct {
// Resize adds "" elements. The capacity parameter is ignored unless the
// new length or capacity is longer that the current capacity.
func (p *StringVector) Resize(length, capacity int) *StringVector {
- i := p.Len();
- p.Vector.Resize(length, capacity);
+ i := p.Len()
+ p.Vector.Resize(length, capacity)
for a := p.a; i < len(a); i++ {
a[i] = ""
}
- return p;
+ return p
}
// At returns the i'th element of the vector.
-func (p *StringVector) At(i int) string { return p.Vector.At(i).(string) }
+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 }
+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) }
+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());
+ arr := make([]string, p.Len())
for i, v := range p.a {
arr[i] = v.(string)
}
- return arr;
+ return arr
}
@@ -69,11 +69,11 @@ func (p *StringVector) Slice(i, j int) *StringVector {
// Push appends x to the end of the vector.
-func (p *StringVector) Push(x string) { p.Vector.Push(x) }
+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) }
+func (p *StringVector) Pop() string { return p.Vector.Pop().(string) }
// AppendVector appends the entire StringVector x to the end of this vector.
@@ -84,7 +84,7 @@ func (p *StringVector) AppendVector(x *StringVector) {
// sort.Interface 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) }
+func (p *StringVector) Less(i, j int) bool { return p.At(i) < p.At(j) }
// Iterate over all elements; driver for range
@@ -92,13 +92,13 @@ func (p *StringVector) iterate(c chan<- string) {
for _, v := range p.a {
c <- v.(string)
}
- close(c);
+ close(c)
}
// Channel iterator for range.
func (p *StringVector) Iter() <-chan string {
- c := make(chan string);
- go p.iterate(c);
- return c;
+ c := make(chan string)
+ go p.iterate(c)
+ return c
}
diff --git a/src/pkg/container/vector/vector.go b/src/pkg/container/vector/vector.go
index 0408490bea..ed1845b27c 100644
--- a/src/pkg/container/vector/vector.go
+++ b/src/pkg/container/vector/vector.go
@@ -9,8 +9,8 @@ package vector
// Vector is the container itself.
// The zero value for Vector is an empty vector ready to use.
type Vector struct {
- a []interface{};
- bootstrap [8]interface{};
+ a []interface{}
+ bootstrap [8]interface{}
}
@@ -21,31 +21,31 @@ func (p *Vector) realloc(length, capacity int) (b []interface{}) {
} else {
b = make([]interface{}, length, capacity)
}
- copy(b, p.a);
- p.a = b;
- return;
+ copy(b, p.a)
+ p.a = b
+ return
}
// Insert n elements at position i.
func (p *Vector) expand(i, n int) {
- a := p.a;
+ a := p.a
// make sure we have enough space
- len0 := len(a);
- len1 := len0 + n;
+ len0 := len(a)
+ len1 := len0 + n
if len1 <= cap(a) {
// enough space - just expand
a = a[0:len1]
} else {
// not enough space - double capacity
- capb := cap(a) * 2;
+ capb := cap(a) * 2
if capb < len1 {
// still not enough - use required length
capb = len1
}
// capb >= len1
- a = p.realloc(len1, capb);
+ a = p.realloc(len1, capb)
}
// make a hole
@@ -53,7 +53,7 @@ func (p *Vector) expand(i, n int) {
a[j+n] = a[j]
}
- p.a = a;
+ p.a = a
}
@@ -64,7 +64,7 @@ func (p *Vector) expand(i, n int) {
// new length or capacity is longer that the current capacity. The resized
// vector's capacity may be larger than the requested capacity.
func (p *Vector) Resize(length, capacity int) *Vector {
- a := p.a;
+ a := p.a
if length > cap(a) || capacity > cap(a) {
// not enough space or larger capacity requested explicitly
@@ -76,91 +76,91 @@ func (p *Vector) Resize(length, capacity int) *Vector {
}
}
- p.a = a[0:length];
- return p;
+ p.a = a[0:length]
+ return p
}
// Len returns the number of elements in the vector.
-func (p *Vector) Len() int { return len(p.a) }
+func (p *Vector) Len() int { return len(p.a) }
// Cap returns the capacity of the vector; that is, the
// maximum length the vector can grow without resizing.
-func (p *Vector) Cap() int { return cap(p.a) }
+func (p *Vector) Cap() int { return cap(p.a) }
// At returns the i'th element of the vector.
-func (p *Vector) At(i int) interface{} { return p.a[i] }
+func (p *Vector) At(i int) interface{} { return p.a[i] }
// Set sets the i'th element of the vector to value x.
-func (p *Vector) Set(i int, x interface{}) { p.a[i] = x }
+func (p *Vector) Set(i int, x interface{}) { p.a[i] = x }
// Last returns the element in the vector of highest index.
-func (p *Vector) Last() interface{} { return p.a[len(p.a)-1] }
+func (p *Vector) Last() interface{} { return p.a[len(p.a)-1] }
// Data returns all the elements as a slice.
func (p *Vector) Data() []interface{} {
- arr := make([]interface{}, p.Len());
+ arr := make([]interface{}, p.Len())
for i, v := range p.a {
arr[i] = v
}
- return arr;
+ return arr
}
// Insert inserts into the vector an element of value x before
// the current element at index i.
func (p *Vector) Insert(i int, x interface{}) {
- p.expand(i, 1);
- p.a[i] = x;
+ p.expand(i, 1)
+ p.a[i] = x
}
// Delete deletes the i'th element of the vector. The gap is closed so the old
// element at index i+1 has index i afterwards.
func (p *Vector) Delete(i int) {
- a := p.a;
- n := len(a);
+ a := p.a
+ n := len(a)
- copy(a[i:n-1], a[i+1:n]);
- a[n-1] = nil; // support GC, nil out entry
- p.a = a[0 : n-1];
+ copy(a[i:n-1], a[i+1:n])
+ a[n-1] = nil // support GC, nil out entry
+ p.a = a[0 : n-1]
}
// 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 *Vector) InsertVector(i int, x *Vector) {
- p.expand(i, len(x.a));
- copy(p.a[i:i+len(x.a)], x.a);
+ p.expand(i, len(x.a))
+ copy(p.a[i:i+len(x.a)], x.a)
}
// Cut deletes elements i through j-1, inclusive.
func (p *Vector) Cut(i, j int) {
- a := p.a;
- n := len(a);
- m := n - (j - i);
+ a := p.a
+ n := len(a)
+ m := n - (j - i)
- copy(a[i:m], a[j:n]);
+ copy(a[i:m], a[j:n])
for k := m; k < n; k++ {
- a[k] = nil // support GC, nil out entries
+ a[k] = nil // support GC, nil out entries
}
- p.a = a[0:m];
+ p.a = a[0:m]
}
// Slice returns a new Vector by slicing the old one to extract slice [i:j].
// The elements are copied. The original vector is unchanged.
func (p *Vector) Slice(i, j int) *Vector {
- s := new(Vector).Resize(j-i, 0); // will fail in Init() if j < i
- copy(s.a, p.a[i:j]);
- return s;
+ s := new(Vector).Resize(j-i, 0) // will fail in Init() if j < i
+ copy(s.a, p.a[i:j])
+ return s
}
@@ -168,7 +168,7 @@ func (p *Vector) Slice(i, j int) *Vector {
// The function should not change the indexing of the vector underfoot.
func (p *Vector) Do(f func(elem interface{})) {
for i := 0; i < len(p.a); i++ {
- f(p.a[i]) // not too safe if f changes the Vector
+ f(p.a[i]) // not too safe if f changes the Vector
}
}
@@ -176,39 +176,39 @@ func (p *Vector) Do(f func(elem interface{})) {
// Convenience wrappers
// Push appends x to the end of the vector.
-func (p *Vector) Push(x interface{}) { p.Insert(len(p.a), x) }
+func (p *Vector) Push(x interface{}) { p.Insert(len(p.a), x) }
// Pop deletes the last element of the vector.
func (p *Vector) Pop() interface{} {
- i := len(p.a) - 1;
- x := p.a[i];
- p.a[i] = nil; // support GC, nil out entry
- p.a = p.a[0:i];
- return x;
+ i := len(p.a) - 1
+ x := p.a[i]
+ p.a[i] = nil // support GC, nil out entry
+ p.a = p.a[0:i]
+ return x
}
// AppendVector appends the entire Vector x to the end of this vector.
-func (p *Vector) AppendVector(x *Vector) { p.InsertVector(len(p.a), x) }
+func (p *Vector) AppendVector(x *Vector) { p.InsertVector(len(p.a), x) }
// Partial sort.Interface support
// LessInterface provides partial support of the sort.Interface.
type LessInterface interface {
- Less(y interface{}) bool;
+ Less(y interface{}) bool
}
// Less returns a boolean denoting whether the i'th element is less than the j'th element.
-func (p *Vector) Less(i, j int) bool { return p.a[i].(LessInterface).Less(p.a[j]) }
+func (p *Vector) Less(i, j int) bool { return p.a[i].(LessInterface).Less(p.a[j]) }
// Swap exchanges the elements at indexes i and j.
func (p *Vector) Swap(i, j int) {
- a := p.a;
- a[i], a[j] = a[j], a[i];
+ a := p.a
+ a[i], a[j] = a[j], a[i]
}
@@ -217,13 +217,13 @@ func (p *Vector) iterate(c chan<- interface{}) {
for _, v := range p.a {
c <- v
}
- close(c);
+ close(c)
}
// Channel iterator for range.
func (p *Vector) Iter() <-chan interface{} {
- c := make(chan interface{});
- go p.iterate(c);
- return c;
+ c := make(chan interface{})
+ go p.iterate(c)
+ return c
}
diff --git a/src/pkg/container/vector/vector_test.go b/src/pkg/container/vector/vector_test.go
index f187f72188..755ba7cad0 100644
--- a/src/pkg/container/vector/vector_test.go
+++ b/src/pkg/container/vector/vector_test.go
@@ -10,7 +10,7 @@ import "fmt"
func TestZeroLen(t *testing.T) {
- a := new(Vector);
+ a := new(Vector)
if a.Len() != 0 {
t.Errorf("B) expected 0, got %d", a.Len())
}
@@ -18,8 +18,8 @@ func TestZeroLen(t *testing.T) {
type VectorInterface interface {
- Len() int;
- Cap() int;
+ Len() int
+ Cap() int
}
@@ -34,27 +34,27 @@ func checkSize(t *testing.T, v VectorInterface, len, cap int) {
func TestResize(t *testing.T) {
- var a Vector;
- checkSize(t, &a, 0, 0);
- checkSize(t, a.Resize(0, 5), 0, 5);
- checkSize(t, a.Resize(1, 0), 1, 5);
- checkSize(t, a.Resize(10, 0), 10, 10);
- checkSize(t, a.Resize(5, 0), 5, 10);
- checkSize(t, a.Resize(3, 8), 3, 10);
- checkSize(t, a.Resize(0, 100), 0, 100);
- checkSize(t, a.Resize(11, 100), 11, 100);
+ var a Vector
+ checkSize(t, &a, 0, 0)
+ checkSize(t, a.Resize(0, 5), 0, 5)
+ checkSize(t, a.Resize(1, 0), 1, 5)
+ checkSize(t, a.Resize(10, 0), 10, 10)
+ checkSize(t, a.Resize(5, 0), 5, 10)
+ checkSize(t, a.Resize(3, 8), 3, 10)
+ checkSize(t, a.Resize(0, 100), 0, 100)
+ checkSize(t, a.Resize(11, 100), 11, 100)
}
func TestIntResize(t *testing.T) {
- var a IntVector;
- checkSize(t, &a, 0, 0);
- a.Push(1);
- a.Push(2);
- a.Push(3);
- a.Push(4);
- checkSize(t, &a, 4, 4);
- checkSize(t, a.Resize(10, 0), 10, 10);
+ var a IntVector
+ checkSize(t, &a, 0, 0)
+ a.Push(1)
+ a.Push(2)
+ a.Push(3)
+ a.Push(4)
+ checkSize(t, &a, 4, 4)
+ checkSize(t, a.Resize(10, 0), 10, 10)
for i := 4; i < a.Len(); i++ {
if a.At(i) != 0 {
t.Errorf("expected a.At(%d) == 0; found %d", i, a.At(i))
@@ -64,14 +64,14 @@ func TestIntResize(t *testing.T) {
func TestStringResize(t *testing.T) {
- var a StringVector;
- checkSize(t, &a, 0, 0);
- a.Push("1");
- a.Push("2");
- a.Push("3");
- a.Push("4");
- checkSize(t, &a, 4, 4);
- checkSize(t, a.Resize(10, 0), 10, 10);
+ var a StringVector
+ checkSize(t, &a, 0, 0)
+ a.Push("1")
+ a.Push("2")
+ a.Push("3")
+ a.Push("4")
+ checkSize(t, &a, 4, 4)
+ checkSize(t, a.Resize(10, 0), 10, 10)
for i := 4; i < a.Len(); i++ {
if a.At(i) != "" {
t.Errorf("expected a.At(%d) == "+"; found %s", i, a.At(i))
@@ -95,25 +95,25 @@ func checkNil(t *testing.T, a *Vector, i int) {
func TestTrailingElements(t *testing.T) {
- var a Vector;
+ var a Vector
for i := 0; i < 10; i++ {
a.Push(i)
}
- checkNil(t, &a, 10);
- checkSize(t, &a, 10, 16);
- checkSize(t, a.Resize(5, 0), 5, 16);
- checkSize(t, a.Resize(10, 0), 10, 16);
- checkNil(t, &a, 5);
+ checkNil(t, &a, 10)
+ checkSize(t, &a, 10, 16)
+ checkSize(t, a.Resize(5, 0), 5, 16)
+ checkSize(t, a.Resize(10, 0), 10, 16)
+ checkNil(t, &a, 5)
}
-func val(i int) int { return i*991 - 1234 }
+func val(i int) int { return i*991 - 1234 }
func TestAccess(t *testing.T) {
- const n = 100;
- var a Vector;
- a.Resize(n, 0);
+ const n = 100
+ var a Vector
+ a.Resize(n, 0)
for i := 0; i < n; i++ {
a.Set(i, val(i))
}
@@ -126,14 +126,14 @@ func TestAccess(t *testing.T) {
func TestInsertDeleteClear(t *testing.T) {
- const n = 100;
- var a Vector;
+ const n = 100
+ var a Vector
for i := 0; i < n; i++ {
if a.Len() != i {
t.Errorf("A) wrong len %d (expected %d)", a.Len(), i)
}
- a.Insert(0, val(i));
+ a.Insert(0, val(i))
if a.Last().(int) != val(0) {
t.Error("B")
}
@@ -145,7 +145,7 @@ func TestInsertDeleteClear(t *testing.T) {
if a.At(0).(int) != val(i) {
t.Error("D")
}
- a.Delete(0);
+ a.Delete(0)
if a.Len() != i {
t.Errorf("E) wrong len %d (expected %d)", a.Len(), i)
}
@@ -155,7 +155,7 @@ func TestInsertDeleteClear(t *testing.T) {
t.Errorf("F) wrong len %d (expected 0)", a.Len())
}
for i := 0; i < n; i++ {
- a.Push(val(i));
+ a.Push(val(i))
if a.Len() != i+1 {
t.Errorf("G) wrong len %d (expected %d)", a.Len(), i+1)
}
@@ -163,17 +163,17 @@ func TestInsertDeleteClear(t *testing.T) {
t.Error("H")
}
}
- a.Resize(0, 0);
+ a.Resize(0, 0)
if a.Len() != 0 {
t.Errorf("I wrong len %d (expected 0)", a.Len())
}
- const m = 5;
+ const m = 5
for j := 0; j < m; j++ {
- a.Push(j);
+ a.Push(j)
for i := 0; i < n; i++ {
- x := val(i);
- a.Push(x);
+ x := val(i)
+ a.Push(x)
if a.Pop().(int) != x {
t.Error("J")
}
@@ -195,7 +195,7 @@ func verify_slice(t *testing.T, x *Vector, elt, i, j int) {
}
}
- s := x.Slice(i, j);
+ s := x.Slice(i, j)
for k, n := 0, j-i; k < n; k++ {
if s.At(k).(int) != elt {
t.Errorf("N) wrong [%d] element %d (expected %d)", k, x.At(k).(int), elt)
@@ -205,54 +205,54 @@ func verify_slice(t *testing.T, x *Vector, elt, i, j int) {
func verify_pattern(t *testing.T, x *Vector, a, b, c int) {
- n := a + b + c;
+ n := a + b + c
if x.Len() != n {
t.Errorf("O) wrong len %d (expected %d)", x.Len(), n)
}
- verify_slice(t, x, 0, 0, a);
- verify_slice(t, x, 1, a, a+b);
- verify_slice(t, x, 0, a+b, n);
+ verify_slice(t, x, 0, 0, a)
+ verify_slice(t, x, 1, a, a+b)
+ verify_slice(t, x, 0, a+b, n)
}
func make_vector(elt, len int) *Vector {
- x := new(Vector).Resize(len, 0);
+ x := new(Vector).Resize(len, 0)
for i := 0; i < len; i++ {
x.Set(i, elt)
}
- return x;
+ return x
}
func TestInsertVector(t *testing.T) {
// 1
- a := make_vector(0, 0);
- b := make_vector(1, 10);
- a.InsertVector(0, b);
- verify_pattern(t, a, 0, 10, 0);
+ a := make_vector(0, 0)
+ b := make_vector(1, 10)
+ a.InsertVector(0, b)
+ verify_pattern(t, a, 0, 10, 0)
// 2
- a = make_vector(0, 10);
- b = make_vector(1, 0);
- a.InsertVector(5, b);
- verify_pattern(t, a, 5, 0, 5);
+ a = make_vector(0, 10)
+ b = make_vector(1, 0)
+ a.InsertVector(5, b)
+ verify_pattern(t, a, 5, 0, 5)
// 3
- a = make_vector(0, 10);
- b = make_vector(1, 3);
- a.InsertVector(3, b);
- verify_pattern(t, a, 3, 3, 7);
+ a = make_vector(0, 10)
+ b = make_vector(1, 3)
+ a.InsertVector(3, b)
+ verify_pattern(t, a, 3, 3, 7)
// 4
- a = make_vector(0, 10);
- b = make_vector(1, 1000);
- a.InsertVector(8, b);
- verify_pattern(t, a, 8, 1000, 2);
+ a = make_vector(0, 10)
+ b = make_vector(1, 1000)
+ a.InsertVector(8, b)
+ verify_pattern(t, a, 8, 1000, 2)
}
// This also tests IntVector and StringVector
func TestSorting(t *testing.T) {
- const n = 100;
+ const n = 100
- a := new(IntVector).Resize(n, 0);
+ a := new(IntVector).Resize(n, 0)
for i := n - 1; i >= 0; i-- {
a.Set(i, n-1-i)
}
@@ -260,7 +260,7 @@ func TestSorting(t *testing.T) {
t.Error("int vector not sorted")
}
- b := new(StringVector).Resize(n, 0);
+ b := new(StringVector).Resize(n, 0)
for i := n - 1; i >= 0; i-- {
b.Set(i, fmt.Sprint(n-1-i))
}
@@ -271,20 +271,20 @@ func TestSorting(t *testing.T) {
func TestDo(t *testing.T) {
- const n = 25;
- const salt = 17;
- a := new(IntVector).Resize(n, 0);
+ const n = 25
+ const salt = 17
+ a := new(IntVector).Resize(n, 0)
for i := 0; i < n; i++ {
a.Set(i, salt*i)
}
- count := 0;
+ count := 0
a.Do(func(e interface{}) {
- i := e.(int);
+ i := e.(int)
if i != count*salt {
t.Error("value at", count, "should be", count*salt, "not", i)
}
- count++;
- });
+ count++
+ })
if count != n {
t.Error("should visit", n, "values; did visit", count)
}
@@ -292,17 +292,17 @@ func TestDo(t *testing.T) {
func TestIter(t *testing.T) {
- const Len = 100;
- x := new(Vector).Resize(Len, 0);
+ const Len = 100
+ x := new(Vector).Resize(Len, 0)
for i := 0; i < Len; i++ {
x.Set(i, i*i)
}
- i := 0;
+ i := 0
for v := range x.Iter() {
if v.(int) != i*i {
t.Error("Iter expected", i*i, "got", v.(int))
}
- i++;
+ i++
}
if i != Len {
t.Error("Iter stopped at", i, "not", Len)