Skip to content

Commit c0b3e46

Browse files
committed
reuse: Various bugfixes
1 parent b6275a6 commit c0b3e46

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/utils/reuse.jl

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ mutable struct ReusableLinkedList{T} <: AbstractVector{T}
150150
tail::Union{ReusableNode{T},Nothing}
151151
free_nodes::ReusableNode{T}
152152
null::T
153-
len::Int
153+
maxlen::Int
154154
function ReusableLinkedList{T}(null, N) where T
155155
free_root = ReusableNode{T}(null, nothing)
156156
for _ in 1:N
@@ -163,6 +163,7 @@ mutable struct ReusableLinkedList{T} <: AbstractVector{T}
163163
end
164164
Base.eltype(list::ReusableLinkedList{T}) where T = T
165165
function Base.getindex(list::ReusableLinkedList{T}, idx::Integer) where T
166+
checkbounds(list, idx)
166167
node = list.head
167168
for _ in 1:(idx-1)
168169
node === nothing && throw(BoundsError(list, idx))
@@ -172,6 +173,7 @@ function Base.getindex(list::ReusableLinkedList{T}, idx::Integer) where T
172173
return node.value
173174
end
174175
function Base.setindex!(list::ReusableLinkedList{T}, value::T, idx::Integer) where T
176+
checkbounds(list, idx)
175177
node = list.head
176178
for _ in 1:(idx-1)
177179
node === nothing && throw(BoundsError(list, idx))
@@ -199,7 +201,29 @@ function Base.push!(list::ReusableLinkedList{T}, value) where T
199201
end
200202
return list
201203
end
204+
202205
function Base.pop!(list::ReusableLinkedList{T}) where T
206+
if list.head === nothing
207+
throw(ArgumentError("list must be non-empty"))
208+
end
209+
prev = node = list.head
210+
while node.next !== nothing
211+
prev = node
212+
node = node.next
213+
end
214+
if prev !== node
215+
list.tail = prev
216+
else
217+
list.head = list.tail = nothing
218+
end
219+
prev.next = nothing
220+
node.next = list.free_nodes
221+
list.free_nodes = node
222+
value = node.value
223+
node.value = list.null
224+
return value
225+
end
226+
function Base.popfirst!(list::ReusableLinkedList{T}) where T
203227
if list.head === nothing
204228
throw(ArgumentError("list must be non-empty"))
205229
end

0 commit comments

Comments
 (0)