diff options
author | Russ Cox <rsc@golang.org> | 2009-10-06 14:55:39 -0700 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2009-10-06 14:55:39 -0700 |
commit | d2c438c32027a1bcaa2a20e7626495fedcef804a (patch) | |
tree | d4e306848aa66193a42b8531963e246bdb4ed364 /src/pkg/container | |
parent | a7bf4bfaf01a627bbb1be972608820be08e80512 (diff) | |
download | golang-d2c438c32027a1bcaa2a20e7626495fedcef804a.tar.gz |
another round of gofmt applications
R=gri
DELTA=900 (106 added, 31 deleted, 763 changed)
OCL=35384
CL=35396
Diffstat (limited to 'src/pkg/container')
-rw-r--r-- | src/pkg/container/heap/heap.go | 8 | ||||
-rwxr-xr-x | src/pkg/container/list/list.go | 72 | ||||
-rwxr-xr-x | src/pkg/container/list/list_test.go | 62 | ||||
-rw-r--r-- | src/pkg/container/ring/ring.go | 12 | ||||
-rw-r--r-- | src/pkg/container/ring/ring_test.go | 20 | ||||
-rw-r--r-- | src/pkg/container/vector/intvector.go | 26 | ||||
-rw-r--r-- | src/pkg/container/vector/stringvector.go | 26 | ||||
-rw-r--r-- | src/pkg/container/vector/vector.go | 76 | ||||
-rw-r--r-- | src/pkg/container/vector/vector_test.go | 128 |
9 files changed, 237 insertions, 193 deletions
diff --git a/src/pkg/container/heap/heap.go b/src/pkg/container/heap/heap.go index 6e7eccb5f..6b10872e1 100644 --- a/src/pkg/container/heap/heap.go +++ b/src/pkg/container/heap/heap.go @@ -37,7 +37,7 @@ func Init(h HeapInterface) { // func Push(h HeapInterface, x interface{}) { h.Push(x); - up(h, h.Len()-1); + up(h, h.Len() - 1); } @@ -45,7 +45,7 @@ func Push(h HeapInterface, x interface{}) { // and returns it. The complexity is O(log(n)) where n = h.Len(). // func Pop(h HeapInterface) interface{} { - n := h.Len()-1; + n := h.Len() - 1; h.Swap(0, n); down(h, 0, n); return h.Pop(); @@ -56,7 +56,7 @@ func Pop(h HeapInterface) interface{} { // The complexity is O(log(n)) where n = h.Len(). // func Remove(h HeapInterface, i int) interface{} { - n := h.Len()-1; + n := h.Len() - 1; if n != i { h.Swap(n, i); down(h, i, n); @@ -85,7 +85,7 @@ func down(h HeapInterface, i, n int) { break; } if j1 := j+1; j1 < n && !h.Less(j, j1) { - j = j1; // = 2*i + 2 + j = j1; // = 2*i + 2 } if h.Less(i, j) { break; diff --git a/src/pkg/container/list/list.go b/src/pkg/container/list/list.go index 64000e463..acd34c417 100755 --- a/src/pkg/container/list/list.go +++ b/src/pkg/container/list/list.go @@ -9,30 +9,30 @@ 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 + return e.next; } // Prev returns the previous list element or nil. func (e *Element) Prev() *Element { - return e.prev + 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. @@ -41,28 +41,28 @@ func (l *List) Init() *List { l.back = nil; l.len = 0; l.id = new(byte); - return l + return l; } // New returns an initialized list. func New() *List { - return new(List).Init() + return new(List).Init(); } // Front returns the first element in the list. func (l *List) Front() *Element { - return l.front + return l.front; } // Back returns the last element in the list. func (l *List) Back() *Element { - return l.back + return l.back; } // Remove removes the element from the list. func (l *List) Remove(e *Element) { if e.id != l.id { - return + return; } if e.prev == nil { l.front = e.next; @@ -112,7 +112,7 @@ func (l *List) insertFront(e *Element) { l.front, l.back = e, e; e.prev, e.next = nil, nil; l.len = 1; - return + return; } l.insertBefore(e, l.front); } @@ -123,55 +123,55 @@ func (l *List) insertBack(e *Element) { l.front, l.back = e, e; e.prev, e.next = nil, nil; l.len = 1; - return + return; } l.insertAfter(e, l.back); } // PushFront inserts the value at the front of the list and returns a new Element containing the value. -func (l *List) PushFront(value interface {}) *Element { +func (l *List) PushFront(value interface{}) *Element { if l.id == nil { l.Init(); } - e := &Element{ nil, nil, l.id, value }; + e := &Element{nil, nil, l.id, value}; l.insertFront(e); - return e + return e; } // PushBack inserts the value at the back of the list and returns a new Element containing the value. -func (l *List) PushBack(value interface {}) *Element { +func (l *List) PushBack(value interface{}) *Element { if l.id == nil { l.Init(); } - e := &Element{ nil, nil, l.id, value }; + e := &Element{nil, nil, l.id, value}; l.insertBack(e); - return e + return e; } // InsertBefore inserts the value immediately before mark and returns a new Element containing the value. -func (l *List) InsertBefore(value interface {}, mark *Element) *Element { +func (l *List) InsertBefore(value interface{}, mark *Element) *Element { if mark.id != l.id { - return nil + return nil; } - e := &Element{ nil, nil, l.id, value }; + e := &Element{nil, nil, l.id, value}; l.insertBefore(e, mark); - return e + return e; } // InsertAfter inserts the value immediately after mark and returns a new Element containing the value. -func (l *List) InsertAfter(value interface {}, mark *Element) *Element { +func (l *List) InsertAfter(value interface{}, mark *Element) *Element { if mark.id != l.id { - return nil + return nil; } - e := &Element{ nil, nil, l.id, value }; + e := &Element{nil, nil, l.id, value}; l.insertAfter(e, mark); - return e + return e; } // MoveToFront moves the element to the front of the list. func (l *List) MoveToFront(e *Element) { if e.id != l.id || l.front == e { - return + return; } l.Remove(e); l.insertFront(e); @@ -180,7 +180,7 @@ func (l *List) MoveToFront(e *Element) { // MoveToBack moves the element to the back of the list. func (l *List) MoveToBack(e *Element) { if e.id != l.id || l.back == e { - return + return; } l.Remove(e); l.insertBack(e); @@ -188,18 +188,18 @@ func (l *List) MoveToBack(e *Element) { // Len returns the number of elements in the list. func (l *List) Len() int { - return l.len + return l.len; } -func (l *List) iterate(c chan<- interface {}) { +func (l *List) iterate(c chan<- interface{}) { for e := l.front; e != nil; e = e.next { c <- e.Value; } close(c); } -func (l *List) Iter() <-chan interface {} { - c := make(chan interface {}); +func (l *List) Iter() <-chan interface{} { + c := make(chan interface{}); go l.iterate(c); - return c + return c; } diff --git a/src/pkg/container/list/list_test.go b/src/pkg/container/list/list_test.go index 741aa5516..5fa9e62c6 100755 --- a/src/pkg/container/list/list_test.go +++ b/src/pkg/container/list/list_test.go @@ -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] { @@ -29,7 +29,7 @@ func checkListPointers(t *testing.T, l *List, es []*Element) { if i > 0 { e_prev = es[i-1]; } - if i < len(es) - 1 { + if i < len(es)-1 { e_next = es[i+1]; } if e.prev != e_prev { @@ -55,11 +55,11 @@ func TestList(t *testing.T) { // Single element list e := l.PushFront("a"); checkListLen(t, l, 1); - checkListPointers(t, l, []*Element{ e }); + checkListPointers(t, l, []*Element{e}); l.MoveToFront(e); - checkListPointers(t, l, []*Element{ e }); + checkListPointers(t, l, []*Element{e}); l.MoveToBack(e); - checkListPointers(t, l, []*Element{ e }); + checkListPointers(t, l, []*Element{e}); checkListLen(t, l, 1); l.Remove(e); checkListPointers(t, l, []*Element{}); @@ -70,48 +70,48 @@ func TestList(t *testing.T) { e1 := l.PushFront(1); e3 := l.PushBack(3); e4 := l.PushBack("banana"); - checkListPointers(t, l, []*Element{ e1, e2, e3, e4 }); + checkListPointers(t, l, []*Element{e1, e2, e3, e4}); checkListLen(t, l, 4); l.Remove(e2); - checkListPointers(t, l, []*Element{ e1, e3, e4 }); + 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.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 }); + 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 }); + 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 }); + 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 }); + 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 }); + 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 }); + e2 = l.InsertAfter(2, e3); // insert after back + checkListPointers(t, l, []*Element{e1, e4, e3, e2}); l.Remove(e2); // Check standard iteration. diff --git a/src/pkg/container/ring/ring.go b/src/pkg/container/ring/ring.go index fc1aef96d..14593f215 100644 --- a/src/pkg/container/ring/ring.go +++ b/src/pkg/container/ring/ring.go @@ -12,8 +12,8 @@ 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 } @@ -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)); } @@ -138,8 +138,8 @@ func (r *Ring) Len() int { } -func (r *Ring) Iter() <-chan interface {} { - c := make(chan interface {}); +func (r *Ring) Iter() <-chan interface{} { + c := make(chan interface{}); go func() { if r != nil { c <- r.Value; @@ -149,5 +149,5 @@ func (r *Ring) Iter() <-chan interface {} { } close(c); }(); - return c + return c; } diff --git a/src/pkg/container/ring/ring_test.go b/src/pkg/container/ring/ring_test.go index ee9ce27a6..0dc3a71d1 100644 --- a/src/pkg/container/ring/ring_test.go +++ b/src/pkg/container/ring/ring_test.go @@ -54,7 +54,7 @@ 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); @@ -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,8 +99,8 @@ 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); @@ -186,13 +186,13 @@ func TestLink2(t *testing.T) { verify(t, r1a, 1, 42); r1a.Link(r1b); - verify(t, r1a, 2, 42 + 77); + verify(t, r1a, 2, 42+77); r10.Link(r0); verify(t, r10, 10, sumN(10)); r10.Link(r1a); - verify(t, r10, 12, sumN(10) + 42 + 77); + verify(t, r10, 12, sumN(10)+42+77); } @@ -220,11 +220,11 @@ func TestUnlink(t *testing.T) { r1 := r10.Unlink(1); verify(t, r1, 1, 2); - verify(t, r10, 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); + verify(t, r9, 9, sum10-2); + verify(t, r10, 9, sum10-2); } diff --git a/src/pkg/container/vector/intvector.go b/src/pkg/container/vector/intvector.go index 950926e10..0ae25b982 100644 --- a/src/pkg/container/vector/intvector.go +++ b/src/pkg/container/vector/intvector.go @@ -22,25 +22,25 @@ func (p *IntVector) Init(len int) *IntVector { // NewIntVector returns an initialized new IntVector with length at least len. func NewIntVector(len int) *IntVector { - return new(IntVector).Init(len) + return new(IntVector).Init(len); } // At returns the i'th element of the vector. func (p *IntVector) At(i int) int { - return p.Vector.At(i).(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 + p.a[i] = x; } // Last returns the element in the vector of highest index. func (p *IntVector) Last() int { - return p.Vector.Last().(int) + return p.Vector.Last().(int); } @@ -48,42 +48,42 @@ func (p *IntVector) Last() int { func (p *IntVector) Data() []int { arr := make([]int, p.Len()); for i, v := range p.a { - arr[i] = v.(int) + 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) + p.Vector.Insert(i, x); } // 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 *IntVector) InsertVector(i int, x *IntVector) { - p.Vector.InsertVector(i, &x.Vector) + p.Vector.InsertVector(i, &x.Vector); } // Slice returns a new IntVector by slicing the old one to extract slice [i:j]. // The elements are copied. The original vector is unchanged. func (p *IntVector) Slice(i, j int) *IntVector { - return &IntVector{ *p.Vector.Slice(i, j) }; + return &IntVector{*p.Vector.Slice(i, j)}; } // Push appends x to the end of the vector. func (p *IntVector) Push(x int) { - p.Vector.Push(x) + p.Vector.Push(x); } // Pop deletes and returns the last element of the vector. func (p *IntVector) Pop() int { - return p.Vector.Pop().(int) + return p.Vector.Pop().(int); } @@ -96,14 +96,14 @@ func (p *IntVector) AppendVector(x *IntVector) { // SortInterface 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) + return p.At(i) < p.At(j); } // Iterate over all elements; driver for range func (p *IntVector) iterate(c chan<- int) { for _, v := range p.a { - c <- v.(int) + c <- v.(int); } close(c); } diff --git a/src/pkg/container/vector/stringvector.go b/src/pkg/container/vector/stringvector.go index cda9760d2..4949d06b7 100644 --- a/src/pkg/container/vector/stringvector.go +++ b/src/pkg/container/vector/stringvector.go @@ -21,25 +21,25 @@ func (p *StringVector) Init(len int) *StringVector { // NewStringVector returns an initialized new StringVector with length at least len. func NewStringVector(len int) *StringVector { - return new(StringVector).Init(len) + return new(StringVector).Init(len); } // At returns the i'th element of the vector. func (p *StringVector) At(i int) string { - return p.Vector.At(i).(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 + p.a[i] = x; } // Last returns the element in the vector of highest index. func (p *StringVector) Last() string { - return p.Vector.Last().(string) + return p.Vector.Last().(string); } @@ -47,42 +47,42 @@ func (p *StringVector) Last() string { func (p *StringVector) Data() []string { arr := make([]string, p.Len()); for i, v := range p.a { - arr[i] = v.(string) + arr[i] = v.(string); } - return arr + return arr; } // Insert inserts into the vector an element of value x before // the current element at index i. func (p *StringVector) Insert(i int, x string) { - p.Vector.Insert(i, x) + p.Vector.Insert(i, x); } // 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 *StringVector) InsertVector(i int, x *StringVector) { - p.Vector.InsertVector(i, &x.Vector) + p.Vector.InsertVector(i, &x.Vector); } // Slice returns a new StringVector by slicing the old one to extract slice [i:j]. // The elements are copied. The original vector is unchanged. func (p *StringVector) Slice(i, j int) *StringVector { - return &StringVector{ *p.Vector.Slice(i, j) }; + return &StringVector{*p.Vector.Slice(i, j)}; } // Push appends x to the end of the vector. func (p *StringVector) Push(x string) { - p.Vector.Push(x) + p.Vector.Push(x); } // Pop deletes and returns the last element of the vector. func (p *StringVector) Pop() string { - return p.Vector.Pop().(string) + return p.Vector.Pop().(string); } @@ -95,14 +95,14 @@ func (p *StringVector) AppendVector(x *StringVector) { // SortInterface 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) + return p.At(i) < p.At(j); } // Iterate over all elements; driver for range func (p *StringVector) iterate(c chan<- string) { for _, v := range p.a { - c <- v.(string) + c <- v.(string); } close(c); } diff --git a/src/pkg/container/vector/vector.go b/src/pkg/container/vector/vector.go index 179152c9e..c29edb83f 100644 --- a/src/pkg/container/vector/vector.go +++ b/src/pkg/container/vector/vector.go @@ -8,19 +8,19 @@ package vector // Element is an empty-interface object representing the contents of // a cell in the vector. -type Element interface {} +type Element interface{} // Vector is the container itself. // The zero value for Vector is an empty vector ready to use. type Vector struct { - a []Element + a []Element; } func copy(dst, src []Element) { for i := 0; i < len(src); i++ { - dst[i] = src[i] + dst[i] = src[i]; } } @@ -29,28 +29,28 @@ func copy(dst, src []Element) { func expand(a []Element, i, n int) []Element { // make sure we have enough space len0 := len(a); - len1 := len0 + n; + len1 := len0+n; if len1 < cap(a) { // enough space - just expand - a = a[0 : len1] + a = a[0:len1]; } else { // not enough space - double capacity capb := cap(a)*2; if capb < len1 { // still not enough - use required length - capb = len1 + capb = len1; } // capb >= len1 b := make([]Element, len1, capb); copy(b, a); - a = b + a = b; } // make a hole - for j := len0-1; j >= i ; j-- { - a[j+n] = a[j] + for j := len0-1; j >= i; j-- { + a[j+n] = a[j]; } - return a + return a; } @@ -61,26 +61,26 @@ func (p *Vector) Init(initial_len int) *Vector { a := p.a; if cap(a) == 0 || cap(a) < initial_len { - n := 8; // initial capacity + n := 8; // initial capacity if initial_len > n { - n = initial_len + n = initial_len; } a = make([]Element, n); } else { // nil out entries - for j := len(a) - 1; j >= 0; j-- { - a[j] = nil + for j := len(a)-1; j >= 0; j-- { + a[j] = nil; } } - p.a = a[0 : initial_len]; - return p + p.a = a[0:initial_len]; + return p; } // New returns an initialized new Vector with length at least len. func New(len int) *Vector { - return new(Vector).Init(len) + return new(Vector).Init(len); } @@ -90,25 +90,25 @@ func (p *Vector) Len() int { if p == nil { return 0; } - return len(p.a) + return len(p.a); } // At returns the i'th element of the vector. func (p *Vector) At(i int) Element { - return p.a[i] + return p.a[i]; } // Set sets the i'th element of the vector to value x. func (p *Vector) Set(i int, x Element) { - p.a[i] = x + p.a[i] = x; } // Last returns the element in the vector of highest index. func (p *Vector) Last() Element { - return p.a[len(p.a) - 1] + return p.a[len(p.a) - 1]; } @@ -116,9 +116,9 @@ func (p *Vector) Last() Element { func (p *Vector) Data() []Element { arr := make([]Element, p.Len()); for i, v := range p.a { - arr[i] = v + arr[i] = v; } - return arr + return arr; } @@ -137,7 +137,7 @@ func (p *Vector) Delete(i int) { n := len(a); copy(a[i : n-1], a[i+1 : n]); - a[n-1] = nil; // support GC, nil out entry + a[n-1] = nil; // support GC, nil out entry p.a = a[0 : n-1]; } @@ -154,22 +154,22 @@ func (p *Vector) InsertVector(i int, x *Vector) { func (p *Vector) Cut(i, j int) { a := p.a; n := len(a); - m := n - (j - i); + 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(j - i); // will fail in Init() if j < j - copy(s.a, p.a[i : j]); + s := New(j-i); // will fail in Init() if j < j + copy(s.a, p.a[i:j]); return s; } @@ -178,7 +178,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 Element)) { 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 } } @@ -187,7 +187,7 @@ func (p *Vector) Do(f func(elem Element)) { // Push appends x to the end of the vector. func (p *Vector) Push(x Element) { - p.Insert(len(p.a), x) + p.Insert(len(p.a), x); } @@ -195,8 +195,8 @@ func (p *Vector) Push(x Element) { func (p *Vector) Pop() Element { i := len(p.a) - 1; x := p.a[i]; - p.a[i] = nil; // support GC, nil out entry - p.a = p.a[0 : i]; + p.a[i] = nil; // support GC, nil out entry + p.a = p.a[0:i]; return x; } @@ -211,27 +211,27 @@ func (p *Vector) AppendVector(x *Vector) { // LessInterface provides partial support of the SortInterface. type LessInterface interface { - Less(y Element) bool + Less(y Element) 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]) + 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[i], a[j] = a[j], a[i]; } // Iterate over all elements; driver for range func (p *Vector) iterate(c chan<- Element) { for _, v := range p.a { - c <- v + c <- v; } close(c); } diff --git a/src/pkg/container/vector/vector_test.go b/src/pkg/container/vector/vector_test.go index c0d67957d..32a21f3eb 100644 --- a/src/pkg/container/vector/vector_test.go +++ b/src/pkg/container/vector/vector_test.go @@ -11,29 +11,45 @@ import "fmt" func TestZeroLen(t *testing.T) { var a *Vector; - if a.Len() != 0 { t.Errorf("A) expected 0, got %d", a.Len()); } + if a.Len() != 0 { + t.Errorf("A) expected 0, got %d", a.Len()); + } a = New(0); - if a.Len() != 0 { t.Errorf("B) expected 0, got %d", a.Len()); } + if a.Len() != 0 { + t.Errorf("B) expected 0, got %d", a.Len()); + } } func TestInit(t *testing.T) { var a Vector; - if a.Init(0).Len() != 0 { t.Error("A") } - if a.Init(1).Len() != 1 { t.Error("B") } - if a.Init(10).Len() != 10 { t.Error("C") } + if a.Init(0).Len() != 0 { + t.Error("A"); + } + if a.Init(1).Len() != 1 { + t.Error("B"); + } + if a.Init(10).Len() != 10 { + t.Error("C"); + } } func TestNew(t *testing.T) { - if New(0).Len() != 0 { t.Error("A") } - if New(1).Len() != 1 { t.Error("B") } - if New(10).Len() != 10 { t.Error("C") } + if New(0).Len() != 0 { + t.Error("A"); + } + if New(1).Len() != 1 { + t.Error("B"); + } + if New(10).Len() != 10 { + t.Error("C"); + } } func val(i int) int { - return i*991 - 1234 + return i*991 - 1234; } @@ -45,7 +61,9 @@ func TestAccess(t *testing.T) { a.Set(i, val(i)); } for i := 0; i < n; i++ { - if a.At(i).(int) != val(i) { t.Error(i) } + if a.At(i).(int) != val(i) { + t.Error(i); + } } } @@ -55,25 +73,43 @@ func TestInsertDeleteClear(t *testing.T) { var a Vector; for i := 0; i < n; i++ { - if a.Len() != i { t.Errorf("A) wrong len %d (expected %d)", a.Len(), i) } + if a.Len() != i { + t.Errorf("A) wrong len %d (expected %d)", a.Len(), i); + } a.Insert(0, val(i)); - if a.Last().(int) != val(0) { t.Error("B") } + if a.Last().(int) != val(0) { + t.Error("B"); + } } for i := n-1; i >= 0; i-- { - if a.Last().(int) != val(0) { t.Error("C") } - if a.At(0).(int) != val(i) { t.Error("D") } + if a.Last().(int) != val(0) { + t.Error("C"); + } + if a.At(0).(int) != val(i) { + t.Error("D"); + } a.Delete(0); - if a.Len() != i { t.Errorf("E) wrong len %d (expected %d)", a.Len(), i) } + if a.Len() != i { + t.Errorf("E) wrong len %d (expected %d)", a.Len(), i); + } } - if a.Len() != 0 { t.Errorf("F) wrong len %d (expected 0)", a.Len()) } + if a.Len() != 0 { + t.Errorf("F) wrong len %d (expected 0)", a.Len()); + } for i := 0; i < n; i++ { a.Push(val(i)); - if a.Len() != i+1 { t.Errorf("G) wrong len %d (expected %d)", a.Len(), i+1) } - if a.Last().(int) != val(i) { t.Error("H") } + if a.Len() != i+1 { + t.Errorf("G) wrong len %d (expected %d)", a.Len(), i+1); + } + if a.Last().(int) != val(i) { + t.Error("H"); + } } a.Init(0); - if a.Len() != 0 { t.Errorf("I wrong len %d (expected 0)", a.Len()) } + if a.Len() != 0 { + t.Errorf("I wrong len %d (expected 0)", a.Len()); + } const m = 5; for j := 0; j < m; j++ { @@ -81,38 +117,44 @@ func TestInsertDeleteClear(t *testing.T) { for i := 0; i < n; i++ { x := val(i); a.Push(x); - if a.Pop().(int) != x { t.Error("J") } - if a.Len() != j+1 { t.Errorf("K) wrong len %d (expected %d)", a.Len(), j+1) } + if a.Pop().(int) != x { + t.Error("J"); + } + if a.Len() != j+1 { + t.Errorf("K) wrong len %d (expected %d)", a.Len(), j+1); + } } } - if a.Len() != m { t.Errorf("L) wrong len %d (expected %d)", a.Len(), m) } + if a.Len() != m { + t.Errorf("L) wrong len %d (expected %d)", a.Len(), m); + } } func verify_slice(t *testing.T, x *Vector, elt, i, j int) { for k := i; k < j; k++ { if x.At(k).(int) != elt { - t.Errorf("M) wrong [%d] element %d (expected %d)", k, x.At(k).(int), elt) + t.Errorf("M) wrong [%d] element %d (expected %d)", k, x.At(k).(int), elt); } } 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) + t.Errorf("N) wrong [%d] element %d (expected %d)", k, x.At(k).(int), elt); } } } 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) + 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, 1, a, a+b); + verify_slice(t, x, 0, a+b, n); } @@ -157,13 +199,17 @@ func TestSorting(t *testing.T) { for i := n-1; i >= 0; i-- { a.Set(i, n-1-i); } - if sort.IsSorted(a) { t.Error("int vector not sorted") } + if sort.IsSorted(a) { + t.Error("int vector not sorted"); + } b := NewStringVector(n); for i := n-1; i >= 0; i-- { b.Set(i, fmt.Sprint(n-1-i)); } - if sort.IsSorted(b) { t.Error("string vector not sorted") } + if sort.IsSorted(b) { + t.Error("string vector not sorted"); + } } @@ -172,20 +218,18 @@ func TestDo(t *testing.T) { const salt = 17; a := NewIntVector(n); for i := 0; i < n; i++ { - a.Set(i, salt * i); + a.Set(i, salt*i); } count := 0; - a.Do( - func(e Element) { - i := e.(int); - if i != count*salt { - t.Error("value at", count, "should be", count*salt, "not", i) - } - count++; + a.Do(func(e Element) { + i := e.(int); + if i != count*salt { + t.Error("value at", count, "should be", count*salt, "not", i); } - ); + count++; + }); if count != n { - t.Error("should visit", n, "values; did visit", count) + t.Error("should visit", n, "values; did visit", count); } } @@ -199,11 +243,11 @@ func TestIter(t *testing.T) { i := 0; for v := range x.Iter() { if v.(int) != i*i { - t.Error("Iter expected", i*i, "got", v.(int)) + t.Error("Iter expected", i*i, "got", v.(int)); } i++; } if i != Len { - t.Error("Iter stopped at", i, "not", Len) + t.Error("Iter stopped at", i, "not", Len); } } |