1
0
mirror of https://github.com/golang/go synced 2024-11-22 06:04:39 -07:00

Implemented ExtendFront/Back functions to insert a list of elements into a list.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/181151
This commit is contained in:
Jan H. Hosang 2010-01-15 13:26:13 -08:00 committed by Russ Cox
parent 9a6b8e21e4
commit 67237c0f11
2 changed files with 81 additions and 0 deletions

View File

@ -191,3 +191,25 @@ func (l *List) Iter() <-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()
for e := ol.Front(); e != nil; e = e.Next() {
l.PushBack(e.Value)
if e == last {
break
}
}
}
// PushFrontList inserts each element of ol at the front of the list. The ordering of the passed list is preserved.
func (l *List) PushFrontList(ol *List) {
first := ol.Front()
for e := ol.Back(); e != nil; e = e.Prev() {
l.PushFront(e.Value)
if e == first {
break
}
}
}

View File

@ -134,3 +134,62 @@ func TestList(t *testing.T) {
checkListPointers(t, l, []*Element{})
checkListLen(t, l, 0)
}
func checkList(t *testing.T, l *List, es []interface{}) {
if l.Len() != len(es) {
t.Errorf("list has len=%v, want %v", l.Len(), len(es))
return
}
i := 0
for le := range l.Iter() {
if le != es[i] {
t.Errorf("elt #%d has value=%v, want %v", i, le, es[i])
}
i++
}
}
func TestExtending(t *testing.T) {
l1 := New()
l2 := New()
l1.PushBack(1)
l1.PushBack(2)
l1.PushBack(3)
l2.PushBack(4)
l2.PushBack(5)
l3 := New()
l3.PushBackList(l1)
checkList(t, l3, []interface{}{1, 2, 3})
l3.PushBackList(l2)
checkList(t, l3, []interface{}{1, 2, 3, 4, 5})
l3 = New()
l3.PushFrontList(l2)
checkList(t, l3, []interface{}{4, 5})
l3.PushFrontList(l1)
checkList(t, l3, []interface{}{1, 2, 3, 4, 5})
checkList(t, l1, []interface{}{1, 2, 3})
checkList(t, l2, []interface{}{4, 5})
l3 = New()
l3.PushBackList(l1)
checkList(t, l3, []interface{}{1, 2, 3})
l3.PushBackList(l3)
checkList(t, l3, []interface{}{1, 2, 3, 1, 2, 3})
l3 = New()
l3.PushFrontList(l1)
checkList(t, l3, []interface{}{1, 2, 3})
l3.PushFrontList(l3)
checkList(t, l3, []interface{}{1, 2, 3, 1, 2, 3})
l3 = New()
l1.PushBackList(l3)
checkList(t, l1, []interface{}{1, 2, 3})
l1.PushFrontList(l3)
checkList(t, l1, []interface{}{1, 2, 3})
}