From 3776487f01ecdd9951dc9d193aca35a742fa88ef Mon Sep 17 00:00:00 2001 From: Jun Tian Date: Thu, 17 Mar 2022 10:07:30 +0800 Subject: [PATCH 1/2] fix #12 --- src/CircularArrayBuffers.jl | 25 ++++++++++++++++++++----- test/runtests.jl | 6 ++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/CircularArrayBuffers.jl b/src/CircularArrayBuffers.jl index f717c4b..1572fbe 100644 --- a/src/CircularArrayBuffers.jl +++ b/src/CircularArrayBuffers.jl @@ -47,6 +47,25 @@ Base.size(cb::CircularArrayBuffer{T,N}, i::Integer) where {T,N} = i == N ? cb.nf Base.size(cb::CircularArrayBuffer{T,N}) where {T,N} = ntuple(i -> size(cb, i), N) Base.getindex(cb::CircularArrayBuffer{T,N}, i::Int) where {T,N} = getindex(cb.buffer, _buffer_index(cb, i)) Base.getindex(cb::CircularArrayBuffer{T,N}, I...) where {T,N} = getindex(cb.buffer, Base.front(I)..., _buffer_frame(cb, Base.last(I))) + +# !!! +# strange, but we need this function to show `CircularVectorBuffer` correctly +# `Base.print_array` will try to use `isassigned(cb, i, j)` to print elements +# And, `X::AbstractVector[2, 1]` is valid !!! +# without this line +# ```julia +# julia> cb = CircularArrayBuffer([1., 2.]) +# CircularVectorBuffer(::Vector{Float64}) with eltype Float64: +# 1.0 +# 2.0 + +# julia> push!(cb, 3) +# CircularVectorBuffer(::Vector{Float64}) with eltype Float64: +# #undef +# #undef +# ``` +Base.getindex(cb::CircularVectorBuffer, i, j) = getindex(cb.buffer, _buffer_frame(cb, i), j) + Base.setindex!(cb::CircularArrayBuffer{T,N}, v, i::Int) where {T,N} = setindex!(cb.buffer, v, _buffer_index(cb, i)) Base.setindex!(cb::CircularArrayBuffer{T,N}, v, I...) where {T,N} = setindex!(cb.buffer, v, Base.front(I)..., _buffer_frame(cb, Base.last(I))) @@ -92,11 +111,7 @@ function Base.push!(cb::CircularArrayBuffer{T,N}, data) where {T,N} end if N == 1 i = _buffer_frame(cb, cb.nframes) - if ndims(data) == 0 - cb.buffer[i:i] .= data[] - else - cb.buffer[i:i] .= data - end + cb.buffer[i:i] .= data else cb.buffer[ntuple(_ -> (:), N - 1)..., _buffer_frame(cb, cb.nframes)] .= data end diff --git a/test/runtests.jl b/test/runtests.jl index cfdc412..91cdc7f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -16,6 +16,12 @@ CUDA.allowscalar(false) @test b[1] == 0 end + @testset "1D Symbol" begin + b = CircularArrayBuffer([:a, :b]) + push!(b, :c) + @test b == [:b, :c] + end + @testset "1D Int" begin b = CircularArrayBuffer{Int}(3) From 9c5ce7dc6f03784be06f3346a58aa30c264a9b20 Mon Sep 17 00:00:00 2001 From: Jun Tian Date: Thu, 17 Mar 2022 10:32:28 +0800 Subject: [PATCH 2/2] add more test cases --- src/CircularArrayBuffers.jl | 2 +- test/runtests.jl | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/CircularArrayBuffers.jl b/src/CircularArrayBuffers.jl index 1572fbe..5c290fd 100644 --- a/src/CircularArrayBuffers.jl +++ b/src/CircularArrayBuffers.jl @@ -111,7 +111,7 @@ function Base.push!(cb::CircularArrayBuffer{T,N}, data) where {T,N} end if N == 1 i = _buffer_frame(cb, cb.nframes) - cb.buffer[i:i] .= data + cb.buffer[i:i] .= Ref(data) else cb.buffer[ntuple(_ -> (:), N - 1)..., _buffer_frame(cb, cb.nframes)] .= data end diff --git a/test/runtests.jl b/test/runtests.jl index 91cdc7f..b24af66 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -11,11 +11,17 @@ CUDA.allowscalar(false) # https://github.com/JuliaReinforcementLearning/ReinforcementLearning.jl/issues/551 @testset "1D with 0d data" begin b = CircularArrayBuffer{Int}(3) - push!(b, zeros(Int, ())) + append!(b, zeros(Int, ())) # !!! not push! @test length(b) == 1 @test b[1] == 0 end + @testset "1D vector" begin + b = CircularArrayBuffer([[1], [2, 3]]) + push!(b, [4, 5, 6]) + @test b == [[2, 3], [4, 5, 6]] + end + @testset "1D Symbol" begin b = CircularArrayBuffer([:a, :b]) push!(b, :c) @@ -195,7 +201,7 @@ if CUDA.functional() # https://github.com/JuliaReinforcementLearning/ReinforcementLearning.jl/issues/551 @testset "1D with 0d data" begin b = adapt(CuArray, CircularArrayBuffer{Int}(3)) - CUDA.@allowscalar push!(b, CUDA.zeros(Int, ())) + append!(b, CUDA.zeros(Int, ())) # !!! not push! @test length(b) == 1 @test CUDA.@allowscalar b[1] == 0 end