diff --git a/.travis.yml b/.travis.yml index f2b1db8a4..6b53b3bf9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,6 +20,8 @@ after_success: # Documenter auto-deploy # https://juliadocs.github.io/Documenter.jl/stable/man/hosting/#.travis.yml-Configuration-1 jobs: + allow_failures: + - julia: nightly include: - name: "Benchmark" julia: 1.2 diff --git a/Project.toml b/Project.toml index a3d3f358c..5e23d0d5f 100644 --- a/Project.toml +++ b/Project.toml @@ -2,6 +2,7 @@ name = "DataStructures" uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" version = "0.18.5" + [deps] Compat = "34da2185-b29b-5c13-b0c7-acf172513d20" InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" diff --git a/appveyor.yml b/appveyor.yml index ef1ff1a05..55ef8a7fb 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -9,9 +9,9 @@ platform: ## uncomment the following lines to allow failures on nightly julia ## (tests will run but not make your overall status red) -#matrix: -# allow_failures: -# - julia_version: latest +matrix: + allow_failures: + - julia_version: latest branches: only: diff --git a/docs/src/stack_and_queue.md b/docs/src/stack_and_queue.md index da3f2849b..aa7148bef 100644 --- a/docs/src/stack_and_queue.md +++ b/docs/src/stack_and_queue.md @@ -1,4 +1,3 @@ - # Stack and Queue The `Stack` and `Queue` types are a light-weight wrapper of a deque @@ -7,17 +6,17 @@ type, which respectively provide interfaces for LIFO and FIFO access. Usage of Stack: ```julia -s = Stack{Int}() # create a stack -isempty(s) # check whether the stack is empty -length(s) # get the number of elements -eltype(s) # get the type of elements -push!(s, 1) # push back a item -first(s) # get an item from the top of stack -pop!(s) # get and remove a first item -empty!(s) # make a stack empty -iterate(s::Stack) # Get a LIFO iterator of a stack -reverse_iter(s::Stack{T}) # Get a FILO iterator of a stack -s1 == s2 # check whether the two stacks are same +s = Stack{Int}() # create a stack +isempty(s) # check whether the stack is empty +length(s) # get the number of elements +eltype(s) # get the type of elements +push!(s, 1) # push back a item +first(s) # get an item from the top of stack +pop!(s) # get and remove a first item +empty!(s) # make a stack empty +iterate(s::Stack) # Get a LIFO iterator of a stack +Iterators.reverse(s::Stack{T}) # Get a FILO iterator of a stack +s1 == s2 # check whether the two stacks are same ``` Usage of Queue: diff --git a/src/DataStructures.jl b/src/DataStructures.jl index 31814754d..c9d386f39 100644 --- a/src/DataStructures.jl +++ b/src/DataStructures.jl @@ -1,6 +1,6 @@ module DataStructures - using Base: HasEltype, HasLength, IteratorEltype, IteratorSize, SizeUnknown, + using Base: Iterators, HasEltype, HasLength, IteratorEltype, IteratorSize, SizeUnknown, lt, Ordering, ForwardOrdering, Forward, ReverseOrdering, Reverse, Lt, isbitsunion, isiterable, dict_with_eltype, KeySet, Callable, _tablesz, findnextnot, unsafe_getindex, unsafe_setindex!, peek @@ -15,7 +15,7 @@ module DataStructures export complement, complement! export Deque, Stack, Queue, CircularDeque - export enqueue!, dequeue!, dequeue_pair!, update!, reverse_iter + export enqueue!, dequeue!, dequeue_pair!, update! export capacity, num_blocks, top_with_handle, sizehint! export Accumulator, counter, reset!, inc!, dec! @@ -112,4 +112,4 @@ module DataStructures include("splay_tree.jl") include("deprecations.jl") -end +end \ No newline at end of file diff --git a/src/deprecations.jl b/src/deprecations.jl index 276c2fedb..1367b7937 100644 --- a/src/deprecations.jl +++ b/src/deprecations.jl @@ -1,3 +1,5 @@ # 0.18 deprecations. Remove before releasing 0.19 @deprecate path(t::Trie, str::AbstractString) partial_path(t::Trie, str::AbstractString) @deprecate find_root find_root! +@deprecate top first +@deprecate reverse_iter Iterators.reverse diff --git a/src/deque.jl b/src/deque.jl index 7b74d27cb..17599b1ff 100644 --- a/src/deque.jl +++ b/src/deque.jl @@ -115,6 +115,8 @@ struct DequeIterator{T} q::Deque end +Base.last(qi::DequeIterator) = last(qi.q) + function Base.iterate(qi::DequeIterator{T}, (cb, i) = (qi.q.head, qi.q.head.front)) where T i > cb.back && return nothing x = cb.data[i] @@ -130,11 +132,7 @@ end # Backwards deque iteration -struct ReverseDequeIterator{T} - q::Deque -end - -function Base.iterate(qi::ReverseDequeIterator{T}, (cb, i) = (qi.q.rear, qi.q.rear.back)) where T +function Base.iterate(qi::Iterators.Reverse{<:Deque}, (cb, i) = (qi.itr.rear, qi.itr.rear.back)) i < cb.front && return nothing x = cb.data[i] @@ -148,12 +146,9 @@ function Base.iterate(qi::ReverseDequeIterator{T}, (cb, i) = (qi.q.rear, qi.q.re return (x, (cb, i)) end -reverse_iter(q::Deque{T}) where {T} = ReverseDequeIterator{T}(q) - Base.iterate(q::Deque{T}, s...) where {T} = iterate(DequeIterator{T}(q), s...) Base.length(qi::DequeIterator{T}) where {T} = qi.q.len -Base.length(qi::ReverseDequeIterator{T}) where {T} = qi.q.len Base.collect(q::Deque{T}) where {T} = T[x for x in q] diff --git a/src/queue.jl b/src/queue.jl index 9e7d969c1..7a77e2a99 100644 --- a/src/queue.jl +++ b/src/queue.jl @@ -42,6 +42,6 @@ Base.empty!(s::Queue) = (empty!(s.store); s) Base.iterate(q::Queue, s...) = iterate(q.store, s...) -reverse_iter(q::Queue) = reverse_iter(q.store) +Iterators.reverse(q::Queue) = Iterators.reverse(q.store) Base.:(==)(x::Queue, y::Queue) = x.store == y.store diff --git a/src/stack.jl b/src/stack.jl index 1e8c5af53..f3e0f98ac 100644 --- a/src/stack.jl +++ b/src/stack.jl @@ -41,6 +41,7 @@ Base.eltype(::Type{Stack{T}}) where T = T 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) @@ -51,13 +52,8 @@ Base.pop!(s::Stack) = pop!(s.store) Base.empty!(s::Stack) = (empty!(s.store); s) -Base.iterate(st::Stack, s...) = iterate(reverse_iter(st.store), s...) +Base.iterate(st::Stack, s...) = iterate(Iterators.reverse(st.store), s...) -""" - reverse_iterate(s::Stack) - -Get a FILO iterator of a stack -""" -reverse_iter(s::Stack{T}) where {T} = DequeIterator{T}(s.store) +Iterators.reverse(s::Stack{T}) where {T} = DequeIterator{T}(s.store) Base.:(==)(x::Stack, y::Stack) = x.store == y.store diff --git a/test/test_deprecations.jl b/test/test_deprecations.jl index 9e71e9067..f33cd60f0 100644 --- a/test/test_deprecations.jl +++ b/test/test_deprecations.jl @@ -14,4 +14,30 @@ @test collect(path(t, "robb")) == [t0, t1, t2, t3] @test collect(path(t, "ro")) == [t0, t1, t2] @test collect(path(t, "roa")) == [t0, t1, t2] -end \ No newline at end of file +end + +@testset "top" begin + hh = BinaryMinHeap{Float64}([1,2,3]) + @test top(hh) == 1 +end + +function test_reverse_iter(it::T) where T + arr = [i for i in it] + index = length(arr) + for i in reverse_iter(it) + @test arr[index] == i + index -= 1 + end + + @test reverse(arr) == [i for i in reverse_iter(it)] +end +@testset "reverse_iter" begin + @testset "Queue" begin + q = Queue{Int}(); enqueue!(q, 1); enqueue!(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 diff --git a/test/test_queue.jl b/test/test_queue.jl index a1c421b21..3686198b5 100644 --- a/test/test_queue.jl +++ b/test/test_queue.jl @@ -93,14 +93,17 @@ @testset "reverse iterator" begin index = length(arr) - for i in reverse_iter(q) + for i in Iterators.reverse(q) @test(arr[index] == i) index -= 1 end end @test arr == [i for i in q] - @test reverse(arr) == [i for i in reverse_iter(q)] + @test reverse(arr) == [i for i in Iterators.reverse(q)] + @test first(Iterators.reverse(q)) === last(q) + @test last(Iterators.reverse(q)) === first(q) + @test length(Iterators.reverse(q)) === length(q) end end # @testset Queue diff --git a/test/test_stack.jl b/test/test_stack.jl index 448675fd6..95b914fea 100644 --- a/test/test_stack.jl +++ b/test/test_stack.jl @@ -87,14 +87,17 @@ @testset "reverse iterator" begin index = 1 - for i in reverse_iter(stk) + for i in Iterators.reverse(stk) @test(arr[index] == i) index += 1 end end - @test arr == [i for i in reverse_iter(stk)] + @test arr == [i for i in Iterators.reverse(stk)] @test reverse(arr) == [i for i in stk] + @test first(Iterators.reverse(stk)) === last(stk) + @test last(Iterators.reverse(stk)) === first(stk) + @test length(Iterators.reverse(stk)) === length(stk) end end # @testset Stack