Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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: 18 additions & 7 deletions src/CircularArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,30 @@ Alias for [`CircularArray{T,1,A}`](@ref).
"""
const CircularVector{T} = CircularArray{T, 1}

@inline clamp_bounds(arr::CircularArray, I::Tuple{Vararg{Int}})::AbstractArray{Int, 1} = map(Base.splat(mod), zip(I, axes(arr.data)))

CircularArray(data::AbstractArray{T,N}) where {T,N} = CircularArray{T,N}(data)
CircularArray{T}(data::AbstractArray{T,N}) where {T,N} = CircularArray{T,N}(data)
CircularArray(def::T, size) where T = CircularArray(fill(def, size))

@inline Base.getindex(arr::CircularArray, i::Int) = @inbounds getindex(arr.data, mod(i, Base.axes1(arr.data)))
@inline Base.setindex!(arr::CircularArray, v, i::Int) = @inbounds setindex!(arr.data, v, mod(i, Base.axes1(arr.data)))
@inline Base.getindex(arr::CircularArray, I::Vararg{Int}) = @inbounds getindex(arr.data, clamp_bounds(arr, I)...)
@inline Base.setindex!(arr::CircularArray, v, I::Vararg{Int}) = @inbounds setindex!(arr.data, v, clamp_bounds(arr, I)...)
@inline Base.getindex(arr::CircularArray, i::Integer) =
@inbounds getindex(arr.data, mod(i, eachindex(LinearIndices(arr.data))))
@inline Base.getindex(arr::CircularArray{T,N}, I::Vararg{<:Integer,N}) where {T,N} =
@inbounds getindex(arr.data, mod.(I, axes(arr))...)

@inline Base.setindex!(arr::CircularArray, v, i::Integer) =
@inbounds setindex!(arr.data, v, mod(i, eachindex(LinearIndices(arr.data))))
@inline Base.setindex!(arr::CircularArray{T,N}, v, I::Vararg{<:Integer,N}) where {T,N} =
@inbounds setindex!(arr.data, v, mod.(I, axes(arr))...)

@inline Base.size(arr::CircularArray) = size(arr.data)
@inline Base.axes(arr::CircularArray) = axes(arr.data)
Base.parent(arr::CircularArray) = arr.data

@inline Base.checkbounds(::CircularArray, _...) = nothing
@inline function Base.checkbounds(arr::CircularArray, I...)
J = Base.to_indices(arr, I)
length(J) == 1 && return nothing
length(J) >= ndims(arr) && return nothing
throw(BoundsError(arr, I))
end

@inline _similar(arr::CircularArray, ::Type{T}, dims) where T = CircularArray(similar(arr.data,T,dims))
@inline Base.similar(arr::CircularArray, ::Type{T}, dims::Tuple{Base.DimOrInd, Vararg{Base.DimOrInd}}) where T = _similar(arr,T,dims)
Expand All @@ -50,6 +60,7 @@ CircularArray(def::T, size) where T = CircularArray(fill(def, size))
CircularVector(data::AbstractArray{T, 1}) where T = CircularVector{T}(data)
CircularVector(def::T, size::Int) where T = CircularVector{T}(fill(def, size))

Base.IndexStyle(::Type{CircularArray{T,N,A}}) where {T,N,A} = IndexStyle(A)
Copy link
Owner

@Vexatos Vexatos Nov 22, 2020

Choose a reason for hiding this comment

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

The question came to me why this line would be needed. Wouldn't the issues with CartesianIndex be resolved if the IndexStyle defaulted to IndexCartesian for anyhting but CircularVector? That would result in the calls with CartesianIndex parameters to run through the second implementation of getindex instead of the first. Then all the hacks into to_indices could also be removed. I would be very interested to see your "more evil test cases" and see if they work with this, because all the current tests pass like this.

Copy link
Author

Choose a reason for hiding this comment

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

I thought this was necessary for speed, although it's entirely possible I have got lost in all the versions now. It causes eachindex(A) to prefer linear indexing, which can drop straight through to the underlying memory, whereas iterating CartesianIndices involves some multiplications by strides... necessary when the underlying array is transposed, say.

I've pushed what I had lying around.

Base.IndexStyle(::Type{<:CircularVector}) = IndexLinear()

end
27 changes: 26 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,31 @@ end

a2 = CircularArray(4, (2, 3))
@test isa(a2, CircularArray{Int, 2})

@test IndexStyle(a1) == IndexLinear()
@test a1[3] == a1[3,1]
@test a1[4] == a1[1,2]
@test a1[-1] == a1[length(a1)-1]

@test a1[2, 3, 1] == 17 # trailing index
@test a1[2, 3, 99] == 17
@test a1[2, 3, :] == [17]
end

@testset "3-array" begin
t3 = collect(reshape('a':'x', 2,3,4))
c3 = CircularArray(t3)

@test c3[1,3,3] == c3[3,3,3] == c3[3,3,7]

c3[3,3,7] = 'Z'
@test t3[1,3,3] == 'Z'

@test IndexStyle(c3) == IndexLinear()
@test c3[-1] == t3[length(t3)-1]

@test_throws BoundsError c3[2,3] # too few indices
@test_throws BoundsError c3[CartesianIndex(2,3)]
end

@testset "offset indices" begin
Expand All @@ -103,6 +128,6 @@ end
@test collect(a) == data
@test all(a[x,y] == data[mod1(x+1,3),mod1(y+1,3)] for x=-10:10, y=-10:10)
@test a[i,1] == CircularArray(OffsetArray([5,6,4,5,6],-2:2))
@test a[CartesianIndex.(i,i)] == CircularArray(OffsetArray([5,9,1,5,9],-2:2))
@test_broken a[CartesianIndex.(i,i)] == CircularArray(OffsetArray([5,9,1,5,9],-2:2))
@test a[a .> 4] == 5:9
end