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

Accelerate some more predicates (like ==) #17

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
47 changes: 47 additions & 0 deletions src/HashIndex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@ function Base.count(f::Fix2{typeof(isequal)}, a::AcceleratedArray{<:Any, <:Any,
end
end

function Base.count(f::Fix2{typeof(==)}, a::AcceleratedArray{<:Any, <:Any, <:Any, <:HashIndex})
out = 0

for x in other_equal(f.x) # Search for other things that might be == but not isequal (like -0.0)
(hasindex, token) = gettoken(a.index.dict, x)
@inbounds if hasindex
out += length(@inbounds gettokenvalue(a.index.dict, token))
end
end

if f(f.x) # Exclude things that are not == to themselves (like NaN)
(hasindex, token) = gettoken(a.index.dict, f.x)
if hasindex
return out + length(@inbounds gettokenvalue(a.index.dict, token))
end
end

return out
end

function Base.findall(f::Fix2{typeof(isequal)}, a::AcceleratedArray{<:Any, <:Any, <:Any, <:HashIndex})
(hasindex, token) = gettoken(a.index.dict, f.x)
if hasindex
Expand All @@ -41,6 +61,33 @@ function Base.findall(f::Fix2{typeof(isequal)}, a::AcceleratedArray{<:Any, <:Any
end
end

function Base.findall(f::Fix2{typeof(==)}, a::AcceleratedArray{<:Any, <:Any, <:Any, <:HashIndex})
out = Vector{keytype(a)}()
n_matches = 0

for x in other_equal(f.x) # Search for other things that might be == but not isequal (like -0.0)
(hasindex, token) = gettoken(a.index.dict, x)
@inbounds if hasindex
append!(out, @inbounds gettokenvalue(a.index.dict, token))
n_matches += 1
end
end

if f(f.x) # Exclude things that are not == to themselves (like NaN)
(hasindex, token) = gettoken(a.index.dict, f.x)
if hasindex
append!(out, @inbounds gettokenvalue(a.index.dict, token))
n_matches += 1
end
end

if n_matches > 1
sort!(out)
end

return out
end

# TODO: findall for arbitrary predicates by just checking each unique key? (Sometimes faster, sometimes slower?)

function Base.findfirst(f::Fix2{typeof(isequal)}, a::AcceleratedArray{<:Any, <:Any, <:Any, <:HashIndex})
Expand Down
13 changes: 13 additions & 0 deletions src/predicates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,16 @@ if VERSION < v"1.2.0-DEV.257"
Base.:(>=)(x) = Fix2(>=, x)
Base.:(!=)(x) = Fix2(!=, x)
end

# Search for other things that are == but not isequal (for example -0.0)
other_equal(::Any) = ()
other_equal(x::AbstractFloat) = isequal(x, 0.0) ? MaybeVector(convert(typeof(x), -0.0)) : isequal(x, -0.0) ? MaybeVector(convert(typeof(x), 0.0)) : MaybeVector{typeof(x)}()

# ismissing
Base.count(f::typeof(ismissing), a::AcceleratedArray) = count(isequal(missing), a)
Base.findall(f::typeof(ismissing), a::AcceleratedArray) = findall(isequal(missing), a)
Base.findfirst(f::typeof(ismissing), a::AcceleratedArray) = findfirst(isequal(missing), a)
Base.findlast(f::typeof(ismissing), a::AcceleratedArray) = findlast(isequal(missing), a)
Base.filter(f::typeof(ismissing), a::AcceleratedArray) = filter(isequal(missing), a)

# TODO isnan, iszero, isone, isfinite (all are slightly strange when considering things like Complex, Matrix and String)