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

Implement efficent methods for any/all #32

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions src/PaddedViews.jl
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,14 @@ function Base.showarg(io::IO, A::PaddedView, toplevel)
toplevel && print(io, " with eltype ", eltype(A))
end

# The fallbacks work, but this is more efficient
# TODO use use short-circuit evaluation,
Base.any(f::Function, A::PaddedView) = f(A.fillvalue) | any(f, parent(A))
Base.all(f::Function, A::PaddedView) = f(A.fillvalue) & all(f, parent(A))
Copy link
Member

Choose a reason for hiding this comment

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

it doesn't support dims keyword

julia> pa = PaddedView(false, a, (1:2, 1:2));

julia> any(pa; dims=1)
ERROR: ArgumentError: No method is implemented for reducing index range of type typeof(i). Please implement
reduced_index for this index type or report this as an issue.

Copy link
Member

Choose a reason for hiding this comment

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

Base.all(A::PaddedView) = all(identity, A)

is still needed, otherwise it calls the fallback.

Copy link
Member

Choose a reason for hiding this comment

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

julia> a = fill(true, 2, 2)
2×2 Array{Bool,2}:
 1  1
 1  1

julia> A = PaddedView(false, a, axes(a))
2×2 PaddedView(false, ::Array{Bool,2}, (Base.OneTo(2), Base.OneTo(2))) with eltype Bool:
 1  1
 1  1

julia> all(identity, A)
false

julia> all(identity, collect(A))
true

Choose a reason for hiding this comment

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

@goretkin, I pointed this problem in JuliaLang/julia#35563 (comment).

Choose a reason for hiding this comment

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

Similarly, there is a problem with cropping.
I have a sense of foreboding about the combination of those and dims.:fearful:

Copy link
Author

Choose a reason for hiding this comment

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

@kimikage Yeah, I am not sure it is worthwhile to implement anything clever for dims. The required reasoning would be substantially more complex, and I can imagine it being slower for many cases where there isn't that much padding.

Not sure what problem with cropping you mean.

Choose a reason for hiding this comment

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

julia> a = Bool[0 0; 1 1]
2×2 Array{Bool,2}:
 0  0
 1  1

julia> A = PaddedView(false, a, (1,3))
1×3 PaddedView(false, ::Array{Bool,2}, (Base.OneTo(1), Base.OneTo(3))) with eltype Bool:
 0  0  0

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the example! Yeah, that would also need to be handled for sure.

if v"0.7" <= VERSION < v"1.1"
# ambiguity patch
# https://github.com/JuliaLang/julia/pull/30904
Base.all(::typeof(isinteger), ::PaddedView{<:Integer}) = true
end

end # module
28 changes: 28 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,31 @@ end
@test Ap[axes(A)...] == A
end
end

@testset "specializations" begin
@testset "trivalent logic" begin
for (p, v) in Iterators.product(ntuple(_->(true, false, missing), 2)...)
a = fill(v, (1, 1))
pa = PaddedView(p, a, (1:2, 1:2))
mpa = collect(pa)
# ensure specializations are equivalent to fallbacks
for f in (any, all)
@test isequal(f(pa), f(mpa))
end
end
end
@testset "with predicates" begin
for (p, v) in Iterators.product(ntuple(_->(0, 1), 2)...)
a = fill(v, (1, 1))
pa = PaddedView(p, a, (1:2, 1:2))
mpa = collect(pa)
# ensure specializations are equivalent to fallbacks
fp = ==(0)
for f in (any, all)
v1 = f(fp, pa)
v2 = f(fp, mpa)
@test isequal(v1, v2)
end
end
end
end