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 support for the StdDeque and its iterator interface #376

Merged
merged 2 commits into from
Sep 25, 2023
Merged
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
32 changes: 21 additions & 11 deletions src/StdLib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,27 @@ 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)

function StdDeque(v::Vector{T}) where {T}
return StdDeque{T}(v, length(v))
function StdDeque() where {T}
return StdDeque{T}()
end

Base.IndexStyle(::Type{<:StdDeque}) = IndexLinear()
Base.size(v::StdDeque) = (Int(cppsize(v)),)
Base.getindex(v::StdDeque, i::Int) = cxxgetindex(v,i)[]
Base.setindex!(v::StdDeque{T}, val, i::Int) where {T} = cxxsetindex!(v, convert(T,val), i)
Base.push!(v::StdDeque, x) = push_back!(v, x)
Base.pushfirst!(v::StdDeque, x) = push_front!(v, x)
Base.pop!(v::StdDeque) = pop_back!(v)
Base.popfirst!(v::StdDeque) = pop_front!(v)
Base.resize!(v::StdDeque, n::Integer) = resize!(v, n)
end
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.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.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