Repository navigation
add support for Not with CartesianIndices
#47
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -177,52 +177,97 @@ uniquesort(r::AbstractRange) = step(r) > 0 ? r : step(r) == 0 ? r[end:end] : rev | |
| uniquesort(A::Base.LogicalIndex) = A | ||
| uniquesort(x) = x | ||
|
|
||
| @inline function Base.to_indices(A, inds, I::Tuple{InvertedIndex, Vararg{Any}}) | ||
| new_indices = to_indices(A, inds, (I[1].skip, tail(I)...)) | ||
| skips = uniquesort(new_indices[1]) | ||
| picks = spanned_indices(inds, skips)[1] | ||
| return (InvertedIndexIterator(skips, picks), tail(new_indices)...) | ||
| # The cartesian product of the tuple of indices, like CartesianIndices but with | ||
| # support for integers, arbitrary vectors, and heterogeneous mixtures thereof | ||
| # The vectors must be sorted for use in an InvertedIndexIterator | ||
| struct CartesianIndexArray{M,N,X} <: AbstractArray{CartesianIndex{M},N} | ||
| tup::X | ||
| end | ||
|
|
||
| struct ZeroDArray{T} <: AbstractArray{T,0} | ||
| x::T | ||
| @inline Base.axes(ca::CartesianIndexArray) = Base.index_shape(ca.tup...) | ||
| @inline Base.size(ca::CartesianIndexArray) = map(length, Base.index_shape(ca.tup...)) | ||
| @inline function Base.getindex(ca::CartesianIndexArray{<:Any,N}, I::Vararg{Int, N}) where {N} | ||
| @boundscheck checkbounds(ca, I...) | ||
| CartesianIndex(reindex(ca.tup, I)) | ||
| end | ||
| Base.size(::ZeroDArray) = () | ||
| Base.getindex(Z::ZeroDArray) = Z.x | ||
|
|
||
| # Be careful with CartesianIndex as they splat out to a variable number of new indices and do not iterate | ||
| function Base.to_indices(A, inds, I::Tuple{InvertedIndex{<:CartesianIndex}, Vararg{Any}}) | ||
| skips = ZeroDArray(I[1].skip) | ||
| picks, tails = spanned_indices(inds, skips) | ||
| return (InvertedIndexIterator(skips, picks), to_indices(A, tails, tail(I))...) | ||
| end | ||
| cartesian(tup) = CartesianIndexArray{length(tup), length(Base.index_shape(tup...)), typeof(tup)}(tup) | ||
| cartesian(tup::Tuple{Vararg{AbstractUnitRange}}) = CartesianIndices(tup) | ||
|
|
||
| # Either return a CartesianRange or an axis vector | ||
| @inline spanned_indices(inds, ::Any) = inds[1], tail(inds) | ||
| const NIdx{N} = Union{CartesianIndex{N}, AbstractArray{CartesianIndex{N}}, AbstractArray{Bool,N}} | ||
| @inline spanned_indices(inds, ::NIdx{0}) = CartesianIndices(()), inds | ||
| @inline spanned_indices(inds, ::NIdx{1}) = inds[1], tail(inds) | ||
| @inline function spanned_indices(inds, ::NIdx{N}) where N | ||
| heads, tails = Base.IteratorsMD.split(inds, Val(N)) | ||
| return CartesianIndices(heads), tails | ||
| @inline reindex(tup::Tuple{<:AbstractVector, Vararg{Any}}, I) = (tup[1][I[1]], reindex(tail(tup), Base.safe_tail(I))...) | ||
| reindex(::Tuple{<:AbstractArray, Vararg{Any}}, I) = throw(AssertionError("InvertedIndex did not expect a multidimensional array within a cartesian product")) | ||
| @inline reindex(tup::Tuple{Any, Vararg{Any}}, I) = (tup[1], reindex(tail(tup), I)...) | ||
| reindex(::Tuple{}, I) = () | ||
|
|
||
| @inline take_n_axes(A, inds::Tuple, n::Tuple) = take_n_axes(A, 1, inds, n) | ||
| @inline take_n_axes(A, i, inds::Tuple, n::Tuple) = (inds[1], take_n_axes(A, i, tail(inds), tail(n))...) | ||
| @inline take_n_axes(A, i, ::Tuple{}, n::Tuple) = (axes(A, ndims(A)+i), take_n_axes(A, i+1, (), tail(n))...) | ||
| take_n_axes(A, i, ::Tuple, ::Tuple{}) = () | ||
| take_n_axes(A, i, ::Tuple{}, ::Tuple{}) = () | ||
|
|
||
| function Base.to_indices(A, inds, I::Tuple{InvertedIndex, Vararg{Any}}) | ||
| new_indices = to_indices(A, inds, (I[1].skip, tail(I)...)) | ||
| # Now the hard part is connecting the picks (from inds) and skips (from new_indices) | ||
| # The easy one to get is the number of dimensions (axes) that the inverted index spans: | ||
| n_picks = index_ndims(A, I[1].skip) | ||
| picks = take_n_axes(A, inds, n_picks) | ||
| # The hard one is the number of indices that we need to pull out of new_indices; we need | ||
| # the same `index_ndims` as the picks, but this may not be the same index _count_. | ||
| skips = spanned_indices(A, new_indices, n_picks) | ||
| _, tails = Base.IteratorsMD.split(new_indices, Val(length(skips))) | ||
| if length(picks) == length(skips) == 1 | ||
| # The common case | ||
| return (InvertedIndexIterator(uniquesort(skips[1]), picks[1]), tails...) | ||
| elseif length(skips) == 1 | ||
| # The picks are always tuples of AbstractUnitRanges | ||
| return (InvertedIndexIterator(uniquesort(skips[1]), CartesianIndices(picks)), tails...) | ||
| else | ||
| # But the skips might be a tuple of other index types | ||
| return (InvertedIndexIterator(cartesian(map(uniquesort, skips)), CartesianIndices(picks)), tails...) | ||
| end | ||
| end | ||
|
|
||
| # It's possible more indices were specified than there were axes | ||
| @inline spanned_indices(inds::Tuple{}, ::Any) = Base.OneTo(1), () | ||
| @inline spanned_indices(inds::Tuple{}, ::NIdx{0}) = CartesianIndices(()), () | ||
| @inline spanned_indices(inds::Tuple{}, ::NIdx{1}) = Base.OneTo(1), () | ||
| @inline spanned_indices(inds::Tuple{}, ::NIdx{N}) where N = CartesianIndices(ntuple(i->Base.OneTo(1), N)), () | ||
| spanned_indices(A, ::Tuple{}, n) = throw(AssertionError("Not enough indices to span the picks")) | ||
| spanned_indices(A, ::Tuple{}, ::Tuple{}) = () | ||
| @inline function spanned_indices(A, inds, ::Tuple{}) | ||
| # This is a little tricky; we need to keep pulling inds out as long as they're 0-ndim | ||
| i1 = inds[1] | ||
| dims1 = index_ndims(A, i1) | ||
| return dims1 == () ? (i1, spanned_indices(A, tail(inds), ())...) : () | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Preserve zero-dimensional indices after the inverted index After all axes belonging to AI disclosure: This work was prepared with assistance from OpenAI Codex. |
||
| end | ||
| @inline function spanned_indices(A, inds, n) | ||
| i1 = inds[1] | ||
| dims1 = index_ndims(A, i1) | ||
| (i1, spanned_indices(A, tail(inds), Base.IteratorsMD.split(n, Val(length(dims1)))[2])...) | ||
| end | ||
|
|
||
| # This is an interesting need — we need this because otherwise indexing with a | ||
| # single multidimensional boolean array ends up comparing a multidimensional cartesian | ||
| # index to a linear index. | ||
| const NIdx{N} = Union{CartesianIndex{N}, AbstractArray{CartesianIndex{N}}, AbstractArray{Bool,N}} | ||
| @inline Base.to_indices(A, I::Tuple{Not{<:NIdx{1}}}) = to_indices(A, (eachindex(IndexLinear(), A),), I) | ||
| @inline Base.to_indices(A, I::Tuple{Not{<:NIdx}}) = to_indices(A, axes(A), I) | ||
|
|
||
| # The number of dimensions that an index spans is tricky due to historical bad behavior of | ||
| # logical indices. In Julias 1.10 and before, the behavior of a logical index depends upon | ||
| # three things: (1) if it's the only index and (2) the IndexStyle of the parent and (3) the index type | ||
| # After 1.10, the behaviors no longer depend on (1) and (2), and we could remove the A argument | ||
| index_ndims(A, indices...) = _index_ndims(indices...) | ||
| _index_ndims() = () | ||
| _index_ndims(i1::NIdx{N}, I...) where {N} = (ntuple(x->true, Val(N))..., _index_ndims(I...)...) | ||
| _index_ndims(i1, I...) = (true, _index_ndims(I...)...) | ||
| if VERSION < v"1.11.0-DEV.1157" | ||
| # Arrays of Bool are even more confusing as they're sometimes linear and sometimes not | ||
| # Arrays of Bool are even more confusing as they're sometimes linear and sometimes not... | ||
| # and worse, it depends upon the _parent_ being indexed into | ||
| # This was addressed in Base with JuliaLang/julia#45869. | ||
| @inline Base.to_indices(A, I::Tuple{Not{<:AbstractArray{Bool, 1}}}) = to_indices(A, (eachindex(IndexLinear(), A),), I) | ||
| @inline Base.to_indices(A, I::Tuple{Not{<:Union{Array{Bool}, BitArray}}}) = to_indices(A, (eachindex(A),), I) | ||
| @inline Base.to_indices(A, I::Tuple{Not{<:AbstractArray{Bool, 1}}}) = | ||
| to_indices(A, (eachindex(IndexLinear(), A),), I) | ||
| @inline Base.to_indices(A, I::Tuple{Not{<:Union{Array{Bool}, BitArray}}}) = | ||
| to_indices(A, (IndexStyle(A) isa IndexLinear ? (eachindex(A),) : axes(A)), I) | ||
| # This also affects the _number_ of dimensions that these logical indices span, but only | ||
| # when it's the only index | ||
| index_ndims(A, i1::AbstractArray{Bool, 1}) = (true,) | ||
| index_ndims(A, i1::Union{Array{Bool}, BitArray}) = | ||
| IndexStyle(A) isa IndexLinear ? (true,) : ntuple(x->true, Val(ndims(i1))) | ||
| end | ||
|
|
||
| # a cleaner implementation is nt[filter(∉(I.skip), keys(nt))] instead of Base.structdiff, but this would only work on Julia 1.7+ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P2] Keep the full indexing context for older Boolean masks
index_ndims(A, I[1].skip)always calls the one-index specialization, including whenNothas a following index. On Julia 1.6.7,A = reshape(collect(1:24), 4, 3, 2); mask = falses(4, 3); mask[2:3, 1:2] .= true; A[Not(mask), 1]works on main and should equalA[:, :, 1][.!mask], but this head throwsBoundsError. Both BitMatrix and Matrix{Bool}, including views, regress becausepicksspans one axis while the converted logical index spans two. Apply the legacy one-index adjustment only when the originalIactually has one index; otherwise retain the mask dimension count.AI disclosure: This work was prepared with assistance from OpenAI Codex.