Skip to content

Commit

Permalink
add BroadcastedInvertedIndex for InvertedIndex broadcasting
Browse files Browse the repository at this point in the history
  • Loading branch information
bkamins authored Sep 4, 2021
2 parents 0405fad + 56d384c commit ff02824
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "InvertedIndices"
uuid = "41ab1584-1d38-5bbf-9106-f11c6c58b48f"
version = "1.0.0"
version = "1.1.0"

[compat]
julia = "0.7, 1.0"
Expand Down
24 changes: 24 additions & 0 deletions src/InvertedIndices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,30 @@ inverted index will similarly span multiple dimensions.
"""
InvertedIndex, Not

"""
BroadcastedInvertedIndex(x::InvertedIndex)
A wrapper for `InvertedIndex` if it is used in broadcasting context.
Since `InvertedIndex` does not have a reference to the collection it applies to
it is impossible to define its `axes` eagerly.
Therefore it is the responsiblity of the caller to resolve the handling
of the broadcast result.
# Examples
julia> Not(1) .=> sin
InvertedIndices.BroadcastedInvertedIndex(InvertedIndex{Int64}(1)) => sin
julia> Not(:col) .=> [minimum, maximum]
2-element Vector{Pair{InvertedIndices.BroadcastedInvertedIndex, _A} where _A}:
InvertedIndices.BroadcastedInvertedIndex(InvertedIndex{Symbol}(:col)) => minimum
InvertedIndices.BroadcastedInvertedIndex(InvertedIndex{Symbol}(:col)) => maximum
"""
struct BroadcastedInvertedIndex
sel::InvertedIndex
end

Base.Broadcast.broadcastable(x::InvertedIndex) = Ref(BroadcastedInvertedIndex(x))

# A very simple and primitive static array to avoid allocations for Not(1,2,3) while fulfilling the indexing API
struct TupleVector{T<:Tuple} <: AbstractVector{Int}
Expand Down
36 changes: 36 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,39 @@ end
ex = try A[Not(2,3)] catch ex; ex end
@test occursin("Not([2, 3])", sprint(Base.showerror, ex))
end

@testset "broadcasting" begin
# these scenarios should be later properly handled by DataFrames.jl by expanding first dimension as appropriate
@test (Not(1) .=> sin) == (InvertedIndices.BroadcastedInvertedIndex(Not(1)) => sin)
@test (Not(1) .=> [sin cos]) ==
[InvertedIndices.BroadcastedInvertedIndex(Not(1)) => sin InvertedIndices.BroadcastedInvertedIndex(Not(1)) => cos]
@test (Not(1) .=> [sin cos] .=> [:a, :b, :c]) ==
[InvertedIndices.BroadcastedInvertedIndex(Not(1)) => sin => :a InvertedIndices.BroadcastedInvertedIndex(Not(1)) => cos => :a
InvertedIndices.BroadcastedInvertedIndex(Not(1)) => sin => :b InvertedIndices.BroadcastedInvertedIndex(Not(1)) => cos => :b
InvertedIndices.BroadcastedInvertedIndex(Not(1)) => sin => :c InvertedIndices.BroadcastedInvertedIndex(Not(1)) => cos => :c]
@test (Not(1) .=> [sin cos] .=> Not(1)) ==
[InvertedIndices.BroadcastedInvertedIndex(Not(1)) => sin => InvertedIndices.BroadcastedInvertedIndex(Not(1)) InvertedIndices.BroadcastedInvertedIndex(Not(1)) => cos => InvertedIndices.BroadcastedInvertedIndex(Not(1))]
@test (Not(1) .=> [sin, cos]) ==
[InvertedIndices.BroadcastedInvertedIndex(Not(1)) => sin, InvertedIndices.BroadcastedInvertedIndex(Not(1)) => cos]
@test (Not(1) .=> [sin, cos] .=> [:a, :b]) ==
[InvertedIndices.BroadcastedInvertedIndex(Not(1)) => sin => :a, InvertedIndices.BroadcastedInvertedIndex(Not(1)) => cos => :b]
@test (Not(1) .=> [sin, cos] .=> Not(1)) ==
[InvertedIndices.BroadcastedInvertedIndex(Not(1)) => sin => InvertedIndices.BroadcastedInvertedIndex(Not(1)), InvertedIndices.BroadcastedInvertedIndex(Not(1)) => cos => InvertedIndices.BroadcastedInvertedIndex(Not(1))]

@test identity.(Not(1)) == InvertedIndices.BroadcastedInvertedIndex(Not(1))
@test (identity.(Not(1)) .=> sin) == (InvertedIndices.BroadcastedInvertedIndex(Not(1)) => sin)
@test (identity.(Not(1)) .=> [sin cos]) ==
[InvertedIndices.BroadcastedInvertedIndex(Not(1)) => sin InvertedIndices.BroadcastedInvertedIndex(Not(1)) => cos]
@test (identity.(Not(1)) .=> [sin cos] .=> [:a, :b, :c]) ==
[InvertedIndices.BroadcastedInvertedIndex(Not(1)) => sin => :a InvertedIndices.BroadcastedInvertedIndex(Not(1)) => cos => :a
InvertedIndices.BroadcastedInvertedIndex(Not(1)) => sin => :b InvertedIndices.BroadcastedInvertedIndex(Not(1)) => cos => :b
InvertedIndices.BroadcastedInvertedIndex(Not(1)) => sin => :c InvertedIndices.BroadcastedInvertedIndex(Not(1)) => cos => :c]
@test (identity.(Not(1)) .=> [sin cos] .=> Not(1)) ==
[InvertedIndices.BroadcastedInvertedIndex(Not(1)) => sin => InvertedIndices.BroadcastedInvertedIndex(Not(1)) InvertedIndices.BroadcastedInvertedIndex(Not(1)) => cos => InvertedIndices.BroadcastedInvertedIndex(Not(1))]
@test (identity.(Not(1)) .=> [sin, cos]) ==
[InvertedIndices.BroadcastedInvertedIndex(Not(1)) => sin, InvertedIndices.BroadcastedInvertedIndex(Not(1)) => cos]
@test (identity.(Not(1)) .=> [sin, cos] .=> [:a, :b]) ==
[InvertedIndices.BroadcastedInvertedIndex(Not(1)) => sin => :a, InvertedIndices.BroadcastedInvertedIndex(Not(1)) => cos => :b]
@test (identity.(Not(1)) .=> [sin, cos] .=> Not(1)) ==
[InvertedIndices.BroadcastedInvertedIndex(Not(1)) => sin => InvertedIndices.BroadcastedInvertedIndex(Not(1)), InvertedIndices.BroadcastedInvertedIndex(Not(1)) => cos => InvertedIndices.BroadcastedInvertedIndex(Not(1))]
end

2 comments on commit ff02824

@bkamins
Copy link
Member Author

@bkamins bkamins commented on ff02824 Sep 4, 2021

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/44216

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.1.0 -m "<description of version>" ff0282444aec75d5893cd2d3e04cc462e6f0c958
git push origin v1.1.0

Please sign in to comment.