Skip to content

Commit

Permalink
feat(LinkedList): delete the tail field of DoubleList type
Browse files Browse the repository at this point in the history
  • Loading branch information
guo-yong-zhi committed Nov 6, 2024
1 parent 30431bf commit 9197d23
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
34 changes: 22 additions & 12 deletions src/linkedlist.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module LinkedList
export DoubleList, ListNode, movetofirst!, IntMap, ishead, istail, seek_tail
export DoubleList, ListNode, movetofirst!, IntMap, ishead, istail, seek_head, seek_tail
mutable struct ListNode{T}
value::T
prev::ListNode{T}
Expand All @@ -19,16 +19,22 @@ end
ListNode(value::T) where T = ListNode{T}(value)
ishead(n::ListNode) = n.prev === n
istail(n::ListNode) = n.next === n
function Base.iterate(l::ListNode, p=l)
istail(p) ? nothing : (p.value, p.next)
function Base.iterate(l::ListNode, p=l) # from this node to the end
istail(p) ? nothing : (p.value, p.next)
end
Base.IteratorSize(::Type{<:ListNode}) = Base.SizeUnknown()
Base.eltype(::Type{ListNode{T}}) where T = T
function seek_tail(l::ListNode)
while !istail(l)
l = l.next
function seek_tail(n::ListNode)
while !istail(n)
n = n.next
end
l
n
end
function seek_head(n::ListNode)
while !ishead(n)
n = n.prev
end
n
end
function Base.pop!(n::ListNode)
n.prev.next = n.next
Expand All @@ -38,23 +44,27 @@ end

mutable struct DoubleList{T}
head::ListNode{T}
tail::ListNode{T}
# tail::ListNode{T}
end
function DoubleList{T}() where T
h = ListNode{T}()
t = ListNode{T}()
h.next = t
t.prev = h
DoubleList(h, t)
DoubleList(h)
end
function DoubleList{T}(hv::T, tv::T) where T
l = DoubleList{T}()
l.head.value = hv
l.tail.value = tv
l.head.next.value = tv
l
end

Base.isempty(l::DoubleList) = l.head.next === l.tail
function DoubleList{T}(hv::T) where T
l = DoubleList{T}()
l.head.value = hv
l
end
Base.isempty(l::DoubleList) = istail(l.head.next)
function Base.pushfirst!(l::DoubleList, n::ListNode)
n.next = l.head.next
n.prev = l.head
Expand Down
7 changes: 3 additions & 4 deletions src/qtrees.jl
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,7 @@ const SpacialQTreeNode = QTreeNode{LabelList}
new_spacial_qtree_node(index::Index) = QTreeNode(LabelList(index))
new_spacial_qtree_node() = QTreeNode(LabelList())
function new_labellist(node::QTreeNode)
dl = DoubleList{Int}()
dl.tail.value = Int(pointer_from_objref(node))
dl = DoubleList{Int}(Int(pointer_from_objref(node)))
# @assert node == seek_treenode(dl.tail)
node.value.list = dl
end
Expand Down Expand Up @@ -512,8 +511,8 @@ function Base.push!(t::LinkedSpacialQTree, inds, label::Int)
end
end
function seek_treenode(listnode::ListNode{Int})
tail = seek_tail(listnode)
unsafe_pointer_to_objref(Ptr{Any}(tail.value))::SpacialQTreeNode
head = seek_head(listnode)
unsafe_pointer_to_objref(Ptr{Any}(head.value))::SpacialQTreeNode
end
function Base.empty!(t::LinkedSpacialQTree, label)
if haskey(t.map, label) && !isempty(t.map[label])
Expand Down

0 comments on commit 9197d23

Please sign in to comment.