Skip to content

Commit

Permalink
Added support for the StdDeque and its iterator interface
Browse files Browse the repository at this point in the history
  • Loading branch information
abdoei authored and barche committed Sep 25, 2023
1 parent 8085e0d commit 9e1eb76
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/StdLib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ 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()
Expand All @@ -140,3 +140,24 @@ 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.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.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

0 comments on commit 9e1eb76

Please sign in to comment.