Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

conversion from AbstractArray and converting constructors #13

Merged
merged 1 commit into from
Apr 21, 2024
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
26 changes: 26 additions & 0 deletions src/FixedSizeArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,30 @@ function Base.similar(
similar(FixedSizeArray{E}, axes(bc))
end

# one-based indexing check

axes_are_one_based(axes) = all(isone ∘ first, axes)

# converting constructors for copying other array types

function FixedSizeArray{T,N}(src::AbstractArray{S,N}) where {T,N,S}
axs = axes(src)
axes_are_one_based(axs) ||
throw(DimensionMismatch("source array has a non-one-based indexing axis"))
# Can't use `Base.size` because, according to it's doc string, it's not
# available for all `AbstractArray` types.
size = map(length, axs)
dst = FixedSizeArray{T,N}(undef, size)
copyto!(dst, src)::FixedSizeArray{T,N}
end

FixedSizeArray{T}(a::AbstractArray{<:Any,N}) where {T,N} = FixedSizeArray{T,N}(a)
FixedSizeArray{<:Any,N}(a::AbstractArray{T,N}) where {T,N} = FixedSizeArray{T,N}(a)
FixedSizeArray(a::AbstractArray{T,N}) where {T,N} = FixedSizeArray{T,N}(a)

# conversion

Base.convert(::Type{T}, a::T) where {T<:FixedSizeArray} = a
Base.convert(::Type{T}, a::AbstractArray) where {T<:FixedSizeArray} = T(a)::T

end # module FixedSizeArrays
32 changes: 32 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ using FixedSizeArrays
@test similar(FixedSizeVector{Int}, (2,)) isa FixedSizeVector{Int}
@test similar(FixedSizeArray{Int}, (2,)) isa FixedSizeVector{Int}
@test FixedSizeArray{Int}(undef, 2) isa FixedSizeVector{Int}
for T ∈ (FixedSizeArray, FixedSizeVector)
a = 1:3
@test convert(T, a) isa FixedSizeVector{Int}
@test convert(T, a) == a
@test convert(T, convert(T, a)) isa FixedSizeVector{Int}
@test convert(T, convert(T, a)) == a
end
for T ∈ (FixedSizeArray{Int}, FixedSizeVector{Int})
for S ∈ (Int, Float64)
a = map(S, 1:3)
@test convert(T, a) isa FixedSizeVector{Int}
@test convert(T, a) == a
@test convert(T, convert(T, a)) isa FixedSizeVector{Int}
@test convert(T, convert(T, a)) == a
end
end
end

@testset "FixedSizeMatrix" begin
Expand All @@ -32,6 +48,22 @@ using FixedSizeArrays
@test similar(FixedSizeMatrix{Int}, (2, 3)) isa FixedSizeMatrix{Int}
@test similar(FixedSizeArray{Int}, (2, 3)) isa FixedSizeMatrix{Int}
@test FixedSizeArray{Int}(undef, 2, 3) isa FixedSizeMatrix{Int}
for T ∈ (FixedSizeArray, FixedSizeMatrix)
a = reshape(1:9, (3, 3))
@test convert(T, a) isa FixedSizeMatrix{Int}
@test convert(T, a) == a
@test convert(T, convert(T, a)) isa FixedSizeMatrix{Int}
@test convert(T, convert(T, a)) == a
end
for T ∈ (FixedSizeArray{Int}, FixedSizeMatrix{Int})
for S ∈ (Int, Float64)
a = map(S, reshape(1:9, (3, 3)))
@test convert(T, a) isa FixedSizeMatrix{Int}
@test convert(T, a) == a
@test convert(T, convert(T, a)) isa FixedSizeMatrix{Int}
@test convert(T, convert(T, a)) == a
end
end
end

@testset "broadcasting" begin
Expand Down
Loading