Skip to content

Commit

Permalink
feat: StdPriorityQueue (#441)
Browse files Browse the repository at this point in the history
* feat: StdPriorityQueue

* test: StdPriorityQueue
  • Loading branch information
PraneethJain authored Jun 16, 2024
1 parent 26ff819 commit 7d07332
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/CxxWrap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -948,8 +948,8 @@ ConstCxxPtr, ConstCxxRef, CxxRef, CxxPtr,
CppEnum, ConstArray, CxxBool, CxxLong, CxxULong, CxxChar, CxxChar16, CxxChar32, CxxWchar, CxxUChar, CxxSignedChar,
CxxLongLong, CxxULongLong, ptrunion, gcprotect, gcunprotect, isnull

using .StdLib: StdVector, StdString, StdWString, StdValArray, StdThread, StdDeque, StdQueue, StdSet, StdMultiset, StdUnorderedSet, StdUnorderedMultiset
using .StdLib: StdVector, StdString, StdWString, StdValArray, StdThread, StdDeque, StdQueue, StdSet, StdMultiset, StdUnorderedSet, StdUnorderedMultiset, StdPriorityQueue

export StdLib, StdVector, StdString, StdWString, StdValArray, StdThread, StdDeque, StdQueue, StdSet, StdMultiset, StdUnorderedSet, StdUnorderedMultiset
export StdLib, StdVector, StdString, StdWString, StdValArray, StdThread, StdDeque, StdQueue, StdSet, StdMultiset, StdUnorderedSet, StdUnorderedMultiset, StdPriorityQueue

end # module
12 changes: 12 additions & 0 deletions src/StdLib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,18 @@ for StdMultisetType in (StdMultiset, StdUnorderedMultiset)
Base.count(x, v::StdMultisetType) = multiset_count(v, x)
end

Base.size(v::StdPriorityQueue) = (Int(cppsize(v)),)
Base.length(v::StdPriorityQueue) = Int(cppsize(v))
Base.isempty(v::StdPriorityQueue) = pq_isempty(v)
Base.first(v::StdPriorityQueue) = isempty(v) ? nothing : pq_top(v)
Base.push!(v::StdPriorityQueue, x) = (pq_push!(v, x); v)
function Base.pop!(v::StdPriorityQueue)
isempty(v) && throw(ArgumentError("Cannot pop from an empty priority queue"))
val = pq_top(v)
pq_pop!(v)
return val
end

function Base.fill!(v::T, x) where T <: Union{StdVector, StdValArray, StdDeque}
StdFill(v, x)
return v
Expand Down
20 changes: 20 additions & 0 deletions test/stdlib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,26 @@ let
@test length(queue) == 1
end

@testset "StdPriorityQueue" begin
pq = StdPriorityQueue{Int64}()
@test length(pq) == 0
push!(pq, 5)
push!(pq, 1)
@test isempty(pq) == false
pq = push!(pq, 4)
pq = push!(pq, 10)
@test length(pq) == 4
@test first(pq) == 10
@test pop!(pq) == 10
@test length(pq) == 3
@test pop!(pq) == 5
@test pop!(pq) == 4
@test pop!(pq) == 1
@test isempty(pq) == true
@test isnothing(first(pq))
@test_throws ArgumentError pop!(pq)
end

@testset "StdSet and StdUnorderedSet" begin
for StdSetType in (StdSet, StdUnorderedSet)
@testset "Set with integers" begin
Expand Down

0 comments on commit 7d07332

Please sign in to comment.