1// Copyright 2009 The Go Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style 3// license that can be found in the LICENSE file. 4 5// Package list implements a doubly linked list. 6// 7// To iterate over a list (where l is a *List): 8// for e := l.Front(); e != nil; e = e.Next() { 9// // do something with e.Value 10// } 11// 12package list 13 14// Element is an element of a linked list. 15type Element struct { 16 // Next and previous pointers in the doubly-linked list of elements. 17 // To simplify the implementation, internally a list l is implemented 18 // as a ring, such that &l.root is both the next element of the last 19 // list element (l.Back()) and the previous element of the first list 20 // element (l.Front()). 21 next, prev *Element 22 23 // The list to which this element belongs. 24 list *List 25 26 // The value stored with this element. 27 Value interface{} 28} 29 30// Next returns the next list element or nil. 31func (e *Element) Next() *Element { 32 if p := e.next; p != &e.list.root { 33 return p 34 } 35 return nil 36} 37 38// Prev returns the previous list element or nil. 39func (e *Element) Prev() *Element { 40 if p := e.prev; p != &e.list.root { 41 return p 42 } 43 return nil 44} 45 46// List represents a doubly linked list. 47// The zero value for List is an empty list ready to use. 48type List struct { 49 root Element // sentinel list element, only &root, root.prev, and root.next are used 50 len int // current list length excluding (this) sentinel element 51} 52 53// Init initializes or clears list l. 54func (l *List) Init() *List { 55 l.root.next = &l.root 56 l.root.prev = &l.root 57 l.len = 0 58 return l 59} 60 61// New returns an initialized list. 62func New() *List { return new(List).Init() } 63 64// Len returns the number of elements of list l. 65func (l *List) Len() int { return l.len } 66 67// Front returns the first element of list l or nil 68func (l *List) Front() *Element { 69 if l.len == 0 { 70 return nil 71 } 72 return l.root.next 73} 74 75// Back returns the last element of list l or nil. 76func (l *List) Back() *Element { 77 if l.len == 0 { 78 return nil 79 } 80 return l.root.prev 81} 82 83// lazyInit lazily initializes a zero List value. 84func (l *List) lazyInit() { 85 if l.root.next == nil { 86 l.Init() 87 } 88} 89 90// insert inserts e after at, increments l.len, and returns e. 91func (l *List) insert(e, at *Element) *Element { 92 n := at.next 93 at.next = e 94 e.prev = at 95 e.next = n 96 n.prev = e 97 e.list = l 98 l.len++ 99 return e 100} 101 102// insertValue is a convenience wrapper for insert(&Element{Value: v}, at). 103func (l *List) insertValue(v interface{}, at *Element) *Element { 104 return l.insert(&Element{Value: v}, at) 105} 106 107// remove removes e from its list, decrements l.len, and returns e. 108func (l *List) remove(e *Element) *Element { 109 e.prev.next = e.next 110 e.next.prev = e.prev 111 e.next = nil // avoid memory leaks 112 e.prev = nil // avoid memory leaks 113 e.list = nil 114 l.len-- 115 return e 116} 117 118// Remove removes e from l if e is an element of list l. 119// It returns the element value e.Value. 120func (l *List) Remove(e *Element) interface{} { 121 if e.list == l { 122 // if e.list == l, l must have been initialized when e was inserted 123 // in l or l == nil (e is a zero Element) and l.remove will crash 124 l.remove(e) 125 } 126 return e.Value 127} 128 129// Pushfront inserts a new element e with value v at the front of list l and returns e. 130func (l *List) PushFront(v interface{}) *Element { 131 l.lazyInit() 132 return l.insertValue(v, &l.root) 133} 134 135// PushBack inserts a new element e with value v at the back of list l and returns e. 136func (l *List) PushBack(v interface{}) *Element { 137 l.lazyInit() 138 return l.insertValue(v, l.root.prev) 139} 140 141// InsertBefore inserts a new element e with value v immediately before mark and returns e. 142// If mark is not an element of l, the list is not modified. 143func (l *List) InsertBefore(v interface{}, mark *Element) *Element { 144 if mark.list != l { 145 return nil 146 } 147 // see comment in List.Remove about initialization of l 148 return l.insertValue(v, mark.prev) 149} 150 151// InsertAfter inserts a new element e with value v immediately after mark and returns e. 152// If mark is not an element of l, the list is not modified. 153func (l *List) InsertAfter(v interface{}, mark *Element) *Element { 154 if mark.list != l { 155 return nil 156 } 157 // see comment in List.Remove about initialization of l 158 return l.insertValue(v, mark) 159} 160 161// MoveToFront moves element e to the front of list l. 162// If e is not an element of l, the list is not modified. 163func (l *List) MoveToFront(e *Element) { 164 if e.list != l || l.root.next == e { 165 return 166 } 167 // see comment in List.Remove about initialization of l 168 l.insert(l.remove(e), &l.root) 169} 170 171// MoveToBack moves element e to the back of list l. 172// If e is not an element of l, the list is not modified. 173func (l *List) MoveToBack(e *Element) { 174 if e.list != l || l.root.prev == e { 175 return 176 } 177 // see comment in List.Remove about initialization of l 178 l.insert(l.remove(e), l.root.prev) 179} 180 181// PushBackList inserts a copy of an other list at the back of list l. 182// The lists l and other may be the same. 183func (l *List) PushBackList(other *List) { 184 l.lazyInit() 185 for i, e := other.Len(), other.Front(); i > 0; i, e = i-1, e.Next() { 186 l.insertValue(e.Value, l.root.prev) 187 } 188} 189 190// PushFrontList inserts a copy of an other list at the front of list l. 191// The lists l and other may be the same. 192func (l *List) PushFrontList(other *List) { 193 l.lazyInit() 194 for i, e := other.Len(), other.Back(); i > 0; i, e = i-1, e.Prev() { 195 l.insertValue(e.Value, &l.root) 196 } 197} 198