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

findfirst, findlast, etc. appears to break due to keys #12

Open
dahong67 opened this issue Sep 4, 2020 · 2 comments · May be fixed by #13
Open

findfirst, findlast, etc. appears to break due to keys #12

dahong67 opened this issue Sep 4, 2020 · 2 comments · May be fixed by #13

Comments

@dahong67
Copy link

dahong67 commented Sep 4, 2020

findfirst appears to sometimes break, e.g., in this MWE:

julia> using IdentityRanges
julia> findfirst(x -> x < 0, IdentityRange(-2:2))
ERROR: BoundsError: attempt to access 5-element IdentityRange{Int64} with indices -2:2 at index [3]
Stacktrace:
 [1] throw_boundserror(::IdentityRange{Int64}, ::Int64) at ./abstractarray.jl:541
 [2] getindex at /Users/dahong67/.julia/packages/IdentityRanges/liKDH/src/IdentityRanges.jl:70 [inlined]
 [3] findnext(::var"#9#10", ::IdentityRange{Int64}, ::Int64) at ./array.jl:1810
 [4] findfirst(::Function, ::IdentityRange{Int64}) at ./array.jl:1861
 [5] top-level scope at REPL[8]:1

Similarly for findlast, findall.

It seems to happen because keys currently always start at 1:

julia> using IdentityRanges
julia> keys(IdentityRange(-2:2))
5-element LinearIndices{1,Tuple{UnitRange{Int64}}}:
 1
 2
 3
 4
 5

Is this the intended behavior?

Interestingly, related to #9, Base.IdentityUnitRange behaves differently:

julia> keys(Base.IdentityUnitRange(-2:2))
5-element LinearIndices{1,Tuple{Base.IdentityUnitRange{UnitRange{Int64}}}} with indices -2:2:
 -2
 -1
  0
  1
  2
@timholy
Copy link
Member

timholy commented Sep 9, 2020

Nice catch! Fundamentally this comes about because of this:

julia> r = Base.IdentityUnitRange(-2:2);

julia> rax = axes(r)[1]
Base.IdentityUnitRange(-2:2)

vs

julia> r = IdentityRange(-2:2)
IdentityRange(-2:2)

julia> rax = axes(r)[1]
-2:2

Care to try making a fix?

@dahong67
Copy link
Author

Thanks! Oh interesting, yes I would be happy to attempt a fix. I'll make a PR. :)

@dahong67 dahong67 linked a pull request Sep 17, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants