forked from chen3feng/stl4go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlist.go
216 lines (189 loc) · 4.26 KB
/
dlist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package stl4go
import "fmt"
// DList is a doubly linked list.
type DList[T any] struct {
head *dListNode[T]
length int
}
type dListNode[T any] struct {
prev, next *dListNode[T]
value T
}
// DListOf make a new DList from a serial of values.
func DListOf[T any](vs ...T) DList[T] {
l := DList[T]{}
for _, v := range vs {
l.PushBack(v)
}
return l
}
// Clear cleanup the list.
func (l *DList[T]) Clear() {
if l.head != nil {
l.head.prev = l.head
l.head.next = l.head
}
l.length = 0
}
// Len return the length of the list.
func (l *DList[T]) Len() int {
return l.length
}
// IsEmpty return whether the list is empty.
func (l *DList[T]) IsEmpty() bool {
return l.length == 0
}
// String convert the list to string.
func (l *DList[T]) String() string {
return fmt.Sprintf("DList[%v]", nameOfType[T]())
}
type dlistIterator[T any] struct {
dl *DList[T]
node *dListNode[T]
}
func (it *dlistIterator[T]) IsNotEnd() bool {
return it.node != it.dl.head
}
func (it *dlistIterator[T]) MoveToNext() {
it.node = it.node.next
}
func (it *dlistIterator[T]) Value() T {
return it.node.value
}
func (it *dlistIterator[T]) Pointer() *T {
return &it.node.value
}
// Iterate returns an iterator to the first element in the list.
func (l *DList[T]) Iterate() MutableIterator[T] {
node := l.head
if node != nil {
node = node.next
}
return &dlistIterator[T]{l, node}
}
// Front returns the first element in the container.
func (l *DList[T]) Front() T {
if l.IsEmpty() {
panic("!IsEmpty")
}
return l.head.next.value
}
// Back returns the last element in the container.
func (l *DList[T]) Back() T {
if l.IsEmpty() {
panic("!IsEmpty")
}
return l.head.prev.value
}
// PushFront pushes an element at the front of the list.
func (l *DList[T]) PushFront(val T) {
l.ensureHead()
n := dListNode[T]{l.head, l.head.next, val}
l.head.next.prev = &n
l.head.next = &n
l.length++
}
// PushBack pushes an element at the back of the list.
func (l *DList[T]) PushBack(val T) {
l.ensureHead()
n := dListNode[T]{l.head.prev, l.head, val}
l.head.prev.next = &n
l.head.prev = &n
l.length++
}
// PopFront popups a element from the front of the list.
func (l *DList[T]) PopFront() T {
r, ok := l.TryPopFront()
if !ok {
panic("DList.PopFront: empty list")
}
return r
}
// PopBack popups a element from the back of the list.
func (l *DList[T]) PopBack() T {
r, ok := l.TryPopBack()
if !ok {
panic("DList.PopBack: empty list")
}
return r
}
// TryPopFront tries to popup a element from the front of the list.
func (l *DList[T]) TryPopFront() (T, bool) {
var val T
if l.length == 0 {
return val, false
}
node := l.head.next
val = node.value
l.head.next = node.next
l.head.prev = l.head
node.prev = nil
node.next = nil
l.length--
return val, true
}
// TryPopBack tries to popup a element from the back of the list.
func (l *DList[T]) TryPopBack() (T, bool) {
var val T
if l.length == 0 {
return val, false
}
node := l.head.prev
val = node.value
l.head.prev = l.head.prev.prev
l.head.prev.next = l.head
node.prev = nil
node.next = nil
l.length--
return val, true
}
// ForEach iterate the list, apply each element to the cb callback function.
func (l *DList[T]) ForEach(cb func(val T)) {
if l.head == nil {
return
}
for n := l.head.next; n != l.head; n = n.next {
cb(n.value)
}
}
// ForEachIf iterate the list, apply each element to the cb callback function,
// stop if cb returns false.
func (l *DList[T]) ForEachIf(cb func(val T) bool) {
if l.head == nil {
return
}
for n := l.head.next; n != l.head; n = n.next {
if !cb(n.value) {
break
}
}
}
// ForEachMutable iterate the list, apply pointer of each element to the cb callback function.
func (l *DList[T]) ForEachMutable(cb func(val *T)) {
if l.head == nil {
return
}
for n := l.head.next; n != l.head; n = n.next {
cb(&n.value)
}
}
// ForEachMutableIf iterate the list, apply pointer of each element to the cb callback function,
// stop if cb returns false.
func (l *DList[T]) ForEachMutableIf(cb func(val *T) bool) {
if l.head == nil {
return
}
for n := l.head.next; n != l.head; n = n.next {
if !cb(&n.value) {
break
}
}
}
// ensureHead ensure head is valid.
func (l *DList[T]) ensureHead() {
if l.head == nil {
l.head = &dListNode[T]{}
l.head.prev = l.head
l.head.next = l.head
}
}