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; e.list != nil && 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; e.list != nil && 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.
65// The complexity is O(1).
66func (l *List) Len() int { return l.len }
67
68// Front returns the first element of list l or nil if the list is empty.
69func (l *List) Front() *Element {
70	if l.len == 0 {
71		return nil
72	}
73	return l.root.next
74}
75
76// Back returns the last element of list l or nil if the list is empty.
77func (l *List) Back() *Element {
78	if l.len == 0 {
79		return nil
80	}
81	return l.root.prev
82}
83
84// lazyInit lazily initializes a zero List value.
85func (l *List) lazyInit() {
86	if l.root.next == nil {
87		l.Init()
88	}
89}
90
91// insert inserts e after at, increments l.len, and returns e.
92func (l *List) insert(e, at *Element) *Element {
93	n := at.next
94	at.next = e
95	e.prev = at
96	e.next = n
97	n.prev = e
98	e.list = l
99	l.len++
100	return e
101}
102
103// insertValue is a convenience wrapper for insert(&Element{Value: v}, at).
104func (l *List) insertValue(v interface{}, at *Element) *Element {
105	return l.insert(&Element{Value: v}, at)
106}
107
108// remove removes e from its list, decrements l.len, and returns e.
109func (l *List) remove(e *Element) *Element {
110	e.prev.next = e.next
111	e.next.prev = e.prev
112	e.next = nil // avoid memory leaks
113	e.prev = nil // avoid memory leaks
114	e.list = nil
115	l.len--
116	return e
117}
118
119// move moves e to next to at and returns e.
120func (l *List) move(e, at *Element) *Element {
121	if e == at {
122		return e
123	}
124	e.prev.next = e.next
125	e.next.prev = e.prev
126
127	n := at.next
128	at.next = e
129	e.prev = at
130	e.next = n
131	n.prev = e
132
133	return e
134}
135
136// Remove removes e from l if e is an element of list l.
137// It returns the element value e.Value.
138// The element must not be nil.
139func (l *List) Remove(e *Element) interface{} {
140	if e.list == l {
141		// if e.list == l, l must have been initialized when e was inserted
142		// in l or l == nil (e is a zero Element) and l.remove will crash
143		l.remove(e)
144	}
145	return e.Value
146}
147
148// PushFront inserts a new element e with value v at the front of list l and returns e.
149func (l *List) PushFront(v interface{}) *Element {
150	l.lazyInit()
151	return l.insertValue(v, &l.root)
152}
153
154// PushBack inserts a new element e with value v at the back of list l and returns e.
155func (l *List) PushBack(v interface{}) *Element {
156	l.lazyInit()
157	return l.insertValue(v, l.root.prev)
158}
159
160// InsertBefore inserts a new element e with value v immediately before mark and returns e.
161// If mark is not an element of l, the list is not modified.
162// The mark must not be nil.
163func (l *List) InsertBefore(v interface{}, mark *Element) *Element {
164	if mark.list != l {
165		return nil
166	}
167	// see comment in List.Remove about initialization of l
168	return l.insertValue(v, mark.prev)
169}
170
171// InsertAfter inserts a new element e with value v immediately after mark and returns e.
172// If mark is not an element of l, the list is not modified.
173// The mark must not be nil.
174func (l *List) InsertAfter(v interface{}, mark *Element) *Element {
175	if mark.list != l {
176		return nil
177	}
178	// see comment in List.Remove about initialization of l
179	return l.insertValue(v, mark)
180}
181
182// MoveToFront moves element e to the front of list l.
183// If e is not an element of l, the list is not modified.
184// The element must not be nil.
185func (l *List) MoveToFront(e *Element) {
186	if e.list != l || l.root.next == e {
187		return
188	}
189	// see comment in List.Remove about initialization of l
190	l.move(e, &l.root)
191}
192
193// MoveToBack moves element e to the back of list l.
194// If e is not an element of l, the list is not modified.
195// The element must not be nil.
196func (l *List) MoveToBack(e *Element) {
197	if e.list != l || l.root.prev == e {
198		return
199	}
200	// see comment in List.Remove about initialization of l
201	l.move(e, l.root.prev)
202}
203
204// MoveBefore moves element e to its new position before mark.
205// If e or mark is not an element of l, or e == mark, the list is not modified.
206// The element and mark must not be nil.
207func (l *List) MoveBefore(e, mark *Element) {
208	if e.list != l || e == mark || mark.list != l {
209		return
210	}
211	l.move(e, mark.prev)
212}
213
214// MoveAfter moves element e to its new position after mark.
215// If e or mark is not an element of l, or e == mark, the list is not modified.
216// The element and mark must not be nil.
217func (l *List) MoveAfter(e, mark *Element) {
218	if e.list != l || e == mark || mark.list != l {
219		return
220	}
221	l.move(e, mark)
222}
223
224// PushBackList inserts a copy of an other list at the back of list l.
225// The lists l and other may be the same. They must not be nil.
226func (l *List) PushBackList(other *List) {
227	l.lazyInit()
228	for i, e := other.Len(), other.Front(); i > 0; i, e = i-1, e.Next() {
229		l.insertValue(e.Value, l.root.prev)
230	}
231}
232
233// PushFrontList inserts a copy of an other list at the front of list l.
234// The lists l and other may be the same. They must not be nil.
235func (l *List) PushFrontList(other *List) {
236	l.lazyInit()
237	for i, e := other.Len(), other.Back(); i > 0; i, e = i-1, e.Prev() {
238		l.insertValue(e.Value, &l.root)
239	}
240}
241