Skip to content

Commit

Permalink
Resolved further ambiguities with Base.similar
Browse files Browse the repository at this point in the history
Closes #14.
  • Loading branch information
Vexatos committed Aug 9, 2021
1 parent 4078a8b commit b0f8cd4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/CircularArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ Base.parent(arr::CircularArray) = arr.data
nothing
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)
@inline _similar(arr::CircularArray, ::Type{T}, dims) where T = CircularArray(similar(parent(arr), T, dims))
@inline Base.similar(arr::CircularArray, ::Type{T}, dims::Dims) where T = _similar(arr, T, dims)
@inline Base.similar(arr::CircularArray, ::Type{T}, dims::Tuple{Base.DimOrInd, Vararg{Base.DimOrInd}}) where T = _similar(arr, T, dims)
# Ambiguity resolution with Base
@inline Base.similar(arr::CircularArray, ::Type{T}, dims::Tuple{Int64,Vararg{Int64}}) where T = _similar(arr,T,dims)
@inline Base.similar(arr::CircularArray, ::Type{T}, dims::Tuple{Int64, Vararg{Int64}}) where T = _similar(arr, T, dims)
@inline Base.similar(arr::CircularArray, ::Type{T}, dims::Tuple{Union{Integer, Base.OneTo}, Vararg{Union{Integer, Base.OneTo}}}) where T = _similar(arr, T, dims)

function Base.showarg(io::IO, arr::CircularArray, toplevel)
print(io, ndims(arr) == 1 ? "CircularVector(" : "CircularArray(")
Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ end

@test all(a[i] == b[i] for i in -50:50)
end

v3 = @inferred(map(x -> x+1, CircularArray([1, 2, 3, 4])))
@test v3 == CircularArray([2, 3, 4, 5])
@test similar(v3, Base.OneTo(4)) isa typeof(v3)
end

@testset "matrix" begin
Expand Down Expand Up @@ -196,6 +200,8 @@ end
@test a[1:10][-10] == 3
@test a[i] == OffsetArray([4,5,1,2,3],-3)

@test @inferred(similar(a)) isa CircularVector

circ_a = circshift(a,3)
@test axes(circ_a) == axes(a)
@test circ_a[1:5] == 1:5
Expand Down

0 comments on commit b0f8cd4

Please sign in to comment.