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

Inverting arrays of custom index types #7

Open
mbauman opened this issue Jan 24, 2019 · 0 comments
Open

Inverting arrays of custom index types #7

mbauman opened this issue Jan 24, 2019 · 0 comments

Comments

@mbauman
Copy link
Contributor

mbauman commented Jan 24, 2019

MWE:

julia> struct NamedVector{T,A,B} <: AbstractArray{T,1}
           data::A
           names::B
       end
       function NamedVector(data, names)
           @assert size(data) == size(names)
           NamedVector{eltype(data), typeof(data), typeof(names)}(data, names)
       end
       Base.size(n::NamedVector) = size(n.data)
       Base.getindex(n::NamedVector, i::Int) = n.data[i]
       Base.to_index(n::NamedVector, name::Symbol) = findfirst(==(name), n.names)
       Base.checkbounds(::Type{Bool}, n::NamedVector, names::AbstractArray{Symbol}) = all(name in n.names for name in names)

julia> n = NamedVector(1:4, [:a, :b, :c, :d]);

julia> using InvertedIndices

julia> n[Not([:a,:b])]
2-element Array{Int64,1}:
 1
 2

julia> n[[:a,:b]]
2-element Array{Int64,1}:
 1
 2

The issue is that arrays don't get their elements converted by to_indices, but we check each element assuming that it did.

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

No branches or pull requests

1 participant