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

Added the StdDequeIterator and its testset #433

Closed
Closed
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
40 changes: 27 additions & 13 deletions src/StdLib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,27 +126,41 @@ Base.size(v::StdValArray) = (Int(cppsize(v)),)
Base.getindex(v::StdValArray, i::Int) = cxxgetindex(v,i)[]
Base.setindex!(v::StdValArray{T}, val, i::Int) where {T} = cxxsetindex!(v, convert(T,val), i)


# Deque
function StdDeque() where {T}
return StdDeque{T}()
end

Base.IndexStyle(::Type{<:StdDeque}) = IndexLinear()
Base.size(d::StdDeque) = (Int(cppsize(d)),)
Base.resize!(d::StdDeque, n::Integer) = resize(d, n)
Base.getindex(d::StdDeque, i::Int) = cxxgetindex(d, i)[]
Base.setindex!(d::StdDeque{T}, val, i::Int) where {T} = cxxsetindex(d, convert(T, val), i)
#TODO: edit the cxx part to enable push to get more than two arguments
Base.getindex(d::StdDeque, i::Int) = cxxgetindex(d,i)[]
Base.setindex!(d::StdDeque{T}, val, i::Int) where {T} = cxxsetindex(d, convert(T,val), i)
Base.push!(d::StdDeque, x) = push_back(d, x)
Base.pushfirst!(d::StdDeque, x) = push_front(d, x)
Base.pop!(d::StdDeque) = pop_back(d)
Base.popfirst!(d::StdDeque) = pop_front(d)
Base.isempty(d::StdDeque) = isEmpty(d)
Base.resize!(d::StdDeque, n::Integer) = resize(d, n)
Base.empty!(d::StdDeque) = clear(d)

# Iteration utilities
Base.:(==)(a::StdIterator, b::StdIterator) = iterator_is_equal(a, b)
_deque_iteration_tuple(d::StdDeque, state::StdIterator) = (state == iteratorend(d)) ? nothing : (iterator_value(state), state)
Base.iterate(d::StdDeque) = _deque_iteration_tuple(d, iteratorbegin(d))
Base.iterate(d::StdDeque, state::StdIterator) = (state != iteratorend(d)) ? _deque_iteration_tuple(d, iterator_next(state)) : nothing
#TODO:remove the iterator_value method from the cxx part, since it is not needed
end # module
Base.:(==)(a::StdDequeIterator, b::StdDequeIterator) = iterator_is_equal(a,b)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests fail because StdDequeIterator is not defined.

Perhaps this PR is supposed to be used together with a changed libcxxwrap-julia ? Perhaps with JuliaInterop/libcxxwrap-julia#133 ? (It would be a good idea to point out something like this in the PR description)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

function _iteration_tuple(d::StdDeque, state::StdDequeIterator)
if state == iteratorend(d) return nothing end
return (iterator_value(state), state)
end
Base.iterate(d::StdDeque) = _iteration_tuple(d, iteratorbegin(d))
Base.iterate(d::StdDeque, state::StdDequeIterator) = _iteration_tuple(d, iterator_next(state))



# Queue
Base.size(v::StdQueue) = (Int(cppsize(v)),)
Base.push!(v::StdQueue, x) = push_back(v, x)
Base.first(v::StdQueue) = front(v)
Base.pop!(v::StdQueue) = pop_front(v)
Comment on lines +156 to +159
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code seems to be unrelated to StdDeque? Perhaps this PR needs to be rebased on the latest CxxWrap master?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't handle this nicely in one commit and keep this out of the changes as the testjll-stditerator branch itself is far behind the master.


function Base.fill!(v::T, x) where T <: Union{StdVector, StdValArray, StdDeque}
StdFill(v, x)
return v
end

end
60 changes: 59 additions & 1 deletion test/stdlib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,62 @@ let
@test state == nothing
end

end
let
@show "test queue"
queue = StdQueue{Int64}()
@test length(queue) == 0
push!(queue, 10)
push!(queue, 20)
@test length(queue) == 2
@test first(queue) == 10
pop!(queue)
@test first(queue) == 20
@test length(queue) == 1
end

@static if isdefined(StdLib, :HAS_RANGES)

@testset "StdFill" begin
@testset "fill StdVector" begin
v = StdVector{Int64}([1, 2, 3, 4, 5])
fill!(v, 1)
for x in v
@test x == 1
end
end

@testset "fill StdValArray" begin
v = StdValArray([1.0, 2.0, 3.0])
fill!(v, 2)
for x in v
@test x == 2
end
end

@testset "fill StdDeque" begin
deq = StdDeque{Int64}()
for i = 1:10
push!(deq, i)
end
fill!(deq, 3)
for x in deq
@test x == 3
end
end
end

@testset "StdDequeIterator" begin
d = StdDeque{Int64}()
for i = 1:4
push!(d, i)
end
iteration_tuple = iterate(d)
for i = 1:4
@test iteration_tuple[1] == i
iteration_tuple = iterate(d, iteration_tuple[2])
end
end

end

end # StdLib
Loading