forked from simoneguidi94/gopapageno
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlops.go
223 lines (173 loc) · 4.21 KB
/
lops.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
package gopapageno
// LOPS is a list of pointer stacks.
type LOPS[T any] struct {
head *stack[*T]
cur *stack[*T]
headFirst int
len int
pool *Pool[stack[*T]]
}
// NewLOPS creates a new LOPS initialized with an empty stack.
func NewLOPS[T any](pool *Pool[stack[*T]]) *LOPS[T] {
s := pool.Get()
return &LOPS[T]{
head: s,
cur: s,
len: 0,
pool: pool,
}
}
// Push adds an element to the LOPS.
// By default, the element is added to the current stack;
// if that is full, a new one is obtained from the pool.
func (l *LOPS[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.headFirst = 0
}
l.cur.Data[l.cur.Tos] = t
l.cur.Tos++
l.len++
return t
}
// Pop removes the topmost element from the LOPS and returns it.
func (l *LOPS[T]) Pop() *T {
l.cur.Tos--
if l.cur.Tos >= l.headFirst {
l.len--
return l.cur.Data[l.cur.Tos]
}
l.cur.Tos = l.headFirst
if l.cur.Prev == nil {
return nil
}
l.headFirst = 0
l.cur = l.cur.Prev
l.cur.Tos--
l.len--
return l.cur.Data[l.cur.Tos]
}
// Get returns the topmost element from the LOPS.
func (l *LOPS[T]) Get() *T {
if l.cur.Tos > l.headFirst {
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]
}
// Clear empties the LOPS.
func (l *LOPS[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
}
// NumStacks returns the number of stacks contained in the LOPS.
// It takes linear time (in the number of stacks) to execute.
func (l *LOPS[T]) NumStacks() int {
n := 0
for cur := l.head; cur != nil; cur = cur.Next {
n++
}
return n
}
// MaxLength returns the maximum occupancy of the data structure so far, i.e. what is
// the maximum amount of items in use at any given time.
func (l *LOPS[T]) MaxLength() int {
stacks := l.NumStacks()
lastSize := l.cur.Tos
for _, e := range l.cur.Data[l.cur.Tos+1:] {
if e != nil {
lastSize++
} else {
break
}
}
return (stacks-1)*l.head.Size + lastSize
}
// Capacity returns the maximum capacity of the current allocated structure.
func (l *LOPS[T]) Capacity() int {
stacks := l.NumStacks()
return (stacks) * l.head.Size
}
// Length returns the number of items contained in the LOPS.
// It takes constant time to execute.
func (l *LOPS[T]) Length() int {
return l.len
}
// LOPSIt allows to iterate over a LOPS, either forward or backwards.
type LOPSIt[T any] struct {
los *LOPS[T]
cur *stack[*T]
pos int
}
// HeadIterator returns an iterator initialized to point before the first element of the list.
func (l *LOPS[T]) HeadIterator() *LOPSIt[T] {
return &LOPSIt[T]{l, l.head, l.headFirst - 1}
}
// Prev moves the iterator one position backward and returns a pointer to the new current element.
// It returns nil if it points before the first element of the list.
func (i *LOPSIt[T]) Prev() *T {
curStack := i.cur
i.pos--
if i.pos >= i.los.headFirst {
return curStack.Data[i.pos]
}
i.pos = -1
if curStack.Prev == nil {
return nil
}
curStack = curStack.Prev
i.cur = curStack
i.pos = curStack.Tos - 1
return curStack.Data[i.pos]
}
// 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 *LOPSIt[T]) Cur() *T {
curStack := i.cur
if i.pos >= i.los.headFirst && 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 *LOPSIt[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 *LOPSIt[T]) IsLast() bool {
if i.pos+1 < i.cur.Tos {
return false
}
if i.cur.Next == nil {
return true
}
return false
}