|
| 1 | +/* |
| 2 | + * Copyright 2025 The Go-Spring Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package listutil |
| 18 | + |
| 19 | +import ( |
| 20 | + "container/list" |
| 21 | +) |
| 22 | + |
| 23 | +// Element is an element of a linked list. |
| 24 | +type Element[T any] struct { |
| 25 | + *list.Element |
| 26 | +} |
| 27 | + |
| 28 | +// Valid returns true if e is a valid element of list l. |
| 29 | +func (e Element[T]) Valid() bool { |
| 30 | + return e.Element != nil |
| 31 | +} |
| 32 | + |
| 33 | +// Value returns the value of element e. |
| 34 | +func (e Element[T]) Value() T { |
| 35 | + return e.Element.Value.(T) |
| 36 | +} |
| 37 | + |
| 38 | +// Next returns the next element of list l or nil if e is the last element. |
| 39 | +func (e Element[T]) Next() Element[T] { |
| 40 | + return Element[T]{e.Element.Next()} |
| 41 | +} |
| 42 | + |
| 43 | +// Prev returns the previous element of list l or nil if e is the first element. |
| 44 | +func (e Element[T]) Prev() Element[T] { |
| 45 | + return Element[T]{e.Element.Prev()} |
| 46 | +} |
| 47 | + |
| 48 | +// List is a doubly linked list. |
| 49 | +type List[T any] struct { |
| 50 | + *list.List |
| 51 | +} |
| 52 | + |
| 53 | +// New returns an empty list. |
| 54 | +func New[T any]() *List[T] { |
| 55 | + return &List[T]{List: list.New()} |
| 56 | +} |
| 57 | + |
| 58 | +// Len returns the number of elements of list l. |
| 59 | +// The complexity is O(1). |
| 60 | +func (l *List[T]) Len() int { return l.List.Len() } |
| 61 | + |
| 62 | +// Front returns the first element of list l or nil if the list is empty. |
| 63 | +func (l *List[T]) Front() Element[T] { |
| 64 | + return Element[T]{l.List.Front()} |
| 65 | +} |
| 66 | + |
| 67 | +// Back returns the last element of list l or nil if the list is empty. |
| 68 | +func (l *List[T]) Back() Element[T] { |
| 69 | + return Element[T]{l.List.Back()} |
| 70 | +} |
| 71 | + |
| 72 | +// Remove removes e from l if e is an element of list l. |
| 73 | +// It returns the element value e.Value. |
| 74 | +// The element must not be nil. |
| 75 | +func (l *List[T]) Remove(e Element[T]) T { |
| 76 | + return l.List.Remove(e.Element).(T) |
| 77 | +} |
| 78 | + |
| 79 | +// PushFront inserts a new element e with value v at the front of list l and returns e. |
| 80 | +func (l *List[T]) PushFront(v T) Element[T] { |
| 81 | + return Element[T]{l.List.PushFront(v)} |
| 82 | +} |
| 83 | + |
| 84 | +// PushBack inserts a new element e with value v at the back of list l and returns e. |
| 85 | +func (l *List[T]) PushBack(v T) Element[T] { |
| 86 | + return Element[T]{l.List.PushBack(v)} |
| 87 | +} |
| 88 | + |
| 89 | +// InsertBefore inserts a new element e with value v immediately before mark and returns e. |
| 90 | +// If mark is not an element of l, the list is not modified. |
| 91 | +// The mark must not be nil. |
| 92 | +func (l *List[T]) InsertBefore(v T, mark Element[T]) Element[T] { |
| 93 | + return Element[T]{l.List.InsertBefore(v, mark.Element)} |
| 94 | +} |
| 95 | + |
| 96 | +// InsertAfter inserts a new element e with value v immediately after mark and returns e. |
| 97 | +// If mark is not an element of l, the list is not modified. |
| 98 | +// The mark must not be nil. |
| 99 | +func (l *List[T]) InsertAfter(v T, mark Element[T]) Element[T] { |
| 100 | + return Element[T]{l.List.InsertAfter(v, mark.Element)} |
| 101 | +} |
| 102 | + |
| 103 | +// MoveToFront moves element e to the front of list l. |
| 104 | +// If e is not an element of l, the list is not modified. |
| 105 | +// The element must not be nil. |
| 106 | +func (l *List[T]) MoveToFront(e Element[T]) { |
| 107 | + l.List.MoveToFront(e.Element) |
| 108 | +} |
| 109 | + |
| 110 | +// MoveToBack moves element e to the back of list l. |
| 111 | +// If e is not an element of l, the list is not modified. |
| 112 | +// The element must not be nil. |
| 113 | +func (l *List[T]) MoveToBack(e Element[T]) { |
| 114 | + l.List.MoveToBack(e.Element) |
| 115 | +} |
| 116 | + |
| 117 | +// MoveBefore moves element e to its new position before mark. |
| 118 | +// If e or mark is not an element of l, or e == mark, the list is not modified. |
| 119 | +// The element and mark must not be nil. |
| 120 | +func (l *List[T]) MoveBefore(e, mark Element[T]) { |
| 121 | + l.List.MoveBefore(e.Element, mark.Element) |
| 122 | +} |
| 123 | + |
| 124 | +// MoveAfter moves element e to its new position after mark. |
| 125 | +// If e or mark is not an element of l, or e == mark, the list is not modified. |
| 126 | +// The element and mark must not be nil. |
| 127 | +func (l *List[T]) MoveAfter(e, mark Element[T]) { |
| 128 | + l.List.MoveAfter(e.Element, mark.Element) |
| 129 | +} |
| 130 | + |
| 131 | +// PushBackList inserts a copy of another list at the back of list l. |
| 132 | +// The lists l and other may be the same. They must not be nil. |
| 133 | +func (l *List[T]) PushBackList(other *List[T]) { |
| 134 | + l.List.PushBackList(other.List) |
| 135 | +} |
| 136 | + |
| 137 | +// PushFrontList inserts a copy of another list at the front of list l. |
| 138 | +// The lists l and other may be the same. They must not be nil. |
| 139 | +func (l *List[T]) PushFrontList(other *List[T]) { |
| 140 | + l.List.PushFrontList(other.List) |
| 141 | +} |
0 commit comments