14
// Element is an element in the linked list.
14
// Element is an element of a linked list.
15
15
type Element struct {
16
16
// Next and previous pointers in the doubly-linked list of elements.
17
// The front of the list has prev = nil, and the back has next = nil.
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()).
18
21
next, prev *Element
20
23
// The list to which this element belongs.
23
// The contents of this list element.
26
// The value stored with this element.
27
30
// Next returns the next list element or nil.
28
func (e *Element) Next() *Element { return e.next }
31
func (e *Element) Next() *Element {
32
if p := e.next; p != &e.list.root {
30
38
// Prev returns the previous list element or nil.
31
func (e *Element) Prev() *Element { return e.prev }
39
func (e *Element) Prev() *Element {
40
if p := e.prev; p != &e.list.root {
33
46
// List represents a doubly linked list.
34
47
// The zero value for List is an empty list ready to use.
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
40
// Init initializes or clears a List.
53
// Init initializes or clears list l.
41
54
func (l *List) Init() *List {
48
61
// New returns an initialized list.
49
func New() *List { return new(List) }
51
// Front returns the first element in the list.
52
func (l *List) Front() *Element { return l.front }
54
// Back returns the last element in the list.
55
func (l *List) Back() *Element { return l.back }
57
// Remove removes the element from the list
58
// and returns its Value.
62
func New() *List { return new(List).Init() }
64
// Len returns the number of elements of list l.
65
func (l *List) Len() int { return l.len }
67
// Front returns the first element of list l or nil
68
func (l *List) Front() *Element {
75
// Back returns the last element of list l or nil.
76
func (l *List) Back() *Element {
83
// lazyInit lazily initializes a zero List value.
84
func (l *List) lazyInit() {
85
if l.root.next == nil {
90
// insert inserts e after at, increments l.len, and returns e.
91
func (l *List) insert(e, at *Element) *Element {
102
// insertValue is a convenience wrapper for insert(&Element{Value: v}, at).
103
func (l *List) insertValue(v interface{}, at *Element) *Element {
104
return l.insert(&Element{Value: v}, at)
107
// remove removes e from its list, decrements l.len, and returns e.
108
func (l *List) remove(e *Element) *Element {
111
e.next = nil // avoid memory leaks
112
e.prev = nil // avoid memory leaks
118
// Remove removes e from l if e is an element of list l.
119
// It returns the element value e.Value.
59
120
func (l *List) Remove(e *Element) interface{} {
61
e.list = nil // do what remove does not
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
65
// remove the element from the list, but do not clear the Element's list field.
66
// This is so that other List methods may use remove when relocating Elements
67
// without needing to restore the list field.
68
func (l *List) remove(e *Element) {
88
func (l *List) insertBefore(e *Element, mark *Element) {
90
// new front of the list
101
func (l *List) insertAfter(e *Element, mark *Element) {
102
if mark.next == nil {
103
// new back of the list
114
func (l *List) insertFront(e *Element) {
117
l.front, l.back = e, e
118
e.prev, e.next = nil, nil
122
l.insertBefore(e, l.front)
125
func (l *List) insertBack(e *Element) {
128
l.front, l.back = e, e
129
e.prev, e.next = nil, nil
133
l.insertAfter(e, l.back)
136
// PushFront inserts the value at the front of the list and returns a new Element containing the value.
137
func (l *List) PushFront(value interface{}) *Element {
138
e := &Element{nil, nil, l, value}
143
// PushBack inserts the value at the back of the list and returns a new Element containing the value.
144
func (l *List) PushBack(value interface{}) *Element {
145
e := &Element{nil, nil, l, value}
150
// InsertBefore inserts the value immediately before mark and returns a new Element containing the value.
151
func (l *List) InsertBefore(value interface{}, mark *Element) *Element {
155
e := &Element{nil, nil, l, value}
156
l.insertBefore(e, mark)
160
// InsertAfter inserts the value immediately after mark and returns a new Element containing the value.
161
func (l *List) InsertAfter(value interface{}, mark *Element) *Element {
165
e := &Element{nil, nil, l, value}
166
l.insertAfter(e, mark)
170
// MoveToFront moves the element to the front of the list.
129
// Pushfront inserts a new element e with value v at the front of list l and returns e.
130
func (l *List) PushFront(v interface{}) *Element {
132
return l.insertValue(v, &l.root)
135
// PushBack inserts a new element e with value v at the back of list l and returns e.
136
func (l *List) PushBack(v interface{}) *Element {
138
return l.insertValue(v, l.root.prev)
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.
143
func (l *List) InsertBefore(v interface{}, mark *Element) *Element {
147
// see comment in List.Remove about initialization of l
148
return l.insertValue(v, mark.prev)
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.
153
func (l *List) InsertAfter(v interface{}, mark *Element) *Element {
157
// see comment in List.Remove about initialization of l
158
return l.insertValue(v, mark)
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.
171
163
func (l *List) MoveToFront(e *Element) {
172
if e.list != l || l.front == e {
164
if e.list != l || l.root.next == e {
167
// see comment in List.Remove about initialization of l
168
l.insert(l.remove(e), &l.root)
179
// MoveToBack moves the element to the back of the list.
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.
180
173
func (l *List) MoveToBack(e *Element) {
181
if e.list != l || l.back == e {
174
if e.list != l || l.root.prev == e {
177
// see comment in List.Remove about initialization of l
178
l.insert(l.remove(e), l.root.prev)
188
// Len returns the number of elements in the list.
189
func (l *List) Len() int { return l.len }
191
// PushBackList inserts each element of ol at the back of the list.
192
func (l *List) PushBackList(ol *List) {
194
for e := ol.Front(); e != nil; e = e.Next() {
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.
183
func (l *List) PushBackList(other *List) {
185
for i, e := other.Len(), other.Front(); i > 0; i, e = i-1, e.Next() {
186
l.insertValue(e.Value, l.root.prev)
202
// PushFrontList inserts each element of ol at the front of the list. The ordering of the passed list is preserved.
203
func (l *List) PushFrontList(ol *List) {
205
for e := ol.Back(); e != nil; e = e.Prev() {
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.
192
func (l *List) PushFrontList(other *List) {
194
for i, e := other.Len(), other.Back(); i > 0; i, e = i-1, e.Prev() {
195
l.insertValue(e.Value, &l.root)