@@ -48,16 +48,16 @@ Base.eachindex(A::AxisArray) = eachindex(A.data)
48
48
This internal function determines the new set of axes that are constructed upon
49
49
indexing with I.
50
50
"""
51
- reaxis (A:: AxisArray , I:: Idx... ) = _reaxis (make_axes_match (axes (A), I), I)
51
+ reaxis (A:: AxisArray , copy :: Val , I:: Idx... ) = _reaxis (make_axes_match (axes (A), I), copy , I)
52
52
# Linear indexing
53
- reaxis (A:: AxisArray{<:Any,1} , I:: AbstractArray{Int} ) = _new_axes (A. axes[1 ], I)
54
- reaxis (A:: AxisArray , I:: AbstractArray{Int} ) = default_axes (I)
55
- reaxis (A:: AxisArray{<:Any,1} , I:: Real ) = ()
56
- reaxis (A:: AxisArray , I:: Real ) = ()
57
- reaxis (A:: AxisArray{<:Any,1} , I:: Colon ) = _new_axes (A. axes[1 ], Base. axes (A, 1 ))
58
- reaxis (A:: AxisArray , I:: Colon ) = default_axes (Base. OneTo (length (A)))
59
- reaxis (A:: AxisArray{<:Any,1} , I:: AbstractArray{Bool} ) = _new_axes (A. axes[1 ], findall (I))
60
- reaxis (A:: AxisArray , I:: AbstractArray{Bool} ) = default_axes (findall (I))
53
+ reaxis (A:: AxisArray{<:Any,1} , copy :: Val , I:: AbstractArray{Int} ) = _new_axes (A. axes[1 ], copy , I)
54
+ reaxis (A:: AxisArray , copy :: Val , I:: AbstractArray{Int} ) = default_axes (I)
55
+ reaxis (A:: AxisArray{<:Any,1} , copy :: Val , I:: Real ) = ()
56
+ reaxis (A:: AxisArray , copy :: Val , I:: Real ) = ()
57
+ reaxis (A:: AxisArray{<:Any,1} , copy :: Val , I:: Colon ) = _new_axes (A. axes[1 ], copy , Base. axes (A, 1 ))
58
+ reaxis (A:: AxisArray , copy :: Val , I:: Colon ) = default_axes (Base. OneTo (length (A)))
59
+ reaxis (A:: AxisArray{<:Any,1} , copy :: Val , I:: AbstractArray{Bool} ) = _new_axes (A. axes[1 ], copy , findall (I))
60
+ reaxis (A:: AxisArray , copy :: Val , I:: AbstractArray{Bool} ) = default_axes (findall (I))
61
61
62
62
# Ensure the number of axes matches the number of indexing dimensions
63
63
@inline function make_axes_match (axs, idxs)
@@ -66,28 +66,43 @@ reaxis(A::AxisArray, I::AbstractArray{Bool}) = default_axes(findall(I))
66
66
end
67
67
68
68
# Now we can reaxis without worrying about mismatched axes/indices
69
- @inline _reaxis (axs:: Tuple{} , idxs:: Tuple{} ) = ()
69
+ @inline _reaxis (axs:: Tuple{} , copy :: Val , idxs:: Tuple{} ) = ()
70
70
# Scalars are dropped
71
71
const ScalarIndex = Union{Real, AbstractArray{<: Any , 0 }}
72
- @inline _reaxis (axs:: Tuple , idxs:: Tuple{ScalarIndex, Vararg{Any}} ) = _reaxis (tail (axs), tail (idxs))
72
+ @inline _reaxis (axs:: Tuple , copy :: Val , idxs:: Tuple{ScalarIndex, Vararg{Any}} ) = _reaxis (tail (axs), copy , tail (idxs))
73
73
# Colon passes straight through
74
- @inline _reaxis (axs:: Tuple , idxs:: Tuple{Colon, Vararg{Any}} ) = (axs[1 ], _reaxis (tail (axs), tail (idxs))... )
74
+ @inline _reaxis (axs:: Tuple , copy :: Val , idxs:: Tuple{Colon, Vararg{Any}} ) = (axs[1 ], _reaxis (tail (axs), copy , tail (idxs))... )
75
75
# But arrays can add or change dimensions and accompanying axis names
76
- @inline _reaxis (axs:: Tuple , idxs:: Tuple{AbstractArray, Vararg{Any}} ) =
77
- (_new_axes (axs[1 ], idxs[1 ])... , _reaxis (tail (axs), tail (idxs))... )
76
+ @inline _reaxis (axs:: Tuple , copy :: Val , idxs:: Tuple{AbstractArray, Vararg{Any}} ) =
77
+ (_new_axes (axs[1 ], copy, idxs[1 ])... , _reaxis (tail (axs), copy , tail (idxs))... )
78
78
79
79
# Vectors simply create new axes with the same name; just subsetted by their value
80
- @inline _new_axes (ax:: Axis{name} , idx:: AbstractVector ) where {name} = (Axis {name} (ax. val[idx]),)
80
+ @inline _new_axes (ax:: Axis{name} , copy:: Val{true} , idx:: AbstractVector ) where {name} = (Axis {name} (ax. val[idx]),)
81
+ @inline _new_axes (ax:: Axis{name} , copy:: Val{false} , idx:: AbstractVector ) where {name} = (Axis {name} (view (ax. val, idx)),)
82
+
83
+ # @inline _new_axes(ax::Axis{name}, copy::Val{false}, idx::AxisArray{T,1,D,Ax}) where {Ax, D, T, name} = _new_axes(ax, copy, idx)
84
+
81
85
# Arrays create multiple axes with _N appended to the axis name containing their indices
82
- @generated function _new_axes (ax:: Axis{name} , idx:: AbstractArray{<:Any,N} ) where {name,N}
86
+ @generated function _new_axes (ax:: Axis{name} , copy :: Val , idx:: AbstractArray{<:Any,N} ) where {name, N}
83
87
newaxes = Expr (:tuple )
84
88
for i= 1 : N
85
89
push! (newaxes. args, :($ (Axis{Symbol (name, " _" , i)})(Base. axes (idx, $ i))))
86
90
end
87
91
newaxes
88
92
end
93
+
89
94
# And indexing with an AxisArray joins the name and overrides the values
90
- @generated function _new_axes (ax:: Axis{name} , idx:: AxisArray{<:Any, N} ) where {name,N}
95
+ @generated function _new_axes (ax:: Axis{name} , copy:: Val{true} , idx:: AxisArray{<:Any, N} ) where {name,N}
96
+ newaxes = Expr (:tuple )
97
+ idxnames = axisnames (idx)
98
+ for i= 1 : N
99
+ push! (newaxes. args, :($ (Axis{Symbol (name, " _" , idxnames[i])})(idx. axes[$ i]. val)))
100
+ end
101
+ newaxes
102
+ end
103
+
104
+ # TODO : this is duplicated from the above
105
+ @generated function _new_axes (ax:: Axis{name} , copy:: Val{false} , idx:: AxisArray{<:Any, N} ) where {name,N}
91
106
newaxes = Expr (:tuple )
92
107
idxnames = axisnames (idx)
93
108
for i= 1 : N
97
112
end
98
113
99
114
@propagate_inbounds function Base. getindex (A:: AxisArray , idxs:: Idx... )
100
- AxisArray (A. data[idxs... ], reaxis (A, idxs... ))
115
+ AxisArray (A. data[idxs... ], reaxis (A, Val ( true ), idxs... ))
101
116
end
102
117
103
118
# To resolve ambiguities, we need several definitions
104
119
using Base: AbstractCartesianIndex
105
- @propagate_inbounds Base. view (A:: AxisArray , idxs:: Idx... ) = AxisArray (view (A. data, idxs... ), reaxis (A, idxs... ))
120
+ @propagate_inbounds Base. view (A:: AxisArray , idxs:: Idx... ) = AxisArray (view (A. data, idxs... ), reaxis (A, Val ( false ), idxs... ))
106
121
107
122
# Setindex is so much simpler. Just assign it to the data:
108
123
@propagate_inbounds Base. setindex! (A:: AxisArray , v, idxs:: Idx... ) = (A. data[idxs... ] = v)
109
124
110
125
# Logical indexing
111
126
@propagate_inbounds function Base. getindex (A:: AxisArray , idx:: AbstractArray{Bool} )
112
- AxisArray (A. data[idx], reaxis (A, idx))
127
+ AxisArray (A. data[idx], reaxis (A, Val ( true ), idx))
113
128
end
114
129
@propagate_inbounds Base. setindex! (A:: AxisArray , v, idx:: AbstractArray{Bool} ) = (A. data[idx] = v)
115
130
0 commit comments