|
| 1 | +// Package xlist is a copy of standard container/list but uses generics |
| 2 | +// for strict checks on compile time |
| 3 | +package xlist |
| 4 | + |
| 5 | +// Element is an element of a linked list. |
| 6 | +type Element[T any] struct { |
| 7 | + // Next and previous pointers in the doubly-linked list of elements. |
| 8 | + // To simplify the implementation, internally a list l is implemented |
| 9 | + // as a ring, such that &l.root is both the next element of the last |
| 10 | + // list element (l.Back()) and the previous element of the first list |
| 11 | + // element (l.Front()). |
| 12 | + next, prev *Element[T] |
| 13 | + |
| 14 | + // The list to which this element belongs. |
| 15 | + list *List[T] |
| 16 | + |
| 17 | + // The value stored with this element. |
| 18 | + Value T |
| 19 | +} |
| 20 | + |
| 21 | +// Next returns the next list element or nil. |
| 22 | +func (e *Element[T]) Next() *Element[T] { |
| 23 | + if p := e.next; e.list != nil && p != &e.list.root { |
| 24 | + return p |
| 25 | + } |
| 26 | + |
| 27 | + return nil |
| 28 | +} |
| 29 | + |
| 30 | +// Prev returns the previous list element or nil. |
| 31 | +func (e *Element[T]) Prev() *Element[T] { |
| 32 | + if p := e.prev; e.list != nil && p != &e.list.root { |
| 33 | + return p |
| 34 | + } |
| 35 | + |
| 36 | + return nil |
| 37 | +} |
| 38 | + |
| 39 | +// List represents a doubly linked list. |
| 40 | +// The zero value for List is an empty list ready to use. |
| 41 | +type List[T any] struct { |
| 42 | + root Element[T] // sentinel list element, only &root, root.prev, and root.next are used |
| 43 | + len int // current list length excluding (this) sentinel element |
| 44 | +} |
| 45 | + |
| 46 | +// Init initializes or clears list l. |
| 47 | +func (l *List[T]) Init() *List[T] { |
| 48 | + l.root.next = &l.root |
| 49 | + l.root.prev = &l.root |
| 50 | + l.len = 0 |
| 51 | + |
| 52 | + return l |
| 53 | +} |
| 54 | + |
| 55 | +// New returns an initialized list. |
| 56 | +func New[T any]() *List[T] { return new(List[T]).Init() } |
| 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.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 | + if l.len == 0 { |
| 65 | + return nil |
| 66 | + } |
| 67 | + |
| 68 | + return l.root.next |
| 69 | +} |
| 70 | + |
| 71 | +// Back returns the last element of list l or nil if the list is empty. |
| 72 | +func (l *List[T]) Back() *Element[T] { |
| 73 | + if l.len == 0 { |
| 74 | + return nil |
| 75 | + } |
| 76 | + |
| 77 | + return l.root.prev |
| 78 | +} |
| 79 | + |
| 80 | +// lazyInit lazily initializes a zero List value. |
| 81 | +func (l *List[T]) lazyInit() { |
| 82 | + if l.root.next == nil { |
| 83 | + l.Init() |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// insert inserts e after at, increments l.len, and returns e. |
| 88 | +func (l *List[T]) insert(e, at *Element[T]) *Element[T] { |
| 89 | + e.prev = at |
| 90 | + e.next = at.next |
| 91 | + e.prev.next = e |
| 92 | + e.next.prev = e |
| 93 | + e.list = l |
| 94 | + l.len++ |
| 95 | + |
| 96 | + return e |
| 97 | +} |
| 98 | + |
| 99 | +// insertValue is a convenience wrapper for insert(&Element{Value: v}, at). |
| 100 | +func (l *List[T]) insertValue(v T, at *Element[T]) *Element[T] { |
| 101 | + return l.insert(&Element[T]{Value: v}, at) |
| 102 | +} |
| 103 | + |
| 104 | +// remove removes e from its list, decrements l.len |
| 105 | +func (l *List[T]) remove(e *Element[T]) { |
| 106 | + e.prev.next = e.next |
| 107 | + e.next.prev = e.prev |
| 108 | + e.next = nil // avoid memory leaks |
| 109 | + e.prev = nil // avoid memory leaks |
| 110 | + e.list = nil |
| 111 | + l.len-- |
| 112 | +} |
| 113 | + |
| 114 | +// move moves e to next to at. |
| 115 | +func (l *List[T]) move(e, at *Element[T]) { |
| 116 | + if e == at { |
| 117 | + return |
| 118 | + } |
| 119 | + e.prev.next = e.next |
| 120 | + e.next.prev = e.prev |
| 121 | + |
| 122 | + e.prev = at |
| 123 | + e.next = at.next |
| 124 | + e.prev.next = e |
| 125 | + e.next.prev = e |
| 126 | +} |
| 127 | + |
| 128 | +// Remove removes e from l if e is an element of list l. |
| 129 | +// It returns the element value e.Value. |
| 130 | +// The element must not be nil. |
| 131 | +func (l *List[T]) Remove(e *Element[T]) T { |
| 132 | + if e.list == l { |
| 133 | + // if e.list == l, l must have been initialized when e was inserted |
| 134 | + // in l or l == nil (e is a zero Element) and l.remove will crash |
| 135 | + l.remove(e) |
| 136 | + } |
| 137 | + |
| 138 | + return e.Value |
| 139 | +} |
| 140 | + |
| 141 | +// PushFront inserts a new element e with value v at the front of list l and returns e. |
| 142 | +func (l *List[T]) PushFront(v T) *Element[T] { |
| 143 | + l.lazyInit() |
| 144 | + |
| 145 | + return l.insertValue(v, &l.root) |
| 146 | +} |
| 147 | + |
| 148 | +// PushBack inserts a new element e with value v at the back of list l and returns e. |
| 149 | +func (l *List[T]) PushBack(v T) *Element[T] { |
| 150 | + l.lazyInit() |
| 151 | + |
| 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. |
| 158 | +func (l *List[T]) InsertBefore(v T, mark *Element[T]) *Element[T] { |
| 159 | + if mark.list != l { |
| 160 | + return nil |
| 161 | + } |
| 162 | + |
| 163 | + // see comment in List.Remove about initialization of l |
| 164 | + return l.insertValue(v, mark.prev) |
| 165 | +} |
| 166 | + |
| 167 | +// InsertAfter inserts a new element e with value v immediately after mark and returns e. |
| 168 | +// If mark is not an element of l, the list is not modified. |
| 169 | +// The mark must not be nil. |
| 170 | +func (l *List[T]) InsertAfter(v T, mark *Element[T]) *Element[T] { |
| 171 | + if mark.list != l { |
| 172 | + return nil |
| 173 | + } |
| 174 | + |
| 175 | + // see comment in List.Remove about initialization of l |
| 176 | + return l.insertValue(v, mark) |
| 177 | +} |
| 178 | + |
| 179 | +// MoveToFront moves element e to the front of list l. |
| 180 | +// If e is not an element of l, the list is not modified. |
| 181 | +// The element must not be nil. |
| 182 | +func (l *List[T]) MoveToFront(e *Element[T]) { |
| 183 | + if e.list != l || l.root.next == e { |
| 184 | + return |
| 185 | + } |
| 186 | + // see comment in List.Remove about initialization of l |
| 187 | + l.move(e, &l.root) |
| 188 | +} |
| 189 | + |
| 190 | +// MoveToBack moves element e to the back of list l. |
| 191 | +// If e is not an element of l, the list is not modified. |
| 192 | +// The element must not be nil. |
| 193 | +func (l *List[T]) MoveToBack(e *Element[T]) { |
| 194 | + if e.list != l || l.root.prev == e { |
| 195 | + return |
| 196 | + } |
| 197 | + // see comment in List.Remove about initialization of l |
| 198 | + l.move(e, l.root.prev) |
| 199 | +} |
| 200 | + |
| 201 | +// MoveBefore moves element e to its new position before mark. |
| 202 | +// If e or mark is not an element of l, or e == mark, the list is not modified. |
| 203 | +// The element and mark must not be nil. |
| 204 | +func (l *List[T]) MoveBefore(e, mark *Element[T]) { |
| 205 | + if e.list != l || e == mark || mark.list != l { |
| 206 | + return |
| 207 | + } |
| 208 | + l.move(e, mark.prev) |
| 209 | +} |
| 210 | + |
| 211 | +// MoveAfter moves element e to its new position after mark. |
| 212 | +// If e or mark is not an element of l, or e == mark, the list is not modified. |
| 213 | +// The element and mark must not be nil. |
| 214 | +func (l *List[T]) MoveAfter(e, mark *Element[T]) { |
| 215 | + if e.list != l || e == mark || mark.list != l { |
| 216 | + return |
| 217 | + } |
| 218 | + l.move(e, mark) |
| 219 | +} |
| 220 | + |
| 221 | +// PushBackList inserts a copy of another list at the back of list l. |
| 222 | +// The lists l and other may be the same. They must not be nil. |
| 223 | +func (l *List[T]) PushBackList(other *List[T]) { |
| 224 | + l.lazyInit() |
| 225 | + for i, e := other.Len(), other.Front(); i > 0; i, e = i-1, e.Next() { |
| 226 | + l.insertValue(e.Value, l.root.prev) |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | +// PushFrontList inserts a copy of another list at the front of list l. |
| 231 | +// The lists l and other may be the same. They must not be nil. |
| 232 | +func (l *List[T]) PushFrontList(other *List[T]) { |
| 233 | + l.lazyInit() |
| 234 | + for i, e := other.Len(), other.Back(); i > 0; i, e = i-1, e.Prev() { |
| 235 | + l.insertValue(e.Value, &l.root) |
| 236 | + } |
| 237 | +} |
0 commit comments