Skip to content

Commit 4a1613e

Browse files
authored
conversion from AbstractArray and converting constructors (#13)
Fixes #4
1 parent b6865c0 commit 4a1613e

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/FixedSizeArrays.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,30 @@ function Base.similar(
4949
similar(FixedSizeArray{E}, axes(bc))
5050
end
5151

52+
# one-based indexing check
53+
54+
axes_are_one_based(axes) = all(isone first, axes)
55+
56+
# converting constructors for copying other array types
57+
58+
function FixedSizeArray{T,N}(src::AbstractArray{S,N}) where {T,N,S}
59+
axs = axes(src)
60+
axes_are_one_based(axs) ||
61+
throw(DimensionMismatch("source array has a non-one-based indexing axis"))
62+
# Can't use `Base.size` because, according to it's doc string, it's not
63+
# available for all `AbstractArray` types.
64+
size = map(length, axs)
65+
dst = FixedSizeArray{T,N}(undef, size)
66+
copyto!(dst, src)::FixedSizeArray{T,N}
67+
end
68+
69+
FixedSizeArray{T}(a::AbstractArray{<:Any,N}) where {T,N} = FixedSizeArray{T,N}(a)
70+
FixedSizeArray{<:Any,N}(a::AbstractArray{T,N}) where {T,N} = FixedSizeArray{T,N}(a)
71+
FixedSizeArray(a::AbstractArray{T,N}) where {T,N} = FixedSizeArray{T,N}(a)
72+
73+
# conversion
74+
75+
Base.convert(::Type{T}, a::T) where {T<:FixedSizeArray} = a
76+
Base.convert(::Type{T}, a::AbstractArray) where {T<:FixedSizeArray} = T(a)::T
77+
5278
end # module FixedSizeArrays

test/runtests.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ using FixedSizeArrays
1616
@test similar(FixedSizeVector{Int}, (2,)) isa FixedSizeVector{Int}
1717
@test similar(FixedSizeArray{Int}, (2,)) isa FixedSizeVector{Int}
1818
@test FixedSizeArray{Int}(undef, 2) isa FixedSizeVector{Int}
19+
for T (FixedSizeArray, FixedSizeVector)
20+
a = 1:3
21+
@test convert(T, a) isa FixedSizeVector{Int}
22+
@test convert(T, a) == a
23+
@test convert(T, convert(T, a)) isa FixedSizeVector{Int}
24+
@test convert(T, convert(T, a)) == a
25+
end
26+
for T (FixedSizeArray{Int}, FixedSizeVector{Int})
27+
for S (Int, Float64)
28+
a = map(S, 1:3)
29+
@test convert(T, a) isa FixedSizeVector{Int}
30+
@test convert(T, a) == a
31+
@test convert(T, convert(T, a)) isa FixedSizeVector{Int}
32+
@test convert(T, convert(T, a)) == a
33+
end
34+
end
1935
end
2036

2137
@testset "FixedSizeMatrix" begin
@@ -32,6 +48,22 @@ using FixedSizeArrays
3248
@test similar(FixedSizeMatrix{Int}, (2, 3)) isa FixedSizeMatrix{Int}
3349
@test similar(FixedSizeArray{Int}, (2, 3)) isa FixedSizeMatrix{Int}
3450
@test FixedSizeArray{Int}(undef, 2, 3) isa FixedSizeMatrix{Int}
51+
for T (FixedSizeArray, FixedSizeMatrix)
52+
a = reshape(1:9, (3, 3))
53+
@test convert(T, a) isa FixedSizeMatrix{Int}
54+
@test convert(T, a) == a
55+
@test convert(T, convert(T, a)) isa FixedSizeMatrix{Int}
56+
@test convert(T, convert(T, a)) == a
57+
end
58+
for T (FixedSizeArray{Int}, FixedSizeMatrix{Int})
59+
for S (Int, Float64)
60+
a = map(S, reshape(1:9, (3, 3)))
61+
@test convert(T, a) isa FixedSizeMatrix{Int}
62+
@test convert(T, a) == a
63+
@test convert(T, convert(T, a)) isa FixedSizeMatrix{Int}
64+
@test convert(T, convert(T, a)) == a
65+
end
66+
end
3567
end
3668

3769
@testset "broadcasting" begin

0 commit comments

Comments
 (0)