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

simplify undef-taking constructors #96

Merged
merged 7 commits into from
Feb 5, 2025
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
57 changes: 38 additions & 19 deletions src/FixedSizeArray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,52 @@
const FixedSizeVector{T} = FixedSizeArray{T,1}
const FixedSizeMatrix{T} = FixedSizeArray{T,2}

function FixedSizeArray{T,N,V}(::UndefInitializer, size::NTuple{N,Int}) where {T,N,V}
new_fixed_size_array(V(undef, checked_dims(size))::V, size)
function parent_type_with_default(::Type{<:(FixedSizeArray{E, N, T} where {N})}) where {E, T <: DenseVector{E}}
T
end
function FixedSizeArray{T,N,V}(::UndefInitializer, size::NTuple{N,Integer}) where {T,N,V}
ints = map(Int, size)::NTuple{N,Int} # prevent infinite recursion
FixedSizeArray{T,N,V}(undef, ints)
function parent_type_with_default(::Type{<:FixedSizeArray{E}}) where {E}
default_underlying_storage_type{E}
end
function FixedSizeArray{T,N,V}(::UndefInitializer, size::Vararg{Integer,N}) where {T,N,V}
FixedSizeArray{T,N,V}(undef, size)
function parent_type_with_default(::Type{<:FixedSizeArray})

Check warning on line 30 in src/FixedSizeArray.jl

View check run for this annotation

Codecov / codecov/patch

src/FixedSizeArray.jl#L30

Added line #L30 was not covered by tests
default_underlying_storage_type
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is not concrete, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That particular method shouldn't ever be called here, it's used in collect_as.

Copy link
Collaborator Author

@nsajko nsajko Feb 1, 2025

Choose a reason for hiding this comment

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

All the parent_type_with_default methods were moved from collect_as.jl to this file in the first commit, to be shared between files.

end
function FixedSizeArray{T,<:Any,V}(::UndefInitializer, size::NTuple{N,Integer}) where {T,N,V}
FixedSizeArray{T,N,V}(undef, size)
for T ∈ (Vector, optional_memory...)
FSA = FixedSizeArray{E, N, T{E}} where {E, N}
@eval begin
function parent_type_with_default(::Type{$FSA})
$T

Check warning on line 37 in src/FixedSizeArray.jl

View check run for this annotation

Codecov / codecov/patch

src/FixedSizeArray.jl#L36-L37

Added lines #L36 - L37 were not covered by tests
end
function parent_type_with_default(::Type{($FSA){E, N} where {E}}) where {N}
$T

Check warning on line 40 in src/FixedSizeArray.jl

View check run for this annotation

Codecov / codecov/patch

src/FixedSizeArray.jl#L39-L40

Added lines #L39 - L40 were not covered by tests
end
end
end
function FixedSizeArray{T,<:Any,V}(::UndefInitializer, size::Vararg{Integer,N}) where {T,N,V}
FixedSizeArray{T,N,V}(undef, size)

function check_ndims(::Type{FSA}, size::Tuple{Vararg{Integer}}) where {N, FSA <: (FixedSizeArray{E, N} where {E})}
if size isa Tuple{Vararg{Any, N}}
size
else
throw(DimensionMismatch("mismatch between dimension count in type and the length of the size tuple"))
end
end
function FixedSizeArray{T,N}(::UndefInitializer, size::NTuple{N,Integer}) where {T,N}
FixedSizeArray{T,N,default_underlying_storage_type{T}}(undef, size)
function check_ndims(::Type{<:FixedSizeArray}, size::Tuple{Vararg{Integer}})
nsajko marked this conversation as resolved.
Show resolved Hide resolved
size
end
function FixedSizeArray{T,N}(::UndefInitializer, size::Vararg{Integer,N}) where {T,N}
FixedSizeArray{T,N}(undef, size)

function undef_constructor(::Type{FSA}, size::Tuple{Vararg{Integer}}) where {E, FSA <: FixedSizeArray{E}}
size = check_ndims(FSA, size)
s = map(Int, size)
Mem = parent_type_with_default(FSA)
len = checked_dims(s)
mem = Mem(undef, len)
new_fixed_size_array(mem, s)
end
function FixedSizeArray{T}(::UndefInitializer, size::Vararg{Integer,N}) where {T,N}
FixedSizeArray{T,N}(undef, size)

function (::Type{FSA})(::UndefInitializer, size::Tuple{Vararg{Integer}}) where {E, FSA <: FixedSizeArray{E}}
undef_constructor(FSA, size)
end
function FixedSizeArray{T}(::UndefInitializer, size::NTuple{N,Integer}) where {T,N}
FixedSizeArray{T,N}(undef, size)
function (::Type{FSA})(::UndefInitializer, size::Vararg{Integer}) where {E, FSA <: FixedSizeArray{E}}
undef_constructor(FSA, size)
end

macro assume_noub_if_noinbounds(x)
Expand Down
21 changes: 0 additions & 21 deletions src/collect_as.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,6 @@ function make_vector_from_tuple(::Type{V}, elems::Tuple) where {V <: DenseVector
make_abstract_vector_from_collection_with_length(ret_type, elems)
end

function parent_type_with_default(::Type{<:(FixedSizeArray{E, N, T} where {N})}) where {E, T <: DenseVector{E}}
T
end
function parent_type_with_default(::Type{<:FixedSizeArray{E}}) where {E}
default_underlying_storage_type{E}
end
function parent_type_with_default(::Type{<:FixedSizeArray})
default_underlying_storage_type
end
for T ∈ (Vector, optional_memory...)
FSA = FixedSizeArray{E, N, T{E}} where {E, N}
@eval begin
function parent_type_with_default(::Type{$FSA})
$T
end
function parent_type_with_default(::Type{($FSA){E, N} where {E}}) where {N}
$T
end
end
end

function push(v::Vector, e)
E = typejoin(typeof(e), eltype(v))
ret = Vector{E}(undef, length(v) + 1)
Expand Down
8 changes: 4 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,10 @@ end
@test_throws MethodError FSM(undef, 1, 1)
end
@testset "mismatched ndims" begin
@test_throws MethodError FSV{Int}(undef)
@test_throws MethodError FSV{Int}(undef, 1, 1)
@test_throws MethodError FSM{Int}(undef)
@test_throws MethodError FSM{Int}(undef, 1)
@test_throws DimensionMismatch FSV{Int}(undef)
@test_throws DimensionMismatch FSV{Int}(undef, 1, 1)
@test_throws DimensionMismatch FSM{Int}(undef)
@test_throws DimensionMismatch FSM{Int}(undef, 1)
end
for dim_count ∈ 0:4
siz = ntuple(Returns(2), dim_count)
Expand Down
Loading