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

Make searchsorted on ClosedInterval generic over AbstractVector #119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

iamed2
Copy link
Collaborator

@iamed2 iamed2 commented Sep 11, 2017

Previously, searchsorted with ClosedInterval as the index could only search a Range.
But, since searchsorted(first|last) works for any type with an ordering, we can extend searchsorted to search any AbstractVector.

This allows forcing Dimensional behaviour with SortedVector the same way one can force Categorical behaviour with CategoricalVector.

julia> A = AxisArray([1,3,2], Axis{:a}(SortedVector(["one", "three", "two"])))
1-dimensional AxisArray{Int64,1,...} with axes:
    :a, String["one", "three", "two"]
And data, a 3-element Array{Int64,1}:
 1
 3
 2

Before:

julia> A["three"]
ERROR: MethodError: no method matching isless(::String, ::IntervalSets.ClosedInterval{String})
Closest candidates are:
  isless(::AbstractString, ::AbstractString) at strings/basic.jl:124
  isless(::IntervalSets.ClosedInterval, ::IntervalSets.ClosedInterval) at /Users/ericdavies/.julia/v0.6/AxisArrays/src/intervals.jl:26
  isless(::Union{Base.Dates.AbstractTime, Number}, ::IntervalSets.ClosedInterval) at /Users/ericdavies/.julia/v0.6/AxisArrays/src/intervals.jl:53
Stacktrace:
 [1] searchsorted(::AxisArrays.SortedVector{String}, ::IntervalSets.ClosedInterval{String}, ::Int64, ::Int64, ::Base.Order.ForwardOrdering) at ./sort.jl:139
 [2] searchsorted(::AxisArrays.SortedVector{String}, ::IntervalSets.ClosedInterval{String}, ::Base.Order.ForwardOrdering) at ./sort.jl:213
 [3] axisindexes(::Type{AxisArrays.Dimensional}, ::AxisArrays.SortedVector{String}, ::String) at /Users/ericdavies/.julia/v0.6/AxisArrays/src/indexing.jl:190
 [4] getindex(::AxisArrays.AxisArray{Int64,1,Array{Int64,1},Tuple{AxisArrays.Axis{:a,AxisArrays.SortedVector{String}}}}, ::String) at /Users/ericdavies/.julia/v0.6/AxisArrays/src/indexing.jl:123

After:

julia> A["three"]
3

julia> A["three".."three"]
1-dimensional AxisArray{Int64,1,...} with axes:
    :a, String["three"]
And data, a 1-element Array{Int64,1}:
 3

julia> A["thra".."thro"]
1-dimensional AxisArray{Int64,1,...} with axes:
    :a, String["three"]
And data, a 1-element Array{Int64,1}:
 3

julia> A["a".."a"]
1-dimensional AxisArray{Int64,1,...} with axes:
    :a, String[]
And data, a 0-element Array{Int64,1}

julia> A["a".."z"]
1-dimensional AxisArray{Int64,1,...} with axes:
    :a, String["one", "three", "two"]
And data, a 3-element Array{Int64,1}:
 1
 3
 2

@nickrobinson251
Copy link

Bump. Is this still a thing we want and makes sense? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants