diff --git a/Project.toml b/Project.toml index 9615d0a00..d192c3495 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "DataStructures" uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" -version = "0.19.0-DEV" +version = "0.18.0" [deps] Compat = "34da2185-b29b-5c13-b0c7-acf172513d20" diff --git a/src/stack.jl b/src/stack.jl index f3e0f98ac..c6b03109a 100644 --- a/src/stack.jl +++ b/src/stack.jl @@ -43,15 +43,16 @@ Get the top item from the stack. Sometimes called peek. Base.first(s::Stack) = last(s.store) Base.last(s::Stack) = first(s.store) -function Base.push!(s::Stack, x) - push!(s.store, x) - return s -end +Base.push!(s::Stack, x) = (push!(s.store, x); s) +Base.pushfirst!(s::Stack, x) = (pushfirst!(s.store, x); s) Base.pop!(s::Stack) = pop!(s.store) +Base.popfirst!(s::Stack) = popfirst!(s.store) Base.empty!(s::Stack) = (empty!(s.store); s) +Base.collect(s::Stack) = collect(s.store) + Base.iterate(st::Stack, s...) = iterate(Iterators.reverse(st.store), s...) Iterators.reverse(s::Stack{T}) where {T} = DequeIterator{T}(s.store) diff --git a/test/test_stack.jl b/test/test_stack.jl index 95b914fea..422282fed 100644 --- a/test/test_stack.jl +++ b/test/test_stack.jl @@ -38,6 +38,28 @@ @test isempty(s) == (i == n) @test length(s) == n - i end + + for i = 1 : n + pushfirst!(s, i) + @test first(s) == 1 + @test last(s) == i + @test !isempty(s) + @test length(s) == i + end + + @test collect(s) == collect(n:-1:1) + + for i = 1 : n + x = popfirst!(s) + @test x == n - i + 1 + if i < n + @test first(s) == 1 + else + @test_throws ArgumentError first(s) + end + @test isempty(s) == (i == n) + @test length(s) == n - i + end end @testset "==" begin