From 73a426b04339cf9d7b56c55122e41558295cf855 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 26 Sep 2024 22:43:43 +0100 Subject: [PATCH] Support arrays of integers as indices --- src/varname.jl | 3 +++ test/varname.jl | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/src/varname.jl b/src/varname.jl index 425a4a5..3261691 100644 --- a/src/varname.jl +++ b/src/varname.jl @@ -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") @@ -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" diff --git a/test/varname.jl b/test/varname.jl index 9a71dc0..ffa2a6d 100644 --- a/test/varname.jl +++ b/test/varname.jl @@ -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