Skip to content

Commit 1f9544c

Browse files
abdoeibarche
authored andcommitted
Added support for the StdDeque and its iterator interface
1 parent 8085e0d commit 1f9544c

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/StdLib.jl

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ Base.size(v::StdValArray) = (Int(cppsize(v)),)
126126
Base.getindex(v::StdValArray, i::Int) = cxxgetindex(v,i)[]
127127
Base.setindex!(v::StdValArray{T}, val, i::Int) where {T} = cxxsetindex!(v, convert(T,val), i)
128128

129-
function StdDeque(v::Vector{T}) where {T}
130-
return StdDeque{T}(v, length(v))
129+
function StdDeque() where {T}
130+
return StdDeque{T}()
131131
end
132132

133133
Base.IndexStyle(::Type{<:StdDeque}) = IndexLinear()
@@ -140,3 +140,24 @@ Base.pop!(v::StdDeque) = pop_back!(v)
140140
Base.popfirst!(v::StdDeque) = pop_front!(v)
141141
Base.resize!(v::StdDeque, n::Integer) = resize!(v, n)
142142
end
143+
144+
Base.IndexStyle(::Type{<:StdDeque}) = IndexLinear()
145+
Base.size(d::StdDeque) = (Int(cppsize(d)),)
146+
Base.resize!(d::StdDeque, n::Integer) = resize(d, n)
147+
Base.getindex(d::StdDeque, i::Int) = cxxgetindex(d, i)[]
148+
Base.setindex!(d::StdDeque{T}, val, i::Int) where {T} = cxxsetindex(d, convert(T, val), i)
149+
#TODO: edit the cxx part to enable push to get more than two arguments
150+
Base.push!(d::StdDeque, x) = push_back(d, x)
151+
Base.pushfirst!(d::StdDeque, x) = push_front(d, x)
152+
Base.pop!(d::StdDeque) = pop_back(d)
153+
Base.popfirst!(d::StdDeque) = pop_front(d)
154+
Base.isempty(d::StdDeque) = isEmpty(d)
155+
Base.empty!(d::StdDeque) = clear(d)
156+
157+
# Iteration utilities
158+
Base.:(==)(a::StdIterator, b::StdIterator) = iterator_is_equal(a, b)
159+
_deque_iteration_tuple(d::StdDeque, state::StdIterator) = (state == iteratorend(d)) ? nothing : (iterator_value(state), state)
160+
Base.iterate(d::StdDeque) = _deque_iteration_tuple(d, iteratorbegin(d))
161+
Base.iterate(d::StdDeque, state::StdIterator) = (state != iteratorend(d)) ? _deque_iteration_tuple(d, iterator_next(state)) : nothing
162+
#TODO:remove the iterator_value method from the cxx part, since it is not needed
163+
end # module

0 commit comments

Comments
 (0)