Skip to content

Commit a41cf13

Browse files
authored
Merge pull request #80 from Tokazama/master
Add known_length method for generators
2 parents 713d6fa + 8c8f9fa commit a41cf13

File tree

4 files changed

+7
-2
lines changed

4 files changed

+7
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ArrayInterface"
22
uuid = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
3-
version = "2.13.6"
3+
version = "2.13.7"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/ArrayInterface.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ known_length(x) = known_length(typeof(x))
3636
known_length(::Type{<:NamedTuple{L}}) where {L} = length(L)
3737
known_length(::Type{T}) where {T<:Slice} = known_length(parent_type(T))
3838
known_length(::Type{<:Tuple{Vararg{Any,N}}}) where {N} = N
39+
known_length(::Type{T}) where {Itr,T<:Base.Generator{Itr}} = known_length(Itr)
3940
known_length(::Type{<:Number}) = 1
4041
function known_length(::Type{T}) where {T}
4142
if parent_type(T) <: T
@@ -50,6 +51,7 @@ end
5051
_known_length(x::Tuple{Vararg{<:Union{Int,Nothing}}}) = nothing
5152
_known_length(x::Tuple{Vararg{Int}}) = prod(x)
5253

54+
5355
"""
5456
can_change_size(::Type{T}) -> Bool
5557

src/indexing.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ to_indices(A, axs::Tuple{}, args::Tuple{}) = ()
218218

219219

220220
_multi_check_index(axs::Tuple, arg) = _multi_check_index(axs, axes(arg))
221-
_multi_check_index(axs::Tuple, arg::LogicalIndex) = prod(map(length, axs)) == length(arg)
221+
_multi_check_index(axs::Tuple, arg::LogicalIndex) = axs == axes(arg.mask)
222222
function _multi_check_index(axs::Tuple, arg::AbstractArray{T}) where {T<:CartesianIndex}
223223
b = true
224224
for i in arg

test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,9 @@ end
411411
x = view(SArray{Tuple{3,3,3}}(ones(3,3,3)), :, SOneTo(2), 2)
412412
@test @inferred(ArrayInterface.known_length(x)) == 6
413413
@test @inferred(ArrayInterface.known_length(x')) == 6
414+
415+
itr = StaticInt(1):StaticInt(10)
416+
@inferred(ArrayInterface.known_length((i for i in itr))) == 10
414417
end
415418

416419
@testset "indices" begin

0 commit comments

Comments
 (0)