Skip to content

Commit

Permalink
Improve support for Not with multiple indices passed (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkamins authored Mar 14, 2023
1 parent 6cf39bc commit b7d7ce0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
fail-fast: false
matrix:
version:
- '1.0'
- '1.6'
- '1' # automatically expands to the latest stable 1.x release of Julia
- 'nightly'
os:
Expand Down
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name = "InvertedIndices"
uuid = "41ab1584-1d38-5bbf-9106-f11c6c58b48f"
version = "1.2.0"
version = "1.3.0"

[compat]
julia = "0.7, 1.0"
julia = "1.6"

[extras]
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
Expand Down
31 changes: 30 additions & 1 deletion src/InvertedIndices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,34 @@ struct InvertedIndex{S}
skip::S
end
const Not = InvertedIndex

nonBool2Int(x::Bool) = throw(ArgumentError("invalid index $x of type Bool"))
nonBool2Int(x::Integer) = Int(x)

# Support easily inverting multiple indices without a temporary array in Not([...])
InvertedIndex(i₁::Integer, i₂::Integer, iₓ::Integer...) = InvertedIndex(TupleVector((i₁, i₂, iₓ...)))
function InvertedIndex(i₁::Integer, i₂::Integer, iₓ::Integer...)
InvertedIndex(TupleVector((nonBool2Int(i₁), nonBool2Int(i₂), nonBool2Int.(iₓ)...)))
end

"""
NotMultiIndex(indices)
An unexported type that is meant to signal that `Not` was called with
multiple indices that were not all integer. This is meant to allow for
packages that support non-integer indexing to define custom handling
of such cases.
In particular `Base.to_indices` is on purpose not supported for values
of this type and proper handling of such `Not` index must be handled
explicitly by packages opting-in for support of non-integer indices.
"""
struct NotMultiIndex
indices
end

function InvertedIndex(i₁, i₂, iₓ...)
InvertedIndex(NotMultiIndex((i₁, i₂, iₓ...)))
end

"""
InvertedIndex(idx)
Expand Down Expand Up @@ -173,4 +199,7 @@ end
I.skip keys(nt) ? Base.structdiff(nt, NamedTuple{I.skip}) :
error("type NamedTuple has no fields $(join(I.skip, ", "))")

@inline Base.to_indices(A, inds, I::Tuple{InvertedIndex{NotMultiIndex}, Vararg{Any}}) =
throw(ArgumentError("Multiple arguments other than integers are not supported."))

end # module
23 changes: 23 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,26 @@ end
@test @inferred(f(nt)) === (c=:c,)
end
end

@testset "multi index" begin
x = Not(1, 2)
v = [1, 2, 3]
@test x.skip.data === (1, 2)
@test v[x] == [3]
x = Not(0x1, 0x2)
@test x.skip.data === (1, 2)
@test v[x] == [3]
x = Not(1, 0x2)
@test x.skip.data === (1, 2)
@test v[x] == [3]

@test v[Not(begin, end)] == [2]
@test v[Not(begin, 0x3)] == [2]

@test_throws ArgumentError v[Not(true)]
@test_throws ArgumentError Not(1, true)

x = Not(1, "a")
@test x isa InvertedIndex{InvertedIndices.NotMultiIndex}
@test_throws ArgumentError v[x]
end

2 comments on commit b7d7ce0

@bkamins
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/79584

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.3.0 -m "<description of version>" b7d7ce078888b2966f4e9acee80170bc48612fa0
git push origin v1.3.0

Please sign in to comment.