forked from simoneguidi94/gopapageno
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlos.go
253 lines (194 loc) · 4.58 KB
/
los.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
package gopapageno
import (
"fmt"
"math"
)
// LOS is a list of stacks.
type LOS[T any] struct {
head *stack[T]
cur *stack[T]
headFirst int
len int
pool *Pool[stack[T]]
}
// NewLOS creates a new LOS initialized with an empty stack.
func NewLOS[T any](pool *Pool[stack[T]]) *LOS[T] {
s := pool.Get()
return &LOS[T]{
head: s,
cur: s,
len: 0,
pool: pool,
}
}
// Push adds an element to the LOS.
// By default, the element is added to the current stack;
// if that is full, a new one is obtained from the pool.
func (l *LOS[T]) Push(t T) *T {
// If the current stack is full, we must obtain a new one and set it as the current one.
if l.cur.Tos >= l.cur.Size {
if l.cur.Next != nil {
l.cur = l.cur.Next
} else {
s := l.pool.Get()
l.cur.Next = s
s.Prev = l.cur
l.cur = s
}
}
l.cur.Data[l.cur.Tos] = t
ptr := &l.cur.Data[l.cur.Tos]
l.cur.Tos++
l.len++
return ptr
}
// Pop removes the topmost element from the LOS and returns it.
func (l *LOS[T]) Pop() *T {
l.cur.Tos--
if l.cur.Tos >= 0 {
l.len--
return &l.cur.Data[l.cur.Tos]
}
l.cur.Tos = 0
if l.cur.Prev == nil {
return nil
}
l.cur = l.cur.Prev
l.cur.Tos--
l.len--
return &l.cur.Data[l.cur.Tos]
}
// Get returns the topmost element from the LOS.
func (l *LOS[T]) Get() *T {
if l.cur.Tos > 0 {
return &l.cur.Data[l.cur.Tos-1]
}
if l.cur.Prev == nil {
return nil
}
return &l.cur.Prev.Data[l.cur.Prev.Tos-1]
}
// GetNext returns the first empty element from the LOS.
func (l *LOS[T]) GetNext() *T {
if l.cur.Tos >= 0 {
return &l.cur.Data[l.cur.Tos]
}
if l.cur.Prev == nil {
return nil
}
return &l.cur.Prev.Data[l.cur.Prev.Tos]
}
// Clear empties the LOS.
func (l *LOS[T]) Clear() {
// Reset length
l.len = 0
// Reset Top of Stack for every stack
for s := l.head; s != nil; s = s.Next {
s.Tos = 0
}
// Reset current stack
l.cur = l.head
}
// Merge links the stacks of the current and of another LOS.
func (l *LOS[T]) Merge(other *LOS[T]) {
l.cur.Next = other.head
other.head.Prev = l.cur
l.cur = other.cur
l.len += other.len
}
// Split splits a LOS into a slice of LOS of length n.
// The original LOS should not be used after this operation.
func (l *LOS[T]) Split(n int) ([]*LOS[T], error) {
numStacks := l.NumStacks()
if n > numStacks {
return nil, fmt.Errorf("not enough stacks in LOS")
}
lists := make([]*LOS[T], n)
curList := 0
deltaStacks := float64(numStacks) / float64(n)
assignedStacks := 0
remainder := float64(0)
curStack := l.head
for assignedStacks < numStacks {
remainder += deltaStacks
stacksToAssign := int(math.Floor(remainder + 0.5))
curStack.Prev = nil
lists[curList] = &LOS[T]{
head: curStack,
cur: curStack,
len: curStack.Tos,
pool: l.pool,
}
for i := 1; i < stacksToAssign; i++ {
curStack = curStack.Next
lists[curList].cur = curStack
lists[curList].len += curStack.Tos
}
next := curStack.Next
curStack.Next = nil
curStack = next
remainder -= float64(stacksToAssign)
assignedStacks += stacksToAssign
curList++
}
return lists, nil
}
// NumStacks returns the number of stacks contained in the LOS.
// It takes linear time (in the number of stacks) to execute.
func (l *LOS[T]) NumStacks() int {
n := 0
for cur := l.head; cur != nil; cur = cur.Next {
n++
}
return n
}
// Length returns the number of items contained in the LOS.
// It takes constant time to execute.
func (l *LOS[T]) Length() int {
return l.len
}
// LOSIt allows to iterate over a LOS, either forward or backwards.
type LOSIt[T any] struct {
los *LOS[T]
cur *stack[T]
pos int
}
// HeadIterator returns an iterator initialized to point before the first element of the list.
func (l *LOS[T]) HeadIterator() *LOSIt[T] {
return &LOSIt[T]{l, l.head, l.headFirst - 1}
}
// Cur returns a pointer to the current element.
// It returns nil if it points before the first element or after the last element of the list.
func (i *LOSIt[T]) Cur() *T {
curStack := i.cur
if i.pos >= 0 && i.pos < curStack.Tos {
return &curStack.Data[i.pos]
}
return nil
}
// Next moves the iterator one position forward and returns a pointer to the new current element.
// It returns nil if it points after the last element of the list.
func (i *LOSIt[T]) Next() *T {
curStack := i.cur
i.pos++
if i.pos < curStack.Tos {
return &curStack.Data[i.pos]
}
i.pos = curStack.Tos
if curStack.Next == nil {
return nil
}
curStack = curStack.Next
i.cur = curStack
i.pos = 0
return &curStack.Data[i.pos]
}
func (i *LOSIt[T]) IsLast() bool {
if i.pos+1 < i.cur.Tos {
return false
}
if i.cur.Next == nil {
return true
}
return false
}