From de782dd146ede31408b8212f8f5b72457c132387 Mon Sep 17 00:00:00 2001 From: Robert Griesemer <gri@golang.org> Date: Fri, 28 Sep 2012 10:58:46 -0700 Subject: [PATCH] container/list: slightly better code factoring R=golang-dev, adg CC=golang-dev https://golang.org/cl/6569077 --- src/pkg/container/list/list.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/pkg/container/list/list.go b/src/pkg/container/list/list.go index 17f5d17e69..e29e3a79ac 100644 --- a/src/pkg/container/list/list.go +++ b/src/pkg/container/list/list.go @@ -83,8 +83,7 @@ func (l *List) Back() *Element { // lazyInit lazily initializes a zero List value. func (l *List) lazyInit() { if l.root.next == nil { - l.root.next = &l.root - l.root.prev = &l.root + l.Init() } } @@ -100,6 +99,11 @@ func (l *List) insert(e, at *Element) *Element { return e } +// insertValue is a convenience wrapper for insert(&Element{Value: v}, at). +func (l *List) insertValue(v interface{}, at *Element) *Element { + return l.insert(&Element{Value: v}, at) +} + // remove removes e from its list, decrements l.len, and returns e. func (l *List) remove(e *Element) *Element { e.prev.next = e.next @@ -123,13 +127,13 @@ func (l *List) Remove(e *Element) interface{} { // Pushfront inserts a new element e with value v at the front of list l and returns e. func (l *List) PushFront(v interface{}) *Element { l.lazyInit() - return l.insert(&Element{Value: v}, &l.root) + return l.insertValue(v, &l.root) } // PushBack inserts a new element e with value v at the back of list l and returns e. func (l *List) PushBack(v interface{}) *Element { l.lazyInit() - return l.insert(&Element{Value: v}, l.root.prev) + return l.insertValue(v, l.root.prev) } // InsertBefore inserts a new element e with value v immediately before mark and returns e. @@ -139,17 +143,17 @@ func (l *List) InsertBefore(v interface{}, mark *Element) *Element { return nil } // see comment in List.Remove about initialization of l - return l.insert(&Element{Value: v}, mark.prev) + return l.insertValue(v, mark.prev) } // InsertAfter inserts a new element e with value v immediately after mark and returns e. // If mark is not an element of l, the list is not modified. -func (l *List) InsertAfter(value interface{}, mark *Element) *Element { +func (l *List) InsertAfter(v interface{}, mark *Element) *Element { if mark.list != l { return nil } // see comment in List.Remove about initialization of l - return l.insert(&Element{Value: value}, mark) + return l.insertValue(v, mark) } // MoveToFront moves element e to the front of list l. @@ -177,7 +181,7 @@ func (l *List) MoveToBack(e *Element) { func (l *List) PushBackList(other *List) { l.lazyInit() for i, e := other.Len(), other.Front(); i > 0; i, e = i-1, e.Next() { - l.insert(&Element{Value: e.Value}, l.root.prev) + l.insertValue(e.Value, l.root.prev) } } @@ -186,6 +190,6 @@ func (l *List) PushBackList(other *List) { func (l *List) PushFrontList(other *List) { l.lazyInit() for i, e := other.Len(), other.Back(); i > 0; i, e = i-1, e.Prev() { - l.insert(&Element{Value: e.Value}, &l.root) + l.insertValue(e.Value, &l.root) } } -- 2.30.9