From ac06c3d504b8a69c465a50b1f5254429476f83e0 Mon Sep 17 00:00:00 2001 From: Abdo Eid Date: Sat, 4 May 2024 04:34:55 +0300 Subject: [PATCH 1/3] Added the StdDequeIterator and its testset --- src/StdLib.jl | 54 ++++++++++++++++++++++++++++----------------- test/stdlib.jl | 60 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 93 insertions(+), 21 deletions(-) diff --git a/src/StdLib.jl b/src/StdLib.jl index 5a11931..22996c1 100644 --- a/src/StdLib.jl +++ b/src/StdLib.jl @@ -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.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 +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) +Base.empty!(v::StdDeque) = clear(v) + +Base.:(==)(a::StdDequeIterator, b::StdDequeIterator) = iterator_is_equal(a,b) +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) + +function Base.fill!(v::T, x) where T <: Union{StdVector, StdValArray, StdDeque} + StdFill(v, x) + return v +end + +end diff --git a/test/stdlib.jl b/test/stdlib.jl index 37d7b70..8c7a2b9 100644 --- a/test/stdlib.jl +++ b/test/stdlib.jl @@ -122,4 +122,62 @@ let @test state == nothing end -end \ No newline at end of file +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 From a82df04799951dc92240b126d69fefc946c182a7 Mon Sep 17 00:00:00 2001 From: Abdo Eid Date: Thu, 20 Jun 2024 19:03:02 +0300 Subject: [PATCH 2/3] undo the renaming parameter StdDeque from `d` to `v` --- src/StdLib.jl | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/StdLib.jl b/src/StdLib.jl index 22996c1..425857c 100644 --- a/src/StdLib.jl +++ b/src/StdLib.jl @@ -132,15 +132,15 @@ 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) -Base.empty!(v::StdDeque) = clear(v) +Base.size(d::StdDeque) = (Int(cppsize(v)),) +Base.getindex(d::StdDeque, i::Int) = cxxgetindex(v,i)[] +Base.setindex!(d::StdDeque{T}, val, i::Int) where {T} = cxxsetindex(v, convert(T,val), i) +Base.push!(d::StdDeque, x) = push_back(v, x) +Base.pushfirst!(d::StdDeque, x) = push_front(v, x) +Base.pop!(d::StdDeque) = pop_back(v) +Base.popfirst!(d::StdDeque) = pop_front(v) +Base.resize!(d::StdDeque, n::Integer) = resize(v, n) +Base.empty!(d::StdDeque) = clear(v) Base.:(==)(a::StdDequeIterator, b::StdDequeIterator) = iterator_is_equal(a,b) function _iteration_tuple(d::StdDeque, state::StdDequeIterator) From 4dc10a27cb636d7b80aa060b37b1f8cd3b2007ff Mon Sep 17 00:00:00 2001 From: Abdo Eid Date: Thu, 20 Jun 2024 21:22:58 +0300 Subject: [PATCH 3/3] fixed an error in the last commit --- src/StdLib.jl | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/StdLib.jl b/src/StdLib.jl index 425857c..febac44 100644 --- a/src/StdLib.jl +++ b/src/StdLib.jl @@ -132,15 +132,15 @@ function StdDeque() where {T} return StdDeque{T}() end Base.IndexStyle(::Type{<:StdDeque}) = IndexLinear() -Base.size(d::StdDeque) = (Int(cppsize(v)),) -Base.getindex(d::StdDeque, i::Int) = cxxgetindex(v,i)[] -Base.setindex!(d::StdDeque{T}, val, i::Int) where {T} = cxxsetindex(v, convert(T,val), i) -Base.push!(d::StdDeque, x) = push_back(v, x) -Base.pushfirst!(d::StdDeque, x) = push_front(v, x) -Base.pop!(d::StdDeque) = pop_back(v) -Base.popfirst!(d::StdDeque) = pop_front(v) -Base.resize!(d::StdDeque, n::Integer) = resize(v, n) -Base.empty!(d::StdDeque) = clear(v) +Base.size(d::StdDeque) = (Int(cppsize(d)),) +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.resize!(d::StdDeque, n::Integer) = resize(d, n) +Base.empty!(d::StdDeque) = clear(d) Base.:(==)(a::StdDequeIterator, b::StdDequeIterator) = iterator_is_equal(a,b) function _iteration_tuple(d::StdDeque, state::StdDequeIterator)