-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslist.go
293 lines (230 loc) · 5.51 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package Slist
type Snode struct {
item interface{}
next *Snode
}
func NilSnode() *Snode {
return nil
}
type Slist struct {
head *Snode
tail *Snode
}
// Swap in O(1) two sequences
func (seq *Slist) Swap(rhs interface{}) interface{} {
other := rhs.(*Slist)
seq.head, other.head = other.head, seq.head
seq.tail, other.tail = other.tail, seq.tail
return seq
}
// New Create a new sequence with the received items
func New(items ...interface{}) *Slist {
seq := new(Slist)
for _, item := range items {
seq.Append(item)
}
return seq
}
func (seq *Slist) Create(items ...interface{}) interface{} {
return New(items...)
}
// IsEmpty Return true if sequence is empty
func (seq *Slist) IsEmpty() bool {
return seq.head == nil && seq.tail == nil // double check!
}
// ToSlice Return a slice with the elements of the list
func (seq *Slist) ToSlice() []interface{} {
ret := make([]interface{}, 0, 4)
for it := NewIterator(seq); it.HasCurr(); it.Next() {
ret = append(ret, it.GetCurr())
}
return ret
}
func (seq *Slist) __append(item interface{}) *Slist {
ptr := new(Snode)
ptr.item = item
if seq.IsEmpty() {
seq.head = ptr
seq.tail = ptr
return seq
}
seq.tail.next = ptr
seq.tail = ptr
return seq
}
// Append the received items at the end of the sequence
func (seq *Slist) Append(item interface{}, items ...interface{}) interface{} {
result := seq.__append(item)
for _, i := range items {
result.__append(i)
}
return result
}
func (seq *Slist) __insert(item interface{}) *Slist {
ptr := new(Snode)
ptr.item = item
if seq.IsEmpty() {
seq.head = ptr
seq.tail = ptr
return seq
}
ptr.next = seq.head
seq.head = ptr
return seq
}
// Insert at the beginning of the sequence all the received items (in the given order)
func (seq *Slist) Insert(item interface{}, items ...interface{}) *Slist {
result := seq.__insert(item)
for _, i := range items {
result.__insert(i)
}
return result
}
// RemoveFirst Remove the first item of the sequence
func (seq *Slist) RemoveFirst() interface{} {
if seq.IsEmpty() {
return nil
}
ret := seq.head.item
seq.head = seq.head.next
if seq.head == nil { // list became empty?
seq.tail = nil
}
return ret
}
// First Return the first element of the list
func (seq *Slist) First() interface{} {
if seq == nil {
return nil
}
return seq.head.item
}
// Last Return the last element of the list
func (seq *Slist) Last() interface{} {
if seq == nil {
return nil
}
return seq.tail.item
}
// Empty Return true if the list is empty
func (seq *Slist) Empty() *Slist {
seq.head = nil
seq.tail = nil
return seq
}
// Append in O(1) l to list_ptr and destroys l
func (seq *Slist) staticAppendList(l *Slist) *Slist {
if l.IsEmpty() {
return seq
}
if seq.IsEmpty() {
return seq.Swap(l).(*Slist)
}
seq.tail.next = l.head
seq.tail = l.tail
l.head = nil
l.tail = nil
return seq
}
// AppendList Append to seq the received lists. Complexity O(n) where n is the number of received list
func (seq *Slist) AppendList(l *Slist, ln ...*Slist) *Slist {
seq.staticAppendList(l)
for _, ll := range ln {
seq.staticAppendList(ll)
}
return seq
}
type Iterator struct {
listPtr *Slist
curr *Snode
}
// NewIterator Return an iterator to the list
func NewIterator(seq *Slist) *Iterator {
it := new(Iterator)
it.listPtr = seq
it.curr = seq.head
return it
}
// CreateIterator Return an iterator to the list
func (seq *Slist) CreateIterator() interface{} {
return NewIterator(seq)
}
// ResetFirst Reset the iterator to the first element of the list
func (it *Iterator) ResetFirst() interface{} {
it.curr = it.listPtr.head
return it
}
// HasCurr Return true if the iterator is positioned on a valid element
func (it *Iterator) HasCurr() bool {
return it.curr != nil
}
// IsLast Return true if the current element of the list is the last of the list
func (it *Iterator) IsLast() bool {
return it.curr == it.listPtr.tail
}
// GetCurr Return the current element of the list
func (it *Iterator) GetCurr() interface{} {
if it.curr == nil {
return nil
}
return it.curr.item
}
// Next Advance the iterator to the next element of the list
func (it *Iterator) Next() interface{} {
it.curr = it.curr.next
if it.curr == nil {
return nil
}
return it
}
// Size Return the number of elements of the list
func (seq *Slist) Size() int {
n := 0
for it := NewIterator(seq); it.HasCurr(); it.Next() {
n++
}
return n
}
// Traverse the list and execute operation. It stops if the operation return false. Return true if
// all the elements of the list were traversed
func (seq *Slist) Traverse(operation func(key interface{}) bool) bool {
for it := NewIterator(seq); it.HasCurr(); it.Next() {
if !operation(it.GetCurr()) {
return false
}
}
return true
}
func (seq *Slist) clone() *Slist {
ret := New()
for it := NewIterator(seq); it.HasCurr(); it.Next() {
ret.Append(it.GetCurr())
}
return ret
}
// ReverseInPlace Reverse the list in place
func (seq *Slist) ReverseInPlace() *Slist {
tmp := New()
for !seq.IsEmpty() {
tmp.Insert(seq.RemoveFirst())
}
return seq.Swap(tmp).(*Slist)
}
// Reverse Return a reversed copy of seq
func (seq *Slist) Reverse() *Slist {
return seq.clone().ReverseInPlace()
}
// RotateLeftInPlace Rotate in place n positions to left
func (seq *Slist) RotateLeftInPlace(n int) *Slist {
if seq.IsEmpty() || n == 0 {
return seq
}
for i := 0; i < n; i++ {
seq.Append(seq.RemoveFirst())
}
return seq
}
// RotateLeft Return a copy of seq rotated n positions to left
func (seq *Slist) RotateLeft(n int) *Slist {
return seq.clone().RotateLeftInPlace(n)
}