From eaea1600a8d662a36c97077ae56399fa38fc5695 Mon Sep 17 00:00:00 2001 From: Koustav Chowdhury Date: Sun, 31 Dec 2023 20:47:07 +0530 Subject: [PATCH 1/2] Fix problem with deque clear! --- src/deque.jl | 2 ++ test/test_deque.jl | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/deque.jl b/src/deque.jl index edd5aa94b..9d674c09d 100644 --- a/src/deque.jl +++ b/src/deque.jl @@ -38,6 +38,8 @@ isrear(blk::DequeBlock) = blk.next === blk # reset the block to empty, and position function reset!(blk::DequeBlock{T}, front::Int) where T + empty!(blk.data) + blk.data = Vector{T}(undef, blk.capa) blk.front = front blk.back = front - 1 blk.prev = blk diff --git a/test/test_deque.jl b/test/test_deque.jl index 8c35ba289..d5891fee0 100644 --- a/test/test_deque.jl +++ b/test/test_deque.jl @@ -196,5 +196,20 @@ @test length(sprint(dump,q)) >= 0 @test typeof(empty!(q)) === typeof(Deque{Int}()) @test isempty(q) + @test num_blocks(q) == 1 + @test q.head === q.rear + @test sizeof(q.head.data) == q.head.capa * sizeof(eltype(q)) + end + + @testset "push! after empty!" begin + q = Deque{Int}(1) + push!(q,1) + push!(q,2) + empty!(q) + push!(q,3) + @test length(q) == 1 + @test first(q) == 3 + @test last(q) == 3 + @test num_blocks(q) == 1 end end # @testset Deque From 6dc9f8a2f1ac243c3e34717ef95ff3b89c3f2b4c Mon Sep 17 00:00:00 2001 From: Koustav Chowdhury Date: Tue, 2 Jan 2024 09:53:19 +0530 Subject: [PATCH 2/2] Use resize! instead of allocating undef in block head --- src/deque.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/deque.jl b/src/deque.jl index 9d674c09d..ce4544b10 100644 --- a/src/deque.jl +++ b/src/deque.jl @@ -39,7 +39,7 @@ isrear(blk::DequeBlock) = blk.next === blk function reset!(blk::DequeBlock{T}, front::Int) where T empty!(blk.data) - blk.data = Vector{T}(undef, blk.capa) + resize!(blk.data, blk.capa) blk.front = front blk.back = front - 1 blk.prev = blk