Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change enqueue! dequeue! to push! popfirst! #743

Merged
merged 9 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/src/stack_and_queue.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ Usage of Queue:

```julia
q = Queue{Int}()
enqueue!(q, x)
push!(q, x)
x = first(q)
x = last(q)
x = dequeue!(q)
x = popfirst!(q)
```

Both `Stack` and `Queue` implement the Iterator interface; iterating
Expand Down
3 changes: 3 additions & 0 deletions src/deprecations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
Base.@deprecate_binding DisjointSets DisjointSet
Base.@deprecate_binding IntDisjointSets IntDisjointSet
@deprecate DisjointSets(xs...) DisjointSet(xs)
# Enqueue and dequeue deprecations
@deprecate enqueue!(q::Queue, x) Base.push!(q, x)
@deprecate dequeue!(q::Queue) Base.popfirst!(q)
8 changes: 4 additions & 4 deletions src/queue.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ Base.first(s::Queue) = first(s.store)
Base.last(s::Queue) = last(s.store)

"""
enqueue!(s::Queue, x)
push!(s::Queue, x)

Inserts the value `x` to the end of the queue `s`.
"""
function enqueue!(s::Queue, x)
function Base.push!(s::Queue, x)
push!(s.store, x)
return s
end

"""
dequeue!(s::Queue)
popfirst!(s::Queue)

Removes an element from the front of the queue `s` and returns it.
"""
dequeue!(s::Queue) = popfirst!(s.store)
Base.popfirst!(s::Queue) = popfirst!(s.store)

Base.empty!(s::Queue) = (empty!(s.store); s)

Expand Down
37 changes: 36 additions & 1 deletion test/test_deprecations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,46 @@ function test_reverse_iter(it::T) where T
end
@testset "reverse_iter" begin
@testset "Queue" begin
q = Queue{Int}(); enqueue!(q, 1); enqueue!(q, 2)
q = Queue{Int}(); push!(q, 1); push!(q, 2)
test_reverse_iter(q)
end
@testset "Stack" begin
s = Stack{Int}(); push!(s, 1); push!(s, 2)
test_reverse_iter(s)
end
end

@testset "enqueue! dequeue!" begin
s = Queue{Int}(5)
n = 100

@test length(s) == 0
@test eltype(s) == Int
@test eltype(typeof(s)) == Int
@test isempty(s)
@test_throws ArgumentError first(s)
@test_throws ArgumentError last(s)
@test_throws ArgumentError dequeue!(s)

for i = 1 : n
enqueue!(s, i)
@test first(s) == 1
@test last(s) == i
@test !isempty(s)
@test length(s) == i
end

for i = 1 : n
x = dequeue!(s)
@test x == i
if i < n
@test first(s) == i + 1
@test last(s) == n
else
@test_throws ArgumentError first(s)
@test_throws ArgumentError last(s)
end
@test isempty(s) == (i == n)
@test length(s) == n - i
end
end
20 changes: 10 additions & 10 deletions test/test_queue.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@
@test isempty(s)
@test_throws ArgumentError first(s)
@test_throws ArgumentError last(s)
@test_throws ArgumentError dequeue!(s)
@test_throws ArgumentError popfirst!(s)

for i = 1 : n
enqueue!(s, i)
push!(s, i)
@test first(s) == 1
@test last(s) == i
@test !isempty(s)
@test length(s) == i
end

for i = 1 : n
x = dequeue!(s)
x = popfirst!(s)
@test x == i
if i < n
@test first(s) == i + 1
Expand All @@ -48,24 +48,24 @@
s = Queue{Int}()

@test s == t
enqueue!(s, 10)
push!(s, 10)
@test s != t
enqueue!(t, 10)
push!(t, 10)
@test s == t
enqueue!(t, 20)
push!(t, 20)
@test s != t

@testset "different types" begin
r = Queue{Float32}()
enqueue!(r, 10)
push!(r, 10)
@test s == r
end
end

@testset "emptyness" begin
s = Queue{Int}()
enqueue!(s, 1)
enqueue!(s, 3)
push!(s, 1)
push!(s, 3)
@test !isempty(s)
empty!(s)
@test isempty(s)
Expand All @@ -79,7 +79,7 @@
arr = Int64[]

for i = 1:n
enqueue!(q,i)
push!(q,i)
push!(arr,i)
end

Expand Down