Skip to content

Commit 8790826

Browse files
Merge pull request #275 from JuliaArrays/simplify_known_length
Simplify implementation of known_length
2 parents ccf4e1c + 9cdf1ae commit 8790826

File tree

1 file changed

+5
-19
lines changed

1 file changed

+5
-19
lines changed

src/size.jl

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -160,27 +160,13 @@ known_length(::Type{<:Tuple{Vararg{Any,N}}}) where {N} = N
160160
known_length(::Type{<:Number}) = 1
161161
known_length(::Type{<:AbstractCartesianIndex{N}}) where {N} = N
162162
known_length(::Type{T}) where {T} = _maybe_known_length(Base.IteratorSize(T), T)
163-
164-
@generated function _prod_or_nothing(x::Tuple)
165-
p = 1
166-
for i in eachindex(x.parameters)
167-
x.parameters[i] === Nothing && return nothing
168-
p *= x.parameters[i].parameters[1]
169-
end
170-
StaticInt(p)
163+
function known_length(::Type{<:Iterators.Flatten{I}}) where {I}
164+
_prod_or_nothing((known_length(I),known_length(eltype(I))))
171165
end
172166

173-
function _maybe_known_length(::Base.HasShape, ::Type{T}) where {T}
174-
t = map(_static_or_nothing, known_size(T))
175-
_int_or_nothing(_prod_or_nothing(t))
176-
end
167+
_prod_or_nothing(x::Tuple{Vararg{Int}}) = prod(x)
168+
_prod_or_nothing(_) = nothing
177169

170+
_maybe_known_length(::Base.HasShape, ::Type{T}) where {T} = _prod_or_nothing(known_size(T))
178171
_maybe_known_length(::Base.IteratorSize, ::Type) = nothing
179-
_static_or_nothing(::Nothing) = nothing
180-
@inline _static_or_nothing(x::Int) = StaticInt{x}()
181-
_int_or_nothing(::StaticInt{N}) where {N} = N
182-
_int_or_nothing(::Nothing) = nothing
183-
function known_length(::Type{<:Iterators.Flatten{I}}) where {I}
184-
_int_or_nothing(_prod_or_nothing((_static_or_nothing(known_length(I)),_static_or_nothing(known_length(eltype(I))))))
185-
end
186172

0 commit comments

Comments
 (0)