3 // Copyright 2021 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 // Package list provides a doubly linked list of some element type
8 // (generic form of the "container/list" package).
17 // Element is an element of a linked list.
18 type _Element[T any] struct {
19 // Next and previous pointers in the doubly-linked list of elements.
20 // To simplify the implementation, internally a list l is implemented
21 // as a ring, such that &l.root is both the next element of the last
22 // list element (l.Back()) and the previous element of the first list
23 // element (l.Front()).
24 next, prev *_Element[T]
26 // The list to which this element belongs.
29 // The value stored with this element.
33 // Next returns the next list element or nil.
34 func (e *_Element[T]) Next() *_Element[T] {
35 if p := e.next; e.list != nil && p != &e.list.root {
41 // Prev returns the previous list element or nil.
42 func (e *_Element[T]) Prev() *_Element[T] {
43 if p := e.prev; e.list != nil && p != &e.list.root {
49 // _List represents a doubly linked list.
50 // The zero value for _List is an empty list ready to use.
51 type _List[T any] struct {
52 root _Element[T] // sentinel list element, only &root, root.prev, and root.next are used
53 len int // current list length excluding (this) sentinel element
56 // Init initializes or clears list l.
57 func (l *_List[T]) Init() *_List[T] {
64 // New returns an initialized list.
65 func _New[T any]() *_List[T] { return new(_List[T]).Init() }
67 // Len returns the number of elements of list l.
68 // The complexity is O(1).
69 func (l *_List[_]) Len() int { return l.len }
71 // Front returns the first element of list l or nil if the list is empty.
72 func (l *_List[T]) Front() *_Element[T] {
79 // Back returns the last element of list l or nil if the list is empty.
80 func (l *_List[T]) Back() *_Element[T] {
87 // lazyInit lazily initializes a zero _List value.
88 func (l *_List[_]) lazyInit() {
89 if l.root.next == nil {
94 // insert inserts e after at, increments l.len, and returns e.
95 func (l *_List[T]) insert(e, at *_Element[T]) *_Element[T] {
105 // insertValue is a convenience wrapper for insert(&_Element[T]{Value: v}, at).
106 func (l *_List[T]) insertValue(v T, at *_Element[T]) *_Element[T] {
107 return l.insert(&_Element[T]{Value: v}, at)
110 // remove removes e from its list, decrements l.len, and returns e.
111 func (l *_List[T]) remove(e *_Element[T]) *_Element[T] {
114 e.next = nil // avoid memory leaks
115 e.prev = nil // avoid memory leaks
121 // move moves e to next to at and returns e.
122 func (l *_List[T]) move(e, at *_Element[T]) *_Element[T] {
137 // Remove removes e from l if e is an element of list l.
138 // It returns the element value e.Value.
139 // The element must not be nil.
140 func (l *_List[T]) Remove(e *_Element[T]) T {
142 // if e.list == l, l must have been initialized when e was inserted
143 // in l or l == nil (e is a zero _Element) and l.remove will crash
149 // PushFront inserts a new element e with value v at the front of list l and returns e.
150 func (l *_List[T]) PushFront(v T) *_Element[T] {
152 return l.insertValue(v, &l.root)
155 // PushBack inserts a new element e with value v at the back of list l and returns e.
156 func (l *_List[T]) PushBack(v T) *_Element[T] {
158 return l.insertValue(v, l.root.prev)
161 // InsertBefore inserts a new element e with value v immediately before mark and returns e.
162 // If mark is not an element of l, the list is not modified.
163 // The mark must not be nil.
164 func (l *_List[T]) InsertBefore(v T, mark *_Element[T]) *_Element[T] {
168 // see comment in _List.Remove about initialization of l
169 return l.insertValue(v, mark.prev)
172 // InsertAfter inserts a new element e with value v immediately after mark and returns e.
173 // If mark is not an element of l, the list is not modified.
174 // The mark must not be nil.
175 func (l *_List[T]) InsertAfter(v T, mark *_Element[T]) *_Element[T] {
179 // see comment in _List.Remove about initialization of l
180 return l.insertValue(v, mark)
183 // MoveToFront moves element e to the front of list l.
184 // If e is not an element of l, the list is not modified.
185 // The element must not be nil.
186 func (l *_List[T]) MoveToFront(e *_Element[T]) {
187 if e.list != l || l.root.next == e {
190 // see comment in _List.Remove about initialization of l
194 // MoveToBack moves element e to the back of list l.
195 // If e is not an element of l, the list is not modified.
196 // The element must not be nil.
197 func (l *_List[T]) MoveToBack(e *_Element[T]) {
198 if e.list != l || l.root.prev == e {
201 // see comment in _List.Remove about initialization of l
202 l.move(e, l.root.prev)
205 // MoveBefore moves element e to its new position before mark.
206 // If e or mark is not an element of l, or e == mark, the list is not modified.
207 // The element and mark must not be nil.
208 func (l *_List[T]) MoveBefore(e, mark *_Element[T]) {
209 if e.list != l || e == mark || mark.list != l {
215 // MoveAfter moves element e to its new position after mark.
216 // If e or mark is not an element of l, or e == mark, the list is not modified.
217 // The element and mark must not be nil.
218 func (l *_List[T]) MoveAfter(e, mark *_Element[T]) {
219 if e.list != l || e == mark || mark.list != l {
225 // PushBackList inserts a copy of an other list at the back of list l.
226 // The lists l and other may be the same. They must not be nil.
227 func (l *_List[T]) PushBackList(other *_List[T]) {
229 for i, e := other.Len(), other.Front(); i > 0; i, e = i-1, e.Next() {
230 l.insertValue(e.Value, l.root.prev)
234 // PushFrontList inserts a copy of an other list at the front of list l.
235 // The lists l and other may be the same. They must not be nil.
236 func (l *_List[T]) PushFrontList(other *_List[T]) {
238 for i, e := other.Len(), other.Back(); i > 0; i, e = i-1, e.Prev() {
239 l.insertValue(e.Value, &l.root)
243 // Transform runs a transform function on a list returning a new list.
244 func _Transform[TElem1, TElem2 any](lst *_List[TElem1], f func(TElem1) TElem2) *_List[TElem2] {
245 ret := _New[TElem2]()
246 for p := lst.Front(); p != nil; p = p.Next() {
247 ret.PushBack(f(p.Value))
252 func checkListLen[T any](l *_List[T], len int) bool {
253 if n := l.Len(); n != len {
254 panic(fmt.Sprintf("l.Len() = %d, want %d", n, len))
260 func checkListPointers[T any](l *_List[T], es []*_Element[T]) {
263 if !checkListLen(l, len(es)) {
267 // zero length lists must be the zero value or properly initialized (sentinel circle)
269 if l.root.next != nil && l.root.next != root || l.root.prev != nil && l.root.prev != root {
270 panic(fmt.Sprintf("l.root.next = %p, l.root.prev = %p; both should both be nil or %p", l.root.next, l.root.prev, root))
276 // check internal and external prev/next connections
277 for i, e := range es {
279 Prev := (*_Element[T])(nil)
284 if p := e.prev; p != prev {
285 panic(fmt.Sprintf("elt[%d](%p).prev = %p, want %p", i, e, p, prev))
287 if p := e.Prev(); p != Prev {
288 panic(fmt.Sprintf("elt[%d](%p).Prev() = %p, want %p", i, e, p, Prev))
292 Next := (*_Element[T])(nil)
297 if n := e.next; n != next {
298 panic(fmt.Sprintf("elt[%d](%p).next = %p, want %p", i, e, n, next))
300 if n := e.Next(); n != Next {
301 panic(fmt.Sprintf("elt[%d](%p).Next() = %p, want %p", i, e, n, Next))
308 checkListPointers(l, []*(_Element[string]){})
310 // Single element list
311 e := l.PushFront("a")
312 checkListPointers(l, []*(_Element[string]){e})
314 checkListPointers(l, []*(_Element[string]){e})
316 checkListPointers(l, []*(_Element[string]){e})
318 checkListPointers(l, []*(_Element[string]){})
322 e2 := l2.PushFront(2)
323 e1 := l2.PushFront(1)
325 e4 := l2.PushBack(600)
326 checkListPointers(l2, []*(_Element[int]){e1, e2, e3, e4})
329 checkListPointers(l2, []*(_Element[int]){e1, e3, e4})
331 l2.MoveToFront(e3) // move from middle
332 checkListPointers(l2, []*(_Element[int]){e3, e1, e4})
335 l2.MoveToBack(e3) // move from middle
336 checkListPointers(l2, []*(_Element[int]){e1, e4, e3})
338 l2.MoveToFront(e3) // move from back
339 checkListPointers(l2, []*(_Element[int]){e3, e1, e4})
340 l2.MoveToFront(e3) // should be no-op
341 checkListPointers(l2, []*(_Element[int]){e3, e1, e4})
343 l2.MoveToBack(e3) // move from front
344 checkListPointers(l2, []*(_Element[int]){e1, e4, e3})
345 l2.MoveToBack(e3) // should be no-op
346 checkListPointers(l2, []*(_Element[int]){e1, e4, e3})
348 e2 = l2.InsertBefore(2, e1) // insert before front
349 checkListPointers(l2, []*(_Element[int]){e2, e1, e4, e3})
351 e2 = l2.InsertBefore(2, e4) // insert before middle
352 checkListPointers(l2, []*(_Element[int]){e1, e2, e4, e3})
354 e2 = l2.InsertBefore(2, e3) // insert before back
355 checkListPointers(l2, []*(_Element[int]){e1, e4, e2, e3})
358 e2 = l2.InsertAfter(2, e1) // insert after front
359 checkListPointers(l2, []*(_Element[int]){e1, e2, e4, e3})
361 e2 = l2.InsertAfter(2, e4) // insert after middle
362 checkListPointers(l2, []*(_Element[int]){e1, e4, e2, e3})
364 e2 = l2.InsertAfter(2, e3) // insert after back
365 checkListPointers(l2, []*(_Element[int]){e1, e4, e3, e2})
368 // Check standard iteration.
370 for e := l2.Front(); e != nil; e = e.Next() {
374 panic(fmt.Sprintf("sum over l = %d, want 604", sum))
377 // Clear all elements by iterating
378 var next *_Element[int]
379 for e := l2.Front(); e != nil; e = next {
383 checkListPointers(l2, []*(_Element[int]){})
386 func checkList[T comparable](l *_List[T], es []interface{}) {
387 if !checkListLen(l, len(es)) {
392 for e := l.Front(); e != nil; e = e.Next() {
394 // Comparison between a generically-typed variable le and an interface.
396 panic(fmt.Sprintf("elt[%d].Value = %v, want %v", i, le, es[i]))
402 func TestExtending() {
415 checkList(l3, []interface{}{1, 2, 3})
417 checkList(l3, []interface{}{1, 2, 3, 4, 5})
421 checkList(l3, []interface{}{4, 5})
423 checkList(l3, []interface{}{1, 2, 3, 4, 5})
425 checkList(l1, []interface{}{1, 2, 3})
426 checkList(l2, []interface{}{4, 5})
430 checkList(l3, []interface{}{1, 2, 3})
432 checkList(l3, []interface{}{1, 2, 3, 1, 2, 3})
436 checkList(l3, []interface{}{1, 2, 3})
438 checkList(l3, []interface{}{1, 2, 3, 1, 2, 3})
442 checkList(l1, []interface{}{1, 2, 3})
444 checkList(l1, []interface{}{1, 2, 3})
451 checkListPointers(l, []*(_Element[int]){e1, e2})
454 checkListPointers(l, []*(_Element[int]){e2})
456 checkListPointers(l, []*(_Element[int]){e2})
459 func TestIssue4103() {
469 l2.Remove(e) // l2 should not change because e is not an element of l2
470 if n := l2.Len(); n != 2 {
471 panic(fmt.Sprintf("l2.Len() = %d, want 2", n))
474 l1.InsertBefore(8, e)
475 if n := l1.Len(); n != 3 {
476 panic(fmt.Sprintf("l1.Len() = %d, want 3", n))
480 func TestIssue6349() {
488 panic(fmt.Sprintf("e.value = %d, want 1", e.Value))
491 panic(fmt.Sprintf("e.Next() != nil"))
494 panic(fmt.Sprintf("e.Prev() != nil"))
506 checkListPointers(l, []*(_Element[int]){e1, e2, e3, e4})
508 checkListPointers(l, []*(_Element[int]){e1, e2, e3, e4})
511 checkListPointers(l, []*(_Element[int]){e1, e2, e3, e4})
513 checkListPointers(l, []*(_Element[int]){e1, e2, e3, e4})
516 checkListPointers(l, []*(_Element[int]){e1, e3, e2, e4})
520 checkListPointers(l, []*(_Element[int]){e4, e1, e2, e3})
521 e1, e2, e3, e4 = e4, e1, e2, e3
524 checkListPointers(l, []*(_Element[int]){e1, e4, e2, e3})
525 e2, e3, e4 = e4, e2, e3
528 checkListPointers(l, []*(_Element[int]){e1, e3, e2, e4})
532 // Test PushFront, PushBack, PushFrontList, PushBackList with uninitialized _List
533 func TestZeroList() {
534 var l1 = new(_List[int])
536 checkList(l1, []interface{}{1})
538 var l2 = new(_List[int])
540 checkList(l2, []interface{}{1})
542 var l3 = new(_List[int])
544 checkList(l3, []interface{}{1})
546 var l4 = new(_List[int])
548 checkList(l4, []interface{}{1})
551 // Test that a list l is not modified when calling InsertBefore with a mark that is not an element of l.
552 func TestInsertBeforeUnknownMark() {
557 l.InsertBefore(1, new(_Element[int]))
558 checkList(&l, []interface{}{1, 2, 3})
561 // Test that a list l is not modified when calling InsertAfter with a mark that is not an element of l.
562 func TestInsertAfterUnknownMark() {
567 l.InsertAfter(1, new(_Element[int]))
568 checkList(&l, []interface{}{1, 2, 3})
571 // Test that a list l is not modified when calling MoveAfter or MoveBefore with a mark that is not an element of l.
572 func TestMoveUnknownMark() {
580 checkList(&l1, []interface{}{1})
581 checkList(&l2, []interface{}{2})
583 l1.MoveBefore(e1, e2)
584 checkList(&l1, []interface{}{1})
585 checkList(&l2, []interface{}{2})
588 // Test the Transform function.
589 func TestTransform() {
593 l2 := _Transform(l1, strconv.Itoa)
594 checkList(l2, []interface{}{"1", "2"})
605 TestInsertBeforeUnknownMark()
606 TestInsertAfterUnknownMark()