Skip to content

Commit

Permalink
feat: add StdStack
Browse files Browse the repository at this point in the history
  • Loading branch information
PraneethJain committed Jun 16, 2024
1 parent a3be16d commit 93f9ecb
Show file tree
Hide file tree
Showing 3 changed files with 43 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, StdPriorityQueue
using .StdLib: StdVector, StdString, StdWString, StdValArray, StdThread, StdDeque, StdQueue, StdStack, StdSet, StdMultiset, StdUnorderedSet, StdUnorderedMultiset, StdPriorityQueue

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

end # module
7 changes: 7 additions & 0 deletions src/StdLib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,13 @@ Base.push!(v::StdQueue, x) = (push_back!(v, x); v)
Base.first(v::StdQueue) = front(v)
Base.pop!(v::StdQueue) = (pop_front!(v); v)

Base.size(v::StdStack) = (Int(cppsize(v)),)
Base.length(v::StdStack) = Int(cppsize(v))
Base.isempty(v::StdStack) = stack_isempty(v)
Base.push!(v::StdStack, x) = (stack_push!(v, x); v)
Base.first(v::StdStack) = stack_top(v)
Base.pop!(v::StdStack) = (stack_pop!(v); v)

for StdSetType in (StdSet, StdUnorderedSet)
Base.size(v::StdSetType) = (Int(cppsize(v)),)
Base.length(v::StdSetType) = Int(cppsize(v))
Expand Down
34 changes: 34 additions & 0 deletions test/stdlib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,40 @@ end
@test_throws ArgumentError pop!(pq)
end

@testset "StdStack" begin
@testset "Stack with integers" begin
stack = StdStack{Int64}()
@test length(stack) == 0
@test isempty(stack) == true
push!(stack, 7)
stack = push!(stack, 20)
push!(stack, 2)
@test first(stack) == 2
@test length(stack) == 3
stack = pop!(stack)
@test first(stack) == 20
@test isempty(stack) == false
while !isempty(stack)
pop!(stack)
end
@test isempty(stack) == true
end

@testset "Stack with floats" begin
stack = StdStack{Float64}()
@test length(stack) == 0
@test isempty(stack) == true
push!(stack, 1.54)
push!(stack, 20.2)
@test length(stack) == 2
@test first(stack) == 20.2
pop!(stack)
@test first(stack) == 1.54
@test length(stack) == 1
@test isempty(stack) == false
end
end

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

0 comments on commit 93f9ecb

Please sign in to comment.