Skip to content

Commit 73a426b

Browse files
committed
Support arrays of integers as indices
1 parent fbfb306 commit 73a426b

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/varname.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,7 @@ end
868868
# -----------------------------------------
869869

870870
index_to_dict(i::Integer) = Dict(:type => "integer", :value => i)
871+
index_to_dict(v::AbstractVector{Int}) = Dict(:type => "vector", :values => v)
871872
index_to_dict(r::UnitRange) = Dict(:type => "unitrange", :start => r.start, :stop => r.stop)
872873
index_to_dict(r::StepRange) = Dict(:type => "steprange", :start => r.start, :stop => r.stop, :step => r.step)
873874
index_to_dict(::Colon) = Dict(:type => "colon")
@@ -880,6 +881,8 @@ function dict_to_index(dict)
880881
dict = Dict(Symbol(k) => v for (k, v) in dict)
881882
if dict[:type] == "integer"
882883
return dict[:value]
884+
elseif dict[:type] == "vector"
885+
return collect(Int, dict[:values])
883886
elseif dict[:type] == "unitrange"
884887
return dict[:start]:dict[:stop]
885888
elseif dict[:type] == "steprange"

test/varname.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,5 +174,14 @@ end
174174
for vn in vns
175175
@test vn_from_string2(vn_to_string2(vn)) == vn
176176
end
177+
178+
# For this VarName, the {de,}serialisation works correctly but we must
179+
# test in a different way because equality comparison of structs with
180+
# vector fields (such as Accessors.IndexLens) compares the memory
181+
# addresses rather than the contents (thus vn_vec == vn_vec2 returns
182+
# false).
183+
vn_vec = @varname(x[[1, 2, 5, 6]])
184+
vn_vec2 = vn_from_string2(vn_to_string2(vn_vec))
185+
@test hash(vn_vec) == hash(vn_vec2)
177186
end
178187
end

0 commit comments

Comments
 (0)