From 45302a5e8cfa658d0e7f15cb20b392b81184ea9a Mon Sep 17 00:00:00 2001 From: Alexander Plavin Date: Sat, 30 Jul 2022 12:24:45 +0300 Subject: [PATCH] fix interval selection when keys are intervals --- src/selectors.jl | 7 +++++++ test/_basic.jl | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/src/selectors.jl b/src/selectors.jl index d4eb223..84b297b 100644 --- a/src/selectors.jl +++ b/src/selectors.jl @@ -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}) + 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}} = diff --git a/test/_basic.jl b/test/_basic.jl index 822375c..2cb0fd5 100644 --- a/test/_basic.jl +++ b/test/_basic.jl @@ -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]