forked from chen3feng/stl4go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slist.go
185 lines (160 loc) · 4.05 KB
/
slist.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
package stl4go
// SList is a singly linked list.
type SList[T any] struct {
head *sListNode[T]
tail *sListNode[T] // To support Back and PushBack
length int
}
type sListNode[T any] struct {
next *sListNode[T]
value T
}
// SListOf return a SList that contains values.
func SListOf[T any](values ...T) SList[T] {
l := SList[T]{}
for i := range values {
l.PushBack(values[i])
}
return l
}
// IsEmpty checks if the container has no elements.
func (l *SList[T]) IsEmpty() bool {
return l.length == 0
}
// Len returns the number of elements in the container.
func (l *SList[T]) Len() int {
return l.length
}
// Clear erases all elements from the container. After this call, Len() returns zero.
func (l *SList[T]) Clear() {
l.head = nil
l.tail = nil
l.length = 0
}
// Front returns the first element in the list.
func (l *SList[T]) Front() T {
return l.head.value
}
// Back returns the last element in the list.
func (l *SList[T]) Back() T {
return l.tail.value
}
// PushFront pushed an element to the front of the list.
func (l *SList[T]) PushFront(v T) {
node := sListNode[T]{l.head, v}
l.head = &node
if l.tail == nil {
l.tail = &node
}
l.length++
}
// PushBack pushed an element to the tail of the list.
func (l *SList[T]) PushBack(v T) {
node := sListNode[T]{nil, v}
if l.tail != nil {
l.tail.next = &node
}
l.tail = &node
if l.head == nil {
l.head = &node
}
l.length++
}
// PopFront popups an element from the front of the list.
// The list must be non-empty!
func (l *SList[T]) PopFront() T {
node := l.head
if node != nil {
l.head = node.next
if l.head == nil {
l.tail = nil
}
l.length--
}
return node.value
}
// Reverse reverses the order of all elements in the container.
func (l *SList[T]) Reverse() {
var head, tail *sListNode[T]
for node := l.head; node != nil; {
next := node.next
node.next = head
head = node
if tail == nil {
tail = node
}
node = next
}
l.head = head
l.tail = tail
}
// Values copies all elements in the container to a slice and return it.
func (l *SList[T]) Values() []T {
s := make([]T, l.Len())
for node, i := l.head, 0; node != nil; node = node.next {
s[i] = node.value
i++
}
return s
}
// InsertAfter inserts an element after the iterator into the list,
// return an iterator to the inserted element.
// func (l *SList[T]) InsertAfter(it Iterator[T], value T) MutableIterator[T] {
// // cause internal compiler error: panic: runtime error: invalid memory address or nil pointer dereference
// itp := it.(*sListIterator[T])
// node := itp.node
// newNode := sListNode[T]{node.next, value}
// node.next = &newNode
// return &sListIterator[T]{&newNode}
// }
// ForEach iterate the list, apply each element to the cb callback function.
func (l *SList[T]) ForEach(cb func(T)) {
for node := l.head; node != nil; node = node.next {
cb(node.value)
}
}
// ForEachIf iterate the container, apply each element to the cb callback function,
// stop if cb returns false.
func (l *SList[T]) ForEachIf(cb func(T) bool) {
for node := l.head; node != nil; node = node.next {
if !cb(node.value) {
break
}
}
}
// ForEachMutable iterate the container, apply pointer of each element to the cb callback function.
func (l *SList[T]) ForEachMutable(cb func(*T)) {
for node := l.head; node != nil; node = node.next {
cb(&node.value)
}
}
// ForEachMutableIf iterate the container, apply pointer of each element to the cb callback function,
// stop if cb returns false.
func (l *SList[T]) ForEachMutableIf(cb func(*T) bool) {
for node := l.head; node != nil; node = node.next {
if !cb(&node.value) {
break
}
}
}
// Iterate returns an iterator to the whole container.
func (l *SList[T]) Iterate() MutableIterator[T] {
it := sListIterator[T]{l.head}
return &it
}
type sListIterator[T any] struct {
node *sListNode[T]
}
func (it sListIterator[T]) IsNotEnd() bool {
return it.node != nil
}
func (it *sListIterator[T]) MoveToNext() {
it.node = it.node.next
}
func (it sListIterator[T]) Value() T {
return it.node.value
}
func (it sListIterator[T]) Pointer() *T {
return &it.node.value
}
// TODO: Sort