Skip to content

Commit 5f6e060

Browse files
committed
linked list
1 parent bc14ef2 commit 5f6e060

File tree

1 file changed

+223
-0
lines changed

1 file changed

+223
-0
lines changed

btrcontainer/linked_list.go

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

0 commit comments

Comments
 (0)