Skip to content

Commit caea589

Browse files
author
Michael Abbott
committed
remove some findfirst methods
1 parent b139e7f commit caea589

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

src/lookup.jl

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,22 @@ findindex(a::Union{AbstractArray, Base.Generator}, r::AbstractArray) =
8888

8989
findindex(f::Function, r::AbstractArray) = findall(f, r)
9090

91-
# It's possible this should be a method of to_indices or one of its friends?
92-
# https://docs.julialang.org/en/v1/base/arrays/#Base.to_indices
91+
# Faster than Base.findfirst(==(i), 1:10) etc:
92+
93+
findindex(i::Int, r::Base.OneTo{Int}) = 1 <= i <= r.stop ? eq.x : nothing
94+
95+
findindex(i::Int, r::AbstractUnitRange) = first(r) <= i <= last(r) ? 1+Int(i - first(r)) : nothing
96+
97+
# Faster than Base.findall(==(i), 1:10) etc:
98+
99+
function findindex(eq::Base.Fix2{Union{typeof(==),typeof(isequal)},Int}, r::Base.OneTo{Int})
100+
1 <= eq.x <= r.stop ? (eq.x:eq.x) : (1:0)
101+
end
102+
103+
function findindex(eq::Base.Fix2{Union{typeof(==),typeof(isequal)},Int}, r::AbstractUnitRange)
104+
val = 1 + Int(eq.x - first(r))
105+
first(r) <= eq.x <= last(r) ? (val:val) : (1:0)
106+
end
93107

94108

95109
"""

src/notpiracy.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ map(f, t::Tuple, r::Base.RefValue) = error("ref second") # Ref(f(first(t), r[]))
2222
2323
=#
2424

25-
using Compat # 2.0 hasfield + 3.1 filter
25+
# using Compat # 2.0 hasfield + 3.1 filter
2626

2727
#===== Speeding up with same results =====#
2828

29-
findfirst(args...) = Base.findfirst(args...)
29+
# findfirst(args...) = Base.findfirst(args...)
3030
findall(args...) = Base.findall(args...)
3131

32+
#=
3233
for equal in (isequal, Base.:(==))
3334
@eval begin
3435
@@ -53,6 +54,7 @@ for equal in (isequal, Base.:(==))
5354
5455
end
5556
end
57+
=#
5658

5759
findall(eq::Base.Fix2{typeof(<=),Int}, r::Base.OneTo{Int}) =
5860
eq.x < 1 ? Base.OneTo(0) :

test/_notpiracy.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ if VERSION >= v"1.2" # <(3) doesn't exist on 1.1, but Base.Fix2 is fine
2828
@test AxisKeys.findfirst(==(x), r) == findfirst(==(x), collect(r))
2929
@test AxisKeys.findfirst(isequal(x), r) == findfirst(isequal(x), collect(r))
3030

31-
for op in (isequal, Base.:(==), Base.:<, Base.:<=, Base.:>, Base.:>=)
31+
# for op in (isequal, Base.:(==), Base.:<, Base.:<=, Base.:>, Base.:>=)
32+
for op in (Base.:<, Base.:<=, Base.:>, Base.:>=)
3233

3334
@test AxisKeys.findall(op(x), r) == Base.findall(op(x), collect(r))
3435
@test AxisKeys.findall(op(x), r) isa AbstractRange

0 commit comments

Comments
 (0)