diff options
author | Ondřej Surý <ondrej@sury.org> | 2011-01-17 12:40:45 +0100 |
---|---|---|
committer | Ondřej Surý <ondrej@sury.org> | 2011-01-17 12:40:45 +0100 |
commit | 3e45412327a2654a77944249962b3652e6142299 (patch) | |
tree | bc3bf69452afa055423cbe0c5cfa8ca357df6ccf /src/pkg/container | |
parent | c533680039762cacbc37db8dc7eed074c3e497be (diff) | |
download | golang-upstream/2011.01.12.tar.gz |
Imported Upstream version 2011.01.12upstream/2011.01.12
Diffstat (limited to 'src/pkg/container')
-rw-r--r-- | src/pkg/container/heap/Makefile | 2 | ||||
-rw-r--r-- | src/pkg/container/list/Makefile | 2 | ||||
-rw-r--r-- | src/pkg/container/list/list.go | 70 | ||||
-rw-r--r-- | src/pkg/container/list/list_test.go | 24 | ||||
-rw-r--r-- | src/pkg/container/ring/Makefile | 2 | ||||
-rw-r--r-- | src/pkg/container/vector/Makefile | 8 | ||||
-rw-r--r-- | src/pkg/container/vector/intvector.go | 21 | ||||
-rw-r--r-- | src/pkg/container/vector/intvector_test.go | 101 | ||||
-rw-r--r-- | src/pkg/container/vector/numbers_test.go | 2 | ||||
-rw-r--r-- | src/pkg/container/vector/stringvector.go | 21 | ||||
-rw-r--r-- | src/pkg/container/vector/stringvector_test.go | 101 | ||||
-rw-r--r-- | src/pkg/container/vector/vector.go | 21 | ||||
-rw-r--r-- | src/pkg/container/vector/vector_test.go | 101 |
13 files changed, 146 insertions, 330 deletions
diff --git a/src/pkg/container/heap/Makefile b/src/pkg/container/heap/Makefile index 244ebae06..4291d1122 100644 --- a/src/pkg/container/heap/Makefile +++ b/src/pkg/container/heap/Makefile @@ -2,7 +2,7 @@ # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. -include ../../../Make.$(GOARCH) +include ../../../Make.inc TARG=container/heap GOFILES=\ diff --git a/src/pkg/container/list/Makefile b/src/pkg/container/list/Makefile index 2d5b35715..7fcd5f99a 100644 --- a/src/pkg/container/list/Makefile +++ b/src/pkg/container/list/Makefile @@ -2,7 +2,7 @@ # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. -include ../../../Make.$(GOARCH) +include ../../../Make.inc TARG=container/list GOFILES=\ diff --git a/src/pkg/container/list/list.go b/src/pkg/container/list/list.go index 40c968099..c1ebcddaa 100644 --- a/src/pkg/container/list/list.go +++ b/src/pkg/container/list/list.go @@ -3,6 +3,12 @@ // license that can be found in the LICENSE file. // The list package implements a doubly linked list. +// +// To iterate over a list (where l is a *List): +// for e := l.Front(); e != nil; e = e.Next() { +// // do something with e.Value +// } +// package list // Element is an element in the linked list. @@ -11,8 +17,8 @@ type Element struct { // The front of the list has prev = nil, and the back has next = nil. next, prev *Element - // A unique ID for the list to which this element belongs. - id *byte + // The list to which this element belongs. + list *List // The contents of this list element. Value interface{} @@ -25,10 +31,10 @@ func (e *Element) Next() *Element { return e.next } func (e *Element) Prev() *Element { return e.prev } // List represents a doubly linked list. +// The zero value for List is an empty list ready to use. type List struct { front, back *Element len int - id *byte } // Init initializes or clears a List. @@ -36,12 +42,11 @@ func (l *List) Init() *List { 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) } // Front returns the first element in the list. func (l *List) Front() *Element { return l.front } @@ -49,9 +54,19 @@ func (l *List) Front() *Element { return l.front } // Back returns the last element in the list. func (l *List) Back() *Element { return l.back } -// Remove removes the element from the list. -func (l *List) Remove(e *Element) { - if e.id != l.id { +// Remove removes the element from the list +// and returns its Value. +func (l *List) Remove(e *Element) interface{} { + l.remove(e) + e.list = nil // do what remove does not + return e.Value +} + +// remove the element from the list, but do not clear the Element's list field. +// This is so that other List methods may use remove when relocating Elements +// without needing to restore the list field. +func (l *List) remove(e *Element) { + if e.list != l { return } if e.prev == nil { @@ -120,78 +135,59 @@ func (l *List) insertBack(e *Element) { // 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 { - if l.id == nil { - l.Init() - } - e := &Element{nil, nil, l.id, value} + e := &Element{nil, nil, l, value} l.insertFront(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 { - if l.id == nil { - l.Init() - } - e := &Element{nil, nil, l.id, value} + e := &Element{nil, nil, l, value} l.insertBack(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 { - if mark.id != l.id { + if mark.list != l { return nil } - e := &Element{nil, nil, l.id, value} + e := &Element{nil, nil, l, value} l.insertBefore(e, mark) 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 { - if mark.id != l.id { + if mark.list != l { return nil } - e := &Element{nil, nil, l.id, value} + e := &Element{nil, nil, l, value} l.insertAfter(e, mark) 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 { + if e.list != l || l.front == e { return } - l.Remove(e) + l.remove(e) l.insertFront(e) } // MoveToBack moves the element to the back of the list. func (l *List) MoveToBack(e *Element) { - if e.id != l.id || l.back == e { + if e.list != l || l.back == e { return } - l.Remove(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) 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{}) - go l.iterate(c) - return c -} - // PushBackList inserts each element of ol at the back of the list. func (l *List) PushBackList(ol *List) { last := ol.Back() diff --git a/src/pkg/container/list/list_test.go b/src/pkg/container/list/list_test.go index bf35c9dd9..1d44ff84e 100644 --- a/src/pkg/container/list/list_test.go +++ b/src/pkg/container/list/list_test.go @@ -23,8 +23,7 @@ func checkListPointers(t *testing.T, l *List, es []*Element) { t.Errorf("l.back = %v, want %v", l.back, last) } - for i := 0; i < len(es); i++ { - e := es[i] + for i, e := range es { var e_prev, e_next *Element = nil, nil if i > 0 { e_prev = es[i-1] @@ -116,8 +115,8 @@ func TestList(t *testing.T) { // Check standard iteration. sum := 0 - for e := range l.Iter() { - if i, ok := e.(int); ok { + for e := l.Front(); e != nil; e = e.Next() { + if i, ok := e.Value.(int); ok { sum += i } } @@ -141,7 +140,8 @@ func checkList(t *testing.T, l *List, es []interface{}) { return } i := 0 - for le := range l.Iter() { + for e := l.Front(); e != nil; e = e.Next() { + le := e.Value.(int) if le != es[i] { t.Errorf("elt #%d has value=%v, want %v", i, le, es[i]) } @@ -193,3 +193,17 @@ func TestExtending(t *testing.T) { l1.PushFrontList(l3) checkList(t, l1, []interface{}{1, 2, 3}) } + +func TestRemove(t *testing.T) { + l := New() + e1 := l.PushBack(1) + e2 := l.PushBack(2) + checkListPointers(t, l, []*Element{e1, e2}) + e := l.Front() + l.Remove(e) + checkListPointers(t, l, []*Element{e2}) + checkListLen(t, l, 1) + l.Remove(e) + checkListPointers(t, l, []*Element{e2}) + checkListLen(t, l, 1) +} diff --git a/src/pkg/container/ring/Makefile b/src/pkg/container/ring/Makefile index 4755bab07..fb0900774 100644 --- a/src/pkg/container/ring/Makefile +++ b/src/pkg/container/ring/Makefile @@ -2,7 +2,7 @@ # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. -include ../../../Make.$(GOARCH) +include ../../../Make.inc TARG=container/ring GOFILES=\ diff --git a/src/pkg/container/vector/Makefile b/src/pkg/container/vector/Makefile index c456c6a6c..f6b50156f 100644 --- a/src/pkg/container/vector/Makefile +++ b/src/pkg/container/vector/Makefile @@ -2,7 +2,7 @@ # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. -include ../../../Make.$(GOARCH) +include ../../../Make.inc TARG=container/vector GOFILES=\ @@ -42,8 +42,7 @@ generate: vector.go vector_test.go | gofmt -r='make_vector -> make_vectorInt'\ | gofmt -r='TestInsertVector -> TestIntInsertVector'\ | gofmt -r='TestDo -> TestIntDo'\ - | gofmt -r='TestIter -> TestIntIter'\ - | gofmt -r='TestVectorData -> TestIntVectorData'\ + | gofmt -r='TestVectorCopy -> TestIntVectorCopy'\ | gofmt -r='interface{} -> int'\ > intvector_test.go\ @@ -65,7 +64,6 @@ generate: vector.go vector_test.go | gofmt -r='make_vector -> make_vectorStr'\ | gofmt -r='TestInsertVector -> TestStrInsertVector'\ | gofmt -r='TestDo -> TestStrDo'\ - | gofmt -r='TestIter -> TestStrIter'\ - | gofmt -r='TestVectorData -> TestStrVectorData'\ + | gofmt -r='TestVectorCopy -> TestStrVectorCopy'\ | gofmt -r='interface{} -> string'\ > stringvector_test.go diff --git a/src/pkg/container/vector/intvector.go b/src/pkg/container/vector/intvector.go index 6aad358e3..5ad9e294b 100644 --- a/src/pkg/container/vector/intvector.go +++ b/src/pkg/container/vector/intvector.go @@ -104,8 +104,8 @@ func (p *IntVector) Set(i int, x int) { (*p)[i] = x } func (p *IntVector) Last() int { return (*p)[len(*p)-1] } -// Data returns all the elements as a slice. -func (p *IntVector) Data() []int { +// Copy makes a copy of the vector and returns it. +func (p *IntVector) Copy() IntVector { arr := make(IntVector, len(*p)) copy(arr, *p) return arr @@ -199,23 +199,6 @@ func (p *IntVector) Swap(i, j int) { } -// Iterate over all elements; driver for range -func (p *IntVector) iterate(c chan<- int) { - for _, v := range *p { - c <- v - } - close(c) -} - - -// Channel iterator for range. -func (p *IntVector) Iter() <-chan int { - c := make(chan int) - go p.iterate(c) - return c -} - - // Do calls function f for each element of the vector, in order. // The behavior of Do is undefined if f changes *p. func (p *IntVector) Do(f func(elem int)) { diff --git a/src/pkg/container/vector/intvector_test.go b/src/pkg/container/vector/intvector_test.go index c80dd52cc..1e38a1982 100644 --- a/src/pkg/container/vector/intvector_test.go +++ b/src/pkg/container/vector/intvector_test.go @@ -127,59 +127,59 @@ func TestIntInsertDeleteClear(t *testing.T) { for i := 0; i < n; i++ { if a.Len() != i { - t.Errorf("T%: A) wrong Len() %d (expected %d)", a, a.Len(), i) + t.Errorf("%T: A) wrong Len() %d (expected %d)", a, a.Len(), i) } if len(a) != i { - t.Errorf("T%: A) wrong len() %d (expected %d)", a, len(a), i) + t.Errorf("%T: A) wrong len() %d (expected %d)", a, len(a), i) } a.Insert(0, int2IntValue(val(i))) if elem2IntValue(a.Last()) != int2IntValue(val(0)) { - t.Error("T%: B", a) + t.Errorf("%T: B", a) } } for i := n - 1; i >= 0; i-- { if elem2IntValue(a.Last()) != int2IntValue(val(0)) { - t.Error("T%: C", a) + t.Errorf("%T: C", a) } if elem2IntValue(a.At(0)) != int2IntValue(val(i)) { - t.Error("T%: D", a) + t.Errorf("%T: D", a) } if elem2IntValue(a[0]) != int2IntValue(val(i)) { - t.Error("T%: D2", a) + t.Errorf("%T: D2", a) } a.Delete(0) if a.Len() != i { - t.Errorf("T%: E) wrong Len() %d (expected %d)", a, a.Len(), i) + t.Errorf("%T: E) wrong Len() %d (expected %d)", a, a.Len(), i) } if len(a) != i { - t.Errorf("T%: E) wrong len() %d (expected %d)", a, len(a), i) + t.Errorf("%T: E) wrong len() %d (expected %d)", a, len(a), i) } } if a.Len() != 0 { - t.Errorf("T%: F) wrong Len() %d (expected 0)", a, a.Len()) + t.Errorf("%T: F) wrong Len() %d (expected 0)", a, a.Len()) } if len(a) != 0 { - t.Errorf("T%: F) wrong len() %d (expected 0)", a, len(a)) + t.Errorf("%T: F) wrong len() %d (expected 0)", a, len(a)) } for i := 0; i < n; i++ { a.Push(int2IntValue(val(i))) if a.Len() != i+1 { - t.Errorf("T%: G) wrong Len() %d (expected %d)", a, a.Len(), i+1) + t.Errorf("%T: G) wrong Len() %d (expected %d)", a, a.Len(), i+1) } if len(a) != i+1 { - t.Errorf("T%: G) wrong len() %d (expected %d)", a, len(a), i+1) + t.Errorf("%T: G) wrong len() %d (expected %d)", a, len(a), i+1) } if elem2IntValue(a.Last()) != int2IntValue(val(i)) { - t.Error("T%: H", a) + t.Errorf("%T: H", a) } } a.Resize(0, 0) if a.Len() != 0 { - t.Errorf("T%: I wrong Len() %d (expected 0)", a, a.Len()) + t.Errorf("%T: I wrong Len() %d (expected 0)", a, a.Len()) } if len(a) != 0 { - t.Errorf("T%: I wrong len() %d (expected 0)", a, len(a)) + t.Errorf("%T: I wrong len() %d (expected 0)", a, len(a)) } const m = 5 @@ -189,21 +189,21 @@ func TestIntInsertDeleteClear(t *testing.T) { x := val(i) a.Push(int2IntValue(x)) if elem2IntValue(a.Pop()) != int2IntValue(x) { - t.Error("T%: J", a) + t.Errorf("%T: J", a) } if a.Len() != j+1 { - t.Errorf("T%: K) wrong Len() %d (expected %d)", a, a.Len(), j+1) + t.Errorf("%T: K) wrong Len() %d (expected %d)", a, a.Len(), j+1) } if len(a) != j+1 { - t.Errorf("T%: K) wrong len() %d (expected %d)", a, len(a), j+1) + t.Errorf("%T: K) wrong len() %d (expected %d)", a, len(a), j+1) } } } if a.Len() != m { - t.Errorf("T%: L) wrong Len() %d (expected %d)", a, a.Len(), m) + t.Errorf("%T: L) wrong Len() %d (expected %d)", a, a.Len(), m) } if len(a) != m { - t.Errorf("T%: L) wrong len() %d (expected %d)", a, len(a), m) + t.Errorf("%T: L) wrong len() %d (expected %d)", a, len(a), m) } } @@ -211,14 +211,14 @@ func TestIntInsertDeleteClear(t *testing.T) { func verify_sliceInt(t *testing.T, x *IntVector, elt, i, j int) { for k := i; k < j; k++ { if elem2IntValue(x.At(k)) != int2IntValue(elt) { - t.Errorf("T%: M) wrong [%d] element %v (expected %v)", x, k, elem2IntValue(x.At(k)), int2IntValue(elt)) + t.Errorf("%T: M) wrong [%d] element %v (expected %v)", x, k, elem2IntValue(x.At(k)), int2IntValue(elt)) } } s := x.Slice(i, j) for k, n := 0, j-i; k < n; k++ { if elem2IntValue(s.At(k)) != int2IntValue(elt) { - t.Errorf("T%: N) wrong [%d] element %v (expected %v)", x, k, elem2IntValue(x.At(k)), int2IntValue(elt)) + t.Errorf("%T: N) wrong [%d] element %v (expected %v)", x, k, elem2IntValue(x.At(k)), int2IntValue(elt)) } } } @@ -227,10 +227,10 @@ func verify_sliceInt(t *testing.T, x *IntVector, elt, i, j int) { func verify_patternInt(t *testing.T, x *IntVector, a, b, c int) { n := a + b + c if x.Len() != n { - t.Errorf("T%: O) wrong Len() %d (expected %d)", x, x.Len(), n) + t.Errorf("%T: O) wrong Len() %d (expected %d)", x, x.Len(), n) } if len(*x) != n { - t.Errorf("T%: O) wrong len() %d (expected %d)", x, len(*x), n) + t.Errorf("%T: O) wrong len() %d (expected %d)", x, len(*x), n) } verify_sliceInt(t, x, 0, 0, a) verify_sliceInt(t, x, 1, a, a+b) @@ -326,61 +326,14 @@ func TestIntDo(t *testing.T) { } -func TestIntIter(t *testing.T) { - const Len = 100 - x := new(IntVector).Resize(Len, 0) - for i := 0; i < Len; i++ { - x.Set(i, int2IntValue(i*i)) - } - i := 0 - for v := range x.Iter() { - if elem2IntValue(v) != int2IntValue(i*i) { - t.Error(tname(x), "Iter expected", i*i, "got", elem2IntValue(v)) - } - i++ - } - if i != Len { - t.Error(tname(x), "Iter stopped at", i, "not", Len) - } - y := new(IntVector).Resize(Len, 0) - for i := 0; i < Len; i++ { - (*y)[i] = int2IntValue(i * i) - } - i = 0 - for v := range y.Iter() { - if elem2IntValue(v) != int2IntValue(i*i) { - t.Error(tname(y), "y, Iter expected", i*i, "got", elem2IntValue(v)) - } - i++ - } - if i != Len { - t.Error(tname(y), "y, Iter stopped at", i, "not", Len) - } - var z IntVector - z.Resize(Len, 0) - for i := 0; i < Len; i++ { - z[i] = int2IntValue(i * i) - } - i = 0 - for v := range z.Iter() { - if elem2IntValue(v) != int2IntValue(i*i) { - t.Error(tname(z), "z, Iter expected", i*i, "got", elem2IntValue(v)) - } - i++ - } - if i != Len { - t.Error(tname(z), "z, Iter stopped at", i, "not", Len) - } -} - -func TestIntVectorData(t *testing.T) { - // verify Data() returns a slice of a copy, not a slice of the original vector +func TestIntVectorCopy(t *testing.T) { + // verify Copy() returns a copy, not simply a slice of the original vector const Len = 10 var src IntVector for i := 0; i < Len; i++ { src.Push(int2IntValue(i * i)) } - dest := src.Data() + dest := src.Copy() for i := 0; i < Len; i++ { src[i] = int2IntValue(-1) v := elem2IntValue(dest[i]) diff --git a/src/pkg/container/vector/numbers_test.go b/src/pkg/container/vector/numbers_test.go index a44242f67..93335ca60 100644 --- a/src/pkg/container/vector/numbers_test.go +++ b/src/pkg/container/vector/numbers_test.go @@ -20,7 +20,7 @@ func s(n uint64) string { lens := len(str) a := make([]string, (lens+2)/3) start := lens - for i, _ := range a { + for i := range a { start -= 3 if start < 0 { start = 0 diff --git a/src/pkg/container/vector/stringvector.go b/src/pkg/container/vector/stringvector.go index ddc030f81..852685f5a 100644 --- a/src/pkg/container/vector/stringvector.go +++ b/src/pkg/container/vector/stringvector.go @@ -104,8 +104,8 @@ func (p *StringVector) Set(i int, x string) { (*p)[i] = x } func (p *StringVector) Last() string { return (*p)[len(*p)-1] } -// Data returns all the elements as a slice. -func (p *StringVector) Data() []string { +// Copy makes a copy of the vector and returns it. +func (p *StringVector) Copy() StringVector { arr := make(StringVector, len(*p)) copy(arr, *p) return arr @@ -199,23 +199,6 @@ func (p *StringVector) Swap(i, j int) { } -// Iterate over all elements; driver for range -func (p *StringVector) iterate(c chan<- string) { - for _, v := range *p { - c <- v - } - close(c) -} - - -// Channel iterator for range. -func (p *StringVector) Iter() <-chan string { - c := make(chan string) - go p.iterate(c) - return c -} - - // Do calls function f for each element of the vector, in order. // The behavior of Do is undefined if f changes *p. func (p *StringVector) Do(f func(elem string)) { diff --git a/src/pkg/container/vector/stringvector_test.go b/src/pkg/container/vector/stringvector_test.go index 859dac2fd..776ae26de 100644 --- a/src/pkg/container/vector/stringvector_test.go +++ b/src/pkg/container/vector/stringvector_test.go @@ -127,59 +127,59 @@ func TestStrInsertDeleteClear(t *testing.T) { for i := 0; i < n; i++ { if a.Len() != i { - t.Errorf("T%: A) wrong Len() %d (expected %d)", a, a.Len(), i) + t.Errorf("%T: A) wrong Len() %d (expected %d)", a, a.Len(), i) } if len(a) != i { - t.Errorf("T%: A) wrong len() %d (expected %d)", a, len(a), i) + t.Errorf("%T: A) wrong len() %d (expected %d)", a, len(a), i) } a.Insert(0, int2StrValue(val(i))) if elem2StrValue(a.Last()) != int2StrValue(val(0)) { - t.Error("T%: B", a) + t.Errorf("%T: B", a) } } for i := n - 1; i >= 0; i-- { if elem2StrValue(a.Last()) != int2StrValue(val(0)) { - t.Error("T%: C", a) + t.Errorf("%T: C", a) } if elem2StrValue(a.At(0)) != int2StrValue(val(i)) { - t.Error("T%: D", a) + t.Errorf("%T: D", a) } if elem2StrValue(a[0]) != int2StrValue(val(i)) { - t.Error("T%: D2", a) + t.Errorf("%T: D2", a) } a.Delete(0) if a.Len() != i { - t.Errorf("T%: E) wrong Len() %d (expected %d)", a, a.Len(), i) + t.Errorf("%T: E) wrong Len() %d (expected %d)", a, a.Len(), i) } if len(a) != i { - t.Errorf("T%: E) wrong len() %d (expected %d)", a, len(a), i) + t.Errorf("%T: E) wrong len() %d (expected %d)", a, len(a), i) } } if a.Len() != 0 { - t.Errorf("T%: F) wrong Len() %d (expected 0)", a, a.Len()) + t.Errorf("%T: F) wrong Len() %d (expected 0)", a, a.Len()) } if len(a) != 0 { - t.Errorf("T%: F) wrong len() %d (expected 0)", a, len(a)) + t.Errorf("%T: F) wrong len() %d (expected 0)", a, len(a)) } for i := 0; i < n; i++ { a.Push(int2StrValue(val(i))) if a.Len() != i+1 { - t.Errorf("T%: G) wrong Len() %d (expected %d)", a, a.Len(), i+1) + t.Errorf("%T: G) wrong Len() %d (expected %d)", a, a.Len(), i+1) } if len(a) != i+1 { - t.Errorf("T%: G) wrong len() %d (expected %d)", a, len(a), i+1) + t.Errorf("%T: G) wrong len() %d (expected %d)", a, len(a), i+1) } if elem2StrValue(a.Last()) != int2StrValue(val(i)) { - t.Error("T%: H", a) + t.Errorf("%T: H", a) } } a.Resize(0, 0) if a.Len() != 0 { - t.Errorf("T%: I wrong Len() %d (expected 0)", a, a.Len()) + t.Errorf("%T: I wrong Len() %d (expected 0)", a, a.Len()) } if len(a) != 0 { - t.Errorf("T%: I wrong len() %d (expected 0)", a, len(a)) + t.Errorf("%T: I wrong len() %d (expected 0)", a, len(a)) } const m = 5 @@ -189,21 +189,21 @@ func TestStrInsertDeleteClear(t *testing.T) { x := val(i) a.Push(int2StrValue(x)) if elem2StrValue(a.Pop()) != int2StrValue(x) { - t.Error("T%: J", a) + t.Errorf("%T: J", a) } if a.Len() != j+1 { - t.Errorf("T%: K) wrong Len() %d (expected %d)", a, a.Len(), j+1) + t.Errorf("%T: K) wrong Len() %d (expected %d)", a, a.Len(), j+1) } if len(a) != j+1 { - t.Errorf("T%: K) wrong len() %d (expected %d)", a, len(a), j+1) + t.Errorf("%T: K) wrong len() %d (expected %d)", a, len(a), j+1) } } } if a.Len() != m { - t.Errorf("T%: L) wrong Len() %d (expected %d)", a, a.Len(), m) + t.Errorf("%T: L) wrong Len() %d (expected %d)", a, a.Len(), m) } if len(a) != m { - t.Errorf("T%: L) wrong len() %d (expected %d)", a, len(a), m) + t.Errorf("%T: L) wrong len() %d (expected %d)", a, len(a), m) } } @@ -211,14 +211,14 @@ func TestStrInsertDeleteClear(t *testing.T) { func verify_sliceStr(t *testing.T, x *StringVector, elt, i, j int) { for k := i; k < j; k++ { if elem2StrValue(x.At(k)) != int2StrValue(elt) { - t.Errorf("T%: M) wrong [%d] element %v (expected %v)", x, k, elem2StrValue(x.At(k)), int2StrValue(elt)) + t.Errorf("%T: M) wrong [%d] element %v (expected %v)", x, k, elem2StrValue(x.At(k)), int2StrValue(elt)) } } s := x.Slice(i, j) for k, n := 0, j-i; k < n; k++ { if elem2StrValue(s.At(k)) != int2StrValue(elt) { - t.Errorf("T%: N) wrong [%d] element %v (expected %v)", x, k, elem2StrValue(x.At(k)), int2StrValue(elt)) + t.Errorf("%T: N) wrong [%d] element %v (expected %v)", x, k, elem2StrValue(x.At(k)), int2StrValue(elt)) } } } @@ -227,10 +227,10 @@ func verify_sliceStr(t *testing.T, x *StringVector, elt, i, j int) { func verify_patternStr(t *testing.T, x *StringVector, a, b, c int) { n := a + b + c if x.Len() != n { - t.Errorf("T%: O) wrong Len() %d (expected %d)", x, x.Len(), n) + t.Errorf("%T: O) wrong Len() %d (expected %d)", x, x.Len(), n) } if len(*x) != n { - t.Errorf("T%: O) wrong len() %d (expected %d)", x, len(*x), n) + t.Errorf("%T: O) wrong len() %d (expected %d)", x, len(*x), n) } verify_sliceStr(t, x, 0, 0, a) verify_sliceStr(t, x, 1, a, a+b) @@ -326,61 +326,14 @@ func TestStrDo(t *testing.T) { } -func TestStrIter(t *testing.T) { - const Len = 100 - x := new(StringVector).Resize(Len, 0) - for i := 0; i < Len; i++ { - x.Set(i, int2StrValue(i*i)) - } - i := 0 - for v := range x.Iter() { - if elem2StrValue(v) != int2StrValue(i*i) { - t.Error(tname(x), "Iter expected", i*i, "got", elem2StrValue(v)) - } - i++ - } - if i != Len { - t.Error(tname(x), "Iter stopped at", i, "not", Len) - } - y := new(StringVector).Resize(Len, 0) - for i := 0; i < Len; i++ { - (*y)[i] = int2StrValue(i * i) - } - i = 0 - for v := range y.Iter() { - if elem2StrValue(v) != int2StrValue(i*i) { - t.Error(tname(y), "y, Iter expected", i*i, "got", elem2StrValue(v)) - } - i++ - } - if i != Len { - t.Error(tname(y), "y, Iter stopped at", i, "not", Len) - } - var z StringVector - z.Resize(Len, 0) - for i := 0; i < Len; i++ { - z[i] = int2StrValue(i * i) - } - i = 0 - for v := range z.Iter() { - if elem2StrValue(v) != int2StrValue(i*i) { - t.Error(tname(z), "z, Iter expected", i*i, "got", elem2StrValue(v)) - } - i++ - } - if i != Len { - t.Error(tname(z), "z, Iter stopped at", i, "not", Len) - } -} - -func TestStrVectorData(t *testing.T) { - // verify Data() returns a slice of a copy, not a slice of the original vector +func TestStrVectorCopy(t *testing.T) { + // verify Copy() returns a copy, not simply a slice of the original vector const Len = 10 var src StringVector for i := 0; i < Len; i++ { src.Push(int2StrValue(i * i)) } - dest := src.Data() + dest := src.Copy() for i := 0; i < Len; i++ { src[i] = int2StrValue(-1) v := elem2StrValue(dest[i]) diff --git a/src/pkg/container/vector/vector.go b/src/pkg/container/vector/vector.go index 986321b14..f43e4d23c 100644 --- a/src/pkg/container/vector/vector.go +++ b/src/pkg/container/vector/vector.go @@ -104,8 +104,8 @@ func (p *Vector) Set(i int, x interface{}) { (*p)[i] = x } func (p *Vector) Last() interface{} { return (*p)[len(*p)-1] } -// Data returns all the elements as a slice. -func (p *Vector) Data() []interface{} { +// Copy makes a copy of the vector and returns it. +func (p *Vector) Copy() Vector { arr := make(Vector, len(*p)) copy(arr, *p) return arr @@ -199,23 +199,6 @@ func (p *Vector) Swap(i, j int) { } -// Iterate over all elements; driver for range -func (p *Vector) iterate(c chan<- interface{}) { - for _, v := range *p { - c <- v - } - close(c) -} - - -// Channel iterator for range. -func (p *Vector) Iter() <-chan interface{} { - c := make(chan interface{}) - go p.iterate(c) - return c -} - - // Do calls function f for each element of the vector, in order. // The behavior of Do is undefined if f changes *p. func (p *Vector) Do(f func(elem interface{})) { diff --git a/src/pkg/container/vector/vector_test.go b/src/pkg/container/vector/vector_test.go index 158b34479..a9c4ceb55 100644 --- a/src/pkg/container/vector/vector_test.go +++ b/src/pkg/container/vector/vector_test.go @@ -127,59 +127,59 @@ func TestInsertDeleteClear(t *testing.T) { for i := 0; i < n; i++ { if a.Len() != i { - t.Errorf("T%: A) wrong Len() %d (expected %d)", a, a.Len(), i) + t.Errorf("%T: A) wrong Len() %d (expected %d)", a, a.Len(), i) } if len(a) != i { - t.Errorf("T%: A) wrong len() %d (expected %d)", a, len(a), i) + t.Errorf("%T: A) wrong len() %d (expected %d)", a, len(a), i) } a.Insert(0, int2Value(val(i))) if elem2Value(a.Last()) != int2Value(val(0)) { - t.Error("T%: B", a) + t.Errorf("%T: B", a) } } for i := n - 1; i >= 0; i-- { if elem2Value(a.Last()) != int2Value(val(0)) { - t.Error("T%: C", a) + t.Errorf("%T: C", a) } if elem2Value(a.At(0)) != int2Value(val(i)) { - t.Error("T%: D", a) + t.Errorf("%T: D", a) } if elem2Value(a[0]) != int2Value(val(i)) { - t.Error("T%: D2", a) + t.Errorf("%T: D2", a) } a.Delete(0) if a.Len() != i { - t.Errorf("T%: E) wrong Len() %d (expected %d)", a, a.Len(), i) + t.Errorf("%T: E) wrong Len() %d (expected %d)", a, a.Len(), i) } if len(a) != i { - t.Errorf("T%: E) wrong len() %d (expected %d)", a, len(a), i) + t.Errorf("%T: E) wrong len() %d (expected %d)", a, len(a), i) } } if a.Len() != 0 { - t.Errorf("T%: F) wrong Len() %d (expected 0)", a, a.Len()) + t.Errorf("%T: F) wrong Len() %d (expected 0)", a, a.Len()) } if len(a) != 0 { - t.Errorf("T%: F) wrong len() %d (expected 0)", a, len(a)) + t.Errorf("%T: F) wrong len() %d (expected 0)", a, len(a)) } for i := 0; i < n; i++ { a.Push(int2Value(val(i))) if a.Len() != i+1 { - t.Errorf("T%: G) wrong Len() %d (expected %d)", a, a.Len(), i+1) + t.Errorf("%T: G) wrong Len() %d (expected %d)", a, a.Len(), i+1) } if len(a) != i+1 { - t.Errorf("T%: G) wrong len() %d (expected %d)", a, len(a), i+1) + t.Errorf("%T: G) wrong len() %d (expected %d)", a, len(a), i+1) } if elem2Value(a.Last()) != int2Value(val(i)) { - t.Error("T%: H", a) + t.Errorf("%T: H", a) } } a.Resize(0, 0) if a.Len() != 0 { - t.Errorf("T%: I wrong Len() %d (expected 0)", a, a.Len()) + t.Errorf("%T: I wrong Len() %d (expected 0)", a, a.Len()) } if len(a) != 0 { - t.Errorf("T%: I wrong len() %d (expected 0)", a, len(a)) + t.Errorf("%T: I wrong len() %d (expected 0)", a, len(a)) } const m = 5 @@ -189,21 +189,21 @@ func TestInsertDeleteClear(t *testing.T) { x := val(i) a.Push(int2Value(x)) if elem2Value(a.Pop()) != int2Value(x) { - t.Error("T%: J", a) + t.Errorf("%T: J", a) } if a.Len() != j+1 { - t.Errorf("T%: K) wrong Len() %d (expected %d)", a, a.Len(), j+1) + t.Errorf("%T: K) wrong Len() %d (expected %d)", a, a.Len(), j+1) } if len(a) != j+1 { - t.Errorf("T%: K) wrong len() %d (expected %d)", a, len(a), j+1) + t.Errorf("%T: K) wrong len() %d (expected %d)", a, len(a), j+1) } } } if a.Len() != m { - t.Errorf("T%: L) wrong Len() %d (expected %d)", a, a.Len(), m) + t.Errorf("%T: L) wrong Len() %d (expected %d)", a, a.Len(), m) } if len(a) != m { - t.Errorf("T%: L) wrong len() %d (expected %d)", a, len(a), m) + t.Errorf("%T: L) wrong len() %d (expected %d)", a, len(a), m) } } @@ -211,14 +211,14 @@ func TestInsertDeleteClear(t *testing.T) { func verify_slice(t *testing.T, x *Vector, elt, i, j int) { for k := i; k < j; k++ { if elem2Value(x.At(k)) != int2Value(elt) { - t.Errorf("T%: M) wrong [%d] element %v (expected %v)", x, k, elem2Value(x.At(k)), int2Value(elt)) + t.Errorf("%T: M) wrong [%d] element %v (expected %v)", x, k, elem2Value(x.At(k)), int2Value(elt)) } } s := x.Slice(i, j) for k, n := 0, j-i; k < n; k++ { if elem2Value(s.At(k)) != int2Value(elt) { - t.Errorf("T%: N) wrong [%d] element %v (expected %v)", x, k, elem2Value(x.At(k)), int2Value(elt)) + t.Errorf("%T: N) wrong [%d] element %v (expected %v)", x, k, elem2Value(x.At(k)), int2Value(elt)) } } } @@ -227,10 +227,10 @@ 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 if x.Len() != n { - t.Errorf("T%: O) wrong Len() %d (expected %d)", x, x.Len(), n) + t.Errorf("%T: O) wrong Len() %d (expected %d)", x, x.Len(), n) } if len(*x) != n { - t.Errorf("T%: O) wrong len() %d (expected %d)", x, len(*x), n) + t.Errorf("%T: O) wrong len() %d (expected %d)", x, len(*x), n) } verify_slice(t, x, 0, 0, a) verify_slice(t, x, 1, a, a+b) @@ -326,61 +326,14 @@ func TestDo(t *testing.T) { } -func TestIter(t *testing.T) { - const Len = 100 - x := new(Vector).Resize(Len, 0) - for i := 0; i < Len; i++ { - x.Set(i, int2Value(i*i)) - } - i := 0 - for v := range x.Iter() { - if elem2Value(v) != int2Value(i*i) { - t.Error(tname(x), "Iter expected", i*i, "got", elem2Value(v)) - } - i++ - } - if i != Len { - t.Error(tname(x), "Iter stopped at", i, "not", Len) - } - y := new(Vector).Resize(Len, 0) - for i := 0; i < Len; i++ { - (*y)[i] = int2Value(i * i) - } - i = 0 - for v := range y.Iter() { - if elem2Value(v) != int2Value(i*i) { - t.Error(tname(y), "y, Iter expected", i*i, "got", elem2Value(v)) - } - i++ - } - if i != Len { - t.Error(tname(y), "y, Iter stopped at", i, "not", Len) - } - var z Vector - z.Resize(Len, 0) - for i := 0; i < Len; i++ { - z[i] = int2Value(i * i) - } - i = 0 - for v := range z.Iter() { - if elem2Value(v) != int2Value(i*i) { - t.Error(tname(z), "z, Iter expected", i*i, "got", elem2Value(v)) - } - i++ - } - if i != Len { - t.Error(tname(z), "z, Iter stopped at", i, "not", Len) - } -} - -func TestVectorData(t *testing.T) { - // verify Data() returns a slice of a copy, not a slice of the original vector +func TestVectorCopy(t *testing.T) { + // verify Copy() returns a copy, not simply a slice of the original vector const Len = 10 var src Vector for i := 0; i < Len; i++ { src.Push(int2Value(i * i)) } - dest := src.Data() + dest := src.Copy() for i := 0; i < Len; i++ { src[i] = int2Value(-1) v := elem2Value(dest[i]) |