list.go

  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[T any] 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[T]
 22
 23	// The list to which this element belongs.
 24	list *List[T]
 25
 26	// The value stored with this element.
 27	Value T
 28}
 29
 30// Next returns the next list element or nil.
 31func (e *Element[T]) Next() *Element[T] {
 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[T]) Prev() *Element[T] {
 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[T any] struct {
 49	root Element[T] // 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[T]) Init() *List[T] {
 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[T any]() *List[T] { return new(List[T]).Init() }
 63
 64// Len returns the number of elements of list l.
 65// The complexity is O(1).
 66func (l *List[T]) 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[T]) Front() *Element[T] {
 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[T]) Back() *Element[T] {
 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[T]) 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[T]) insert(e, at *Element[T]) *Element[T] {
 93	e.prev = at
 94	e.next = at.next
 95	e.prev.next = e
 96	e.next.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[T]) insertValue(v T, at *Element[T]) *Element[T] {
104	return l.insert(&Element[T]{Value: v}, at)
105}
106
107// remove removes e from its list, decrements l.len
108func (l *List[T]) remove(e *Element[T]) {
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}
116
117// move moves e to next to at.
118func (l *List[T]) move(e, at *Element[T]) {
119	if e == at {
120		return
121	}
122	e.prev.next = e.next
123	e.next.prev = e.prev
124
125	e.prev = at
126	e.next = at.next
127	e.prev.next = e
128	e.next.prev = e
129}
130
131// Remove removes e from l if e is an element of list l.
132// It returns the element value e.Value.
133// The element must not be nil.
134func (l *List[T]) Remove(e *Element[T]) T {
135	if e.list == l {
136		// if e.list == l, l must have been initialized when e was inserted
137		// in l or l == nil (e is a zero Element) and l.remove will crash
138		l.remove(e)
139	}
140	return e.Value
141}
142
143// PushFront inserts a new element e with value v at the front of list l and returns e.
144func (l *List[T]) PushFront(v T) *Element[T] {
145	l.lazyInit()
146	return l.insertValue(v, &l.root)
147}
148
149// PushBack inserts a new element e with value v at the back of list l and returns e.
150func (l *List[T]) PushBack(v T) *Element[T] {
151	l.lazyInit()
152	return l.insertValue(v, l.root.prev)
153}
154
155// InsertBefore inserts a new element e with value v immediately before mark and returns e.
156// If mark is not an element of l, the list is not modified.
157// The mark must not be nil.
158func (l *List[T]) InsertBefore(v T, mark *Element[T]) *Element[T] {
159	if mark.list != l {
160		return nil
161	}
162	// see comment in List.Remove about initialization of l
163	return l.insertValue(v, mark.prev)
164}
165
166// InsertAfter inserts a new element e with value v immediately after mark and returns e.
167// If mark is not an element of l, the list is not modified.
168// The mark must not be nil.
169func (l *List[T]) InsertAfter(v T, mark *Element[T]) *Element[T] {
170	if mark.list != l {
171		return nil
172	}
173	// see comment in List.Remove about initialization of l
174	return l.insertValue(v, mark)
175}
176
177// MoveToFront moves element e to the front of list l.
178// If e is not an element of l, the list is not modified.
179// The element must not be nil.
180func (l *List[T]) MoveToFront(e *Element[T]) {
181	if e.list != l || l.root.next == e {
182		return
183	}
184	// see comment in List.Remove about initialization of l
185	l.move(e, &l.root)
186}
187
188// MoveToBack moves element e to the back of list l.
189// If e is not an element of l, the list is not modified.
190// The element must not be nil.
191func (l *List[T]) MoveToBack(e *Element[T]) {
192	if e.list != l || l.root.prev == e {
193		return
194	}
195	// see comment in List.Remove about initialization of l
196	l.move(e, l.root.prev)
197}
198
199// MoveBefore moves element e to its new position before mark.
200// If e or mark is not an element of l, or e == mark, the list is not modified.
201// The element and mark must not be nil.
202func (l *List[T]) MoveBefore(e, mark *Element[T]) {
203	if e.list != l || e == mark || mark.list != l {
204		return
205	}
206	l.move(e, mark.prev)
207}
208
209// MoveAfter moves element e to its new position after mark.
210// If e or mark is not an element of l, or e == mark, the list is not modified.
211// The element and mark must not be nil.
212func (l *List[T]) MoveAfter(e, mark *Element[T]) {
213	if e.list != l || e == mark || mark.list != l {
214		return
215	}
216	l.move(e, mark)
217}
218
219// PushBackList inserts a copy of another list at the back of list l.
220// The lists l and other may be the same. They must not be nil.
221func (l *List[T]) PushBackList(other *List[T]) {
222	l.lazyInit()
223	for i, e := other.Len(), other.Front(); i > 0; i, e = i-1, e.Next() {
224		l.insertValue(e.Value, l.root.prev)
225	}
226}
227
228// PushFrontList inserts a copy of another list at the front of list l.
229// The lists l and other may be the same. They must not be nil.
230func (l *List[T]) PushFrontList(other *List[T]) {
231	l.lazyInit()
232	for i, e := other.Len(), other.Back(); i > 0; i, e = i-1, e.Prev() {
233		l.insertValue(e.Value, &l.root)
234	}
235}