diff options
Diffstat (limited to 'src/pkg/container/list/list.go')
-rw-r--r-- | src/pkg/container/list/list.go | 238 |
1 files changed, 112 insertions, 126 deletions
diff --git a/src/pkg/container/list/list.go b/src/pkg/container/list/list.go index a3fd4b39f..562a5badb 100644 --- a/src/pkg/container/list/list.go +++ b/src/pkg/container/list/list.go @@ -11,201 +11,187 @@ // package list -// Element is an element in the linked list. +// Element is an element of a linked 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. + // To simplify the implementation, internally a list l is implemented + // as a ring, such that &l.root is both the next element of the last + // list element (l.Back()) and the previous element of the first list + // element (l.Front()). next, prev *Element // The list to which this element belongs. list *List - // The contents of this list element. + // The value stored with this element. Value interface{} } // Next returns the next list element or nil. -func (e *Element) Next() *Element { return e.next } +func (e *Element) Next() *Element { + if p := e.next; p != &e.list.root { + return p + } + return nil +} // Prev returns the previous list element or nil. -func (e *Element) Prev() *Element { return e.prev } +func (e *Element) Prev() *Element { + if p := e.prev; p != &e.list.root { + return p + } + return nil +} // 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 + root Element // sentinel list element, only &root, root.prev, and root.next are used + len int // current list length excluding (this) sentinel element } -// Init initializes or clears a List. +// Init initializes or clears list l. func (l *List) Init() *List { - l.front = nil - l.back = nil + l.root.next = &l.root + l.root.prev = &l.root l.len = 0 return l } // New returns an initialized list. -func New() *List { return new(List) } - -// Front returns the first element in the list. -func (l *List) Front() *Element { return l.front } +func New() *List { return new(List).Init() } -// Back returns the last element in the list. -func (l *List) Back() *Element { return l.back } +// Len returns the number of elements of list l. +func (l *List) Len() int { return l.len } -// 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 +// Front returns the first element of list l or nil +func (l *List) Front() *Element { + if l.len == 0 { + return nil + } + return l.root.next } -// 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 { - l.front = e.next - } else { - e.prev.next = e.next - } - if e.next == nil { - l.back = e.prev - } else { - e.next.prev = e.prev +// Back returns the last element of list l or nil. +func (l *List) Back() *Element { + if l.len == 0 { + return nil } - - e.prev = nil - e.next = nil - l.len-- + return l.root.prev } -func (l *List) insertBefore(e *Element, mark *Element) { - if mark.prev == nil { - // new front of the list - l.front = e - } else { - mark.prev.next = e +// lazyInit lazily initializes a zero List value. +func (l *List) lazyInit() { + if l.root.next == nil { + l.Init() } - e.prev = mark.prev - mark.prev = e - e.next = mark - l.len++ } -func (l *List) insertAfter(e *Element, mark *Element) { - if mark.next == nil { - // new back of the list - l.back = e - } else { - mark.next.prev = e - } - e.next = mark.next - mark.next = e - e.prev = mark +// insert inserts e after at, increments l.len, and returns e. +func (l *List) insert(e, at *Element) *Element { + n := at.next + at.next = e + e.prev = at + e.next = n + n.prev = e + e.list = l l.len++ + return e } -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.insertBefore(e, l.front) +// 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) } -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 +// remove removes e from its list, decrements l.len, and returns e. +func (l *List) remove(e *Element) *Element { + e.prev.next = e.next + e.next.prev = e.prev + e.next = nil // avoid memory leaks + e.prev = nil // avoid memory leaks + e.list = nil + l.len-- + return e +} + +// Remove removes e from l if e is an element of list l. +// It returns the element value e.Value. +func (l *List) Remove(e *Element) interface{} { + if e.list == l { + // if e.list == l, l must have been initialized when e was inserted + // in l or l == nil (e is a zero Element) and l.remove will crash + l.remove(e) } - l.insertAfter(e, l.back) + return e.Value } -// 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 { - e := &Element{nil, nil, l, value} - l.insertFront(e) - return e +// 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.insertValue(v, &l.root) } -// 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 { - e := &Element{nil, nil, l, value} - l.insertBack(e) - return e +// 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.insertValue(v, l.root.prev) } -// InsertBefore inserts the value immediately before mark and returns a new Element containing the value. -func (l *List) InsertBefore(value interface{}, mark *Element) *Element { +// InsertBefore inserts a new element e with value v immediately before mark and returns e. +// If mark is not an element of l, the list is not modified. +func (l *List) InsertBefore(v interface{}, mark *Element) *Element { if mark.list != l { return nil } - e := &Element{nil, nil, l, value} - l.insertBefore(e, mark) - return e + // see comment in List.Remove about initialization of l + return l.insertValue(v, mark.prev) } -// InsertAfter inserts the value immediately after mark and returns a new Element containing the value. -func (l *List) InsertAfter(value interface{}, mark *Element) *Element { +// 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(v interface{}, mark *Element) *Element { if mark.list != l { return nil } - e := &Element{nil, nil, l, value} - l.insertAfter(e, mark) - return e + // see comment in List.Remove about initialization of l + return l.insertValue(v, mark) } -// MoveToFront moves the element to the front of the list. +// MoveToFront moves element e to the front of list l. +// If e is not an element of l, the list is not modified. func (l *List) MoveToFront(e *Element) { - if e.list != l || l.front == e { + if e.list != l || l.root.next == e { return } - l.remove(e) - l.insertFront(e) + // see comment in List.Remove about initialization of l + l.insert(l.remove(e), &l.root) } -// MoveToBack moves the element to the back of the list. +// MoveToBack moves element e to the back of list l. +// If e is not an element of l, the list is not modified. func (l *List) MoveToBack(e *Element) { - if e.list != l || l.back == e { + if e.list != l || l.root.prev == e { return } - l.remove(e) - l.insertBack(e) + // see comment in List.Remove about initialization of l + l.insert(l.remove(e), l.root.prev) } -// Len returns the number of elements in the list. -func (l *List) Len() int { return l.len } - -// 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 - } +// PushBackList inserts a copy of an other list at the back of list l. +// The lists l and other may be the same. +func (l *List) PushBackList(other *List) { + l.lazyInit() + for i, e := other.Len(), other.Front(); i > 0; i, e = i-1, e.Next() { + l.insertValue(e.Value, l.root.prev) } } -// 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 - } +// PushFrontList inserts a copy of an other list at the front of list l. +// The lists l and other may be the same. +func (l *List) PushFrontList(other *List) { + l.lazyInit() + for i, e := other.Len(), other.Back(); i > 0; i, e = i-1, e.Prev() { + l.insertValue(e.Value, &l.root) } } |