1+ // Package xlist is a copy of standard container/list but uses generics
2+ // for strict checks on compile time
3+
14package xlist
25
3- // Element[T] is an element of a linked list.
6+ // Element is an element of a linked list.
47type Element [T any ] struct {
58 // Next and previous pointers in the doubly-linked list of elements.
69 // To simplify the implementation, internally a list l is implemented
@@ -32,8 +35,8 @@ func (e *Element[T]) Prev() *Element[T] {
3235 return nil
3336}
3437
35- // List[T] represents a doubly linked list.
36- // The zero value for List[T] is an empty list ready to use.
38+ // List represents a doubly linked list.
39+ // The zero value for List is an empty list ready to use.
3740type List [T any ] struct {
3841 root Element [T ] // sentinel list element, only &root, root.prev, and root.next are used
3942 len int // current list length excluding (this) sentinel element
@@ -70,7 +73,7 @@ func (l *List[T]) Back() *Element[T] {
7073 return l .root .prev
7174}
7275
73- // lazyInit lazily initializes a zero List[T] value.
76+ // lazyInit lazily initializes a zero List value.
7477func (l * List [T ]) lazyInit () {
7578 if l .root .next == nil {
7679 l .Init ()
@@ -88,7 +91,7 @@ func (l *List[T]) insert(e, at *Element[T]) *Element[T] {
8891 return e
8992}
9093
91- // insertValue is a convenience wrapper for insert(&Element[T] {Value: v}, at).
94+ // insertValue is a convenience wrapper for insert(&Element{Value: v}, at).
9295func (l * List [T ]) insertValue (v T , at * Element [T ]) * Element [T ] {
9396 return l .insert (& Element [T ]{Value : v }, at )
9497}
@@ -123,7 +126,7 @@ func (l *List[T]) move(e, at *Element[T]) {
123126func (l * List [T ]) Remove (e * Element [T ]) T {
124127 if e .list == l {
125128 // if e.list == l, l must have been initialized when e was inserted
126- // in l or l == nil (e is a zero Element[T] ) and l.remove will crash
129+ // in l or l == nil (e is a zero Element) and l.remove will crash
127130 l .remove (e )
128131 }
129132 return e .Value
@@ -148,7 +151,7 @@ func (l *List[T]) InsertBefore(v T, mark *Element[T]) *Element[T] {
148151 if mark .list != l {
149152 return nil
150153 }
151- // see comment in List[T] .Remove about initialization of l
154+ // see comment in List.Remove about initialization of l
152155 return l .insertValue (v , mark .prev )
153156}
154157
@@ -159,7 +162,7 @@ func (l *List[T]) InsertAfter(v T, mark *Element[T]) *Element[T] {
159162 if mark .list != l {
160163 return nil
161164 }
162- // see comment in List[T] .Remove about initialization of l
165+ // see comment in List.Remove about initialization of l
163166 return l .insertValue (v , mark )
164167}
165168
@@ -170,7 +173,7 @@ func (l *List[T]) MoveToFront(e *Element[T]) {
170173 if e .list != l || l .root .next == e {
171174 return
172175 }
173- // see comment in List[T] .Remove about initialization of l
176+ // see comment in List.Remove about initialization of l
174177 l .move (e , & l .root )
175178}
176179
@@ -181,7 +184,7 @@ func (l *List[T]) MoveToBack(e *Element[T]) {
181184 if e .list != l || l .root .prev == e {
182185 return
183186 }
184- // see comment in List[T] .Remove about initialization of l
187+ // see comment in List.Remove about initialization of l
185188 l .move (e , l .root .prev )
186189}
187190
@@ -205,7 +208,7 @@ func (l *List[T]) MoveAfter(e, mark *Element[T]) {
205208 l .move (e , mark )
206209}
207210
208- // PushBackList[T] inserts a copy of another list at the back of list l.
211+ // PushBackList inserts a copy of another list at the back of list l.
209212// The lists l and other may be the same. They must not be nil.
210213func (l * List [T ]) PushBackList (other * List [T ]) {
211214 l .lazyInit ()
@@ -214,7 +217,7 @@ func (l *List[T]) PushBackList(other *List[T]) {
214217 }
215218}
216219
217- // PushFrontList[T] inserts a copy of another list at the front of list l.
220+ // PushFrontList inserts a copy of another list at the front of list l.
218221// The lists l and other may be the same. They must not be nil.
219222func (l * List [T ]) PushFrontList (other * List [T ]) {
220223 l .lazyInit ()
0 commit comments