Skip to content

Commit 01f7a50

Browse files
Michael Abbottmcabbott
authored andcommitted
make axes(A) return KeyedUnitRanges
1 parent 24b89d5 commit 01f7a50

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

src/AxisKeys.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module AxisKeys
33
include("struct.jl")
44
export KeyedArray, axiskeys
55

6+
include("axes.jl")
7+
68
include("lookup.jl")
79

810
include("names.jl")

src/axes.jl

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#=
2+
3+
This is an experiment, with returning something very similar to a KeyedArray from axes(A).
4+
The reason to do so is that things like similar(C, axes(A,1), axes(B,2)) could propagate keys.
5+
However right now axes(A,1) !isa Base.OneTo results in lots of OffsetArrays...
6+
7+
=#
8+
9+
struct KeyedUnitRange{T,AT,KT} <: AbstractUnitRange{T}
10+
data::AT
11+
keys::KT
12+
function KeyedUnitRange(data::AT, keys::KT) where {AT<:AbstractUnitRange{T}, KT<:AbstractVector} where {T}
13+
new{T,AT,KT}(data, keys)
14+
end
15+
end
16+
17+
Base.parent(A::KeyedUnitRange) = getfield(A, :data)
18+
keyless(A::KeyedUnitRange) = parent(A)
19+
20+
for f in [:size, :first, :last, :IndexStyle]
21+
@eval Base.$f(A::KeyedUnitRange) = $f(parent(A))
22+
end
23+
24+
Base.getindex(A::KeyedUnitRange, inds::Integer) = getindex(parent(A), inds)
25+
26+
axiskeys(A::KeyedUnitRange) = tuple(getfield(A, :keys))
27+
axiskeys(A::KeyedUnitRange, d::Integer) = d==1 ? getfield(A, :keys) : Base.OneTo(1)
28+
29+
30+
# getkey(A::AbstractKeyedArray{<:Any,1}, key) = findfirst(isequal(key), axiskeys(A)[1])
31+
32+
function Base.axes(A::KeyedArray)
33+
ntuple(ndims(A)) do d
34+
KeyedUnitRange(axes(parent(A),d), axiskeys(A, d))
35+
end
36+
end
37+
38+
KeyedUnion{T} = Union{KeyedArray{T}, KeyedUnitRange{T}}
39+
40+
Base.summary(io::IO, x::KeyedUnion) = _summary(io, x)
41+
Base.summary(io::IO, A::NamedDimsArray{L,T,N,<:KeyedUnion}) where {L,T,N} = _summary(io, A)
42+
showtype(io::IO, ::KeyedUnitRange) = print(io, "KeyedUnitRange(...)")
43+
44+
function Base.show(io::IO, m::MIME"text/plain", x::KeyedUnitRange)
45+
summary(io, x)
46+
println(io, ":")
47+
keyed_print_matrix(io, x)
48+
end
49+
50+
function Base.show(io::IO, x::KeyedUnitRange)
51+
print(io, "KeyedUnitRange(", keyless(x), ", ", axiskeys(x,1), ")")
52+
end

src/struct.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ KeyedArray(A::KeyedVector, k2::Tuple{AbstractVector}) =
3838

3939
Base.size(x::KeyedArray) = size(parent(x))
4040

41-
Base.axes(x::KeyedArray) = axes(parent(x))
41+
# Base.axes(x::KeyedArray) = axes(parent(x))
4242

4343
Base.parent(x::KeyedArray) = getfield(x, :data)
4444
keyless(x::KeyedArray) = parent(x)

0 commit comments

Comments
 (0)