Skip to content
Merged
Show file tree
Hide file tree
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
25 changes: 20 additions & 5 deletions src/CircularArrayBuffers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

Expand Down Expand Up @@ -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] .= Ref(data)
else
cb.buffer[ntuple(_ -> (:), N - 1)..., _buffer_frame(cb, cb.nframes)] .= data
end
Expand Down
16 changes: 14 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,23 @@ 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!
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In JuliaReinforcementLearning/ReinforcementLearning.jl#551 we should also use append! instead of 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)
@test b == [:b, :c]
end

@testset "1D Int" begin
b = CircularArrayBuffer{Int}(3)

Expand Down Expand Up @@ -189,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
Expand Down