-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
RFC: Change iteratorsize trait of product(itr1, itr2)
#16437
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -298,10 +298,49 @@ done(it::Repeated, state) = false | |
|
||
repeated(x, n::Int) = take(repeated(x), n) | ||
|
||
# product | ||
|
||
# Product -- cartesian product of iterators | ||
|
||
abstract AbstractProdIterator | ||
|
||
length(p::AbstractProdIterator) = prod(size(p)) | ||
size(p::AbstractProdIterator) = _prod_size(p.a, p.b, iteratorsize(p.a), iteratorsize(p.b)) | ||
ndims(p::AbstractProdIterator) = length(size(p)) | ||
|
||
# generic methods to handle size of Prod* types | ||
_prod_size(a, ::HasShape) = size(a) | ||
_prod_size(a, ::HasLength) = (length(a), ) | ||
_prod_size(a, A) = | ||
throw(ArgumentError("Cannot compute size for object of type $(typeof(a))")) | ||
_prod_size(a, b, ::HasLength, ::HasLength) = (length(a), length(b)) | ||
_prod_size(a, b, ::HasLength, ::HasShape) = (length(a), size(b)...) | ||
_prod_size(a, b, ::HasShape, ::HasLength) = (size(a)..., length(b)) | ||
_prod_size(a, b, ::HasShape, ::HasShape) = (size(a)..., size(b)...) | ||
_prod_size(a, b, A, ::Union{HasShape, HasLength}) = | ||
throw(ArgumentError("Cannot compute size for object of type $(typeof(a))")) | ||
_prod_size(a, b, ::Union{HasShape, HasLength}, B) = | ||
throw(ArgumentError("Cannot compute size for object of type $(typeof(b))")) | ||
|
||
# one iterator | ||
immutable Prod1{I} <: AbstractProdIterator | ||
a::I | ||
end | ||
product(a) = Prod1(a) | ||
|
||
eltype{I}(::Type{Prod1{I}}) = Tuple{eltype(I)} | ||
size(p::Prod1) = _prod_size(p.a, iteratorsize(p.a)) | ||
|
||
@inline start(p::Prod1) = start(p.a) | ||
@inline function next(p::Prod1, st) | ||
n, st = next(p.a, st) | ||
(n, ), st | ||
end | ||
@inline done(p::Prod1, st) = done(p.a, st) | ||
|
||
iteratoreltype{I}(::Type{Prod1{I}}) = iteratoreltype(I) | ||
iteratorsize{I}(::Type{Prod1{I}}) = iteratorsize(I) | ||
|
||
# two iterators | ||
immutable Prod2{I1, I2} <: AbstractProdIterator | ||
a::I1 | ||
b::I2 | ||
|
@@ -323,11 +362,11 @@ changes the fastest. Example: | |
(1,5) | ||
(2,5) | ||
""" | ||
product(a) = Zip1(a) | ||
product(a, b) = Prod2(a, b) | ||
|
||
eltype{I1,I2}(::Type{Prod2{I1,I2}}) = Tuple{eltype(I1), eltype(I2)} | ||
|
||
iteratoreltype{I1,I2}(::Type{Prod2{I1,I2}}) = and_iteratoreltype(iteratoreltype(I1),iteratoreltype(I2)) | ||
length(p::AbstractProdIterator) = length(p.a)*length(p.b) | ||
iteratorsize{I1,I2}(::Type{Prod2{I1,I2}}) = prod_iteratorsize(iteratorsize(I1),iteratorsize(I2)) | ||
|
||
function start(p::AbstractProdIterator) | ||
|
@@ -355,13 +394,15 @@ end | |
@inline next(p::Prod2, st) = prod_next(p, st) | ||
@inline done(p::AbstractProdIterator, st) = st[4] | ||
|
||
# n iterators | ||
immutable Prod{I1, I2<:AbstractProdIterator} <: AbstractProdIterator | ||
a::I1 | ||
b::I2 | ||
end | ||
|
||
product(a, b, c...) = Prod(a, product(b, c...)) | ||
|
||
eltype{I1,I2}(::Type{Prod{I1,I2}}) = tuple_type_cons(eltype(I1), eltype(I2)) | ||
|
||
iteratoreltype{I1,I2}(::Type{Prod{I1,I2}}) = and_iteratoreltype(iteratoreltype(I1),iteratoreltype(I2)) | ||
iteratorsize{I1,I2}(::Type{Prod{I1,I2}}) = prod_iteratorsize(iteratorsize(I1),iteratorsize(I2)) | ||
|
||
|
@@ -370,8 +411,10 @@ iteratorsize{I1,I2}(::Type{Prod{I1,I2}}) = prod_iteratorsize(iteratorsize(I1),it | |
((x[1][1],x[1][2]...), x[2]) | ||
end | ||
|
||
prod_iteratorsize(::Union{HasLength,HasShape}, ::Union{HasLength,HasShape}) = HasLength() | ||
prod_iteratorsize(a, ::IsInfinite) = IsInfinite() # products can have an infinite last iterator (which moves slowest) | ||
prod_iteratorsize(::Union{HasLength,HasShape}, ::Union{HasLength,HasShape}) = HasShape() | ||
# products can have an infinite iterator | ||
prod_iteratorsize(a, ::IsInfinite) = IsInfinite() | ||
prod_iteratorsize(::IsInfinite, b) = IsInfinite() | ||
prod_iteratorsize(a, b) = SizeUnknown() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one too, but this should be removed, because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But that would be a special case. How would you handle the majority of cases where you have (finite nonzero size) * (infinite size) ? |
||
|
||
_size(p::Prod2) = (length(p.a), length(p.b)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not needed anymore? |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one is ambiguous