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 73a426b
Show file tree
Hide file tree
Showing 2 changed files with 12 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{Int}) = 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(Int, dict[:values])
elseif dict[:type] == "unitrange"
return dict[:start]:dict[:stop]
elseif dict[:type] == "steprange"
Expand Down
9 changes: 9 additions & 0 deletions test/varname.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,14 @@ end
for vn in vns
@test vn_from_string2(vn_to_string2(vn)) == vn
end

# For this VarName, the {de,}serialisation works correctly but we must
# test in a different way because equality comparison of structs with
# vector fields (such as Accessors.IndexLens) compares the memory
# addresses rather than the contents (thus vn_vec == vn_vec2 returns
# false).
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 73a426b

Please sign in to comment.