Skip to content

Commit

Permalink
Support arrays of integers as indices
Browse files Browse the repository at this point in the history
  • Loading branch information
penelopeysm committed Sep 26, 2024
1 parent fbfb306 commit c5016aa
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/varname.jl
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,7 @@ end
# -----------------------------------------

index_to_dict(i::Integer) = Dict(:type => "integer", :value => i)
index_to_dict(v::AbstractVector{<:Integer}) = Dict(:type => "vector", :values => v)
index_to_dict(r::UnitRange) = Dict(:type => "unitrange", :start => r.start, :stop => r.stop)
index_to_dict(r::StepRange) = Dict(:type => "steprange", :start => r.start, :stop => r.stop, :step => r.step)
index_to_dict(::Colon) = Dict(:type => "colon")
Expand All @@ -880,6 +881,8 @@ function dict_to_index(dict)
dict = Dict(Symbol(k) => v for (k, v) in dict)
if dict[:type] == "integer"
return dict[:value]
elseif dict[:type] == "vector"
return collect(Int64, dict[:values])
elseif dict[:type] == "unitrange"
return dict[:start]:dict[:stop]
elseif dict[:type] == "steprange"
Expand Down
6 changes: 6 additions & 0 deletions test/varname.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,11 @@ end
for vn in vns
@test vn_from_string2(vn_to_string2(vn)) == vn
end

# For this VarName, the {de,}serialisation works but we must test in a
# different way because the optics don't compare equal (not sure why).
vn_vec = @varname(x[[1, 2, 5, 6]])
vn_vec2 = vn_from_string2(vn_to_string2(vn_vec))
@test hash(vn_vec) == hash(vn_vec2)
end
end

0 comments on commit c5016aa

Please sign in to comment.