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

fix interval selection when keys are intervals #120

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/selectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ findindex(int::Interval, r::AbstractVector) =
findindex(int::Interval, r::AbstractRange{T}) where {T<:Union{Number,Char}} =
findall(in(int), r)

# find interval in a vector of intervals: same as generic findindex in lookup.jl
function findindex(int::Interval, r::AbstractVector{<:Interval})
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, I suppose this is a bit ambiguous as you could also want to find the first occurrence in which int overlaps or intersects with one of the intervals in r. I suppose that's what the functional interface is for though? @mcabbott do you have a preference?

Copy link
Collaborator Author

@aplavin aplavin Sep 1, 2022

Choose a reason for hiding this comment

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

My motivation was that keeping invariants goes a long way. And "calling with a value of type eltype(axiskeys(A)) returns the array value at that specific axiskey value" is a nice invariant to follow whenever possible. Overlaps or whatever else can be done with separate selectors, potentially as simple as !isdisjoint(int)/⊆(int)/....

i = findfirst(isequal(int), r)
i === nothing && throw(ArgumentError("could not find key $(repr(a)) in vector $r"))
i
end

# Since that is now efficient for ranges, comparisons can go there:

findindex(eq::Base.Fix2{typeof(<=)}, r::AbstractRange{T}) where {T<:Union{Number,Char}} =
Expand Down
4 changes: 4 additions & 0 deletions test/_basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ end
@test V(Near(0.12)) == V(0.1) == V[2]
@test V(Interval(0.1, 0.3)) == V[2:4]

VI = KeyedArray([1, 2, 3], xs=[Interval(1, 2), Interval(2, 3), Interval(1, 3)])
@test VI(Interval(1, 3)) == VI[3]
@test VI([Interval(1, 2), Interval(1, 3)]) == VI[[1, 3]]

@test V(Index[1]) == V[1]
@test V(Index[2:3]) == V[2:3]
@test V(Index[end]) == V[end]
Expand Down