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

[Question] Why is .keys for a 1-dimensional array (i.e. Vector) a Ref instead of a Tuple #162

Closed
kevinli1993 opened this issue Dec 4, 2024 · 2 comments

Comments

@kevinli1993
Copy link

Hi! I'm curious about the design decision on why the type of .keys is a Tuple when the array is ≥2 dimensional, but a Ref when the array is 1 dimensional?

This prevents me from using X.keys[1] consistently to grab the labels of the first dimension of an array.

Example:

julia> a = KeyedArray(1:10, (1:10,));

julia> b = KeyedArray(reshape(1:10, 5, 2), (1:5, [:A, :B]));

julia> typeof(a.keys)
Base.RefValue{UnitRange{Int64}}

julia> typeof(b.keys)
Tuple{UnitRange{Int64}, Vector{Symbol}}

julia> b.keys[1]
1:5

julia> a.keys[1]
ERROR: MethodError: no method matching getindex(::Base.RefValue{UnitRange{Int64}}, ::Int64)
The function `getindex` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  getindex(::Base.RefValue)
   @ Base refvalue.jl:59
  getindex(::Ref, ::CartesianIndex{0})
   @ Base multidimensional.jl:1952
@mcabbott
Copy link
Owner

mcabbott commented Dec 4, 2024

The "official" way to access these is the function axiskeys, which works like axes:

julia> axiskeys(a)  # all, as a tuple
(1:10,)

julia> axiskeys(b)
(1:5, [:A, :B])

julia> axiskeys(a, 1)  # just one
1:10

julia> axiskeys(b, 1)
1:5

The reason for the Ref is this: the length of a Vector can change, but a Matrix cannot. When you push! to a 1D KeyedArray, whose key-vector is a range, it can mutate the parent Vector, but cannot mutate the range. So instead, it replaces it with an extended version:

julia> ar = KeyedArray(rand(10), (2:7:65,));

julia> axiskeys(ar, 1)
2:7:65

julia> push!(ar, 11.11);

julia> ar
1-dimensional KeyedArray(...) with keys:
   11-element StepRange{Int64,...}
And data, 11-element Vector{Float64}:
  (2)   0.7206034706302128
  (9)   0.46201964758677516
 (16)   0.41270560444156945
 (23)   0.777077885482015
 (30)   0.04286994401519395
 (37)   0.05769776095943535
 (44)   0.4900469322206408
 (51)   0.800467424464092
 (58)   0.5635380666606707
 (65)   0.7245045757977666
 (72)  11.11

julia> axiskeys(ar, 1)
2:7:72

When the key-vector is mutable too, then there is no need for Ref, as we can change its length too:

julia> av = KeyedArray(rand(3), [:a, :b, :c]);

julia> push!(av, :efg => 44.44);  # mutates both parent and keyvector

julia> av
1-dimensional KeyedArray(...) with keys:
   4-element Vector{Symbol}
And data, 4-element Vector{Float64}:
 (:a)     0.48530044992681176
 (:b)     0.34308816841244283
 (:c)     0.4262841094328301
 (:efg)  44.44

@kevinli1993
Copy link
Author

This is a great explanation, it taught me both about axiskeys as well as the ability to push! and have the labels be extended. Thanks for your patience and your work on this package!

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

2 participants