-
-
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
Make checkbounds more consistent & easier to extend #11895
Conversation
Sounds very good to me! |
Alright, rebased. I haven't championed this guy all that much since it does cause some undocumented churn over at DataArrays. But I do think this is important. Should we document and export |
I like this.
What about making |
7245dfb
to
f1a10c8
Compare
I'm not sure about that. I think the rest of the arguments are different enough from It would be nice not to export a new name, for sure. |
Maybe call |
That still sounds like I'm checking a specific index. Since # Throw an error if false
checkbounds(A, indexes...)
checkbounds(size(A,d), i)
# Return true if in-bounds, false otherwise
checkbounds(Bool, A, indexes...)
checkbounds(Bool, size(A,d), i) So you can pass either an |
Yes, that seems like a nice API. Then custom types that want to have special indexing behaviors override The trouble is this allocates a tuple for some reason. Minimal example: f(I...) = f(Bool, I...)
f(::Type{Bool}, I) = I
@code_llvm f(1) Edit: Ugh, wait, I think this might be an artifact of testing these functions at global scope directly instead of within a secondary function. |
I like this, but it changes semantics to a form that seems to be slightly slower in my preliminary tests. :-\ Since we want to only require folks to extend one method, it effectively changes things from: check_dim_1() || error
check_dim_2() || error
check_dim_3() || error to: (check_dim_1() && check_dim_2() && check_dim_3()) || error I haven't dug into this much further, but it'll have to wait until I can be sure that this re-arrangement doesn't incur any penalty. |
Yeah, don't want a penalty. I didn't take the time to dig into the details, but that sounds like something that can be avoided---presumably the |
* Remove index types from `checkbounds(A::AbstractArray, I...)` to make it easier for custom arrays to extend `checkbounds` without ambiguities. * Rename the internal `_checkbounds(sz::Integer, I)` to `checkbounds(::Type{Bool}, sz::Integer, I)` in order to provide a nicer name for custom array types to extend and override if they want to provide an optimized bounds check for the dimension that they are indexing into (which simply returns true or false without throwing errors).
Ok, finally updated once again. I went back to the original code semantics and simplified things considerably. It's now almost entirely just a renaming of If this can get this squeezed into 0.4 it'll make life a little nicer for DataArrays and NullableArrays. |
Really nice! I approve whenever you're ready to merge. |
Make checkbounds more consistent & easier to extend
Update the index checkbounds API to the latest incarnation from JuliaLang/julia#11895.
Now formally supported and documented due to JuliaLang/julia#11895. Note: this does not compat-ably version the change.
Remove index types from allcheckbounds
methods to make it easier for custom arrays to extendcheckbounds
without ambiguities.Provide a nicer name for custom array types to extend and override if they want to provide an optimizedin_bounds
check for the dimension that they are indexing into (which simply returns true or false without throwing errors).Thein_bounds
name already existed in base with only one use and the same meaning. This simply uses it more consistently and in a way that makes it easier to extend. It remains un-exported, and un-documented for now.checkbounds(A::AbstractArray, I...)
to make it easier for custom arrays to extendcheckbounds
without ambiguities._checkbounds(sz::Integer, I)
tocheckbounds(::Type{Bool}, sz::Integer, I)
in order to provide a nicer name for custom array types to extend and override if they want to provide an optimized bounds check for the dimension that they are indexing into (which simply returns true or false without throwing errors).in_bounds
is folded intocheckbounds(::Type{Bool}, …)
.Worked through in collaboration with @davidagold for a NullableArrays use-case, similar to how DataArrays currently works (c.f. https://github.com/JuliaStats/DataArrays.jl/blob/e7172be39310728acc4ded16a988eae603b69787/src/indexing.jl#L103-L113).